From dcb94971484c46e470b89032f174267ace86be5c Mon Sep 17 00:00:00 2001 From: Sorin Davidoi Date: Wed, 19 Jul 2017 22:37:52 +0200 Subject: [PATCH] fix(push_subscription): Guard against malformed favourite notification (#4271) --- app/models/web/push_subscription.rb | 10 ++++------ app/services/notify_service.rb | 7 ++++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/models/web/push_subscription.rb b/app/models/web/push_subscription.rb index dffdbbd05..86df9b591 100644 --- a/app/models/web/push_subscription.rb +++ b/app/models/web/push_subscription.rb @@ -26,8 +26,6 @@ class Web::PushSubscription < ApplicationRecord before_create :send_welcome_notification def push(notification) - return unless pushable? notification - name = display_name notification.from_account title = title_str(name, notification) body = body_str notification @@ -69,6 +67,10 @@ class Web::PushSubscription < ApplicationRecord ) end + def pushable?(notification) + data && data.key?('alerts') && data['alerts'][notification.type.to_s] + end + def as_payload payload = { id: id, @@ -148,10 +150,6 @@ class Web::PushSubscription < ApplicationRecord rtl?(body) ? 'rtl' : 'ltr' end - def pushable?(notification) - data && data.key?('alerts') && data['alerts'][notification.type.to_s] - end - def send_welcome_notification Webpush.payload_send( message: JSON.generate( diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb index c7d8ad50a..a44df5180 100644 --- a/app/services/notify_service.rb +++ b/app/services/notify_service.rb @@ -65,7 +65,12 @@ class NotifyService < BaseService end def send_push_notifications - sessions_with_subscriptions_ids = @recipient.user.session_activations.where.not(web_push_subscription: nil).pluck(:id) + # HACK: Can be caused by quickly unfavouriting a status, since creating + # a favourite and creating a notification are not wrapped in a transaction. + return if @notification.activity.nil? + + sessions_with_subscriptions = @recipient.user.session_activations.where.not(web_push_subscription: nil) + sessions_with_subscriptions_ids = sessions_with_subscriptions.select { |session| session.web_push_subscription.pushable? @notification }.map(&:id) WebPushNotificationWorker.push_bulk(sessions_with_subscriptions_ids) do |session_activation_id| [session_activation_id, @notification.id]