2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-04 18:31:48 -04:00
|
|
|
class PushNotificationPusher
|
2019-02-15 15:11:44 -05:00
|
|
|
TOKEN_VALID_FOR_SECONDS ||= 5 * 60
|
|
|
|
|
2018-05-04 18:31:48 -04:00
|
|
|
def self.push(user, payload)
|
2021-02-25 13:10:37 -05:00
|
|
|
I18n.with_locale(user.effective_locale) do
|
|
|
|
message = {
|
|
|
|
title: I18n.t(
|
|
|
|
"discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}",
|
|
|
|
site_title: SiteSetting.title,
|
|
|
|
topic: payload[:topic_title],
|
|
|
|
username: payload[:username]
|
|
|
|
),
|
|
|
|
body: payload[:excerpt],
|
|
|
|
badge: get_badge,
|
|
|
|
icon: ActionController::Base.helpers.image_url("push-notifications/#{Notification.types[payload[:notification_type]]}.png"),
|
|
|
|
tag: "#{Discourse.current_hostname}-#{payload[:topic_id]}",
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
url: payload[:post_url],
|
|
|
|
hide_when_active: true
|
|
|
|
}
|
2019-01-10 09:38:31 -05:00
|
|
|
|
2021-02-25 13:10:37 -05:00
|
|
|
subscriptions(user).each do |subscription|
|
|
|
|
subscription = JSON.parse(subscription.data)
|
|
|
|
send_notification(user, subscription, message)
|
|
|
|
end
|
2018-05-04 18:31:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.subscriptions(user)
|
|
|
|
user.push_subscriptions
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_subscriptions(user)
|
|
|
|
user.push_subscriptions.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.subscribe(user, subscription, send_confirmation)
|
2018-05-08 20:14:14 -04:00
|
|
|
data = subscription.to_json
|
|
|
|
subscriptions = PushSubscription.where(user: user, data: data)
|
|
|
|
subscriptions_count = subscriptions.count
|
|
|
|
|
|
|
|
if subscriptions_count > 1
|
2018-05-08 18:14:58 -04:00
|
|
|
subscriptions.destroy_all
|
2018-05-08 20:14:14 -04:00
|
|
|
PushSubscription.create!(user: user, data: data)
|
|
|
|
elsif subscriptions_count == 0
|
|
|
|
PushSubscription.create!(user: user, data: data)
|
2018-05-08 18:14:58 -04:00
|
|
|
end
|
2018-05-08 20:14:14 -04:00
|
|
|
|
2018-05-04 18:31:48 -04:00
|
|
|
if send_confirmation == "true"
|
|
|
|
message = {
|
|
|
|
title: I18n.t("discourse_push_notifications.popup.confirm_title",
|
|
|
|
site_title: SiteSetting.title),
|
|
|
|
body: I18n.t("discourse_push_notifications.popup.confirm_body"),
|
2018-12-04 04:48:16 -05:00
|
|
|
icon: ActionController::Base.helpers.image_url("push-notifications/check.png"),
|
2018-05-04 18:31:48 -04:00
|
|
|
badge: get_badge,
|
|
|
|
tag: "#{Discourse.current_hostname}-subscription"
|
|
|
|
}
|
|
|
|
|
2019-01-10 09:38:31 -05:00
|
|
|
send_notification(user, subscription, message)
|
2018-05-04 18:31:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.unsubscribe(user, subscription)
|
2018-05-08 20:14:14 -04:00
|
|
|
PushSubscription.find_by(user: user, data: subscription.to_json)&.destroy!
|
2018-05-04 18:31:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def self.get_badge
|
2019-01-02 02:29:17 -05:00
|
|
|
if (url = SiteSetting.site_push_notifications_icon_url).present?
|
|
|
|
url
|
2018-11-14 02:03:02 -05:00
|
|
|
else
|
|
|
|
ActionController::Base.helpers.image_url("push-notifications/discourse.png")
|
|
|
|
end
|
2018-05-04 18:31:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.send_notification(user, subscription, message)
|
|
|
|
begin
|
2019-01-10 09:38:31 -05:00
|
|
|
Webpush.payload_send(
|
2018-05-04 18:31:48 -04:00
|
|
|
endpoint: subscription["endpoint"],
|
|
|
|
message: message.to_json,
|
|
|
|
p256dh: subscription.dig("keys", "p256dh"),
|
|
|
|
auth: subscription.dig("keys", "auth"),
|
|
|
|
vapid: {
|
|
|
|
subject: Discourse.base_url,
|
|
|
|
public_key: SiteSetting.vapid_public_key,
|
2019-02-15 15:11:44 -05:00
|
|
|
private_key: SiteSetting.vapid_private_key,
|
|
|
|
expiration: TOKEN_VALID_FOR_SECONDS
|
2018-05-04 18:31:48 -04:00
|
|
|
}
|
|
|
|
)
|
2019-01-10 09:38:31 -05:00
|
|
|
rescue Webpush::ExpiredSubscription
|
|
|
|
unsubscribe(user, subscription)
|
|
|
|
rescue Webpush::ResponseError => e
|
2019-02-15 11:40:33 -05:00
|
|
|
if e.response.message == "MismatchSenderId"
|
|
|
|
unsubscribe(user, subscription)
|
|
|
|
else
|
|
|
|
Discourse.warn_exception(
|
|
|
|
e,
|
|
|
|
message: "Failed to send push notification",
|
|
|
|
env: {
|
|
|
|
user_id: user.id,
|
|
|
|
endpoint: subscription["endpoint"],
|
|
|
|
message: message.to_json
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2018-05-04 18:31:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|