2020-05-25 09:33:09 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-07-26 23:59:56 -07:00
|
|
|
class QuestionWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
sidekiq_options queue: :question, retry: false
|
|
|
|
|
2023-11-01 00:45:56 -07:00
|
|
|
# @param follower_id [Integer] user id passed from Devise
|
2015-07-27 00:12:12 -07:00
|
|
|
# @param question_id [Integer] newly created question id
|
2023-11-01 00:45:56 -07:00
|
|
|
def perform(follower_id, question_id)
|
|
|
|
follower = User.includes(:web_push_subscriptions, :mute_rules, :muted_users).find(follower_id)
|
|
|
|
question = Question.includes(:user).find(question_id)
|
2022-09-11 11:25:11 -07:00
|
|
|
webpush_app = Rpush::App.find_by(name: "webpush")
|
2020-05-25 09:33:09 -07:00
|
|
|
|
2023-11-01 14:40:02 -07:00
|
|
|
return if skip_inbox?(follower, question)
|
2022-01-24 13:46:11 -08:00
|
|
|
|
2023-11-01 00:45:56 -07:00
|
|
|
inbox = Inbox.create(user_id: follower.id, question_id:, new: true)
|
|
|
|
follower.push_notification(webpush_app, inbox) if webpush_app
|
2015-07-26 23:59:56 -07:00
|
|
|
end
|
2022-12-26 01:59:56 -08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-11-01 14:40:02 -07:00
|
|
|
def skip_inbox?(follower, question)
|
2023-01-02 03:04:48 -08:00
|
|
|
return true if follower.inbox_locked?
|
|
|
|
return true if follower.banned?
|
|
|
|
return true if muted?(follower, question)
|
2023-11-01 14:40:02 -07:00
|
|
|
return true if follower.muting?(question.user)
|
2023-01-08 10:22:00 -08:00
|
|
|
return true if question.long? && !follower.profile.allow_long_questions
|
2023-01-02 03:04:48 -08:00
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2023-11-01 00:45:56 -07:00
|
|
|
# @param [User] user
|
|
|
|
# @param [Question] question
|
2022-12-26 01:59:56 -08:00
|
|
|
def muted?(user, question)
|
2023-11-01 00:45:56 -07:00
|
|
|
user.mute_rules.any? { |rule| rule.applies_to? question }
|
2022-12-26 01:59:56 -08:00
|
|
|
end
|
2015-07-26 23:59:56 -07:00
|
|
|
end
|