fix(push-subscriptions): Refactor how Sidekiq jobs are handled (#4226)

This commit is contained in:
Sorin Davidoi 2017-07-18 16:25:40 +02:00 committed by Eugen Rochko
parent afa52e4d63
commit 8387b3928e
3 changed files with 19 additions and 17 deletions

View file

@ -12,6 +12,9 @@
# updated_at :datetime not null # updated_at :datetime not null
# #
require 'webpush'
require_relative '../../models/setting'
class Web::PushSubscription < ApplicationRecord class Web::PushSubscription < ApplicationRecord
include RoutingHelper include RoutingHelper
include StreamEntriesHelper include StreamEntriesHelper
@ -37,7 +40,6 @@ class Web::PushSubscription < ApplicationRecord
nsfw = notification.target_status.nil? || notification.target_status.spoiler_text.empty? ? nil : notification.target_status.spoiler_text nsfw = notification.target_status.nil? || notification.target_status.spoiler_text.empty? ? nil : notification.target_status.spoiler_text
# TODO: Make sure that the payload does not exceed 4KB - Webpush::PayloadTooLarge # TODO: Make sure that the payload does not exceed 4KB - Webpush::PayloadTooLarge
# TODO: Queue the requests - Webpush::TooManyRequests
Webpush.payload_send( Webpush.payload_send(
message: JSON.generate( message: JSON.generate(
title: title, title: title,
@ -59,7 +61,7 @@ class Web::PushSubscription < ApplicationRecord
p256dh: key_p256dh, p256dh: key_p256dh,
auth: key_auth, auth: key_auth,
vapid: { vapid: {
# subject: "mailto:#{Setting.site_contact_email}", subject: "mailto:#{Setting.site_contact_email}",
private_key: Rails.configuration.x.vapid_private_key, private_key: Rails.configuration.x.vapid_private_key,
public_key: Rails.configuration.x.vapid_public_key, public_key: Rails.configuration.x.vapid_public_key,
}, },
@ -166,7 +168,7 @@ class Web::PushSubscription < ApplicationRecord
p256dh: key_p256dh, p256dh: key_p256dh,
auth: key_auth, auth: key_auth,
vapid: { vapid: {
# subject: "mailto:#{Setting.site_contact_email}", subject: "mailto:#{Setting.site_contact_email}",
private_key: Rails.configuration.x.vapid_private_key, private_key: Rails.configuration.x.vapid_private_key,
public_key: Rails.configuration.x.vapid_public_key, public_key: Rails.configuration.x.vapid_public_key,
}, },

View file

@ -65,7 +65,11 @@ class NotifyService < BaseService
end end
def send_push_notifications def send_push_notifications
WebPushNotificationWorker.perform_async(@recipient.id, @notification.id) sessions_with_subscriptions_ids = @recipient.user.session_activations.where.not(web_push_subscription: nil).pluck(:id)
WebPushNotificationWorker.push_bulk(sessions_with_subscriptions_ids) do |session_activation_id|
[session_activation_id, @notification.id]
end
end end
def send_email def send_email

View file

@ -5,22 +5,18 @@ class WebPushNotificationWorker
sidekiq_options backtrace: true sidekiq_options backtrace: true
def perform(recipient_id, notification_id) def perform(session_activation_id, notification_id)
recipient = Account.find(recipient_id) session_activation = SessionActivation.find(session_activation_id)
notification = Notification.find(notification_id) notification = Notification.find(notification_id)
sessions_with_subscriptions = recipient.user.session_activations.where.not(web_push_subscription: nil)
sessions_with_subscriptions.each do |session|
begin begin
session.web_push_subscription.push(notification) session_activation.web_push_subscription.push(notification)
rescue Webpush::InvalidSubscription, Webpush::ExpiredSubscription rescue Webpush::InvalidSubscription, Webpush::ExpiredSubscription => e
# Subscription expiration is not currently implemented in any browser # Subscription expiration is not currently implemented in any browser
session.web_push_subscription.destroy! session_activation.web_push_subscription.destroy!
session.update!(web_push_subscription: nil) session_activation.update!(web_push_subscription: nil)
rescue Webpush::PayloadTooLarge => e
Rails.logger.error(e) raise e
end
end end
end end
end end