2023-12-11 10:54:56 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SendToInboxJob
|
2023-12-11 11:02:30 -08:00
|
|
|
include Sidekiq::Job
|
2023-12-11 10:54:56 -08:00
|
|
|
|
|
|
|
sidekiq_options queue: :question, retry: false
|
|
|
|
|
|
|
|
# @param follower_id [Integer] user id passed from Devise
|
|
|
|
# @param question_id [Integer] newly created question id
|
|
|
|
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)
|
|
|
|
webpush_app = Rpush::App.find_by(name: "webpush")
|
|
|
|
|
|
|
|
return if skip_inbox?(follower, question)
|
|
|
|
|
2024-01-27 04:02:58 -08:00
|
|
|
inbox = InboxEntry.create(user_id: follower.id, question_id:, new: true)
|
2023-12-11 10:54:56 -08:00
|
|
|
follower.push_notification(webpush_app, inbox) if webpush_app
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def skip_inbox?(follower, question)
|
|
|
|
return true if follower.inbox_locked?
|
|
|
|
return true if follower.banned?
|
|
|
|
return true if muted?(follower, question)
|
|
|
|
return true if follower.muting?(question.user)
|
|
|
|
return true if question.long? && !follower.profile.allow_long_questions
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# @param [User] user
|
|
|
|
# @param [Question] question
|
|
|
|
def muted?(user, question) = user.mute_rules.any? { |rule| rule.applies_to? question }
|
|
|
|
end
|