2013-02-05 14:16:51 -05:00
|
|
|
class PostAlertObserver < ActiveRecord::Observer
|
2014-03-17 22:12:07 -04:00
|
|
|
observe :post_action, :post_revision
|
|
|
|
|
2016-03-04 06:55:49 -05:00
|
|
|
def self.alerter
|
2014-03-17 22:12:07 -04:00
|
|
|
@alerter ||= PostAlerter.new
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-03-04 06:55:49 -05:00
|
|
|
def alerter
|
|
|
|
self.class.alerter
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# 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
|
|
|
|
2016-03-04 06:55:49 -05:00
|
|
|
def refresh_like_notification(post, read)
|
|
|
|
return unless post && post.user_id
|
|
|
|
|
|
|
|
usernames = post.post_actions.where(post_action_type_id: PostActionType.types[:like])
|
|
|
|
.joins(:user)
|
|
|
|
.order('post_actions.created_at desc')
|
|
|
|
.where('post_actions.created_at > ?', 1.day.ago)
|
|
|
|
.pluck(:username)
|
|
|
|
|
|
|
|
if usernames.length > 0
|
|
|
|
data = {
|
|
|
|
topic_title: post.topic.title,
|
|
|
|
username: usernames[0],
|
|
|
|
display_username: usernames[0],
|
|
|
|
username2: usernames[1],
|
|
|
|
count: usernames.length
|
|
|
|
}
|
|
|
|
Notification.create(
|
|
|
|
notification_type: Notification.types[:liked],
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
post_number: post.post_number,
|
|
|
|
user_id: post.user_id,
|
|
|
|
read: read,
|
|
|
|
data: data.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
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?
|
2016-03-04 06:55:49 -05:00
|
|
|
|
|
|
|
if post_action.post_action_type_id == PostActionType.types[:like] && post_action.post
|
|
|
|
|
|
|
|
read = true
|
|
|
|
|
|
|
|
Notification.where(
|
|
|
|
topic_id: post_action.post.topic_id,
|
|
|
|
user_id: post_action.post.user_id,
|
|
|
|
post_number: post_action.post.post_number,
|
|
|
|
notification_type: Notification.types[:liked]
|
|
|
|
).each do |notification|
|
|
|
|
read = false unless notification.read
|
|
|
|
notification.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
refresh_like_notification(post_action.post, read)
|
|
|
|
else
|
|
|
|
# not using destroy_all cause we want stuff to trigger
|
|
|
|
Notification.where(post_action_id: post_action.id).each(&:destroy)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-03-04 06:55:49 -05:00
|
|
|
def self.after_create_post_action(post_action)
|
2013-02-05 14:16:51 -05:00
|
|
|
# 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
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
alerter.create_notification(
|
|
|
|
post.user,
|
|
|
|
Notification.types[:liked],
|
|
|
|
post,
|
|
|
|
display_username: post_action.user.username,
|
2015-03-25 21:08:04 -04:00
|
|
|
post_action_id: post_action.id,
|
|
|
|
user_id: post_action.user_id
|
2014-10-27 17:06:43 -04:00
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-03-04 06:55:49 -05:00
|
|
|
def after_create_post_action(post_action)
|
|
|
|
self.class.after_create_post_action(post_action)
|
|
|
|
end
|
|
|
|
|
2013-12-11 21:41:34 -05:00
|
|
|
def after_create_post_revision(post_revision)
|
|
|
|
post = post_revision.post
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-01-06 02:12:51 -05:00
|
|
|
return unless post
|
2013-12-11 21:41:34 -05:00
|
|
|
return if post_revision.user.blank?
|
|
|
|
return if post_revision.user_id == post.user_id
|
2013-02-05 14:16:51 -05:00
|
|
|
return if post.topic.private_message?
|
2014-09-09 12:56:04 -04:00
|
|
|
return if SiteSetting.disable_edit_notifications && post_revision.user_id == Discourse::SYSTEM_USER_ID
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-10-07 00:57:48 -04:00
|
|
|
alerter.create_notification(
|
|
|
|
post.user,
|
|
|
|
Notification.types[:edited],
|
|
|
|
post,
|
2014-10-27 17:06:43 -04:00
|
|
|
display_username: post_revision.user.username,
|
|
|
|
acting_user_id: post_revision.try(:user_id)
|
2014-10-07 00:57:48 -04:00
|
|
|
)
|
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
|
|
|
protected
|
|
|
|
|
|
|
|
def callback_for(action, model)
|
|
|
|
"#{action}_#{model.class.name.underscore.gsub(/.+\//, '')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|