mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: ensure PostAlerter is always run in sidekiq
This commit is contained in:
15
app/jobs/regular/notify_category_change.rb
Normal file
15
app/jobs/regular/notify_category_change.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
require_dependency "post_alerter"
|
||||||
|
|
||||||
|
module Jobs
|
||||||
|
class NotifyCategoryChange < Jobs::Base
|
||||||
|
def execute(args)
|
||||||
|
post = Post.find_by(id: args[:post_id])
|
||||||
|
|
||||||
|
if post&.topic
|
||||||
|
post_alerter = PostAlerter.new
|
||||||
|
post_alerter.notify_post_users(post, User.where(id: args[:notified_user_ids]))
|
||||||
|
post_alerter.notify_first_post_watchers(post, post_alerter.category_watchers(post.topic))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -4,9 +4,12 @@ module Jobs
|
|||||||
class PostAlert < Jobs::Base
|
class PostAlert < Jobs::Base
|
||||||
|
|
||||||
def execute(args)
|
def execute(args)
|
||||||
# maybe it was removed by the time we are making the post
|
post = Post.find_by(id: args[:post_id])
|
||||||
post = Post.where(id: args[:post_id]).first
|
if post&.topic
|
||||||
PostAlerter.post_created(post, args[:options] || {}) if post && post.topic
|
opts = args[:options] || {}
|
||||||
|
new_record = true == args[:new_record]
|
||||||
|
PostAlerter.new(opts).after_save_post(post, new_record)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -675,19 +675,9 @@ SQL
|
|||||||
CategoryUser.auto_watch(category_id: new_category.id, topic_id: self.id)
|
CategoryUser.auto_watch(category_id: new_category.id, topic_id: self.id)
|
||||||
CategoryUser.auto_track(category_id: new_category.id, topic_id: self.id)
|
CategoryUser.auto_track(category_id: new_category.id, topic_id: self.id)
|
||||||
|
|
||||||
post = self.ordered_posts.first
|
if post = self.ordered_posts.first
|
||||||
|
notified_user_ids = [post.user_id, post.last_editor_id].uniq
|
||||||
if post
|
Jobs.enqueue(:notify_category_change, post_id: post.id, notified_user_ids: notified_user_ids)
|
||||||
post_alerter = PostAlerter.new
|
|
||||||
|
|
||||||
post_alerter.notify_post_users(
|
|
||||||
post,
|
|
||||||
[post.user, post.last_editor].uniq
|
|
||||||
)
|
|
||||||
|
|
||||||
post_alerter.notify_first_post_watchers(
|
|
||||||
post, post_alerter.category_watchers(self)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -788,8 +778,7 @@ SQL
|
|||||||
|
|
||||||
last_post = posts.order('post_number desc').where('not hidden AND posts.deleted_at IS NULL').first
|
last_post = posts.order('post_number desc').where('not hidden AND posts.deleted_at IS NULL').first
|
||||||
if last_post
|
if last_post
|
||||||
# ensure all the notifications are out
|
Jobs.enqueue(:post_alert, post_id: last_post.id)
|
||||||
PostAlerter.new.after_save_post(last_post)
|
|
||||||
add_small_action(user, "invited_group", group.name)
|
add_small_action(user, "invited_group", group.name)
|
||||||
|
|
||||||
group_id = group.id
|
group_id = group.id
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ class PostActionNotifier
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.post_action_deleted(post_action)
|
def self.post_action_deleted(post_action)
|
||||||
|
|
||||||
return if @disabled
|
return if @disabled
|
||||||
|
|
||||||
# We only care about deleting post actions for now
|
# We only care about deleting post actions for now
|
||||||
@@ -69,7 +68,6 @@ class PostActionNotifier
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.post_action_created(post_action)
|
def self.post_action_created(post_action)
|
||||||
|
|
||||||
return if @disabled
|
return if @disabled
|
||||||
|
|
||||||
# We only notify on likes for now
|
# We only notify on likes for now
|
||||||
@@ -89,7 +87,6 @@ class PostActionNotifier
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.after_create_post_revision(post_revision)
|
def self.after_create_post_revision(post_revision)
|
||||||
|
|
||||||
return if @disabled
|
return if @disabled
|
||||||
|
|
||||||
post = post_revision.post
|
post = post_revision.post
|
||||||
|
|||||||
@@ -134,11 +134,9 @@ class PostAlerter
|
|||||||
user_ids -= [post.user_id]
|
user_ids -= [post.user_id]
|
||||||
users = User.where(id: user_ids)
|
users = User.where(id: user_ids)
|
||||||
|
|
||||||
Scheduler::Defer.later("Notify First Post Watchers") do
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
||||||
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
users.each do |user|
|
||||||
users.each do |user|
|
create_notification(user, Notification.types[:watching_first_post], post)
|
||||||
create_notification(user, Notification.types[:watching_first_post], post)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -406,6 +404,7 @@ class PostAlerter
|
|||||||
DiscourseEvent.trigger(:post_notification_alert, user, payload)
|
DiscourseEvent.trigger(:post_notification_alert, user, payload)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
created.id ? created : nil
|
created.id ? created : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -486,11 +485,9 @@ class PostAlerter
|
|||||||
users = [users] unless users.is_a?(Array)
|
users = [users] unless users.is_a?(Array)
|
||||||
users = users.reject { |u| u.staged? } if post.topic&.private_message?
|
users = users.reject { |u| u.staged? } if post.topic&.private_message?
|
||||||
|
|
||||||
Scheduler::Defer.later("Notify Users") do
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
||||||
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
users.each do |u|
|
||||||
users.each do |u|
|
create_notification(u, Notification.types[type], post, opts)
|
||||||
create_notification(u, Notification.types[type], post, opts)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
users
|
users
|
||||||
@@ -499,28 +496,26 @@ class PostAlerter
|
|||||||
def notify_pm_users(post, reply_to_user, notified)
|
def notify_pm_users(post, reply_to_user, notified)
|
||||||
return unless post.topic
|
return unless post.topic
|
||||||
|
|
||||||
Scheduler::Defer.later("Notify PM Users") do
|
# users that aren't part of any mentioned groups
|
||||||
# users that aren't part of any mentioned groups
|
users = directly_targeted_users(post).reject { |u| notified.include?(u) }
|
||||||
users = directly_targeted_users(post).reject { |u| notified.include?(u) }
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
||||||
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
users.each do |user|
|
||||||
users.each do |user|
|
notification_level = TopicUser.get(post.topic, user)&.notification_level
|
||||||
notification_level = TopicUser.get(post.topic, user)&.notification_level
|
if reply_to_user == user || notification_level == TopicUser.notification_levels[:watching] || user.staged?
|
||||||
if reply_to_user == user || notification_level == TopicUser.notification_levels[:watching] || user.staged?
|
create_notification(user, Notification.types[:private_message], post)
|
||||||
create_notification(user, Notification.types[:private_message], post)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# users that are part of all mentionned groups
|
# users that are part of all mentionned groups
|
||||||
users = indirectly_targeted_users(post).reject { |u| notified.include?(u) }
|
users = indirectly_targeted_users(post).reject { |u| notified.include?(u) }
|
||||||
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
||||||
users.each do |user|
|
users.each do |user|
|
||||||
case TopicUser.get(post.topic, user)&.notification_level
|
case TopicUser.get(post.topic, user)&.notification_level
|
||||||
when TopicUser.notification_levels[:watching]
|
when TopicUser.notification_levels[:watching]
|
||||||
# only create a notification when watching the group
|
# only create a notification when watching the group
|
||||||
create_notification(user, Notification.types[:private_message], post)
|
create_notification(user, Notification.types[:private_message], post)
|
||||||
when TopicUser.notification_levels[:tracking]
|
when TopicUser.notification_levels[:tracking]
|
||||||
notify_group_summary(user, post)
|
notify_group_summary(user, post)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -575,12 +570,10 @@ class PostAlerter
|
|||||||
exclude_user_ids = notified.map(&:id)
|
exclude_user_ids = notified.map(&:id)
|
||||||
notify = notify.where("id NOT IN (?)", exclude_user_ids) if exclude_user_ids.present?
|
notify = notify.where("id NOT IN (?)", exclude_user_ids) if exclude_user_ids.present?
|
||||||
|
|
||||||
Scheduler::Defer.later("Notify Post Users") do
|
DiscourseEvent.trigger(:before_create_notifications_for_users, notify, post)
|
||||||
DiscourseEvent.trigger(:before_create_notifications_for_users, notify, post)
|
notify.pluck(:id).each do |user_id|
|
||||||
notify.pluck(:id).each do |user_id|
|
user = User.find_by(id: user_id)
|
||||||
user = User.find_by(id: user_id)
|
create_notification(user, Notification.types[:posted], post)
|
||||||
create_notification(user, Notification.types[:posted], post)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,11 @@ class PostJobsEnqueuer
|
|||||||
private
|
private
|
||||||
|
|
||||||
def enqueue_post_alerts
|
def enqueue_post_alerts
|
||||||
Jobs.enqueue(:post_alert, post_id: @post.id, options: @opts[:post_alert_options])
|
Jobs.enqueue(:post_alert,
|
||||||
|
post_id: @post.id,
|
||||||
|
new_record: true,
|
||||||
|
options: @opts[:post_alert_options],
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def feature_topic_users
|
def feature_topic_users
|
||||||
|
|||||||
@@ -577,7 +577,7 @@ class PostRevisor
|
|||||||
|
|
||||||
def alert_users
|
def alert_users
|
||||||
return if @editor.id == Discourse::SYSTEM_USER_ID
|
return if @editor.id == Discourse::SYSTEM_USER_ID
|
||||||
PostAlerter.new.after_save_post(@post)
|
Jobs.enqueue(:post_alert, post_id: @post.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def publish_changes
|
def publish_changes
|
||||||
|
|||||||
Reference in New Issue
Block a user