From e0195654b5e851ef2adf0a9d6f35e41eb01539e2 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 1 Jan 2023 21:34:31 +0100 Subject: [PATCH] Send notification on 3 push failures --- app/models/notification/push_subscription_error.rb | 4 ++++ .../notifications/type/_webpushsubscription.html.haml | 10 ++++++++++ config/initializers/rpush.rb | 8 ++++++++ config/locales/views.en.yml | 4 ++++ 4 files changed, 26 insertions(+) create mode 100644 app/models/notification/push_subscription_error.rb create mode 100644 app/views/notifications/type/_webpushsubscription.html.haml diff --git a/app/models/notification/push_subscription_error.rb b/app/models/notification/push_subscription_error.rb new file mode 100644 index 00000000..2e6be2c7 --- /dev/null +++ b/app/models/notification/push_subscription_error.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class Notification::PushSubscriptionError < Notification +end diff --git a/app/views/notifications/type/_webpushsubscription.html.haml b/app/views/notifications/type/_webpushsubscription.html.haml new file mode 100644 index 00000000..ce9b4a80 --- /dev/null +++ b/app/views/notifications/type/_webpushsubscription.html.haml @@ -0,0 +1,10 @@ +.media.notification + .notification__icon + %span.fa-stack + %i.fa.fa-2x.fa-fw.fa-bell + %i.fa.fa-stack-1x.fa-fw.fa-exclamation-triangle.text-danger.pl-2 + .media-body + %h6.media-heading.notification__user + = t(".heading") + .notification__text + = t(".text_html", settings_push: link_to(t(".settings_push"), settings_push_notifications_path)) diff --git a/config/initializers/rpush.rb b/config/initializers/rpush.rb index 3cb8eb77..6c1175e6 100644 --- a/config/initializers/rpush.rb +++ b/config/initializers/rpush.rb @@ -63,6 +63,14 @@ Rpush.reflect do |on| subscription = WebPushSubscription::where("subscription ->> 'endpoint' = ?", notification.registration_ids.first[:endpoint]) subscription.increment :failures subscription.save + + if subscription.failures > 3 + Notification::PushSubscriptionError.create( + target: subscription, + recipient: subscription.user, + new: true + ) + end end end diff --git a/config/locales/views.en.yml b/config/locales/views.en.yml index d4deff76..e52b74ff 100644 --- a/config/locales/views.en.yml +++ b/config/locales/views.en.yml @@ -354,6 +354,10 @@ en: link_text: "your comment" follow: heading_html: "followed you %{time} ago" + webpushsubscription: + heading: "Push notifications are failing to send to one of your devices." + text_html: "Please check the %{settings_push} if you still want to be notified." + settings_push: "push notification settings" expiredtwitterserviceconnection: heading: "Twitter connection expired" text_html: "If you would like to continue automatically sharing your answers to Twitter, head to %{settings_sharing} and re-connect your account."