2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
class Notification < ActiveRecord::Base
|
2013-02-06 03:16:51 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
|
|
|
|
2019-11-28 06:32:35 +08:00
|
|
|
MEMBERSHIP_REQUEST_CONSOLIDATION_WINDOW_HOURS = 24
|
|
|
|
MEMBERSHIP_REQUEST_CONSOLIDATION_THRESHOLD = 3
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
validates_presence_of :data
|
|
|
|
validates_presence_of :notification_type
|
|
|
|
|
2013-03-01 02:54:12 +08:00
|
|
|
scope :unread, lambda { where(read: false) }
|
2017-07-28 09:20:09 +08:00
|
|
|
scope :recent, lambda { |n = nil| n ||= 10; order('notifications.created_at desc').limit(n) }
|
2015-06-23 04:14:22 +08:00
|
|
|
scope :visible , lambda { joins('LEFT JOIN topics ON notifications.topic_id = topics.id')
|
2017-07-28 09:20:09 +08:00
|
|
|
.where('topics.id IS NULL OR topics.deleted_at IS NULL') }
|
2013-03-01 02:54:12 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
scope :filter_by_display_username_and_type, ->(username, notification_type) {
|
|
|
|
where("data::json ->> 'display_username' = ?", username)
|
|
|
|
.where(notification_type: notification_type)
|
2019-01-16 10:40:16 +08:00
|
|
|
.order(created_at: :desc)
|
|
|
|
}
|
|
|
|
|
2017-06-12 15:41:39 +08:00
|
|
|
attr_accessor :skip_send_email
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
after_commit :refresh_notification_count, on: [:create, :update, :destroy]
|
2016-12-22 12:29:34 +08:00
|
|
|
|
2019-08-16 02:45:30 +08:00
|
|
|
after_commit(on: :create) do
|
2019-11-28 07:01:55 +08:00
|
|
|
DiscourseEvent.trigger(:notification_created, self)
|
|
|
|
send_email unless consolidate_membership_requests
|
2019-08-16 02:45:30 +08:00
|
|
|
end
|
|
|
|
|
2013-05-16 15:50:14 +08:00
|
|
|
def self.ensure_consistency!
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec(<<~SQL, Notification.types[:private_message])
|
2017-04-25 04:51:09 +08:00
|
|
|
DELETE
|
|
|
|
FROM notifications n
|
2018-06-19 14:13:14 +08:00
|
|
|
WHERE notification_type = ?
|
2017-04-25 04:51:09 +08:00
|
|
|
AND NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM posts p
|
|
|
|
JOIN topics t ON t.id = p.topic_id
|
|
|
|
WHERE p.deleted_at IS NULL
|
|
|
|
AND t.deleted_at IS NULL
|
|
|
|
AND p.post_number = n.post_number
|
|
|
|
AND t.id = n.topic_id
|
|
|
|
)
|
|
|
|
SQL
|
2013-05-16 15:50:14 +08:00
|
|
|
end
|
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
def self.types
|
2016-01-08 18:53:52 +08:00
|
|
|
@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,
|
2016-01-27 18:38:14 +08:00
|
|
|
group_mentioned: 15,
|
2016-07-07 03:56:40 +08:00
|
|
|
group_message_summary: 16,
|
2017-05-17 02:49:42 +08:00
|
|
|
watching_first_post: 17,
|
2019-01-16 10:40:16 +08:00
|
|
|
topic_reminder: 18,
|
|
|
|
liked_consolidated: 19,
|
2019-06-11 09:17:23 +08:00
|
|
|
post_approved: 20,
|
2019-08-06 18:29:46 +08:00
|
|
|
code_review_commit_approved: 21,
|
2019-11-28 06:32:35 +08:00
|
|
|
membership_request_accepted: 22,
|
|
|
|
membership_request_consolidated: 23
|
2016-01-27 18:38:14 +08:00
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-25 15:42:42 +08:00
|
|
|
def self.mark_posts_read(user, topic_id, post_numbers)
|
2018-05-26 09:11:10 +08:00
|
|
|
Notification
|
2018-05-28 16:41:38 +08:00
|
|
|
.where(
|
|
|
|
user_id: user.id,
|
|
|
|
topic_id: topic_id,
|
|
|
|
post_number: post_numbers,
|
|
|
|
read: false
|
|
|
|
)
|
2018-05-26 08:09:48 +08:00
|
|
|
.update_all(read: true)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-09-16 14:14:00 +08:00
|
|
|
def self.read(user, notification_ids)
|
2018-05-26 09:11:10 +08:00
|
|
|
Notification
|
2018-05-28 16:41:38 +08:00
|
|
|
.where(
|
|
|
|
id: notification_ids,
|
|
|
|
user_id: user.id,
|
|
|
|
read: false
|
|
|
|
)
|
2017-07-28 09:20:09 +08:00
|
|
|
.update_all(read: true)
|
2016-09-16 14:14:00 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.interesting_after(min_date)
|
2017-07-28 09:20:09 +08:00
|
|
|
result = where("created_at > ?", min_date)
|
|
|
|
.includes(:topic)
|
|
|
|
.visible
|
|
|
|
.unread
|
|
|
|
.limit(20)
|
|
|
|
.order("CASE WHEN notification_type = #{Notification.types[:replied]} THEN 1
|
2013-03-01 20:07:44 +08:00
|
|
|
WHEN notification_type = #{Notification.types[:mentioned]} THEN 2
|
2013-02-06 03:16:51 +08:00
|
|
|
ELSE 3
|
|
|
|
END, created_at DESC").to_a
|
|
|
|
|
|
|
|
# Remove any duplicates by type and topic
|
|
|
|
if result.present?
|
2013-02-07 23:45:24 +08:00
|
|
|
seen = {}
|
2013-02-06 03:16:51 +08:00
|
|
|
to_remove = Set.new
|
|
|
|
|
|
|
|
result.each do |r|
|
|
|
|
seen[r.notification_type] ||= Set.new
|
|
|
|
if seen[r.notification_type].include?(r.topic_id)
|
2013-02-07 23:45:24 +08:00
|
|
|
to_remove << r.id
|
2013-02-06 03:16:51 +08:00
|
|
|
else
|
|
|
|
seen[r.notification_type] << r.topic_id
|
|
|
|
end
|
|
|
|
end
|
2017-07-28 09:20:09 +08:00
|
|
|
result.reject! { |r| to_remove.include?(r.id) }
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2014-05-27 01:26:28 +08:00
|
|
|
# Clean up any notifications the user can no longer see. For example, if a topic was previously
|
|
|
|
# public then turns private.
|
|
|
|
def self.remove_for(user_id, topic_id)
|
|
|
|
Notification.where(user_id: user_id, topic_id: topic_id).delete_all
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Be wary of calling this frequently. O(n) JSON parsing can suck.
|
|
|
|
def data_hash
|
|
|
|
@data_hash ||= begin
|
2019-04-16 04:19:32 +08:00
|
|
|
return {} if data.blank?
|
2016-02-23 08:34:16 +08:00
|
|
|
|
2015-09-04 11:34:21 +08:00
|
|
|
parsed = JSON.parse(data)
|
2019-04-16 04:19:32 +08:00
|
|
|
return {} if parsed.blank?
|
2015-09-04 11:34:21 +08:00
|
|
|
|
|
|
|
parsed.with_indifferent_access
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
2016-02-02 02:12:10 +08:00
|
|
|
topic.relative_url(post_number) if topic.present?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def post
|
2013-03-01 02:54:12 +08:00
|
|
|
return if topic_id.blank? || post_number.blank?
|
2014-05-06 21:41:59 +08:00
|
|
|
Post.find_by(topic_id: topic_id, post_number: post_number)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-16 13:03:03 +08:00
|
|
|
|
2014-02-13 14:12:17 +08:00
|
|
|
def self.recent_report(user, count = nil)
|
2016-03-06 06:21:38 +08:00
|
|
|
return unless user && user.user_option
|
|
|
|
|
2014-02-14 04:20:56 +08:00
|
|
|
count ||= 10
|
2015-02-19 09:40:00 +08:00
|
|
|
notifications = user.notifications
|
2017-07-28 09:20:09 +08:00
|
|
|
.visible
|
|
|
|
.recent(count)
|
|
|
|
.includes(:topic)
|
2016-03-06 06:21:38 +08:00
|
|
|
|
|
|
|
if user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:never]
|
2019-01-16 17:08:59 +08:00
|
|
|
[
|
|
|
|
Notification.types[:liked],
|
|
|
|
Notification.types[:liked_consolidated]
|
|
|
|
].each do |notification_type|
|
|
|
|
notifications = notifications.where(
|
|
|
|
'notification_type <> ?', notification_type
|
|
|
|
)
|
|
|
|
end
|
2016-03-06 06:21:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
notifications = notifications.to_a
|
2014-02-13 14:12:17 +08:00
|
|
|
|
|
|
|
if notifications.present?
|
2016-02-15 16:29:35 +08:00
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
ids = DB.query_single(<<~SQL, count.to_i)
|
2016-02-15 16:29:35 +08:00
|
|
|
SELECT n.id FROM notifications n
|
|
|
|
WHERE
|
|
|
|
n.notification_type = 6 AND
|
|
|
|
n.user_id = #{user.id.to_i} AND
|
|
|
|
NOT read
|
|
|
|
ORDER BY n.id ASC
|
2018-06-19 14:13:14 +08:00
|
|
|
LIMIT ?
|
|
|
|
SQL
|
2016-02-15 16:29:35 +08:00
|
|
|
|
|
|
|
if ids.length > 0
|
|
|
|
notifications += user
|
|
|
|
.notifications
|
|
|
|
.order('notifications.created_at DESC')
|
|
|
|
.where(id: ids)
|
|
|
|
.joins(:topic)
|
|
|
|
.limit(count)
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
notifications.uniq(&:id).sort do |x, y|
|
2014-02-13 14:27:35 +08:00
|
|
|
if x.unread_pm? && !y.unread_pm?
|
|
|
|
-1
|
|
|
|
elsif y.unread_pm? && !x.unread_pm?
|
|
|
|
1
|
|
|
|
else
|
|
|
|
y.created_at <=> x.created_at
|
|
|
|
end
|
|
|
|
end.take(count)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2014-02-13 14:12:17 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_pm?
|
|
|
|
Notification.types[:private_message] == self.notification_type && !read
|
|
|
|
end
|
2013-05-16 13:03:03 +08:00
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
def post_id
|
2019-10-21 18:32:27 +08:00
|
|
|
Post.where(topic: topic_id, post_number: post_number).pluck_first(:id)
|
2016-01-27 09:19:49 +08:00
|
|
|
end
|
|
|
|
|
2013-05-16 13:03:03 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def refresh_notification_count
|
2019-10-23 13:09:55 +08:00
|
|
|
if user_id
|
|
|
|
User.find_by(id: user_id)&.publish_notifications_state
|
2018-05-26 08:27:54 +08:00
|
|
|
end
|
2013-05-16 13:03:03 +08:00
|
|
|
end
|
|
|
|
|
2016-12-22 12:29:34 +08:00
|
|
|
def send_email
|
2017-06-12 15:41:39 +08:00
|
|
|
NotificationEmailer.process_notification(self) if !skip_send_email
|
2016-12-22 12:29:34 +08:00
|
|
|
end
|
|
|
|
|
2019-11-28 06:32:35 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def consolidate_membership_requests
|
|
|
|
return unless unread_pm?
|
|
|
|
|
|
|
|
post_id = data_hash[:original_post_id]
|
|
|
|
return if post_id.blank?
|
|
|
|
|
|
|
|
custom_field = PostCustomField.select(:value).find_by(post_id: post_id, name: "requested_group_id")
|
|
|
|
return if custom_field.blank?
|
|
|
|
|
|
|
|
group_id = custom_field.value.to_i
|
|
|
|
group_name = Group.select(:name).find_by(id: group_id)&.name
|
|
|
|
return if group_name.blank?
|
|
|
|
|
|
|
|
consolidation_window = MEMBERSHIP_REQUEST_CONSOLIDATION_WINDOW_HOURS.hours.ago
|
|
|
|
timestamp = Time.zone.now
|
|
|
|
unread = user.notifications.unread
|
|
|
|
|
|
|
|
consolidated_notification = unread
|
|
|
|
.where("created_at > ? AND data::json ->> 'group_name' = ?", consolidation_window, group_name)
|
|
|
|
.find_by(notification_type: Notification.types[:membership_request_consolidated])
|
|
|
|
|
|
|
|
if consolidated_notification.present?
|
|
|
|
data = consolidated_notification.data_hash
|
|
|
|
data["count"] += 1
|
|
|
|
|
|
|
|
Notification.transaction do
|
|
|
|
consolidated_notification.update!(
|
|
|
|
data: data.to_json,
|
|
|
|
read: false,
|
|
|
|
updated_at: timestamp
|
|
|
|
)
|
|
|
|
|
|
|
|
destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
notifications = unread
|
|
|
|
.where(notification_type: Notification.types[:private_message])
|
|
|
|
.where("created_at > ? AND data::json ->> 'topic_title' = ?", consolidation_window, data_hash[:topic_title])
|
|
|
|
|
|
|
|
return if notifications.count < MEMBERSHIP_REQUEST_CONSOLIDATION_THRESHOLD
|
|
|
|
|
|
|
|
Notification.transaction do
|
|
|
|
Notification.create!(
|
|
|
|
notification_type: Notification.types[:membership_request_consolidated],
|
|
|
|
user_id: user_id,
|
|
|
|
data: {
|
|
|
|
group_name: group_name,
|
|
|
|
count: notifications.count
|
|
|
|
}.to_json,
|
|
|
|
updated_at: timestamp,
|
|
|
|
created_at: timestamp
|
|
|
|
)
|
|
|
|
|
|
|
|
notifications.destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-24 10:48:32 +08:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: notifications
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# notification_type :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# data :string(1000) not null
|
|
|
|
# read :boolean default(FALSE), not null
|
2014-08-27 13:19:25 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-24 10:48:32 +08:00
|
|
|
# topic_id :integer
|
|
|
|
# post_number :integer
|
|
|
|
# post_action_id :integer
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2018-07-16 14:18:07 +08:00
|
|
|
# idx_notifications_speedup_unread_count (user_id,notification_type) WHERE (NOT read)
|
2015-09-18 08:41:10 +08:00
|
|
|
# index_notifications_on_post_action_id (post_action_id)
|
2019-05-14 14:02:55 +08:00
|
|
|
# index_notifications_on_read_or_n_type (user_id,id DESC,read,topic_id) UNIQUE WHERE (read OR (notification_type <> 6))
|
2019-10-30 19:59:59 +08:00
|
|
|
# index_notifications_on_topic_id_and_post_number (topic_id,post_number)
|
2015-09-18 08:41:10 +08:00
|
|
|
# index_notifications_on_user_id_and_created_at (user_id,created_at)
|
2018-07-16 14:18:07 +08:00
|
|
|
# index_notifications_on_user_id_and_id (user_id,id) UNIQUE WHERE ((notification_type = 6) AND (NOT read))
|
2015-09-18 08:41:10 +08:00
|
|
|
# index_notifications_on_user_id_and_topic_id_and_post_number (user_id,topic_id,post_number)
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|