2013-02-05 14:16:51 -05:00
|
|
|
class PostAlertObserver < ActiveRecord::Observer
|
|
|
|
observe :post, VestalVersions::Version, :post_action
|
|
|
|
|
|
|
|
# Dispatch to an after_save_#{class_name} method
|
|
|
|
def after_save(model)
|
|
|
|
method_name = callback_for('after_save', model)
|
|
|
|
send(method_name, model) if respond_to?(method_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Dispatch to an after_create_#{class_name} method
|
|
|
|
def after_create(model)
|
|
|
|
method_name = callback_for('after_create', model)
|
|
|
|
send(method_name, model) if respond_to?(method_name)
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# We need to consider new people to mention / quote when a post is edited
|
|
|
|
def after_save_post(post)
|
|
|
|
mentioned_users = extract_mentioned_users(post)
|
|
|
|
quoted_users = extract_quoted_users(post)
|
|
|
|
|
|
|
|
reply_to_user = post.reply_notification_target
|
|
|
|
notify_users(mentioned_users - [reply_to_user], :mentioned, post)
|
|
|
|
notify_users(quoted_users - mentioned_users - [reply_to_user], :quoted, post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_save_post_action(post_action)
|
|
|
|
# We only care about deleting post actions for now
|
2013-02-28 13:54:12 -05:00
|
|
|
return if post_action.deleted_at.blank?
|
|
|
|
Notification.where(post_action_id: post_action.id).each(&:destroy)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_post_action(post_action)
|
|
|
|
# We only notify on likes for now
|
|
|
|
return unless post_action.is_like?
|
|
|
|
|
|
|
|
post = post_action.post
|
2013-02-07 10:45:24 -05:00
|
|
|
return if post_action.user.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
return if post.topic.private_message?
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
create_notification(post.user,
|
2013-03-01 07:07:44 -05:00
|
|
|
Notification.types[:liked],
|
2013-02-07 10:45:24 -05:00
|
|
|
post,
|
2013-02-05 14:16:51 -05:00
|
|
|
display_username: post_action.user.username,
|
2013-02-07 10:45:24 -05:00
|
|
|
post_action_id: post_action.id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_version(version)
|
|
|
|
post = version.versioned
|
|
|
|
|
|
|
|
return unless post.is_a?(Post)
|
|
|
|
return if version.user.blank?
|
|
|
|
return if version.user_id == post.user_id
|
|
|
|
return if post.topic.private_message?
|
|
|
|
|
2013-03-01 07:07:44 -05:00
|
|
|
create_notification(post.user, Notification.types[:edited], post, display_username: version.user.username)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def after_create_post(post)
|
|
|
|
if post.topic.private_message?
|
|
|
|
# If it's a private message, notify the topic_allowed_users
|
2013-03-01 07:07:44 -05:00
|
|
|
post.topic.topic_allowed_users.reject{ |a| a.user_id == post.user_id }.each do |a|
|
|
|
|
create_notification(a.user, Notification.types[:private_message], post)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
# If it's not a private message, notify the users
|
|
|
|
notify_post_users(post)
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def callback_for(action, model)
|
|
|
|
"#{action}_#{model.class.name.underscore.gsub(/.+\//, '')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_notification(user, type, post, opts={})
|
|
|
|
return if user.blank?
|
|
|
|
|
2013-02-19 12:59:46 -05:00
|
|
|
# Make sure the user can see the post
|
|
|
|
return unless Guardian.new(user).can_see?(post)
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# skip if muted on the topic
|
2013-03-06 15:17:07 -05:00
|
|
|
return if TopicUser.get(post.topic, user).try(:notification_level) == TopicUser.notification_levels[:muted]
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Don't notify the same user about the same notification on the same post
|
|
|
|
return if user.notifications.exists?(notification_type: type, topic_id: post.topic_id, post_number: post.post_number)
|
2013-02-27 15:38:44 -05:00
|
|
|
|
|
|
|
# Create the notification
|
2013-02-07 10:45:24 -05:00
|
|
|
user.notifications.create(notification_type: type,
|
2013-02-05 14:16:51 -05:00
|
|
|
topic_id: post.topic_id,
|
|
|
|
post_number: post.post_number,
|
|
|
|
post_action_id: opts[:post_action_id],
|
2013-02-28 13:54:12 -05:00
|
|
|
data: { topic_title: post.topic.title,
|
|
|
|
display_username: opts[:display_username] || post.user.username }.to_json)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a list users who have been mentioned
|
|
|
|
def extract_mentioned_users(post)
|
2013-02-28 13:54:12 -05:00
|
|
|
User.where(username_lower: post.raw_mentions).where("id <> ?", post.user_id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a list of users who were quoted in the post
|
|
|
|
def extract_quoted_users(post)
|
|
|
|
result = []
|
|
|
|
post.raw.scan(/\[quote=\"([^,]+),.+\"\]/).uniq.each do |m|
|
|
|
|
username = m.first.strip.downcase
|
2013-02-12 02:49:57 -05:00
|
|
|
user = User.where("username_lower = :username and id != :id", username: username, id: post.user_id).first
|
2013-02-05 14:16:51 -05:00
|
|
|
result << user if user.present?
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
# Notify a bunch of users
|
|
|
|
def notify_users(users, type, post)
|
|
|
|
users = [users] unless users.is_a?(Array)
|
|
|
|
users.each do |u|
|
2013-03-01 07:07:44 -05:00
|
|
|
create_notification(u, Notification.types[type], post)
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
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)
|
|
|
|
|
|
|
|
# find all users watching
|
|
|
|
if post.post_number > 1
|
|
|
|
exclude_user_ids = []
|
|
|
|
exclude_user_ids << post.user_id
|
|
|
|
exclude_user_ids << reply_to_user.id if reply_to_user.present?
|
2013-02-28 13:54:12 -05:00
|
|
|
exclude_user_ids << extract_mentioned_users(post).map(&:id)
|
|
|
|
exclude_user_ids << extract_quoted_users(post).map(&:id)
|
2013-02-05 14:16:51 -05:00
|
|
|
exclude_user_ids.flatten!
|
2013-03-06 15:17:07 -05:00
|
|
|
TopicUser.where(topic_id: post.topic_id, notification_level: TopicUser.notification_levels[:watching]).includes(:user).each do |tu|
|
2013-03-01 07:07:44 -05:00
|
|
|
create_notification(tu.user, Notification.types[:posted], post) unless exclude_user_ids.include?(tu.user_id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|