2020-05-01 09:23:43 +00:00
|
|
|
class Notification::PushNotificationService
|
2020-05-05 18:40:56 +00:00
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
pattr_initialize [:notification!]
|
|
|
|
|
|
|
|
def perform
|
|
|
|
return unless user_subscribed_to_notification?
|
2020-05-05 18:40:56 +00:00
|
|
|
|
|
|
|
notification_subscriptions.each do |subscription|
|
2020-06-04 18:45:50 +00:00
|
|
|
send_browser_push(subscription)
|
|
|
|
send_fcm_push(subscription)
|
2020-05-05 18:40:56 +00:00
|
|
|
end
|
2020-05-01 09:23:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-05 18:40:56 +00:00
|
|
|
delegate :user, to: :notification
|
|
|
|
delegate :notification_subscriptions, to: :user
|
|
|
|
delegate :notification_settings, to: :user
|
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
def user_subscribed_to_notification?
|
2020-05-05 18:40:56 +00:00
|
|
|
notification_setting = notification_settings.find_by(account_id: notification.account.id)
|
2020-05-01 09:23:43 +00:00
|
|
|
return true if notification_setting.public_send("push_#{notification.notification_type}?")
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
2020-05-05 18:40:56 +00:00
|
|
|
|
|
|
|
def conversation
|
2021-02-15 18:45:45 +00:00
|
|
|
@conversation ||= notification.conversation
|
2020-05-05 18:40:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def push_message
|
|
|
|
{
|
2020-06-09 12:12:18 +00:00
|
|
|
title: notification.push_message_title,
|
2020-05-05 18:40:56 +00:00
|
|
|
tag: "#{notification.notification_type}_#{conversation.display_id}",
|
|
|
|
url: push_url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def push_url
|
|
|
|
app_account_conversation_url(account_id: conversation.account_id, id: conversation.display_id)
|
|
|
|
end
|
|
|
|
|
2020-06-16 14:09:57 +00:00
|
|
|
def send_browser_push?(subscription)
|
|
|
|
ENV['VAPID_PUBLIC_KEY'] && subscription.browser_push?
|
|
|
|
end
|
|
|
|
|
2020-05-05 18:40:56 +00:00
|
|
|
def send_browser_push(subscription)
|
2020-06-16 14:09:57 +00:00
|
|
|
return unless send_browser_push?(subscription)
|
2020-06-04 18:45:50 +00:00
|
|
|
|
2020-05-05 18:40:56 +00:00
|
|
|
Webpush.payload_send(
|
|
|
|
message: JSON.generate(push_message),
|
|
|
|
endpoint: subscription.subscription_attributes['endpoint'],
|
|
|
|
p256dh: subscription.subscription_attributes['p256dh'],
|
|
|
|
auth: subscription.subscription_attributes['auth'],
|
|
|
|
vapid: {
|
|
|
|
subject: push_url,
|
|
|
|
public_key: ENV['VAPID_PUBLIC_KEY'],
|
|
|
|
private_key: ENV['VAPID_PRIVATE_KEY']
|
|
|
|
},
|
|
|
|
ssl_timeout: 5,
|
|
|
|
open_timeout: 5,
|
|
|
|
read_timeout: 5
|
|
|
|
)
|
|
|
|
rescue Webpush::ExpiredSubscription
|
|
|
|
subscription.destroy!
|
2020-09-10 13:49:15 +00:00
|
|
|
rescue Errno::ECONNRESET, Net::OpenTimeout, Net::ReadTimeout => e
|
|
|
|
Rails.logger.info "Webpush operation error: #{e.message}"
|
2020-05-05 18:40:56 +00:00
|
|
|
end
|
2020-06-04 18:45:50 +00:00
|
|
|
|
|
|
|
def send_fcm_push(subscription)
|
2020-06-16 14:09:57 +00:00
|
|
|
return unless ENV['FCM_SERVER_KEY']
|
2020-06-04 18:45:50 +00:00
|
|
|
return unless subscription.fcm?
|
|
|
|
|
|
|
|
fcm = FCM.new(ENV['FCM_SERVER_KEY'])
|
2020-06-15 08:06:56 +00:00
|
|
|
response = fcm.send([subscription.subscription_attributes['push_token']], fcm_options)
|
|
|
|
subscription.destroy! if JSON.parse(response[:body])['results']&.first&.keys&.include?('error')
|
|
|
|
end
|
|
|
|
|
|
|
|
def fcm_options
|
|
|
|
{
|
2020-06-09 12:12:18 +00:00
|
|
|
"notification": {
|
|
|
|
"title": notification.notification_type.titleize,
|
|
|
|
"body": notification.push_message_title
|
|
|
|
},
|
2020-06-15 08:06:56 +00:00
|
|
|
"data": { notification: notification.push_event_data.to_json },
|
|
|
|
"collapse_key": "chatwoot_#{notification.primary_actor_type.downcase}_#{notification.primary_actor_id}"
|
2020-06-09 12:12:18 +00:00
|
|
|
}
|
2020-06-04 18:45:50 +00:00
|
|
|
end
|
2020-05-01 09:23:43 +00:00
|
|
|
end
|