2017-05-26 17:04:40 -04:00
|
|
|
require_dependency 'distributed_mutex'
|
2017-09-15 01:02:11 -04:00
|
|
|
require_dependency 'user_action_creator'
|
2017-05-26 17:04:40 -04:00
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
class PostAlerter
|
2017-06-12 03:41:39 -04:00
|
|
|
def self.post_created(post, opts = {})
|
|
|
|
alerter = PostAlerter.new(opts)
|
2015-11-30 01:03:47 -05:00
|
|
|
alerter.after_save_post(post, true)
|
2014-03-18 00:22:39 -04:00
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2017-06-12 03:41:39 -04:00
|
|
|
def initialize(default_opts = {})
|
|
|
|
@default_opts = default_opts
|
|
|
|
end
|
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
def not_allowed?(user, post)
|
|
|
|
user.blank? ||
|
2017-03-14 02:33:06 -04:00
|
|
|
user.id < 0 ||
|
2015-12-18 10:32:53 -05: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-25 13:15:00 -04:00
|
|
|
def allowed_users(post)
|
2015-12-18 10:32:53 -05:00
|
|
|
@allowed_users ||= post.topic.allowed_users.reject { |u| not_allowed?(u, post) }
|
2015-05-25 13:15:00 -04:00
|
|
|
end
|
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
def allowed_group_users(post)
|
|
|
|
@allowed_group_users ||= post.topic.allowed_group_users.reject { |u| not_allowed?(u, post) }
|
|
|
|
end
|
2014-03-18 00:22:39 -04:00
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
def directly_targeted_users(post)
|
|
|
|
allowed_users(post) - allowed_group_users(post)
|
|
|
|
end
|
2015-12-14 17:17:09 -05:00
|
|
|
|
2016-01-27 05:38:14 -05:00
|
|
|
def indirectly_targeted_users(post)
|
2015-12-18 10:32:53 -05:00
|
|
|
allowed_group_users(post)
|
|
|
|
end
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2017-09-19 09:51:10 -04:00
|
|
|
def notify_about_reply?(post)
|
|
|
|
post.post_type == Post.types[:regular] || post.post_type == Post.types[:whisper]
|
|
|
|
end
|
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
def after_save_post(post, new_record = false)
|
|
|
|
notified = [post.user]
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
# mentions (users/groups)
|
2015-11-30 01:03:47 -05:00
|
|
|
mentioned_groups, mentioned_users = extract_mentions(post)
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2016-03-08 15:26:06 -05:00
|
|
|
if mentioned_groups || mentioned_users
|
|
|
|
mentioned_opts = {}
|
|
|
|
if post.last_editor_id != post.user_id
|
|
|
|
# Mention comes from an edit by someone else, so notification should say who added the mention.
|
|
|
|
editor = post.last_editor
|
2017-07-27 21:20:09 -04:00
|
|
|
mentioned_opts = { user_id: editor.id, original_username: editor.username, display_username: editor.username }
|
2016-03-08 15:26:06 -05:00
|
|
|
end
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2016-03-08 15:26:06 -05:00
|
|
|
expand_group_mentions(mentioned_groups, post) do |group, users|
|
2017-07-27 21:20:09 -04:00
|
|
|
notify_non_pm_users(users - notified, :group_mentioned, post, mentioned_opts.merge(group: group))
|
2016-03-08 15:26:06 -05:00
|
|
|
notified += users
|
|
|
|
end
|
|
|
|
|
|
|
|
if mentioned_users
|
2016-03-21 23:28:14 -04:00
|
|
|
notify_non_pm_users(mentioned_users - notified, :mentioned, post, mentioned_opts)
|
2016-03-08 15:26:06 -05:00
|
|
|
notified += mentioned_users
|
|
|
|
end
|
2015-11-30 01:03:47 -05:00
|
|
|
end
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
# replies
|
|
|
|
reply_to_user = post.reply_notification_target
|
|
|
|
|
2017-09-19 09:51:10 -04:00
|
|
|
if new_record && reply_to_user && !notified.include?(reply_to_user) && notify_about_reply?(post)
|
2016-03-21 23:28:14 -04:00
|
|
|
notify_non_pm_users(reply_to_user, :replied, post)
|
2015-12-18 10:32:53 -05:00
|
|
|
notified += [reply_to_user]
|
|
|
|
end
|
|
|
|
|
|
|
|
# quotes
|
|
|
|
quoted_users = extract_quoted_users(post)
|
2016-03-21 23:28:14 -04:00
|
|
|
notify_non_pm_users(quoted_users - notified, :quoted, post)
|
2014-03-17 22:12:07 -04:00
|
|
|
notified += quoted_users
|
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
# linked
|
|
|
|
linked_users = extract_linked_users(post)
|
2016-03-21 23:28:14 -04:00
|
|
|
notify_non_pm_users(linked_users - notified, :linked, post)
|
2015-12-18 10:32:53 -05:00
|
|
|
notified += linked_users
|
|
|
|
|
|
|
|
# private messages
|
|
|
|
if new_record
|
|
|
|
if post.topic.private_message?
|
2016-03-21 23:28:14 -04:00
|
|
|
# users that aren't part of any mentioned groups
|
2017-07-19 16:51:32 -04:00
|
|
|
users = directly_targeted_users(post)
|
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
|
|
|
users.each do |user|
|
2016-03-21 23:28:14 -04:00
|
|
|
notification_level = TopicUser.get(post.topic, user).try(:notification_level)
|
2017-10-06 10:37:28 -04:00
|
|
|
if notified.include?(user) || notification_level == TopicUser.notification_levels[:watching] || user.staged?
|
2015-12-18 10:32:53 -05:00
|
|
|
create_notification(user, Notification.types[:private_message], post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# users that are part of all mentionned groups
|
2017-07-19 16:51:32 -04:00
|
|
|
users = indirectly_targeted_users(post)
|
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
|
|
|
users.each do |user|
|
2016-03-21 23:28:14 -04:00
|
|
|
# only create a notification when watching the group
|
|
|
|
notification_level = TopicUser.get(post.topic, user).try(:notification_level)
|
|
|
|
|
|
|
|
if notification_level == TopicUser.notification_levels[:watching]
|
|
|
|
create_notification(user, Notification.types[:private_message], post)
|
|
|
|
elsif notification_level == TopicUser.notification_levels[:tracking]
|
|
|
|
if notified.include?(user)
|
2015-12-18 10:32:53 -05:00
|
|
|
create_notification(user, Notification.types[:private_message], post)
|
2016-03-21 23:28:14 -04:00
|
|
|
else
|
2016-01-27 05:38:14 -05:00
|
|
|
notify_group_summary(user, post)
|
2015-12-18 10:32:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif post.post_type == Post.types[:regular]
|
|
|
|
# If it's not a private message and it's not an automatic post caused by a moderator action, notify the users
|
|
|
|
notify_post_users(post, notified)
|
|
|
|
end
|
2015-11-30 01:03:47 -05:00
|
|
|
end
|
|
|
|
|
2015-12-01 00:52:43 -05:00
|
|
|
sync_group_mentions(post, mentioned_groups)
|
2016-07-06 15:56:40 -04:00
|
|
|
|
2016-07-19 15:57:05 -04:00
|
|
|
if new_record && post.post_number == 1
|
2016-07-06 15:56:40 -04:00
|
|
|
topic = post.topic
|
|
|
|
|
|
|
|
if topic.present?
|
|
|
|
cat_watchers = topic.category_users
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(notification_level: CategoryUser.notification_levels[:watching_first_post])
|
|
|
|
.pluck(:user_id)
|
2016-07-06 15:56:40 -04:00
|
|
|
|
|
|
|
tag_watchers = topic.tag_users
|
2017-07-27 21:20:09 -04:00
|
|
|
.where(notification_level: TagUser.notification_levels[:watching_first_post])
|
|
|
|
.pluck(:user_id)
|
2016-07-06 15:56:40 -04:00
|
|
|
|
2016-07-21 15:05:10 -04:00
|
|
|
group_ids = topic.allowed_groups.pluck(:group_id)
|
2016-07-06 15:56:40 -04:00
|
|
|
group_watchers = GroupUser.where(group_id: group_ids,
|
|
|
|
notification_level: GroupUser.notification_levels[:watching_first_post])
|
2017-07-27 21:20:09 -04:00
|
|
|
.pluck(:user_id)
|
2016-07-06 15:56:40 -04:00
|
|
|
|
|
|
|
watchers = [cat_watchers, tag_watchers, group_watchers].flatten
|
|
|
|
|
|
|
|
notify_first_post_watchers(post, watchers)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_first_post_watchers(post, user_ids)
|
|
|
|
return if user_ids.blank?
|
|
|
|
|
|
|
|
user_ids.uniq!
|
|
|
|
|
|
|
|
# Don't notify the OP
|
|
|
|
user_ids -= [post.user_id]
|
|
|
|
|
2017-07-19 16:51:32 -04:00
|
|
|
users = User.where(id: user_ids)
|
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
|
|
|
users.each do |u|
|
2016-07-06 15:56:40 -04:00
|
|
|
create_notification(u, Notification.types[:watching_first_post], post)
|
|
|
|
end
|
2015-12-01 00:52:43 -05: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-17 22:12:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_posts(user, topic)
|
2015-10-19 16:31:48 -04:00
|
|
|
Post.secured(Guardian.new(user))
|
2017-07-27 21:20:09 -04:00
|
|
|
.where('post_number > COALESCE((
|
2014-03-17 22:12:07 -04: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-27 21:20:09 -04:00
|
|
|
.where('reply_to_user_id = ? OR exists(
|
2014-03-17 22:12:07 -04: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-27 21:20:09 -04:00
|
|
|
.where(topic_id: topic.id)
|
2014-03-17 22:12:07 -04: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
|
|
|
|
|
|
|
|
def destroy_notifications(user, type, topic)
|
|
|
|
return if user.blank?
|
|
|
|
return unless Guardian.new(user).can_see?(topic)
|
|
|
|
|
|
|
|
user.notifications.where(notification_type: type,
|
|
|
|
topic_id: topic.id).destroy_all
|
2014-05-22 21:34:34 -04:00
|
|
|
# HACK so notification counts sync up correctly
|
|
|
|
user.reload
|
2014-03-17 22:12:07 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
NOTIFIABLE_TYPES = [:mentioned, :replied, :quoted, :posted, :linked, :private_message, :group_mentioned].map { |t|
|
2015-05-03 23:49:32 -04:00
|
|
|
Notification.types[t]
|
|
|
|
}
|
|
|
|
|
2016-01-27 05:38:14 -05:00
|
|
|
def group_stats(topic)
|
|
|
|
topic.allowed_groups.map do |g|
|
|
|
|
{
|
|
|
|
group_id: g.id,
|
|
|
|
group_name: g.name.downcase,
|
|
|
|
inbox_count: Topic.exec_sql(
|
|
|
|
"SELECT COUNT(*) FROM topics t
|
2016-01-27 07:40:45 -05:00
|
|
|
JOIN topic_allowed_groups g ON g.group_id = :group_id AND g.topic_id = t.id
|
2016-01-27 08:14:26 -05:00
|
|
|
LEFT JOIN group_archived_messages a ON a.topic_id = t.id AND a.group_id = g.group_id
|
2016-01-27 07:40:45 -05:00
|
|
|
WHERE a.id IS NULL AND t.deleted_at is NULL AND t.archetype = 'private_message'",
|
2016-01-27 05:38:14 -05:00
|
|
|
group_id: g.id).values[0][0].to_i
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def notify_group_summary(user, post)
|
2016-01-27 05:38:14 -05:00
|
|
|
|
|
|
|
@group_stats ||= {}
|
|
|
|
stats = (@group_stats[post.topic_id] ||= group_stats(post.topic))
|
|
|
|
return unless stats
|
|
|
|
|
|
|
|
group_id = post.topic
|
2017-07-27 21:20:09 -04:00
|
|
|
.topic_allowed_groups
|
|
|
|
.where(group_id: user.groups.pluck(:id))
|
|
|
|
.pluck(:group_id).first
|
2016-01-27 05:38:14 -05:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
stat = stats.find { |s| s[:group_id] == group_id }
|
2016-01-27 07:13:37 -05:00
|
|
|
return unless stat && stat[:inbox_count] > 0
|
2016-01-27 05:38:14 -05:00
|
|
|
|
|
|
|
notification_type = Notification.types[:group_message_summary]
|
|
|
|
|
2017-05-26 17:04:40 -04: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 05:38:14 -05:00
|
|
|
|
2017-05-26 17:04:40 -04: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 05:38:14 -05:00
|
|
|
|
|
|
|
# TODO decide if it makes sense to also publish a desktop notification
|
|
|
|
end
|
|
|
|
|
2016-03-02 07:16:52 -05:00
|
|
|
def should_notify_edit?(notification, opts)
|
2016-03-03 00:00:14 -05:00
|
|
|
return notification.data_hash["display_username"] != opts[:display_username]
|
2016-03-02 07:16:52 -05: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
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_notify_previous?(user, notification, opts)
|
2016-03-07 15:56:33 -05:00
|
|
|
case notification.notification_type
|
|
|
|
when Notification.types[:edited] then should_notify_edit?(notification, opts)
|
|
|
|
when Notification.types[:liked] then should_notify_like?(user, notification)
|
|
|
|
else false
|
2016-03-02 07:16:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-07 15:56:33 -05:00
|
|
|
COLLAPSED_NOTIFICATION_TYPES ||= [
|
|
|
|
Notification.types[:replied],
|
|
|
|
Notification.types[:quoted],
|
|
|
|
Notification.types[:posted],
|
|
|
|
]
|
|
|
|
|
2017-06-12 03:41:39 -04:00
|
|
|
def create_notification(user, type, post, opts = {})
|
|
|
|
opts = @default_opts.merge(opts)
|
|
|
|
|
2017-07-19 16:51:32 -04:00
|
|
|
DiscourseEvent.trigger(:before_create_notification, user, type, post, opts)
|
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
return if user.blank?
|
2017-03-14 02:33:06 -04:00
|
|
|
return if user.id < 0
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2016-03-05 17:12:59 -05:00
|
|
|
return if type == Notification.types[:liked] && user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:never]
|
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
# Make sure the user can see the post
|
|
|
|
return unless Guardian.new(user).can_see?(post)
|
|
|
|
|
2016-03-08 15:26:06 -05:00
|
|
|
notifier_id = opts[:user_id] || post.user_id # xxxxx look at revision history
|
2015-03-25 21:08:04 -04:00
|
|
|
|
2015-03-23 20:55:22 -04:00
|
|
|
# apply muting here
|
2015-03-25 21:08:04 -04:00
|
|
|
return if notifier_id && MutedUser.where(user_id: user.id, muted_user_id: notifier_id)
|
2017-07-27 21:20:09 -04:00
|
|
|
.joins(:muted_user)
|
|
|
|
.where('NOT admin AND NOT moderator')
|
|
|
|
.exists?
|
2015-03-23 20:55:22 -04:00
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
# skip if muted on the topic
|
2017-06-12 03:41:39 -04:00
|
|
|
return if TopicUser.where(
|
|
|
|
topic: post.topic,
|
|
|
|
user: user,
|
|
|
|
notification_level: TopicUser.notification_levels[:muted]
|
|
|
|
).exists?
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2015-12-14 17:17:09 -05:00
|
|
|
# skip if muted on the group
|
|
|
|
if group = opts[:group]
|
2017-06-12 03:41:39 -04:00
|
|
|
return if GroupUser.where(
|
|
|
|
group_id: opts[:group_id],
|
|
|
|
user_id: user.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:muted]
|
|
|
|
).exists?
|
2015-12-14 17:17:09 -05:00
|
|
|
end
|
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
# Don't notify the same user about the same notification on the same post
|
2014-10-07 00:57:48 -04:00
|
|
|
existing_notification = user.notifications
|
2017-07-27 21:20:09 -04:00
|
|
|
.order("notifications.id DESC")
|
|
|
|
.find_by(topic_id: post.topic_id,
|
|
|
|
post_number: post.post_number,
|
|
|
|
notification_type: type)
|
2014-10-07 00:57:48 -04:00
|
|
|
|
2016-03-02 07:16:52 -05:00
|
|
|
return if existing_notification && !should_notify_previous?(user, existing_notification, opts)
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2016-03-04 06:55:49 -05:00
|
|
|
notification_data = {}
|
|
|
|
|
|
|
|
if existing_notification &&
|
|
|
|
existing_notification.created_at > 1.day.ago &&
|
|
|
|
user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:always]
|
|
|
|
|
|
|
|
data = existing_notification.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.id).destroy_all
|
|
|
|
end
|
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
collapsed = false
|
|
|
|
|
2016-03-07 15:56:33 -05:00
|
|
|
if COLLAPSED_NOTIFICATION_TYPES.include?(type)
|
|
|
|
COLLAPSED_NOTIFICATION_TYPES.each do |t|
|
|
|
|
destroy_notifications(user, t, post.topic)
|
|
|
|
end
|
2014-03-17 22:12:07 -04:00
|
|
|
collapsed = true
|
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
if type == Notification.types[:private_message]
|
2014-03-17 22:12:07 -04:00
|
|
|
destroy_notifications(user, type, post.topic)
|
|
|
|
collapsed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
original_post = post
|
2016-03-08 15:26:06 -05:00
|
|
|
original_username = opts[:display_username] || post.username # xxxxx need something here too
|
2014-03-17 22:12:07 -04:00
|
|
|
|
|
|
|
if collapsed
|
2016-02-01 13:12:10 -05:00
|
|
|
post = first_unread_post(user, post.topic) || post
|
2014-03-17 22:12:07 -04:00
|
|
|
count = unread_count(user, post.topic)
|
2016-02-01 13:12:10 -05:00
|
|
|
if count > 1
|
|
|
|
I18n.with_locale(user.effective_locale) do
|
|
|
|
opts[:display_username] = I18n.t('embed.replies', count: count)
|
|
|
|
end
|
2015-01-18 02:14:59 -05:00
|
|
|
end
|
2014-03-17 22:12:07 -04:00
|
|
|
end
|
|
|
|
|
2016-12-21 23:03:40 -05:00
|
|
|
UserActionCreator.log_notification(original_post, user, type, opts[:acting_user_id])
|
2014-03-17 22:12:07 -04:00
|
|
|
|
2016-02-22 19:34:16 -05:00
|
|
|
topic_title = post.topic.title
|
|
|
|
# when sending a private message email, keep the original title
|
2016-02-24 09:34:40 -05:00
|
|
|
if post.topic.private_message? && modifications = post.revisions.map(&:modifications)
|
|
|
|
if first_title_modification = modifications.find { |m| m.has_key?("title") }
|
2016-02-22 19:34:16 -05:00
|
|
|
topic_title = first_title_modification["title"][0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04: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,
|
|
|
|
display_username: opts[:display_username] || post.user.username)
|
2015-11-30 01:03:47 -05:00
|
|
|
|
|
|
|
if group = opts[:group]
|
|
|
|
notification_data[:group_id] = group.id
|
|
|
|
notification_data[:group_name] = group.name
|
|
|
|
end
|
|
|
|
|
2017-11-10 10:10:25 -05: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-17 22:12:07 -04:00
|
|
|
# Create the notification
|
|
|
|
user.notifications.create(notification_type: type,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
post_number: post.post_number,
|
|
|
|
post_action_id: opts[:post_action_id],
|
2017-06-12 03:41:39 -04:00
|
|
|
data: notification_data.to_json,
|
2017-11-10 10:10:25 -05:00
|
|
|
skip_send_email: skip_send_email)
|
2015-05-03 23:49:32 -04:00
|
|
|
|
2017-04-05 08:28:24 -04:00
|
|
|
if !existing_notification && NOTIFIABLE_TYPES.include?(type) && !user.suspended?
|
2015-05-03 23:49:32 -04:00
|
|
|
# we may have an invalid post somehow, dont blow up
|
|
|
|
post_url = original_post.url rescue nil
|
|
|
|
if post_url
|
2017-07-27 21:20:09 -04:00
|
|
|
payload = {
|
|
|
|
notification_type: type,
|
|
|
|
post_number: original_post.post_number,
|
|
|
|
topic_title: original_post.topic.title,
|
|
|
|
topic_id: original_post.topic.id,
|
|
|
|
excerpt: original_post.excerpt(400, text_entities: true, strip_links: true, remap_emoji: true),
|
|
|
|
username: original_username,
|
|
|
|
post_url: post_url
|
|
|
|
}
|
2016-06-30 12:12:07 -04:00
|
|
|
|
|
|
|
MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])
|
2016-08-25 22:47:10 -04:00
|
|
|
push_notification(user, payload)
|
2016-06-30 12:12:07 -04:00
|
|
|
DiscourseEvent.trigger(:post_notification_alert, user, payload)
|
2015-05-03 23:49:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
end
|
|
|
|
|
2017-11-10 10:10:25 -05:00
|
|
|
def contains_email_address?(addresses, user)
|
|
|
|
return false if addresses.blank?
|
|
|
|
addresses.split(";").include?(user.email)
|
|
|
|
end
|
|
|
|
|
2016-08-25 22:47:10 -04:00
|
|
|
def push_notification(user, payload)
|
2016-10-14 01:05:27 -04:00
|
|
|
if SiteSetting.allow_user_api_key_scopes.split("|").include?("push") && SiteSetting.allowed_user_api_push_urls.present?
|
2016-08-25 22:47:10 -04:00
|
|
|
clients = user.user_api_keys
|
2017-07-27 21:20:09 -04:00
|
|
|
.where("('push' = ANY(scopes) OR 'notifications' = ANY(scopes)) AND push_url IS NOT NULL AND position(push_url in ?) > 0 AND revoked_at IS NULL",
|
2016-08-25 22:47:10 -04:00
|
|
|
SiteSetting.allowed_user_api_push_urls)
|
2017-07-27 21:20:09 -04:00
|
|
|
.pluck(:client_id, :push_url)
|
2016-08-25 22:47:10 -04:00
|
|
|
|
|
|
|
if clients.length > 0
|
|
|
|
Jobs.enqueue(:push_notification, clients: clients, payload: payload, user_id: user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-30 01:03:47 -05:00
|
|
|
def expand_group_mentions(groups, post)
|
|
|
|
return unless post.user && groups
|
|
|
|
|
|
|
|
Group.mentionable(post.user).where(id: groups.map(&:id)).each do |group|
|
|
|
|
next if group.user_count >= SiteSetting.max_users_notified_per_group_mention
|
|
|
|
yield group, group.users
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
# TODO: Move to post-analyzer?
|
2015-11-30 01:03:47 -05:00
|
|
|
def extract_mentions(post)
|
|
|
|
mentions = post.raw_mentions
|
|
|
|
|
|
|
|
return unless mentions && mentions.length > 0
|
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
groups = Group.where('LOWER(name) IN (?)', mentions)
|
2015-11-30 01:03:47 -05:00
|
|
|
mentions -= groups.map(&:name).map(&:downcase)
|
|
|
|
|
|
|
|
return [groups, nil] unless mentions && mentions.length > 0
|
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
users = User.where(username_lower: mentions).where.not(id: post.user_id)
|
2015-11-30 01:03:47 -05:00
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
[groups, users]
|
2014-03-17 22:12:07 -04: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 10:32:53 -05:00
|
|
|
User.find_by("username_lower = :username AND id != :id", username: m.first.strip.downcase, id: post.user_id)
|
2014-03-17 22:12:07 -04:00
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_linked_users(post)
|
2014-07-29 01:40:05 -04:00
|
|
|
post.topic_links.where(reflection: false).map do |link|
|
2014-03-18 21:07:48 -04:00
|
|
|
linked_post = link.link_post
|
|
|
|
if !linked_post && topic = link.link_topic
|
2017-09-25 04:16:37 -04:00
|
|
|
linked_post = topic.posts.find_by(post_number: 1)
|
2014-03-17 22:12:07 -04:00
|
|
|
end
|
2015-12-30 18:51:58 -05:00
|
|
|
(linked_post && post.user_id != linked_post.user_id && linked_post.user) || nil
|
2014-03-17 22:12:07 -04:00
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
# Notify a bunch of users
|
2017-06-12 03:41:39 -04:00
|
|
|
def notify_non_pm_users(users, type, post, opts = {})
|
2015-05-25 13:15:00 -04:00
|
|
|
|
2016-03-21 23:28:14 -04:00
|
|
|
return if post.topic.try(:private_message?)
|
|
|
|
|
|
|
|
users = [users] unless users.is_a?(Array)
|
2015-05-25 13:15:00 -04:00
|
|
|
|
2017-07-19 16:51:32 -04:00
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
2014-03-17 22:12:07 -04:00
|
|
|
users.each do |u|
|
2015-11-30 01:03:47 -05:00
|
|
|
create_notification(u, Notification.types[type], post, opts)
|
2014-03-17 22:12:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-30 01:03:47 -05:00
|
|
|
def notify_post_users(post, notified)
|
2016-07-07 22:58:18 -04:00
|
|
|
return unless post.topic
|
|
|
|
|
|
|
|
condition = <<SQL
|
|
|
|
|
|
|
|
id IN (
|
|
|
|
SELECT user_id FROM topic_users
|
|
|
|
WHERE notification_level = :watching AND topic_id = :topic_id
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/*tags*/
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
tag_ids = post.topic.topic_tags.pluck('topic_tags.tag_id')
|
|
|
|
if tag_ids.present?
|
|
|
|
condition.sub! "/*tags*/", <<SQL
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
notify = User.where(condition,
|
|
|
|
watching: TopicUser.notification_levels[:watching],
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
category_id: post.topic.category_id,
|
|
|
|
tag_ids: tag_ids
|
|
|
|
)
|
2015-11-30 01:03:47 -05:00
|
|
|
|
2015-12-18 10:32:53 -05:00
|
|
|
exclude_user_ids = notified.map(&:id)
|
2016-07-07 22:58:18 -04:00
|
|
|
notify = notify.where("id NOT IN (?)", exclude_user_ids) if exclude_user_ids.present?
|
2015-11-30 01:03:47 -05:00
|
|
|
|
2017-07-19 16:51:32 -04:00
|
|
|
DiscourseEvent.trigger(:before_create_notifications_for_users, notify, post)
|
2016-07-07 22:58:18 -04:00
|
|
|
notify.each do |user|
|
|
|
|
create_notification(user, Notification.types[:posted], post)
|
2015-12-18 10:32:53 -05:00
|
|
|
end
|
2014-03-17 22:12:07 -04:00
|
|
|
end
|
2015-11-30 01:03:47 -05:00
|
|
|
|
2014-03-17 22:12:07 -04:00
|
|
|
end
|