2013-02-06 03:16:51 +08:00
|
|
|
class PostAlertObserver < ActiveRecord::Observer
|
2014-03-18 10:12:07 +08:00
|
|
|
observe :post_action, :post_revision
|
|
|
|
|
|
|
|
def alerter
|
|
|
|
@alerter ||= PostAlerter.new
|
|
|
|
end
|
2013-02-06 03:16:51 +08: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 23:45:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def after_save_post_action(post_action)
|
|
|
|
# We only care about deleting post actions for now
|
2013-03-01 02:54:12 +08:00
|
|
|
return if post_action.deleted_at.blank?
|
|
|
|
Notification.where(post_action_id: post_action.id).each(&:destroy)
|
2013-02-06 03:16:51 +08: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 23:45:24 +08:00
|
|
|
return if post_action.user.blank?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-10-28 05:06:43 +08:00
|
|
|
alerter.create_notification(
|
|
|
|
post.user,
|
|
|
|
Notification.types[:liked],
|
|
|
|
post,
|
|
|
|
display_username: post_action.user.username,
|
2015-03-26 09:08:04 +08:00
|
|
|
post_action_id: post_action.id,
|
|
|
|
user_id: post_action.user_id
|
2014-10-28 05:06:43 +08:00
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-12-12 10:41:34 +08:00
|
|
|
def after_create_post_revision(post_revision)
|
|
|
|
post = post_revision.post
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-01-06 15:12:51 +08:00
|
|
|
return unless post
|
2013-12-12 10:41:34 +08:00
|
|
|
return if post_revision.user.blank?
|
|
|
|
return if post_revision.user_id == post.user_id
|
2013-02-06 03:16:51 +08:00
|
|
|
return if post.topic.private_message?
|
2014-09-10 00:56:04 +08:00
|
|
|
return if SiteSetting.disable_edit_notifications && post_revision.user_id == Discourse::SYSTEM_USER_ID
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-10-07 12:57:48 +08:00
|
|
|
alerter.create_notification(
|
|
|
|
post.user,
|
|
|
|
Notification.types[:edited],
|
|
|
|
post,
|
2014-10-28 05:06:43 +08:00
|
|
|
display_username: post_revision.user.username,
|
|
|
|
acting_user_id: post_revision.try(:user_id)
|
2014-10-07 12:57:48 +08:00
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def callback_for(action, model)
|
|
|
|
"#{action}_#{model.class.name.underscore.gsub(/.+\//, '')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|