-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplugin.rb
88 lines (70 loc) · 2.67 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
# frozen_string_literal: true
# name: discourse-pushover-notifications
# about: Plugin for integrating pushover notifications
# version: 0.1.0
# authors: Jeff Wong
# url: https://github.com/featheredtoast/discourse-pushover-notifications
enabled_site_setting :pushover_notifications_enabled
after_initialize do
module ::DiscoursePushoverNotifications
PLUGIN_NAME ||= 'discourse_pushover_notifications'.freeze
autoload :Pusher, "#{Rails.root}/plugins/discourse-pushover-notifications/services/discourse_pushover_notifications/pusher"
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscoursePushoverNotifications
end
end
User.register_custom_field_type(DiscoursePushoverNotifications::PLUGIN_NAME, :json)
allow_staff_user_custom_field DiscoursePushoverNotifications::PLUGIN_NAME
DiscoursePushoverNotifications::Engine.routes.draw do
post '/subscribe' => 'push#subscribe'
post '/unsubscribe' => 'push#unsubscribe'
end
Discourse::Application.routes.append do
mount ::DiscoursePushoverNotifications::Engine, at: '/pushover_notifications'
end
require_dependency 'application_controller'
class DiscoursePushoverNotifications::PushController < ::ApplicationController
requires_plugin DiscoursePushoverNotifications::PLUGIN_NAME
layout false
before_action :ensure_logged_in
skip_before_action :preload_json
def subscribe
DiscoursePushoverNotifications::Pusher.subscribe(current_user, push_params)
if DiscoursePushoverNotifications::Pusher.confirm_subscribe(current_user)
render json: success_json
else
render json: { failed: 'FAILED', error: I18n.t("discourse_pushover_notifications.subscribe_error") }
end
end
def unsubscribe
DiscoursePushoverNotifications::Pusher.unsubscribe(current_user)
render json: success_json
end
private
def push_params
params.require(:subscription)
end
end
DiscourseEvent.on(:push_notification) do |user, payload|
if SiteSetting.pushover_notifications_enabled?
Jobs.enqueue(:send_pushover_notifications, user_id: user.id, payload: payload)
end
end
DiscourseEvent.on(:user_logged_out) do |user|
if SiteSetting.pushover_notifications_enabled?
DiscoursePushoverNotifications::Pusher.unsubscribe(user)
user.save_custom_fields(true)
end
end
require_dependency 'jobs/base'
module ::Jobs
class SendPushoverNotifications < ::Jobs::Base
def execute(args)
return unless SiteSetting.pushover_notifications_enabled?
user = User.find(args[:user_id])
DiscoursePushoverNotifications::Pusher.push(user, args[:payload])
end
end
end
end