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