Refactor `QuestionWorker` to send to individual users rather than all followers at once

This commit is contained in:
Karina Kwiatek 2023-11-01 08:45:56 +01:00
parent 117a595f49
commit 83ac156382
2 changed files with 15 additions and 17 deletions

View File

@ -5,22 +5,17 @@ class QuestionWorker
sidekiq_options queue: :question, retry: false sidekiq_options queue: :question, retry: false
# @param user_id [Integer] user id passed from Devise # @param follower_id [Integer] user id passed from Devise
# @param question_id [Integer] newly created question id # @param question_id [Integer] newly created question id
def perform(user_id, question_id) def perform(follower_id, question_id)
user = User.find(user_id) follower = User.includes(:web_push_subscriptions, :mute_rules, :muted_users).find(follower_id)
question = Question.find(question_id) question = Question.includes(:user).find(question_id)
webpush_app = Rpush::App.find_by(name: "webpush") webpush_app = Rpush::App.find_by(name: "webpush")
user.followers.each do |f| return if skip_inbox?(follower, question, user)
next if skip_inbox?(f, question, user)
inbox = Inbox.create(user_id: f.id, question_id:, new: true) inbox = Inbox.create(user_id: follower.id, question_id:, new: true)
f.push_notification(webpush_app, inbox) if webpush_app follower.push_notification(webpush_app, inbox) if webpush_app
end
rescue StandardError => e
logger.info "failed to ask question: #{e.message}"
Sentry.capture_exception(e)
end end
private private
@ -35,7 +30,9 @@ class QuestionWorker
false false
end end
# @param [User] user
# @param [Question] question
def muted?(user, question) def muted?(user, question)
MuteRule.where(user:).any? { |rule| rule.applies_to? question } user.mute_rules.any? { |rule| rule.applies_to? question }
end end
end end

View File

@ -13,13 +13,14 @@ module UseCase
author_is_anonymous: false, author_is_anonymous: false,
author_identifier:, author_identifier:,
user: source_user, user: source_user,
direct: false direct: false,
) )
increment_asked_count increment_asked_count
increment_metric increment_metric
QuestionWorker.perform_async(source_user_id, question.id) args = source_user.followers.map { |f| [f.id, question.id] }
QuestionWorker.perform_bulk(args)
{ {
status: 201, status: 201,
@ -40,12 +41,12 @@ module UseCase
anonymous: false, anonymous: false,
followers: true, followers: true,
generated: false, generated: false,
} },
) )
end end
def source_user def source_user
@source_user ||= ::User.find(source_user_id) @source_user ||= ::User.includes(:followers).find(source_user_id)
end end
end end
end end