forked from discourse/discourse-push-notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
94 lines (74 loc) · 2.86 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# name: discourse-push-notifications
# about: Plugin for integrating Chrome and FireFox push notifications
# version: 0.2.0
# authors: Alan Guo Xiang Tan
# url: https://github.com/discourse/discourse-push-notifications
gem 'hkdf', '0.3.0'
gem 'webpush', '0.3.2'
enabled_site_setting :push_notifications_enabled
register_service_worker "javascripts/push-service-worker.js"
after_initialize do
module ::DiscoursePushNotifications
PLUGIN_NAME ||= "discourse_push_notifications".freeze
autoload :Pusher, "#{Rails.root}/plugins/discourse-push-notifications/services/discourse_push_notifications/pusher"
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscoursePushNotifications
end
end
User.register_custom_field_type(DiscoursePushNotifications::PLUGIN_NAME, :json)
if SiteSetting.vapid_public_key.blank? || SiteSetting.vapid_private_key.blank?
vapid_key = Webpush.generate_key
SiteSetting.vapid_public_key = vapid_key.public_key
SiteSetting.vapid_private_key = vapid_key.private_key
end
SiteSetting.vapid_public_key_bytes = Base64.urlsafe_decode64(SiteSetting.vapid_public_key).bytes.join("|")
DiscoursePushNotifications::Engine.routes.draw do
post "/subscribe" => "push#subscribe"
post "/unsubscribe" => "push#unsubscribe"
end
Discourse::Application.routes.append do
mount ::DiscoursePushNotifications::Engine, at: "/push_notifications"
get "/push-service-worker.js" => redirect("/service-worker.js")
end
require_dependency "application_controller"
class DiscoursePushNotifications::PushController < ::ApplicationController
requires_plugin DiscoursePushNotifications::PLUGIN_NAME
layout false
before_action :ensure_logged_in
skip_before_action :preload_json
def subscribe
DiscoursePushNotifications::Pusher.subscribe(current_user, push_params)
render json: success_json
end
def unsubscribe
DiscoursePushNotifications::Pusher.unsubscribe(current_user, push_params)
render json: success_json
end
private
def push_params
params.require(:subscription).permit(:endpoint, keys: [:p256dh, :auth])
end
end
DiscourseEvent.on(:post_notification_alert) do |user, payload|
if SiteSetting.push_notifications_enabled?
Jobs.enqueue(:send_push_notifications, user_id: user.id, payload: payload)
end
end
DiscourseEvent.on(:user_logged_out) do |user|
if SiteSetting.push_notifications_enabled?
DiscoursePushNotifications::Pusher.clear_subscriptions(user)
user.save_custom_fields(true)
end
end
require_dependency "jobs/base"
module ::Jobs
class SendPushNotifications < Jobs::Base
def execute(args)
return if !SiteSetting.push_notifications_enabled?
user = User.find(args[:user_id])
DiscoursePushNotifications::Pusher.push(user, args[:payload])
end
end
end
end