2014-03-18 10:12:07 +08:00
|
|
|
class PostAlerter
|
|
|
|
|
2014-03-18 12:22:39 +08:00
|
|
|
def self.post_created(post)
|
|
|
|
alerter = PostAlerter.new
|
|
|
|
alerter.after_create_post(post)
|
|
|
|
alerter.after_save_post(post)
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2015-05-26 01:15:00 +08:00
|
|
|
def allowed_users(post)
|
|
|
|
post.topic.all_allowed_users.reject do |user|
|
|
|
|
user.blank? ||
|
|
|
|
user.id == Discourse::SYSTEM_USER_ID ||
|
|
|
|
user.id == post.user_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-18 12:22:39 +08:00
|
|
|
def after_create_post(post)
|
|
|
|
if post.topic.private_message?
|
|
|
|
# If it's a private message, notify the topic_allowed_users
|
2015-05-26 01:15:00 +08:00
|
|
|
allowed_users(post).each do |user|
|
2014-03-18 12:22:39 +08:00
|
|
|
if TopicUser.get(post.topic, user).try(:notification_level) == TopicUser.notification_levels[:tracking]
|
2015-04-18 03:38:44 +08:00
|
|
|
next unless post.reply_to_post_number || post.reply_to_post.try(:user_id) == user.id
|
2014-03-18 12:22:39 +08:00
|
|
|
end
|
|
|
|
create_notification(user, Notification.types[:private_message], post)
|
|
|
|
end
|
2015-07-25 04:39:03 +08:00
|
|
|
elsif post.post_type == Post.types[:regular]
|
2014-03-18 12:22:39 +08:00
|
|
|
# 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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
def after_save_post(post)
|
|
|
|
mentioned_users = extract_mentioned_users(post)
|
|
|
|
quoted_users = extract_quoted_users(post)
|
|
|
|
linked_users = extract_linked_users(post)
|
|
|
|
|
|
|
|
reply_to_user = post.reply_notification_target
|
|
|
|
|
|
|
|
notified = [reply_to_user]
|
|
|
|
|
|
|
|
notify_users(mentioned_users - notified, :mentioned, post)
|
|
|
|
|
|
|
|
notified += mentioned_users
|
|
|
|
|
|
|
|
notify_users(quoted_users - notified, :quoted, post)
|
|
|
|
|
|
|
|
notified += quoted_users
|
|
|
|
|
|
|
|
notify_users(linked_users - notified, :linked, post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_posts(user, topic)
|
2015-10-20 04:31:48 +08:00
|
|
|
Post.secured(Guardian.new(user))
|
|
|
|
.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)
|
|
|
|
.where('reply_to_user_id = ? OR exists(
|
|
|
|
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])
|
|
|
|
.where(topic_id: topic.id)
|
|
|
|
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-23 09:34:34 +08:00
|
|
|
# HACK so notification counts sync up correctly
|
|
|
|
user.reload
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
2015-05-04 11:49:32 +08:00
|
|
|
NOTIFIABLE_TYPES = [:mentioned, :replied, :quoted, :posted, :linked, :private_message].map{ |t|
|
|
|
|
Notification.types[t]
|
|
|
|
}
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
def create_notification(user, type, post, opts={})
|
|
|
|
return if user.blank?
|
2015-04-01 17:55:59 +08:00
|
|
|
return if user.id == Discourse::SYSTEM_USER_ID
|
2014-03-18 10:12:07 +08:00
|
|
|
|
|
|
|
# Make sure the user can see the post
|
|
|
|
return unless Guardian.new(user).can_see?(post)
|
|
|
|
|
2015-03-26 09:08:04 +08:00
|
|
|
notifier_id = opts[:user_id] || post.user_id
|
|
|
|
|
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)
|
|
|
|
.joins(:muted_user)
|
|
|
|
.where('NOT admin AND NOT moderator')
|
|
|
|
.exists?
|
2015-03-24 08:55:22 +08:00
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
# skip if muted on the topic
|
|
|
|
return if TopicUser.get(post.topic, user).try(:notification_level) == TopicUser.notification_levels[:muted]
|
|
|
|
|
|
|
|
# Don't notify the same user about the same notification on the same post
|
2014-10-07 12:57:48 +08:00
|
|
|
existing_notification = user.notifications
|
|
|
|
.order("notifications.id desc")
|
2015-06-19 10:31:36 +08:00
|
|
|
.find_by(topic_id: post.topic_id,
|
|
|
|
post_number: post.post_number,
|
|
|
|
notification_type: type)
|
2014-10-07 12:57:48 +08:00
|
|
|
|
2015-05-04 11:49:32 +08:00
|
|
|
if existing_notification && existing_notification.notification_type == type
|
2014-10-07 12:57:48 +08:00
|
|
|
return unless existing_notification.notification_type == Notification.types[:edited] &&
|
|
|
|
existing_notification.data_hash["display_username"] = opts[:display_username]
|
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
|
|
|
|
collapsed = false
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
if type == Notification.types[:replied] || type == Notification.types[:posted]
|
2014-03-18 10:12:07 +08:00
|
|
|
destroy_notifications(user, Notification.types[:replied] , post.topic)
|
|
|
|
destroy_notifications(user, Notification.types[:posted] , post.topic)
|
|
|
|
collapsed = true
|
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
if type == Notification.types[:private_message]
|
2014-03-18 10:12:07 +08:00
|
|
|
destroy_notifications(user, type, post.topic)
|
|
|
|
collapsed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
original_post = post
|
|
|
|
original_username = opts[:display_username] || post.username
|
|
|
|
|
|
|
|
if collapsed
|
|
|
|
post = first_unread_post(user,post.topic) || post
|
|
|
|
count = unread_count(user, post.topic)
|
2015-02-06 11:38:51 +08:00
|
|
|
I18n.with_locale(user.effective_locale) do
|
2015-01-18 15:14:59 +08:00
|
|
|
opts[:display_username] = I18n.t('embed.replies', count: count) if count > 1
|
|
|
|
end
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
UserActionObserver.log_notification(original_post, user, type, opts[:acting_user_id])
|
2014-03-18 10:12:07 +08: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],
|
|
|
|
data: { topic_title: post.topic.title,
|
|
|
|
original_post_id: original_post.id,
|
|
|
|
original_username: original_username,
|
|
|
|
display_username: opts[:display_username] || post.user.username }.to_json)
|
2015-05-04 11:49:32 +08:00
|
|
|
|
|
|
|
if (!existing_notification) && NOTIFIABLE_TYPES.include?(type)
|
|
|
|
|
|
|
|
# we may have an invalid post somehow, dont blow up
|
|
|
|
post_url = original_post.url rescue nil
|
|
|
|
if post_url
|
|
|
|
MessageBus.publish("/notification-alert/#{user.id}", {
|
|
|
|
notification_type: type,
|
|
|
|
post_number: original_post.post_number,
|
|
|
|
topic_title: original_post.topic.title,
|
2015-05-14 09:20:27 +08:00
|
|
|
topic_id: original_post.topic.id,
|
2015-05-04 11:49:32 +08:00
|
|
|
excerpt: original_post.excerpt(400, text_entities: true, strip_links: true),
|
|
|
|
username: original_username,
|
|
|
|
post_url: post_url
|
|
|
|
}, user_ids: [user.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Move to post-analyzer?
|
|
|
|
# Returns a list users who have been mentioned
|
|
|
|
def extract_mentioned_users(post)
|
|
|
|
User.where(username_lower: post.raw_mentions).where("id <> ?", post.user_id)
|
|
|
|
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|
|
2014-05-06 21:41:59 +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
|
|
|
|
linked_post = topic.posts(post_number: 1).first
|
2014-03-18 10:12:07 +08:00
|
|
|
end
|
2014-03-19 09:07:48 +08:00
|
|
|
linked_post && post.user_id != linked_post.user_id && linked_post.user
|
2014-03-18 10:12:07 +08:00
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
# Notify a bunch of users
|
|
|
|
def notify_users(users, type, post)
|
|
|
|
users = [users] unless users.is_a?(Array)
|
2015-05-26 01:15:00 +08:00
|
|
|
|
2015-11-14 00:35:04 +08:00
|
|
|
if post.topic.try(:private_message?)
|
2015-05-26 01:15:00 +08:00
|
|
|
whitelist = allowed_users(post)
|
|
|
|
users.reject! {|u| !whitelist.include?(u)}
|
|
|
|
end
|
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
users.each do |u|
|
|
|
|
create_notification(u, Notification.types[type], post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: This should use javascript for parsing rather than re-doing it this way.
|
|
|
|
def notify_post_users(post)
|
|
|
|
# Is this post a reply to a user?
|
|
|
|
reply_to_user = post.reply_notification_target
|
|
|
|
notify_users(reply_to_user, :replied, post)
|
|
|
|
|
|
|
|
exclude_user_ids = [] <<
|
|
|
|
post.user_id <<
|
|
|
|
extract_mentioned_users(post).map(&:id) <<
|
|
|
|
extract_quoted_users(post).map(&:id)
|
|
|
|
|
|
|
|
exclude_user_ids << reply_to_user.id if reply_to_user.present?
|
|
|
|
exclude_user_ids.flatten!
|
2015-04-21 01:34:57 +08:00
|
|
|
|
2014-03-18 10:12:07 +08:00
|
|
|
TopicUser
|
|
|
|
.where(topic_id: post.topic_id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
.includes(:user).each do |tu|
|
|
|
|
create_notification(tu.user, Notification.types[:posted], post) unless exclude_user_ids.include?(tu.user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|