about summary refs log blame commit diff
path: root/app/workers/web/push_notification_worker.rb
blob: 46aeaa30b1d08ea58b5e3532f379ea2ff5c00380 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12



                                 
                                           




                                                                     
                                    





                                                            


                                     
# frozen_string_literal: true

class Web::PushNotificationWorker
  include Sidekiq::Worker

  sidekiq_options backtrace: true, retry: 5

  def perform(subscription_id, notification_id)
    subscription = ::Web::PushSubscription.find(subscription_id)
    notification = Notification.find(notification_id)

    subscription.push(notification) unless notification.activity.nil?
  rescue Webpush::ResponseError => e
    code = e.response.code.to_i

    if (400..499).cover?(code) && ![408, 429].include?(code)
      subscription.destroy!
    else
      raise e
    end
  rescue ActiveRecord::RecordNotFound
    true
  end
end