retrospring/app/workers/question_worker.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

42 lines
1.1 KiB
Ruby
Raw Normal View History

2020-05-25 18:33:09 +02:00
# frozen_string_literal: true
class QuestionWorker
include Sidekiq::Worker
sidekiq_options queue: :question, retry: false
# @param user_id [Integer] user id passed from Devise
# @param question_id [Integer] newly created question id
2020-05-25 18:33:09 +02:00
def perform(user_id, question_id)
user = User.find(user_id)
2021-12-22 19:21:09 +01:00
question = Question.find(question_id)
webpush_app = Rpush::App.find_by(name: "webpush")
2020-05-25 18:33:09 +02:00
user.followers.each do |f|
next if skip_inbox?(f, question, user)
2022-01-24 22:46:11 +01:00
2022-09-11 23:20:10 +02:00
inbox = Inbox.create(user_id: f.id, question_id:, new: true)
f.push_notification(webpush_app, inbox) if webpush_app
end
2020-05-25 18:33:09 +02:00
rescue StandardError => e
logger.info "failed to ask question: #{e.message}"
2021-12-28 18:32:03 +01:00
Sentry.capture_exception(e)
end
2022-12-26 10:59:56 +01:00
private
def skip_inbox?(follower, question, user)
return true if follower.inbox_locked?
return true if follower.banned?
return true if muted?(follower, question)
return true if user.muting?(question.user)
return true if question.long? && !follower.profile.allow_long_questions
false
end
2022-12-26 10:59:56 +01:00
def muted?(user, question)
MuteRule.where(user:).any? { |rule| rule.applies_to? question }
end
end