fix(push_subscription): Guard against malformed favourite notification (#4271)

This commit is contained in:
Sorin Davidoi 2017-07-19 22:37:52 +02:00 committed by Eugen Rochko
parent 4f2513337f
commit dcb9497148
2 changed files with 10 additions and 7 deletions

View file

@ -26,8 +26,6 @@ class Web::PushSubscription < ApplicationRecord
before_create :send_welcome_notification before_create :send_welcome_notification
def push(notification) def push(notification)
return unless pushable? notification
name = display_name notification.from_account name = display_name notification.from_account
title = title_str(name, notification) title = title_str(name, notification)
body = body_str notification body = body_str notification
@ -69,6 +67,10 @@ class Web::PushSubscription < ApplicationRecord
) )
end end
def pushable?(notification)
data && data.key?('alerts') && data['alerts'][notification.type.to_s]
end
def as_payload def as_payload
payload = { payload = {
id: id, id: id,
@ -148,10 +150,6 @@ class Web::PushSubscription < ApplicationRecord
rtl?(body) ? 'rtl' : 'ltr' rtl?(body) ? 'rtl' : 'ltr'
end end
def pushable?(notification)
data && data.key?('alerts') && data['alerts'][notification.type.to_s]
end
def send_welcome_notification def send_welcome_notification
Webpush.payload_send( Webpush.payload_send(
message: JSON.generate( message: JSON.generate(

View file

@ -65,7 +65,12 @@ class NotifyService < BaseService
end end
def send_push_notifications 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| WebPushNotificationWorker.push_bulk(sessions_with_subscriptions_ids) do |session_activation_id|
[session_activation_id, @notification.id] [session_activation_id, @notification.id]