2016-12-21 23:46:22 -06:00
|
|
|
class PostActionNotifier
|
2014-03-17 21:12:07 -05:00
|
|
|
|
2016-12-21 23:46:22 -06:00
|
|
|
def self.disable
|
|
|
|
@disabled = true
|
2016-03-04 05:55:49 -06:00
|
|
|
end
|
|
|
|
|
2016-12-21 23:46:22 -06:00
|
|
|
def self.enable
|
|
|
|
@disabled = false
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2016-12-21 23:46:22 -06:00
|
|
|
def self.alerter
|
|
|
|
@alerter ||= PostAlerter.new
|
2013-02-07 09:45:24 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2016-12-21 23:46:22 -06:00
|
|
|
def self.refresh_like_notification(post, read)
|
2018-01-17 13:36:20 -06:00
|
|
|
return unless post && post.user_id && post.topic
|
2016-03-04 05:55:49 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-12-21 23:46:22 -06:00
|
|
|
def self.post_action_deleted(post_action)
|
|
|
|
return if @disabled
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# We only care about deleting post actions for now
|
2013-02-28 12:54:12 -06:00
|
|
|
return if post_action.deleted_at.blank?
|
2016-03-04 05:55:49 -06: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 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2016-12-21 23:46:22 -06:00
|
|
|
def self.post_action_created(post_action)
|
|
|
|
return if @disabled
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# We only notify on likes for now
|
|
|
|
return unless post_action.is_like?
|
|
|
|
|
|
|
|
post = post_action.post
|
2013-02-07 09:45:24 -06:00
|
|
|
return if post_action.user.blank?
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2019-01-15 20:40:16 -06:00
|
|
|
user_notifications = post.user.notifications
|
|
|
|
|
|
|
|
consolidation_window =
|
|
|
|
SiteSetting.likes_notification_consolidation_window_mins.minutes.ago
|
|
|
|
|
|
|
|
liked_by_user_notifications =
|
|
|
|
user_notifications
|
|
|
|
.get_liked_by(post_action.user)
|
|
|
|
.where("created_at > ?", consolidation_window)
|
|
|
|
|
|
|
|
user_liked_consolidated_notification =
|
|
|
|
user_notifications
|
|
|
|
.where(
|
2019-01-16 01:01:13 -06:00
|
|
|
"
|
|
|
|
created_at > ? AND
|
|
|
|
notification_type = ? AND
|
|
|
|
data::json ->> 'display_username' = ?
|
|
|
|
",
|
2019-01-15 20:40:16 -06:00
|
|
|
consolidation_window,
|
2019-01-16 01:01:13 -06:00
|
|
|
Notification.types[:liked_consolidated],
|
|
|
|
post_action.user.username_lower
|
2019-01-15 20:40:16 -06:00
|
|
|
)
|
|
|
|
.first
|
|
|
|
|
|
|
|
if user_liked_consolidated_notification
|
|
|
|
update_consolidated_liked_notification_count!(
|
|
|
|
user_liked_consolidated_notification
|
|
|
|
)
|
|
|
|
elsif (
|
|
|
|
liked_by_user_notifications.count >=
|
|
|
|
SiteSetting.likes_notification_consolidation_threshold
|
2014-10-27 16:06:43 -05:00
|
|
|
)
|
2019-01-15 20:40:16 -06:00
|
|
|
create_consolidated_liked_notification!(
|
|
|
|
liked_by_user_notifications,
|
|
|
|
post,
|
|
|
|
post_action
|
|
|
|
)
|
|
|
|
else
|
|
|
|
alerter.create_notification(
|
|
|
|
post.user,
|
|
|
|
Notification.types[:liked],
|
|
|
|
post,
|
|
|
|
display_username: post_action.user.username,
|
|
|
|
post_action_id: post_action.id,
|
|
|
|
user_id: post_action.user_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update_consolidated_liked_notification_count!(notification)
|
|
|
|
Notification.transaction do
|
|
|
|
data = JSON.parse(notification.data)
|
|
|
|
data["count"] += 1
|
|
|
|
|
|
|
|
notification.update!(
|
|
|
|
data: data.to_json,
|
|
|
|
read: false
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private_class_method :update_consolidated_liked_notification_count!
|
|
|
|
|
|
|
|
def self.create_consolidated_liked_notification!(notifications,
|
|
|
|
post,
|
|
|
|
post_action)
|
|
|
|
|
|
|
|
Notification.transaction do
|
|
|
|
timestamp = notifications.last.created_at
|
|
|
|
|
|
|
|
Notification.create!(
|
|
|
|
notification_type: Notification.types[:liked_consolidated],
|
|
|
|
user_id: post.user_id,
|
|
|
|
data: {
|
|
|
|
username: post_action.user.username,
|
|
|
|
display_username: post_action.user.username,
|
|
|
|
count: notifications.count + 1
|
|
|
|
}.to_json,
|
|
|
|
updated_at: timestamp,
|
|
|
|
created_at: timestamp
|
|
|
|
)
|
|
|
|
|
|
|
|
notifications.delete_all
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2019-01-15 20:40:16 -06:00
|
|
|
private_class_method :create_consolidated_liked_notification!
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2016-12-21 23:46:22 -06:00
|
|
|
def self.after_create_post_revision(post_revision)
|
|
|
|
return if @disabled
|
2016-03-04 05:55:49 -06:00
|
|
|
|
2013-12-11 20:41:34 -06:00
|
|
|
post = post_revision.post
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-01-06 01:12:51 -06:00
|
|
|
return unless post
|
2013-12-11 20:41:34 -06:00
|
|
|
return if post_revision.user.blank?
|
|
|
|
return if post_revision.user_id == post.user_id
|
2018-05-15 13:50:31 -05:00
|
|
|
return if post.topic.blank?
|
2013-02-05 13:16:51 -06:00
|
|
|
return if post.topic.private_message?
|
2014-09-09 11:56:04 -05:00
|
|
|
return if SiteSetting.disable_edit_notifications && post_revision.user_id == Discourse::SYSTEM_USER_ID
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-10-06 23:57:48 -05:00
|
|
|
alerter.create_notification(
|
|
|
|
post.user,
|
|
|
|
Notification.types[:edited],
|
|
|
|
post,
|
2014-10-27 16:06:43 -05:00
|
|
|
display_username: post_revision.user.username,
|
2018-08-24 08:13:07 -05:00
|
|
|
acting_user_id: post_revision.try(:user_id),
|
|
|
|
revision_number: post_revision.number
|
2014-10-06 23:57:48 -05:00
|
|
|
)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2018-04-06 16:37:29 -05:00
|
|
|
def self.after_post_unhide(post, flaggers)
|
|
|
|
return if @disabled || post.last_editor.blank? || flaggers.blank?
|
|
|
|
|
|
|
|
flaggers.each do |flagger|
|
|
|
|
alerter.create_notification(
|
|
|
|
flagger,
|
|
|
|
Notification.types[:edited],
|
|
|
|
post,
|
|
|
|
display_username: post.last_editor.username,
|
|
|
|
acting_user_id: post.last_editor.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|