-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplugin.rb
67 lines (53 loc) · 2.02 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
# name: webhooks
# about: Make HTTP requests when certain events occur
# version: 0.1
# authors: Ryan Fox
# url: https://github.com/rcfox/Discourse-Webhooks
after_initialize do
SYSTEM_GUARDIAN = Guardian.new(User.find_by(id: -1))
# Include the SSO record with all User data for staff requests so that you
# can figure out which of your users triggered the current webhook request.
UserSerializer.class_eval do
staff_attributes :single_sign_on_record
end
User.class_eval do
# User.as_json seems to be broken due to no User::View being defined?
# I don't really know what that's about. Let's just hack around it!
def as_json(options = nil)
user_serializer = UserSerializer.new(self, scope: SYSTEM_GUARDIAN)
user_serializer.serializable_hash
end
end
def build_event_url(url_template, event_name)
url = String.new(url_template)
url.gsub!("%{event_name}", event_name)
return url
end
SiteSetting.webhooks_registered_events.split('|').each do |event_name|
DiscourseEvent.on(event_name.to_sym) do |*params|
next unless SiteSetting.webhooks_enabled
if SiteSetting.webhooks_include_api_key
api_key = ApiKey.find_by(user_id: nil)
if not api_key
Rails.logger.warn('Webhooks configured to include the "All User" API key, but it does not exist.')
else
params.unshift(api_key.key)
end
end
uri = URI.parse(build_event_url(SiteSetting.webhooks_url_format, event_name))
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless SiteSetting.webhooks_verify_ssl
request = Net::HTTP::Post.new(uri.path)
request.add_field('Content-Type', 'application/json')
request.body = params.to_json
response = http.request(request)
case response
when Net::HTTPSuccess then
# nothing
else
Rails.logger.error("#{uri}: #{response.code} - #{response.message}")
end
end
end
end