2018-05-11 19:49:12 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Web::PushNotificationWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
2021-04-12 22:25:34 +10:00
|
|
|
sidekiq_options queue: 'push', retry: 5
|
|
|
|
|
|
|
|
TTL = 48.hours.to_s
|
|
|
|
URGENCY = 'normal'
|
2018-05-11 19:49:12 +10:00
|
|
|
|
|
|
|
def perform(subscription_id, notification_id)
|
2021-04-12 22:25:34 +10:00
|
|
|
@subscription = Web::PushSubscription.find(subscription_id)
|
|
|
|
@notification = Notification.find(notification_id)
|
|
|
|
|
|
|
|
# Polymorphically associated activity could have been deleted
|
|
|
|
# in the meantime, so we have to double-check before proceeding
|
|
|
|
return unless @notification.activity.present? && @subscription.pushable?(@notification)
|
|
|
|
|
2024-10-02 21:11:52 +10:00
|
|
|
payload = web_push_request.encrypt(push_notification_json)
|
2018-05-11 19:49:12 +10:00
|
|
|
|
2024-10-02 21:11:52 +10:00
|
|
|
request_pool.with(web_push_request.audience) do |http_client|
|
|
|
|
request = Request.new(:post, web_push_request.endpoint, body: payload.fetch(:ciphertext), http_client: http_client)
|
2019-09-14 03:15:47 +10:00
|
|
|
|
2021-04-12 22:25:34 +10:00
|
|
|
request.add_headers(
|
2023-02-20 16:58:28 +11:00
|
|
|
'Content-Type' => 'application/octet-stream',
|
|
|
|
'Ttl' => TTL,
|
|
|
|
'Urgency' => URGENCY,
|
2021-04-12 22:25:34 +10:00
|
|
|
'Content-Encoding' => 'aesgcm',
|
2023-02-20 16:58:28 +11:00
|
|
|
'Encryption' => "salt=#{Webpush.encode64(payload.fetch(:salt)).delete('=')}",
|
2024-10-02 21:11:52 +10:00
|
|
|
'Crypto-Key' => "dh=#{Webpush.encode64(payload.fetch(:server_public_key)).delete('=')};#{web_push_request.crypto_key_header}",
|
|
|
|
'Authorization' => web_push_request.authorization_header
|
2021-04-12 22:25:34 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
request.perform do |response|
|
|
|
|
# If the server responds with an error in the 4xx range
|
|
|
|
# that isn't about rate-limiting or timeouts, we can
|
|
|
|
# assume that the subscription is invalid or expired
|
|
|
|
# and must be removed
|
|
|
|
|
|
|
|
if (400..499).cover?(response.code) && ![408, 429].include?(response.code)
|
|
|
|
@subscription.destroy!
|
|
|
|
elsif !(200...300).cover?(response.code)
|
|
|
|
raise Mastodon::UnexpectedResponseError, response
|
|
|
|
end
|
|
|
|
end
|
2019-09-14 03:15:47 +10:00
|
|
|
end
|
2018-05-11 19:49:12 +10:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
true
|
|
|
|
end
|
2021-04-12 22:25:34 +10:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-10-02 21:11:52 +10:00
|
|
|
def web_push_request
|
|
|
|
@web_push_request || WebPushRequest.new(@subscription)
|
|
|
|
end
|
|
|
|
|
2021-04-12 22:25:34 +10:00
|
|
|
def push_notification_json
|
2024-10-02 21:11:52 +10:00
|
|
|
I18n.with_locale(@subscription.locale.presence || I18n.default_locale) do
|
2024-10-11 21:24:03 +11:00
|
|
|
Oj.dump(serialized_notification.as_json)
|
2021-04-12 22:25:34 +10:00
|
|
|
end
|
2024-10-02 21:11:52 +10:00
|
|
|
end
|
2021-04-12 22:25:34 +10:00
|
|
|
|
2024-10-02 21:11:52 +10:00
|
|
|
def serialized_notification
|
|
|
|
ActiveModelSerializers::SerializableResource.new(
|
|
|
|
@notification,
|
|
|
|
serializer: Web::NotificationSerializer,
|
|
|
|
scope: @subscription,
|
|
|
|
scope_name: :current_push_subscription
|
|
|
|
)
|
2021-04-12 22:25:34 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def request_pool
|
|
|
|
RequestPool.current
|
|
|
|
end
|
2018-05-11 19:49:12 +10:00
|
|
|
end
|