2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
class PostAlerter
|
2020-02-06 20:14:19 +08:00
|
|
|
USER_BATCH_SIZE = 100
|
|
|
|
|
2017-06-12 15:41:39 +08:00
|
|
|
def self.post_created(post, opts = {})
|
2018-05-15 15:51:32 +08:00
|
|
|
PostAlerter.new(opts).after_save_post(post, true)
|
2014-03-18 12:22:39 +08:00
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2017-06-12 15:41:39 +08:00
|
|
|
def initialize(default_opts = {})
|
|
|
|
@default_opts = default_opts
|
|
|
|
end
|
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
def not_allowed?(user, post)
|
|
|
|
user.blank? ||
|
2019-03-12 07:58:14 +08:00
|
|
|
user.bot? ||
|
2015-12-18 23:32:53 +08:00
|
|
|
user.id == post.user_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_allowed_users(post)
|
|
|
|
@all_allowed_users ||= post.topic.all_allowed_users.reject { |u| not_allowed?(u, post) }
|
|
|
|
end
|
|
|
|
|
2015-05-26 01:15:00 +08:00
|
|
|
def allowed_users(post)
|
2015-12-18 23:32:53 +08:00
|
|
|
@allowed_users ||= post.topic.allowed_users.reject { |u| not_allowed?(u, post) }
|
2015-05-26 01:15:00 +08:00
|
|
|
end
|
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
def allowed_group_users(post)
|
|
|
|
@allowed_group_users ||= post.topic.allowed_group_users.reject { |u| not_allowed?(u, post) }
|
|
|
|
end
|
2014-03-18 12:22:39 +08:00
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
def directly_targeted_users(post)
|
|
|
|
allowed_users(post) - allowed_group_users(post)
|
|
|
|
end
|
2015-12-15 06:17:09 +08:00
|
|
|
|
2016-01-27 18:38:14 +08:00
|
|
|
def indirectly_targeted_users(post)
|
2015-12-18 23:32:53 +08:00
|
|
|
allowed_group_users(post)
|
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2018-04-12 22:19:44 +08:00
|
|
|
def only_allowed_users(users, post)
|
2018-11-24 01:25:40 +08:00
|
|
|
return users unless post.topic.private_message?
|
|
|
|
users.select { |u| all_allowed_users(post).include?(u) }
|
2018-04-12 22:19:44 +08:00
|
|
|
end
|
|
|
|
|
2017-09-19 21:51:10 +08:00
|
|
|
def notify_about_reply?(post)
|
2018-12-04 14:54:27 +08:00
|
|
|
# small actions can be whispers in this case they will have an action code
|
|
|
|
# we never want to notify on this
|
|
|
|
post.post_type == Post.types[:regular] ||
|
|
|
|
(post.post_type == Post.types[:whisper] && post.action_code.nil?)
|
2017-09-19 21:51:10 +08:00
|
|
|
end
|
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
def after_save_post(post, new_record = false)
|
2018-04-16 17:48:06 +08:00
|
|
|
notified = [post.user, post.last_editor].uniq
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
# mentions (users/groups)
|
2015-11-30 14:03:47 +08:00
|
|
|
mentioned_groups, mentioned_users = extract_mentions(post)
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2016-03-09 04:26:06 +08:00
|
|
|
if mentioned_groups || mentioned_users
|
|
|
|
mentioned_opts = {}
|
2018-04-12 22:19:44 +08:00
|
|
|
editor = post.last_editor
|
|
|
|
|
2016-03-09 04:26:06 +08:00
|
|
|
if post.last_editor_id != post.user_id
|
|
|
|
# Mention comes from an edit by someone else, so notification should say who added the mention.
|
2017-07-28 09:20:09 +08:00
|
|
|
mentioned_opts = { user_id: editor.id, original_username: editor.username, display_username: editor.username }
|
2016-03-09 04:26:06 +08:00
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2016-03-09 04:26:06 +08:00
|
|
|
expand_group_mentions(mentioned_groups, post) do |group, users|
|
2018-11-24 01:25:40 +08:00
|
|
|
users = only_allowed_users(users, post)
|
2018-03-08 18:40:48 +08:00
|
|
|
notified += notify_users(users - notified, :group_mentioned, post, mentioned_opts.merge(group: group))
|
2016-03-09 04:26:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if mentioned_users
|
2018-11-24 01:25:40 +08:00
|
|
|
mentioned_users = only_allowed_users(mentioned_users, post)
|
2018-03-08 18:40:48 +08:00
|
|
|
notified += notify_users(mentioned_users - notified, :mentioned, post, mentioned_opts)
|
2016-03-09 04:26:06 +08:00
|
|
|
end
|
2015-11-30 14:03:47 +08:00
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
# replies
|
|
|
|
reply_to_user = post.reply_notification_target
|
|
|
|
|
2017-09-19 21:51:10 +08:00
|
|
|
if new_record && reply_to_user && !notified.include?(reply_to_user) && notify_about_reply?(post)
|
2018-03-08 18:40:48 +08:00
|
|
|
notified += notify_non_pm_users(reply_to_user, :replied, post)
|
2015-12-18 23:32:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# quotes
|
|
|
|
quoted_users = extract_quoted_users(post)
|
2018-03-08 18:40:48 +08:00
|
|
|
notified += notify_non_pm_users(quoted_users - notified, :quoted, post)
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
# linked
|
|
|
|
linked_users = extract_linked_users(post)
|
2018-03-08 18:40:48 +08:00
|
|
|
notified += notify_non_pm_users(linked_users - notified, :linked, post)
|
2015-12-18 23:32:53 +08:00
|
|
|
|
|
|
|
# private messages
|
|
|
|
if new_record
|
|
|
|
if post.topic.private_message?
|
2018-05-15 15:51:32 +08:00
|
|
|
notify_pm_users(post, reply_to_user, notified)
|
2018-07-21 17:20:21 +08:00
|
|
|
elsif notify_about_reply?(post)
|
2015-12-18 23:32:53 +08:00
|
|
|
notify_post_users(post, notified)
|
|
|
|
end
|
2015-11-30 14:03:47 +08:00
|
|
|
end
|
|
|
|
|
2015-12-01 13:52:43 +08:00
|
|
|
sync_group_mentions(post, mentioned_groups)
|
2016-07-07 03:56:40 +08:00
|
|
|
|
2016-07-20 03:57:05 +08:00
|
|
|
if new_record && post.post_number == 1
|
2016-07-07 03:56:40 +08:00
|
|
|
topic = post.topic
|
|
|
|
|
|
|
|
if topic.present?
|
2018-05-17 16:09:21 +08:00
|
|
|
watchers = category_watchers(topic) + tag_watchers(topic) + group_watchers(topic)
|
2016-07-07 03:56:40 +08:00
|
|
|
notify_first_post_watchers(post, watchers)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-17 16:09:21 +08:00
|
|
|
def group_watchers(topic)
|
|
|
|
GroupUser.where(
|
|
|
|
group_id: topic.allowed_groups.pluck(:group_id),
|
|
|
|
notification_level: GroupUser.notification_levels[:watching_first_post]
|
|
|
|
).pluck(:user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_watchers(topic)
|
|
|
|
topic.tag_users
|
|
|
|
.where(notification_level: TagUser.notification_levels[:watching_first_post])
|
|
|
|
.pluck(:user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_watchers(topic)
|
|
|
|
topic.category_users
|
|
|
|
.where(notification_level: CategoryUser.notification_levels[:watching_first_post])
|
|
|
|
.pluck(:user_id)
|
|
|
|
end
|
|
|
|
|
2016-07-07 03:56:40 +08:00
|
|
|
def notify_first_post_watchers(post, user_ids)
|
|
|
|
return if user_ids.blank?
|
|
|
|
user_ids.uniq!
|
|
|
|
|
2018-05-24 23:52:59 +08:00
|
|
|
warn_if_not_sidekiq
|
|
|
|
|
2016-07-07 03:56:40 +08:00
|
|
|
# Don't notify the OP
|
|
|
|
user_ids -= [post.user_id]
|
2017-07-20 04:51:32 +08:00
|
|
|
users = User.where(id: user_ids)
|
2018-05-07 17:44:29 +08:00
|
|
|
|
2018-05-24 23:27:43 +08:00
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
2020-02-06 20:14:19 +08:00
|
|
|
each_user_in_batches(users) do |user|
|
2018-05-24 23:27:43 +08:00
|
|
|
create_notification(user, Notification.types[:watching_first_post], post)
|
2016-07-07 03:56:40 +08:00
|
|
|
end
|
2015-12-01 13:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def sync_group_mentions(post, mentioned_groups)
|
|
|
|
GroupMention.where(post_id: post.id).destroy_all
|
|
|
|
return if mentioned_groups.blank?
|
|
|
|
|
|
|
|
mentioned_groups.each do |group|
|
|
|
|
GroupMention.create(post_id: post.id, group_id: group.id)
|
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_posts(user, topic)
|
2015-10-20 04:31:48 +08:00
|
|
|
Post.secured(Guardian.new(user))
|
2017-07-28 09:20:09 +08:00
|
|
|
.where('post_number > COALESCE((
|
2014-03-18 10:12:07 +08:00
|
|
|
SELECT last_read_post_number FROM topic_users tu
|
|
|
|
WHERE tu.user_id = ? AND tu.topic_id = ? ),0)',
|
|
|
|
user.id, topic.id)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where('reply_to_user_id = ? OR exists(
|
2014-03-18 10:12:07 +08:00
|
|
|
SELECT 1 from topic_users tu
|
|
|
|
WHERE tu.user_id = ? AND
|
|
|
|
tu.topic_id = ? AND
|
|
|
|
notification_level = ?
|
|
|
|
)', user.id, user.id, topic.id, TopicUser.notification_levels[:watching])
|
2017-07-28 09:20:09 +08:00
|
|
|
.where(topic_id: topic.id)
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def first_unread_post(user, topic)
|
|
|
|
unread_posts(user, topic).order('post_number').first
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_count(user, topic)
|
|
|
|
unread_posts(user, topic).count
|
|
|
|
end
|
|
|
|
|
2018-05-25 11:59:29 +08:00
|
|
|
def destroy_notifications(user, types, topic)
|
2014-03-18 10:12:07 +08:00
|
|
|
return if user.blank?
|
|
|
|
return unless Guardian.new(user).can_see?(topic)
|
|
|
|
|
2018-05-25 12:00:13 +08:00
|
|
|
User.transaction do
|
|
|
|
user.notifications.where(
|
|
|
|
notification_type: types,
|
|
|
|
topic_id: topic.id
|
|
|
|
).destroy_all
|
|
|
|
|
|
|
|
# Reload so notification counts sync up correctly
|
|
|
|
user.reload
|
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
2019-01-16 19:49:15 +08:00
|
|
|
NOTIFIABLE_TYPES = [:mentioned, :replied, :quoted, :posted, :linked, :private_message, :group_mentioned, :watching_first_post].map { |t|
|
2015-05-04 11:49:32 +08:00
|
|
|
Notification.types[t]
|
|
|
|
}
|
|
|
|
|
2016-01-27 18:38:14 +08:00
|
|
|
def group_stats(topic)
|
2018-06-19 14:13:14 +08:00
|
|
|
sql = <<~SQL
|
|
|
|
SELECT COUNT(*) FROM topics t
|
|
|
|
JOIN topic_allowed_groups g ON g.group_id = :group_id AND g.topic_id = t.id
|
|
|
|
LEFT JOIN group_archived_messages a ON a.topic_id = t.id AND a.group_id = g.group_id
|
|
|
|
WHERE a.id IS NULL AND t.deleted_at is NULL AND t.archetype = 'private_message'
|
|
|
|
SQL
|
|
|
|
|
2016-01-27 18:38:14 +08:00
|
|
|
topic.allowed_groups.map do |g|
|
|
|
|
{
|
|
|
|
group_id: g.id,
|
|
|
|
group_name: g.name.downcase,
|
2018-06-19 14:13:14 +08:00
|
|
|
inbox_count: DB.query_single(sql, group_id: g.id).first.to_i
|
2016-01-27 18:38:14 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def notify_group_summary(user, post)
|
2016-01-27 18:38:14 +08:00
|
|
|
@group_stats ||= {}
|
|
|
|
stats = (@group_stats[post.topic_id] ||= group_stats(post.topic))
|
|
|
|
return unless stats
|
|
|
|
|
|
|
|
group_id = post.topic
|
2017-07-28 09:20:09 +08:00
|
|
|
.topic_allowed_groups
|
|
|
|
.where(group_id: user.groups.pluck(:id))
|
2019-10-21 18:32:27 +08:00
|
|
|
.pluck_first(:group_id)
|
2016-01-27 18:38:14 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
stat = stats.find { |s| s[:group_id] == group_id }
|
2016-01-27 20:13:37 +08:00
|
|
|
return unless stat && stat[:inbox_count] > 0
|
2016-01-27 18:38:14 +08:00
|
|
|
|
|
|
|
notification_type = Notification.types[:group_message_summary]
|
|
|
|
|
2017-05-27 05:04:40 +08:00
|
|
|
DistributedMutex.synchronize("group_message_notify_#{user.id}") do
|
|
|
|
Notification.where(notification_type: notification_type, user_id: user.id).each do |n|
|
|
|
|
n.destroy if n.data_hash[:group_id] == stat[:group_id]
|
|
|
|
end
|
2016-01-27 18:38:14 +08:00
|
|
|
|
2017-05-27 05:04:40 +08:00
|
|
|
Notification.create(
|
|
|
|
notification_type: notification_type,
|
|
|
|
user_id: user.id,
|
|
|
|
data: {
|
|
|
|
group_id: stat[:group_id],
|
|
|
|
group_name: stat[:group_name],
|
|
|
|
inbox_count: stat[:inbox_count],
|
|
|
|
username: user.username_lower
|
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
end
|
2016-01-27 18:38:14 +08:00
|
|
|
|
|
|
|
# TODO decide if it makes sense to also publish a desktop notification
|
|
|
|
end
|
|
|
|
|
2020-02-03 08:27:19 +08:00
|
|
|
def should_notify_edit?(notification, post, opts)
|
|
|
|
notification.data_hash["display_username"] != (opts[:display_username].presence || post.user.username)
|
2016-03-02 20:16:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def should_notify_like?(user, notification)
|
|
|
|
return true if user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:always]
|
|
|
|
return true if user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:first_time_and_daily] && notification.created_at < 1.day.ago
|
2018-05-15 15:51:32 +08:00
|
|
|
false
|
2016-03-02 20:16:52 +08:00
|
|
|
end
|
|
|
|
|
2020-02-03 08:27:19 +08:00
|
|
|
def should_notify_previous?(user, post, notification, opts)
|
2016-03-08 04:56:33 +08:00
|
|
|
case notification.notification_type
|
2020-02-03 08:27:19 +08:00
|
|
|
when Notification.types[:edited] then should_notify_edit?(notification, post, opts)
|
2016-03-08 04:56:33 +08:00
|
|
|
when Notification.types[:liked] then should_notify_like?(user, notification)
|
|
|
|
else false
|
2016-03-02 20:16:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-08 04:56:33 +08:00
|
|
|
COLLAPSED_NOTIFICATION_TYPES ||= [
|
|
|
|
Notification.types[:replied],
|
|
|
|
Notification.types[:posted],
|
2018-12-24 19:44:50 +08:00
|
|
|
Notification.types[:private_message],
|
2016-03-08 04:56:33 +08:00
|
|
|
]
|
|
|
|
|
2017-06-12 15:41:39 +08:00
|
|
|
def create_notification(user, type, post, opts = {})
|
|
|
|
opts = @default_opts.merge(opts)
|
|
|
|
|
2017-07-20 04:51:32 +08:00
|
|
|
DiscourseEvent.trigger(:before_create_notification, user, type, post, opts)
|
|
|
|
|
2019-03-12 07:58:14 +08:00
|
|
|
return if user.blank? || user.bot?
|
2020-01-28 02:24:11 +08:00
|
|
|
return if (topic = post.topic).blank?
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2019-01-17 14:31:07 +08:00
|
|
|
is_liked = type == Notification.types[:liked]
|
|
|
|
return if is_liked && user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:never]
|
2016-03-06 06:12:59 +08:00
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
# Make sure the user can see the post
|
|
|
|
return unless Guardian.new(user).can_see?(post)
|
|
|
|
|
2020-01-28 02:24:11 +08:00
|
|
|
return if user.staged? && topic.category&.mailinglist_mirror?
|
2017-11-17 21:50:35 +08:00
|
|
|
|
2016-03-09 04:26:06 +08:00
|
|
|
notifier_id = opts[:user_id] || post.user_id # xxxxx look at revision history
|
2015-03-26 09:08:04 +08:00
|
|
|
|
2015-03-24 08:55:22 +08:00
|
|
|
# apply muting here
|
2015-03-26 09:08:04 +08:00
|
|
|
return if notifier_id && MutedUser.where(user_id: user.id, muted_user_id: notifier_id)
|
2018-12-04 14:54:27 +08:00
|
|
|
.joins(:muted_user)
|
|
|
|
.where('NOT admin AND NOT moderator')
|
|
|
|
.exists?
|
2015-03-24 08:55:22 +08:00
|
|
|
|
2019-03-21 19:15:34 +08:00
|
|
|
# apply ignored here
|
|
|
|
return if notifier_id && IgnoredUser.where(user_id: user.id, ignored_user_id: notifier_id)
|
|
|
|
.joins(:ignored_user)
|
|
|
|
.where('NOT admin AND NOT moderator')
|
|
|
|
.exists?
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
# skip if muted on the topic
|
2017-06-12 15:41:39 +08:00
|
|
|
return if TopicUser.where(
|
2020-01-28 02:24:11 +08:00
|
|
|
topic: topic,
|
2017-06-12 15:41:39 +08:00
|
|
|
user: user,
|
|
|
|
notification_level: TopicUser.notification_levels[:muted]
|
|
|
|
).exists?
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2015-12-15 06:17:09 +08:00
|
|
|
# skip if muted on the group
|
|
|
|
if group = opts[:group]
|
2017-06-12 15:41:39 +08:00
|
|
|
return if GroupUser.where(
|
|
|
|
group_id: opts[:group_id],
|
|
|
|
user_id: user.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:muted]
|
|
|
|
).exists?
|
2015-12-15 06:17:09 +08:00
|
|
|
end
|
|
|
|
|
2019-05-16 00:47:36 +08:00
|
|
|
existing_notifications = user.notifications
|
2017-07-28 09:20:09 +08:00
|
|
|
.order("notifications.id DESC")
|
2019-05-16 00:47:36 +08:00
|
|
|
.where(
|
2019-01-17 14:31:07 +08:00
|
|
|
topic_id: post.topic_id,
|
2019-05-16 00:47:36 +08:00
|
|
|
post_number: post.post_number
|
|
|
|
).limit(10)
|
|
|
|
|
|
|
|
# Don't notify the same user about the same type of notification on the same post
|
|
|
|
existing_notification_of_same_type = existing_notifications.find { |n| n.notification_type == type }
|
2014-10-07 12:57:48 +08:00
|
|
|
|
2020-02-03 08:27:19 +08:00
|
|
|
return if existing_notification_of_same_type && !should_notify_previous?(user, post, existing_notification_of_same_type, opts)
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2016-03-04 19:55:49 +08:00
|
|
|
notification_data = {}
|
|
|
|
|
2019-12-05 17:06:06 +08:00
|
|
|
if is_liked &&
|
|
|
|
existing_notification_of_same_type &&
|
|
|
|
existing_notification_of_same_type.created_at > 1.day.ago &&
|
|
|
|
(
|
|
|
|
user.user_option.like_notification_frequency ==
|
|
|
|
UserOption.like_notification_frequency_type[:always]
|
|
|
|
)
|
|
|
|
|
|
|
|
data = existing_notification_of_same_type.data_hash
|
|
|
|
notification_data["username2"] = data["display_username"]
|
|
|
|
notification_data["count"] = (data["count"] || 1).to_i + 1
|
|
|
|
# don't use destroy so we don't trigger a notification count refresh
|
|
|
|
Notification.where(id: existing_notification_of_same_type.id).destroy_all
|
2016-03-04 19:55:49 +08:00
|
|
|
end
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
collapsed = false
|
|
|
|
|
2016-03-08 04:56:33 +08:00
|
|
|
if COLLAPSED_NOTIFICATION_TYPES.include?(type)
|
2020-01-28 02:24:11 +08:00
|
|
|
destroy_notifications(user, COLLAPSED_NOTIFICATION_TYPES, topic)
|
2014-03-18 10:12:07 +08:00
|
|
|
collapsed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
original_post = post
|
2018-12-24 19:44:50 +08:00
|
|
|
original_username = opts[:display_username].presence || post.username
|
2014-03-18 10:12:07 +08:00
|
|
|
|
|
|
|
if collapsed
|
2020-01-28 02:24:11 +08:00
|
|
|
post = first_unread_post(user, topic) || post
|
|
|
|
count = unread_count(user, topic)
|
2016-02-02 02:12:10 +08:00
|
|
|
if count > 1
|
|
|
|
I18n.with_locale(user.effective_locale) do
|
|
|
|
opts[:display_username] = I18n.t('embed.replies', count: count)
|
|
|
|
end
|
2015-01-18 15:14:59 +08:00
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
UserActionManager.notification_created(original_post, user, type, opts[:acting_user_id])
|
2014-03-18 10:12:07 +08:00
|
|
|
|
2020-01-28 02:24:11 +08:00
|
|
|
topic_title = topic.title
|
2016-02-23 08:34:16 +08:00
|
|
|
# when sending a private message email, keep the original title
|
2020-01-28 02:24:11 +08:00
|
|
|
if topic.private_message? && modifications = post.revisions.map(&:modifications)
|
2016-02-24 22:34:40 +08:00
|
|
|
if first_title_modification = modifications.find { |m| m.has_key?("title") }
|
2016-02-23 08:34:16 +08:00
|
|
|
topic_title = first_title_modification["title"][0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
notification_data.merge!(topic_title: topic_title,
|
|
|
|
original_post_id: original_post.id,
|
|
|
|
original_post_type: original_post.post_type,
|
|
|
|
original_username: original_username,
|
2018-08-24 21:13:07 +08:00
|
|
|
revision_number: opts[:revision_number],
|
2017-07-28 09:20:09 +08:00
|
|
|
display_username: opts[:display_username] || post.user.username)
|
2015-11-30 14:03:47 +08:00
|
|
|
|
|
|
|
if group = opts[:group]
|
|
|
|
notification_data[:group_id] = group.id
|
|
|
|
notification_data[:group_name] = group.name
|
|
|
|
end
|
|
|
|
|
2017-11-10 23:10:25 +08:00
|
|
|
if original_post.via_email && (incoming_email = original_post.incoming_email)
|
|
|
|
skip_send_email = contains_email_address?(incoming_email.to_addresses, user) ||
|
|
|
|
contains_email_address?(incoming_email.cc_addresses, user)
|
|
|
|
else
|
|
|
|
skip_send_email = opts[:skip_send_email]
|
|
|
|
end
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
# Create the notification
|
2018-05-24 10:00:15 +08:00
|
|
|
created = user.notifications.create!(
|
2018-01-22 14:11:52 +08:00
|
|
|
notification_type: type,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
post_number: post.post_number,
|
|
|
|
post_action_id: opts[:post_action_id],
|
|
|
|
data: notification_data.to_json,
|
|
|
|
skip_send_email: skip_send_email
|
|
|
|
)
|
|
|
|
|
2019-05-16 00:47:36 +08:00
|
|
|
if created.id && existing_notifications.empty? && NOTIFIABLE_TYPES.include?(type) && !user.suspended?
|
2018-12-05 13:39:17 +08:00
|
|
|
create_notification_alert(user: user, post: original_post, notification_type: type, username: original_username)
|
2018-01-22 14:11:52 +08:00
|
|
|
end
|
2018-05-24 23:27:43 +08:00
|
|
|
|
2018-01-22 14:11:52 +08:00
|
|
|
created.id ? created : nil
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
2018-12-04 14:54:27 +08:00
|
|
|
def create_notification_alert(user:, post:, notification_type:, excerpt: nil, username: nil)
|
|
|
|
if post_url = post.url
|
|
|
|
payload = {
|
|
|
|
notification_type: notification_type,
|
|
|
|
post_number: post.post_number,
|
|
|
|
topic_title: post.topic.title,
|
|
|
|
topic_id: post.topic.id,
|
2019-04-15 22:15:17 +08:00
|
|
|
excerpt: excerpt || post.excerpt(400, text_entities: true, strip_links: true, remap_emoji: true),
|
2018-12-04 14:54:27 +08:00
|
|
|
username: username || post.username,
|
|
|
|
post_url: post_url
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:17:15 +08:00
|
|
|
DiscourseEvent.trigger(:pre_notification_alert, user, payload)
|
2018-12-04 14:54:27 +08:00
|
|
|
MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])
|
|
|
|
push_notification(user, payload)
|
|
|
|
DiscourseEvent.trigger(:post_notification_alert, user, payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-10 23:10:25 +08:00
|
|
|
def contains_email_address?(addresses, user)
|
|
|
|
return false if addresses.blank?
|
|
|
|
addresses.split(";").include?(user.email)
|
|
|
|
end
|
|
|
|
|
2016-08-26 10:47:10 +08:00
|
|
|
def push_notification(user, payload)
|
2018-05-24 10:02:26 +08:00
|
|
|
if user.push_subscriptions.exists?
|
|
|
|
Jobs.enqueue(:send_push_notification, user_id: user.id, payload: payload)
|
|
|
|
end
|
|
|
|
|
2016-10-14 13:05:27 +08:00
|
|
|
if SiteSetting.allow_user_api_key_scopes.split("|").include?("push") && SiteSetting.allowed_user_api_push_urls.present?
|
2016-08-26 10:47:10 +08:00
|
|
|
clients = user.user_api_keys
|
2018-05-26 08:09:48 +08:00
|
|
|
.where("('push' = ANY(scopes) OR 'notifications' = ANY(scopes))")
|
2019-10-16 23:27:07 +08:00
|
|
|
.where("push_url IS NOT NULL AND push_url <> ''")
|
2018-05-26 08:09:48 +08:00
|
|
|
.where("position(push_url IN ?) > 0", SiteSetting.allowed_user_api_push_urls)
|
|
|
|
.where("revoked_at IS NULL")
|
2017-07-28 09:20:09 +08:00
|
|
|
.pluck(:client_id, :push_url)
|
2016-08-26 10:47:10 +08:00
|
|
|
|
|
|
|
if clients.length > 0
|
|
|
|
Jobs.enqueue(:push_notification, clients: clients, payload: payload, user_id: user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-30 14:03:47 +08:00
|
|
|
def expand_group_mentions(groups, post)
|
|
|
|
return unless post.user && groups
|
|
|
|
|
2019-12-12 19:13:40 +08:00
|
|
|
Group.mentionable(post.user, include_public: false).where(id: groups.map(&:id)).each do |group|
|
2015-11-30 14:03:47 +08:00
|
|
|
next if group.user_count >= SiteSetting.max_users_notified_per_group_mention
|
|
|
|
yield group, group.users
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
# TODO: Move to post-analyzer?
|
2015-11-30 14:03:47 +08:00
|
|
|
def extract_mentions(post)
|
|
|
|
mentions = post.raw_mentions
|
2018-11-27 19:43:05 +08:00
|
|
|
return if mentions.blank?
|
2015-11-30 14:03:47 +08:00
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
groups = Group.where('LOWER(name) IN (?)', mentions)
|
2018-11-27 19:43:05 +08:00
|
|
|
mentions -= groups.map(&:name).map(&:downcase)
|
|
|
|
groups = nil if groups.empty?
|
2015-11-30 14:03:47 +08:00
|
|
|
|
2018-11-27 19:43:05 +08:00
|
|
|
if mentions.present?
|
|
|
|
users = User.where(username_lower: mentions).where.not(id: post.user_id)
|
|
|
|
users = nil if users.empty?
|
2018-11-24 01:25:40 +08:00
|
|
|
end
|
2015-11-30 14:03:47 +08:00
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
[groups, users]
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Move to post-analyzer?
|
|
|
|
# Returns a list of users who were quoted in the post
|
|
|
|
def extract_quoted_users(post)
|
|
|
|
post.raw.scan(/\[quote=\"([^,]+),.+\"\]/).uniq.map do |m|
|
2015-12-18 23:32:53 +08:00
|
|
|
User.find_by("username_lower = :username AND id != :id", username: m.first.strip.downcase, id: post.user_id)
|
2014-03-18 10:12:07 +08:00
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_linked_users(post)
|
2014-07-29 13:40:05 +08:00
|
|
|
post.topic_links.where(reflection: false).map do |link|
|
2014-03-19 09:07:48 +08:00
|
|
|
linked_post = link.link_post
|
|
|
|
if !linked_post && topic = link.link_topic
|
2017-09-25 16:16:37 +08:00
|
|
|
linked_post = topic.posts.find_by(post_number: 1)
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
2015-12-31 07:51:58 +08:00
|
|
|
(linked_post && post.user_id != linked_post.user_id && linked_post.user) || nil
|
2014-03-18 10:12:07 +08:00
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
# Notify a bunch of users
|
2017-06-12 15:41:39 +08:00
|
|
|
def notify_non_pm_users(users, type, post, opts = {})
|
2018-03-08 18:40:48 +08:00
|
|
|
return [] if post.topic&.private_message?
|
2015-05-26 01:15:00 +08:00
|
|
|
|
2018-03-08 18:40:48 +08:00
|
|
|
notify_users(users, type, post, opts)
|
|
|
|
end
|
2016-03-22 11:28:14 +08:00
|
|
|
|
2018-03-08 18:40:48 +08:00
|
|
|
def notify_users(users, type, post, opts = {})
|
2016-03-22 11:28:14 +08:00
|
|
|
users = [users] unless users.is_a?(Array)
|
2020-01-17 02:17:16 +08:00
|
|
|
users.reject!(&:staged?) if post.topic&.private_message?
|
2015-05-26 01:15:00 +08:00
|
|
|
|
2018-05-24 23:52:59 +08:00
|
|
|
warn_if_not_sidekiq
|
|
|
|
|
2018-05-24 23:27:43 +08:00
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
|
|
|
users.each do |u|
|
|
|
|
create_notification(u, Notification.types[type], post, opts)
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
2018-03-08 18:40:48 +08:00
|
|
|
|
|
|
|
users
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
2018-05-15 15:51:32 +08:00
|
|
|
def notify_pm_users(post, reply_to_user, notified)
|
2016-07-08 10:58:18 +08:00
|
|
|
return unless post.topic
|
|
|
|
|
2018-05-24 23:52:59 +08:00
|
|
|
warn_if_not_sidekiq
|
|
|
|
|
2018-05-24 23:27:43 +08:00
|
|
|
# users that aren't part of any mentioned groups
|
|
|
|
users = directly_targeted_users(post).reject { |u| notified.include?(u) }
|
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
|
|
|
users.each do |user|
|
|
|
|
notification_level = TopicUser.get(post.topic, user)&.notification_level
|
|
|
|
if reply_to_user == user || notification_level == TopicUser.notification_levels[:watching] || user.staged?
|
|
|
|
create_notification(user, Notification.types[:private_message], post)
|
2018-05-15 15:51:32 +08:00
|
|
|
end
|
2018-05-24 23:27:43 +08:00
|
|
|
end
|
2018-05-15 15:51:32 +08:00
|
|
|
|
2018-05-24 23:27:43 +08:00
|
|
|
# users that are part of all mentionned groups
|
|
|
|
users = indirectly_targeted_users(post).reject { |u| notified.include?(u) }
|
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
|
|
|
users.each do |user|
|
|
|
|
case TopicUser.get(post.topic, user)&.notification_level
|
|
|
|
when TopicUser.notification_levels[:watching]
|
|
|
|
# only create a notification when watching the group
|
|
|
|
create_notification(user, Notification.types[:private_message], post)
|
|
|
|
when TopicUser.notification_levels[:tracking]
|
|
|
|
notify_group_summary(user, post)
|
2018-05-15 15:51:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-29 08:03:47 +08:00
|
|
|
def notify_post_users(post, notified, include_category_watchers: true, include_tag_watchers: true)
|
2018-05-15 15:51:32 +08:00
|
|
|
return unless post.topic
|
2016-07-08 10:58:18 +08:00
|
|
|
|
2018-05-24 23:52:59 +08:00
|
|
|
warn_if_not_sidekiq
|
|
|
|
|
2019-05-03 06:17:27 +08:00
|
|
|
condition = +<<~SQL
|
2018-05-15 15:51:32 +08:00
|
|
|
id IN (
|
|
|
|
SELECT user_id
|
|
|
|
FROM topic_users
|
|
|
|
WHERE notification_level = :watching
|
|
|
|
AND topic_id = :topic_id
|
2019-11-12 13:44:46 +08:00
|
|
|
/*category*/
|
|
|
|
/*tags*/
|
|
|
|
)
|
|
|
|
SQL
|
2016-07-08 10:58:18 +08:00
|
|
|
|
2019-11-12 13:44:46 +08:00
|
|
|
if include_category_watchers
|
|
|
|
condition.sub! "/*category*/", <<~SQL
|
|
|
|
UNION
|
2016-07-08 10:58:18 +08:00
|
|
|
|
2018-05-15 15:51:32 +08:00
|
|
|
SELECT cu.user_id
|
|
|
|
FROM category_users cu
|
|
|
|
LEFT JOIN topic_users tu ON tu.user_id = cu.user_id
|
|
|
|
AND tu.topic_id = :topic_id
|
|
|
|
WHERE cu.notification_level = :watching
|
|
|
|
AND cu.category_id = :category_id
|
|
|
|
AND tu.user_id IS NULL
|
2019-11-12 13:44:46 +08:00
|
|
|
SQL
|
|
|
|
end
|
2016-07-08 10:58:18 +08:00
|
|
|
|
|
|
|
tag_ids = post.topic.topic_tags.pluck('topic_tags.tag_id')
|
|
|
|
|
2019-11-12 13:44:46 +08:00
|
|
|
if include_tag_watchers && tag_ids.present?
|
2018-05-15 15:51:32 +08:00
|
|
|
condition.sub! "/*tags*/", <<~SQL
|
|
|
|
UNION
|
|
|
|
|
|
|
|
SELECT tag_users.user_id
|
|
|
|
FROM tag_users
|
|
|
|
LEFT JOIN topic_users tu ON tu.user_id = tag_users.user_id
|
|
|
|
AND tu.topic_id = :topic_id
|
|
|
|
WHERE tag_users.notification_level = :watching
|
|
|
|
AND tag_users.tag_id IN (:tag_ids)
|
|
|
|
AND tu.user_id IS NULL
|
|
|
|
SQL
|
2016-07-08 10:58:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
notify = User.where(condition,
|
2018-05-15 15:51:32 +08:00
|
|
|
watching: TopicUser.notification_levels[:watching],
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
category_id: post.topic.category_id,
|
|
|
|
tag_ids: tag_ids
|
|
|
|
)
|
2015-11-30 14:03:47 +08:00
|
|
|
|
2015-12-18 23:32:53 +08:00
|
|
|
exclude_user_ids = notified.map(&:id)
|
2016-07-08 10:58:18 +08:00
|
|
|
notify = notify.where("id NOT IN (?)", exclude_user_ids) if exclude_user_ids.present?
|
2015-11-30 14:03:47 +08:00
|
|
|
|
2018-05-24 23:27:43 +08:00
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, notify, post)
|
2020-01-21 05:41:13 +08:00
|
|
|
|
2020-02-06 20:14:19 +08:00
|
|
|
already_seen_user_ids = Set.new TopicUser.where(topic_id: post.topic.id).where("highest_seen_post_number >= ?", post.post_number).pluck(:user_id)
|
2020-01-21 05:41:13 +08:00
|
|
|
|
2020-02-06 20:14:19 +08:00
|
|
|
each_user_in_batches(notify) do |user|
|
|
|
|
notification_type = already_seen_user_ids.include?(user.id) ? Notification.types[:edited] : Notification.types[:posted]
|
2020-01-21 05:41:13 +08:00
|
|
|
create_notification(user, notification_type, post)
|
2015-12-18 23:32:53 +08:00
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
2015-11-30 14:03:47 +08:00
|
|
|
|
2018-05-24 23:52:59 +08:00
|
|
|
def warn_if_not_sidekiq
|
|
|
|
Rails.logger.warn("PostAlerter.#{caller_locations(1, 1)[0].label} was called outside of sidekiq") unless Sidekiq.server?
|
|
|
|
end
|
2020-02-06 20:14:19 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def each_user_in_batches(users)
|
|
|
|
# This is race-condition-safe, unlike #find_in_batches
|
|
|
|
users.pluck(:id).each_slice(USER_BATCH_SIZE) do |user_ids_batch|
|
|
|
|
User.where(id: user_ids_batch).each { |user| yield(user) }
|
|
|
|
end
|
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|