mirror of
https://github.com/Retrospring/retrospring.git
synced 2024-11-20 10:19:52 +01:00
Track failures on Web Push subscriptions
This commit is contained in:
parent
ba3c406bc7
commit
1cfd3250c0
6 changed files with 21 additions and 4 deletions
|
@ -4,6 +4,6 @@ class Settings::PushNotificationsController < ApplicationController
|
|||
before_action :authenticate_user!
|
||||
|
||||
def index
|
||||
@subscriptions = current_user.web_push_subscriptions
|
||||
@subscriptions = current_user.web_push_subscriptions.active
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ module User::PushNotificationMethods
|
|||
def push_notification(app, resource)
|
||||
raise ArgumentError("Resource must respond to `as_push_notification`") unless resource.respond_to? :as_push_notification
|
||||
|
||||
web_push_subscriptions.each do |s|
|
||||
web_push_subscriptions.active.find_each do |s|
|
||||
n = Rpush::Webpush::Notification.new
|
||||
n.app = app
|
||||
n.registration_ids = [s.subscription.symbolize_keys]
|
||||
|
|
|
@ -2,4 +2,7 @@
|
|||
|
||||
class WebPushSubscription < ApplicationRecord
|
||||
belongs_to :user
|
||||
|
||||
scope :active, -> { where(failures: ...3) }
|
||||
scope :failed, -> { where(failures: 3..) }
|
||||
end
|
||||
|
|
|
@ -57,8 +57,14 @@ Rpush.reflect do |on|
|
|||
|
||||
# Called when notification delivery failed.
|
||||
# Call 'error_code' and 'error_description' on the notification for the cause.
|
||||
# on.notification_failed do |notification|
|
||||
# end
|
||||
on.notification_failed do |notification|
|
||||
# See: https://developer.apple.com/documentation/usernotifications/sending_web_push_notifications_in_safari_and_other_browsers#3994594
|
||||
if %i[403 410].include? notification.error_code
|
||||
subscription = WebPushSubscription::where("subscription ->> 'endpoint' = ?", notification.registration_ids.first[:endpoint])
|
||||
subscription.increment :failures
|
||||
subscription.save
|
||||
end
|
||||
end
|
||||
|
||||
# Called when the notification delivery failed and only the notification ID
|
||||
# is present in memory.
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddFailuresToWebPushSubscriptions < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
add_column :web_push_subscriptions, :failures, :integer, default: 0
|
||||
end
|
||||
end
|
|
@ -387,6 +387,7 @@ ActiveRecord::Schema.define(version: 2022_12_27_065923) do
|
|||
t.json "subscription"
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.integer "failures", default: 0
|
||||
t.index ["user_id"], name: "index_web_push_subscriptions_on_user_id"
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue