2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-13 10:58:27 +08:00
|
|
|
return if GlobalSetting.skip_db?
|
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
Rails.application.config.to_prepare do
|
|
|
|
require 'webpush'
|
|
|
|
|
|
|
|
def generate_vapid_key?
|
|
|
|
SiteSetting.vapid_public_key.blank? ||
|
|
|
|
SiteSetting.vapid_private_key.blank? ||
|
|
|
|
SiteSetting.vapid_public_key_bytes.blank? ||
|
|
|
|
SiteSetting.vapid_base_url != Discourse.base_url
|
|
|
|
end
|
2019-07-30 16:08:11 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
SiteSetting.vapid_base_url = Discourse.base_url if SiteSetting.vapid_base_url.blank?
|
2019-07-30 16:08:11 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if generate_vapid_key?
|
|
|
|
vapid_key = Webpush.generate_key
|
|
|
|
SiteSetting.vapid_public_key = vapid_key.public_key
|
|
|
|
SiteSetting.vapid_private_key = vapid_key.private_key
|
2018-05-05 06:31:48 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
SiteSetting.vapid_public_key_bytes = Base64.urlsafe_decode64(SiteSetting.vapid_public_key).bytes.join("|")
|
|
|
|
SiteSetting.vapid_base_url = Discourse.base_url
|
2019-07-30 16:08:11 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if ActiveRecord::Base.connection.table_exists?(:push_subscriptions)
|
|
|
|
PushSubscription.delete_all
|
|
|
|
end
|
2019-07-30 16:23:24 +08:00
|
|
|
end
|
2018-05-05 06:31:48 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
DiscourseEvent.on(:user_logged_out) do |user|
|
|
|
|
PushNotificationPusher.clear_subscriptions(user)
|
|
|
|
end
|
2018-05-05 06:31:48 +08:00
|
|
|
end
|