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
PostActionNotifier.alerter.create_notification( ActiveRecord::Base.transaction do
user, User.where(id: args[:user_ids]).find_each do |user|
Notification.types[:edited], PostActionNotifier.alerter.create_notification(
post_revision.post, user,
display_username: post_revision.user.username, Notification.types[:edited],
acting_user_id: post_revision.try(:user_id), post_revision.post,
revision_number: post_revision.number display_username: post_revision.user.username,
) acting_user_id: post_revision&.user_id,
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?
TopicUser.watching(post.topic_id) user_ids.concat(
.where.not(user_id: post_revision.user_id) TopicUser.watching(post.topic_id)
.where(topic: post.topic) .where.not(user_id: post_revision.user_id)
.find_each do |topic_user| .where(topic: post.topic)
.pluck(:user_id)
)
end
Jobs.enqueue(:notify_post_revision, if user_ids.present?
user_id: topic_user.user_id, Jobs.enqueue(:notify_post_revision,
post_revision_id: post_revision.id user_ids: user_ids,
) post_revision_id: post_revision.id
end )
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