mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 07:37:55 +08:00
DEV: Apply syntax_tree formatting to app/*
This commit is contained in:
@ -12,48 +12,58 @@ class Notification < ActiveRecord::Base
|
||||
validates_presence_of :notification_type
|
||||
|
||||
scope :unread, lambda { where(read: false) }
|
||||
scope :recent, lambda { |n = nil| n ||= 10; order('notifications.created_at desc').limit(n) }
|
||||
scope :visible , lambda { joins('LEFT JOIN topics ON notifications.topic_id = topics.id')
|
||||
.where('topics.id IS NULL OR topics.deleted_at IS NULL') }
|
||||
scope :unread_type, ->(user, type, limit = 30) do
|
||||
unread_types(user, [type], limit)
|
||||
end
|
||||
scope :unread_types, ->(user, types, limit = 30) do
|
||||
where(user_id: user.id, read: false, notification_type: types)
|
||||
.visible
|
||||
.includes(:topic)
|
||||
.limit(limit)
|
||||
end
|
||||
scope :prioritized, ->(deprioritized_types = []) do
|
||||
scope = order("notifications.high_priority AND NOT notifications.read DESC")
|
||||
if deprioritized_types.present?
|
||||
scope = scope.order(DB.sql_fragment("NOT notifications.read AND notifications.notification_type NOT IN (?) DESC", deprioritized_types))
|
||||
else
|
||||
scope = scope.order("NOT notifications.read DESC")
|
||||
end
|
||||
scope.order("notifications.created_at DESC")
|
||||
end
|
||||
scope :for_user_menu, ->(user_id, limit: 30) do
|
||||
where(user_id: user_id)
|
||||
.visible
|
||||
.prioritized
|
||||
.includes(:topic)
|
||||
.limit(limit)
|
||||
end
|
||||
scope :recent,
|
||||
lambda { |n = nil|
|
||||
n ||= 10
|
||||
order("notifications.created_at desc").limit(n)
|
||||
}
|
||||
scope :visible,
|
||||
lambda {
|
||||
joins("LEFT JOIN topics ON notifications.topic_id = topics.id").where(
|
||||
"topics.id IS NULL OR topics.deleted_at IS NULL",
|
||||
)
|
||||
}
|
||||
scope :unread_type, ->(user, type, limit = 30) { unread_types(user, [type], limit) }
|
||||
scope :unread_types,
|
||||
->(user, types, limit = 30) {
|
||||
where(user_id: user.id, read: false, notification_type: types)
|
||||
.visible
|
||||
.includes(:topic)
|
||||
.limit(limit)
|
||||
}
|
||||
scope :prioritized,
|
||||
->(deprioritized_types = []) {
|
||||
scope = order("notifications.high_priority AND NOT notifications.read DESC")
|
||||
if deprioritized_types.present?
|
||||
scope =
|
||||
scope.order(
|
||||
DB.sql_fragment(
|
||||
"NOT notifications.read AND notifications.notification_type NOT IN (?) DESC",
|
||||
deprioritized_types,
|
||||
),
|
||||
)
|
||||
else
|
||||
scope = scope.order("NOT notifications.read DESC")
|
||||
end
|
||||
scope.order("notifications.created_at DESC")
|
||||
}
|
||||
scope :for_user_menu,
|
||||
->(user_id, limit: 30) {
|
||||
where(user_id: user_id).visible.prioritized.includes(:topic).limit(limit)
|
||||
}
|
||||
|
||||
attr_accessor :skip_send_email
|
||||
|
||||
after_commit :refresh_notification_count, on: [:create, :update, :destroy]
|
||||
after_commit :refresh_notification_count, on: %i[create update destroy]
|
||||
after_commit :send_email, on: :create
|
||||
|
||||
after_commit(on: :create) do
|
||||
DiscourseEvent.trigger(:notification_created, self)
|
||||
end
|
||||
after_commit(on: :create) { DiscourseEvent.trigger(:notification_created, self) }
|
||||
|
||||
before_create do
|
||||
# if we have manually set the notification to high_priority on create then
|
||||
# make sure that is respected
|
||||
self.high_priority = self.high_priority || Notification.high_priority_types.include?(self.notification_type)
|
||||
self.high_priority =
|
||||
self.high_priority || Notification.high_priority_types.include?(self.notification_type)
|
||||
end
|
||||
|
||||
def self.consolidate_or_create!(notification_params)
|
||||
@ -103,54 +113,53 @@ class Notification < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def self.types
|
||||
@types ||= Enum.new(mentioned: 1,
|
||||
replied: 2,
|
||||
quoted: 3,
|
||||
edited: 4,
|
||||
liked: 5,
|
||||
private_message: 6,
|
||||
invited_to_private_message: 7,
|
||||
invitee_accepted: 8,
|
||||
posted: 9,
|
||||
moved_post: 10,
|
||||
linked: 11,
|
||||
granted_badge: 12,
|
||||
invited_to_topic: 13,
|
||||
custom: 14,
|
||||
group_mentioned: 15,
|
||||
group_message_summary: 16,
|
||||
watching_first_post: 17,
|
||||
topic_reminder: 18,
|
||||
liked_consolidated: 19,
|
||||
post_approved: 20,
|
||||
code_review_commit_approved: 21,
|
||||
membership_request_accepted: 22,
|
||||
membership_request_consolidated: 23,
|
||||
bookmark_reminder: 24,
|
||||
reaction: 25,
|
||||
votes_released: 26,
|
||||
event_reminder: 27,
|
||||
event_invitation: 28,
|
||||
chat_mention: 29,
|
||||
chat_message: 30,
|
||||
chat_invitation: 31,
|
||||
chat_group_mention: 32, # March 2022 - This is obsolete, as all chat_mentions use `chat_mention` type
|
||||
chat_quoted: 33,
|
||||
assigned: 34,
|
||||
question_answer_user_commented: 35, # Used by https://github.com/discourse/discourse-question-answer
|
||||
watching_category_or_tag: 36,
|
||||
new_features: 37,
|
||||
following: 800, # Used by https://github.com/discourse/discourse-follow
|
||||
following_created_topic: 801, # Used by https://github.com/discourse/discourse-follow
|
||||
following_replied: 802, # Used by https://github.com/discourse/discourse-follow
|
||||
)
|
||||
@types ||=
|
||||
Enum.new(
|
||||
mentioned: 1,
|
||||
replied: 2,
|
||||
quoted: 3,
|
||||
edited: 4,
|
||||
liked: 5,
|
||||
private_message: 6,
|
||||
invited_to_private_message: 7,
|
||||
invitee_accepted: 8,
|
||||
posted: 9,
|
||||
moved_post: 10,
|
||||
linked: 11,
|
||||
granted_badge: 12,
|
||||
invited_to_topic: 13,
|
||||
custom: 14,
|
||||
group_mentioned: 15,
|
||||
group_message_summary: 16,
|
||||
watching_first_post: 17,
|
||||
topic_reminder: 18,
|
||||
liked_consolidated: 19,
|
||||
post_approved: 20,
|
||||
code_review_commit_approved: 21,
|
||||
membership_request_accepted: 22,
|
||||
membership_request_consolidated: 23,
|
||||
bookmark_reminder: 24,
|
||||
reaction: 25,
|
||||
votes_released: 26,
|
||||
event_reminder: 27,
|
||||
event_invitation: 28,
|
||||
chat_mention: 29,
|
||||
chat_message: 30,
|
||||
chat_invitation: 31,
|
||||
chat_group_mention: 32, # March 2022 - This is obsolete, as all chat_mentions use `chat_mention` type
|
||||
chat_quoted: 33,
|
||||
assigned: 34,
|
||||
question_answer_user_commented: 35, # Used by https://github.com/discourse/discourse-question-answer
|
||||
watching_category_or_tag: 36,
|
||||
new_features: 37,
|
||||
following: 800, # Used by https://github.com/discourse/discourse-follow
|
||||
following_created_topic: 801, # Used by https://github.com/discourse/discourse-follow
|
||||
following_replied: 802, # Used by https://github.com/discourse/discourse-follow
|
||||
)
|
||||
end
|
||||
|
||||
def self.high_priority_types
|
||||
@high_priority_types ||= [
|
||||
types[:private_message],
|
||||
types[:bookmark_reminder]
|
||||
]
|
||||
@high_priority_types ||= [types[:private_message], types[:bookmark_reminder]]
|
||||
end
|
||||
|
||||
def self.normal_priority_types
|
||||
@ -158,24 +167,16 @@ class Notification < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def self.mark_posts_read(user, topic_id, post_numbers)
|
||||
Notification
|
||||
.where(
|
||||
user_id: user.id,
|
||||
topic_id: topic_id,
|
||||
post_number: post_numbers,
|
||||
read: false
|
||||
)
|
||||
.update_all(read: true)
|
||||
Notification.where(
|
||||
user_id: user.id,
|
||||
topic_id: topic_id,
|
||||
post_number: post_numbers,
|
||||
read: false,
|
||||
).update_all(read: true)
|
||||
end
|
||||
|
||||
def self.read(user, notification_ids)
|
||||
Notification
|
||||
.where(
|
||||
id: notification_ids,
|
||||
user_id: user.id,
|
||||
read: false
|
||||
)
|
||||
.update_all(read: true)
|
||||
Notification.where(id: notification_ids, user_id: user.id, read: false).update_all(read: true)
|
||||
end
|
||||
|
||||
def self.read_types(user, types = nil)
|
||||
@ -185,15 +186,19 @@ class Notification < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def self.interesting_after(min_date)
|
||||
result = where("created_at > ?", min_date)
|
||||
.includes(:topic)
|
||||
.visible
|
||||
.unread
|
||||
.limit(20)
|
||||
.order("CASE WHEN notification_type = #{Notification.types[:replied]} THEN 1
|
||||
result =
|
||||
where("created_at > ?", min_date)
|
||||
.includes(:topic)
|
||||
.visible
|
||||
.unread
|
||||
.limit(20)
|
||||
.order(
|
||||
"CASE WHEN notification_type = #{Notification.types[:replied]} THEN 1
|
||||
WHEN notification_type = #{Notification.types[:mentioned]} THEN 2
|
||||
ELSE 3
|
||||
END, created_at DESC").to_a
|
||||
END, created_at DESC",
|
||||
)
|
||||
.to_a
|
||||
|
||||
# Remove any duplicates by type and topic
|
||||
if result.present?
|
||||
@ -222,14 +227,15 @@ class Notification < ActiveRecord::Base
|
||||
|
||||
# Be wary of calling this frequently. O(n) JSON parsing can suck.
|
||||
def data_hash
|
||||
@data_hash ||= begin
|
||||
return {} if data.blank?
|
||||
@data_hash ||=
|
||||
begin
|
||||
return {} if data.blank?
|
||||
|
||||
parsed = JSON.parse(data)
|
||||
return {} if parsed.blank?
|
||||
parsed = JSON.parse(data)
|
||||
return {} if parsed.blank?
|
||||
|
||||
parsed.with_indifferent_access
|
||||
end
|
||||
parsed.with_indifferent_access
|
||||
end
|
||||
end
|
||||
|
||||
def url
|
||||
@ -245,26 +251,27 @@ class Notification < ActiveRecord::Base
|
||||
[
|
||||
Notification.types[:liked],
|
||||
Notification.types[:liked_consolidated],
|
||||
Notification.types[:reaction]
|
||||
Notification.types[:reaction],
|
||||
]
|
||||
end
|
||||
|
||||
def self.prioritized_list(user, count: 30, types: [])
|
||||
return [] if !user&.user_option
|
||||
|
||||
notifications = user.notifications
|
||||
.includes(:topic)
|
||||
.visible
|
||||
.prioritized(types.present? ? [] : like_types)
|
||||
.limit(count)
|
||||
notifications =
|
||||
user
|
||||
.notifications
|
||||
.includes(:topic)
|
||||
.visible
|
||||
.prioritized(types.present? ? [] : like_types)
|
||||
.limit(count)
|
||||
|
||||
if types.present?
|
||||
notifications = notifications.where(notification_type: types)
|
||||
elsif user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:never]
|
||||
elsif user.user_option.like_notification_frequency ==
|
||||
UserOption.like_notification_frequency_type[:never]
|
||||
like_types.each do |notification_type|
|
||||
notifications = notifications.where(
|
||||
'notification_type <> ?', notification_type
|
||||
)
|
||||
notifications = notifications.where("notification_type <> ?", notification_type)
|
||||
end
|
||||
end
|
||||
notifications.to_a
|
||||
@ -275,20 +282,16 @@ class Notification < ActiveRecord::Base
|
||||
return unless user && user.user_option
|
||||
|
||||
count ||= 10
|
||||
notifications = user.notifications
|
||||
.visible
|
||||
.recent(count)
|
||||
.includes(:topic)
|
||||
notifications = user.notifications.visible.recent(count).includes(:topic)
|
||||
|
||||
notifications = notifications.where(notification_type: types) if types.present?
|
||||
if user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:never]
|
||||
if user.user_option.like_notification_frequency ==
|
||||
UserOption.like_notification_frequency_type[:never]
|
||||
[
|
||||
Notification.types[:liked],
|
||||
Notification.types[:liked_consolidated]
|
||||
Notification.types[:liked_consolidated],
|
||||
].each do |notification_type|
|
||||
notifications = notifications.where(
|
||||
'notification_type <> ?', notification_type
|
||||
)
|
||||
notifications = notifications.where("notification_type <> ?", notification_type)
|
||||
end
|
||||
end
|
||||
|
||||
@ -313,27 +316,30 @@ class Notification < ActiveRecord::Base
|
||||
ids = builder.query_single
|
||||
|
||||
if ids.length > 0
|
||||
notifications += user
|
||||
.notifications
|
||||
.order('notifications.created_at DESC')
|
||||
.where(id: ids)
|
||||
.joins(:topic)
|
||||
.limit(count)
|
||||
notifications +=
|
||||
user
|
||||
.notifications
|
||||
.order("notifications.created_at DESC")
|
||||
.where(id: ids)
|
||||
.joins(:topic)
|
||||
.limit(count)
|
||||
end
|
||||
|
||||
notifications.uniq(&:id).sort do |x, y|
|
||||
if x.unread_high_priority? && !y.unread_high_priority?
|
||||
-1
|
||||
elsif y.unread_high_priority? && !x.unread_high_priority?
|
||||
1
|
||||
else
|
||||
y.created_at <=> x.created_at
|
||||
notifications
|
||||
.uniq(&:id)
|
||||
.sort do |x, y|
|
||||
if x.unread_high_priority? && !y.unread_high_priority?
|
||||
-1
|
||||
elsif y.unread_high_priority? && !x.unread_high_priority?
|
||||
1
|
||||
else
|
||||
y.created_at <=> x.created_at
|
||||
end
|
||||
end
|
||||
end.take(count)
|
||||
.take(count)
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def unread_high_priority?
|
||||
@ -347,19 +353,18 @@ class Notification < ActiveRecord::Base
|
||||
protected
|
||||
|
||||
def refresh_notification_count
|
||||
if user_id
|
||||
User.find_by(id: user_id)&.publish_notifications_state
|
||||
end
|
||||
User.find_by(id: user_id)&.publish_notifications_state if user_id
|
||||
end
|
||||
|
||||
def send_email
|
||||
return if skip_send_email
|
||||
|
||||
user.do_not_disturb? ?
|
||||
ShelvedNotification.create(notification_id: self.id) :
|
||||
if user.do_not_disturb?
|
||||
ShelvedNotification.create(notification_id: self.id)
|
||||
else
|
||||
NotificationEmailer.process_notification(self)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
Reference in New Issue
Block a user