FIX: Remove expired subscription for push notifications

All other errors get logged, but do not stop the system from sending further push notifications.
This commit is contained in:
Gerhard Schlager 2019-01-10 15:38:31 +01:00
parent 1fdbf0fc9b
commit 978cc0cfca
1 changed files with 31 additions and 21 deletions

View File

@ -2,9 +2,6 @@ require_dependency 'webpush'
class PushNotificationPusher
def self.push(user, payload)
subscriptions(user).each do |subscription|
subscription = JSON.parse(subscription.data)
message = {
title: I18n.t(
"discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}",
@ -20,7 +17,10 @@ class PushNotificationPusher
url: payload[:post_url],
hide_when_active: true
}
send_notification user, subscription, message
subscriptions(user).each do |subscription|
subscription = JSON.parse(subscription.data)
send_notification(user, subscription, message)
end
end
@ -54,7 +54,7 @@ class PushNotificationPusher
tag: "#{Discourse.current_hostname}-subscription"
}
send_notification user, subscription, message
send_notification(user, subscription, message)
end
end
@ -74,7 +74,7 @@ class PushNotificationPusher
def self.send_notification(user, subscription, message)
begin
response = Webpush.payload_send(
Webpush.payload_send(
endpoint: subscription["endpoint"],
message: message.to_json,
p256dh: subscription.dig("keys", "p256dh"),
@ -85,8 +85,18 @@ class PushNotificationPusher
private_key: SiteSetting.vapid_private_key
}
)
rescue Webpush::InvalidSubscription => e
unsubscribe user, subscription
rescue Webpush::ExpiredSubscription
unsubscribe(user, subscription)
rescue Webpush::ResponseError => e
Discourse.warn_exception(
e,
message: "Failed to send push notification",
env: {
user_id: user.id,
endpoint: subscription["endpoint"],
message: message.to_json
}
)
end
end
end