DEV: Reduce number of jobs enqueued.

Apply code review suggestion from
405ba00c08.
This commit is contained in:
Guo Xiang Tan 2019-05-08 15:58:08 +08:00
parent ba6904bb27
commit f04518fdf9
3 changed files with 28 additions and 43 deletions

View File

@ -1,20 +1,23 @@
module Jobs module Jobs
class NotifyPostRevision < Jobs::Base class NotifyPostRevision < Jobs::Base
def execute(args) def execute(args)
user = User.find_by(id: args[:user_id]) raise Discourse::InvalidParameters.new(:user_ids) unless args[:user_ids]
raise Discourse::InvalidParameters.new(:user_id) unless user
post_revision = PostRevision.find_by(id: args[:post_revision_id]) post_revision = PostRevision.find_by(id: args[:post_revision_id])
raise Discourse::InvalidParameters.new(:post_revision_id) unless post_revision raise Discourse::InvalidParameters.new(:post_revision_id) unless post_revision
ActiveRecord::Base.transaction do
User.where(id: args[:user_ids]).find_each do |user|
PostActionNotifier.alerter.create_notification( PostActionNotifier.alerter.create_notification(
user, user,
Notification.types[:edited], Notification.types[:edited],
post_revision.post, post_revision.post,
display_username: post_revision.user.username, display_username: post_revision.user.username,
acting_user_id: post_revision.try(:user_id), acting_user_id: post_revision&.user_id,
revision_number: post_revision.number revision_number: post_revision.number
) )
end end
end end
end
end
end end

View File

@ -97,24 +97,26 @@ class PostActionNotifier
return if post.topic.private_message? return if post.topic.private_message?
return if SiteSetting.disable_edit_notifications && post_revision.user_id == Discourse::SYSTEM_USER_ID return if SiteSetting.disable_edit_notifications && post_revision.user_id == Discourse::SYSTEM_USER_ID
user_ids = []
if post_revision.user_id != post.user_id if post_revision.user_id != post.user_id
Jobs.enqueue(:notify_post_revision, user_ids << post.user_id
user_id: post.user_id,
post_revision_id: post_revision.id
)
end end
if post.wiki && post.is_first_post? if post.wiki && post.is_first_post?
user_ids.concat(
TopicUser.watching(post.topic_id) TopicUser.watching(post.topic_id)
.where.not(user_id: post_revision.user_id) .where.not(user_id: post_revision.user_id)
.where(topic: post.topic) .where(topic: post.topic)
.find_each do |topic_user| .pluck(:user_id)
Jobs.enqueue(:notify_post_revision,
user_id: topic_user.user_id,
post_revision_id: post_revision.id
) )
end end
if user_ids.present?
Jobs.enqueue(:notify_post_revision,
user_ids: user_ids,
post_revision_id: post_revision.id
)
end end
end end

View File

@ -1,20 +0,0 @@
module Jobs
class NotifyPostRevision < Jobs::Base
def execute(args)
user = User.find_by(id: args[:user_id])
raise Discourse::InvalidParameters.new(:user_id) unless user
post_revision = PostRevision.find_by(id: args[:post_revision_id])
raise Discourse::InvalidParameters.new(:post_revision_id) unless post_revision
PostActionNotifier.alerter.create_notification(
user,
Notification.types[:edited],
post_revision.post,
display_username: post_revision.user.username,
acting_user_id: post_revision&.user_id,
revision_number: post_revision.number
)
end
end
end