2016-06-16 01:49:57 +08:00
|
|
|
require 'excon'
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class EmitWebHookEvent < Jobs::Base
|
2018-05-21 16:23:09 +08:00
|
|
|
PING_EVENT = 'ping'.freeze
|
2018-05-30 19:27:40 +08:00
|
|
|
MAX_RETRY_COUNT = 4.freeze
|
|
|
|
RETRY_BACKOFF = 5
|
2018-05-21 16:23:09 +08:00
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
def execute(args)
|
2018-05-21 16:23:09 +08:00
|
|
|
%i{
|
|
|
|
web_hook_id
|
|
|
|
event_type
|
|
|
|
}.each do |key|
|
2016-12-23 00:08:35 +08:00
|
|
|
raise Discourse::InvalidParameters.new(key) unless args[key].present?
|
2016-11-08 11:43:54 +08:00
|
|
|
end
|
|
|
|
|
2018-05-30 19:27:40 +08:00
|
|
|
@orig_args = args.dup
|
|
|
|
|
2016-12-23 00:08:35 +08:00
|
|
|
web_hook = WebHook.find_by(id: args[:web_hook_id])
|
2017-10-02 12:50:22 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:web_hook_id) if web_hook.blank?
|
2016-11-08 11:43:54 +08:00
|
|
|
|
2016-12-23 00:08:35 +08:00
|
|
|
unless ping_event?(args[:event_type])
|
2016-11-08 11:43:54 +08:00
|
|
|
return unless web_hook.active?
|
2016-12-23 00:08:35 +08:00
|
|
|
|
2016-11-08 11:43:54 +08:00
|
|
|
return if web_hook.group_ids.present? && (args[:group_id].present? ||
|
|
|
|
!web_hook.group_ids.include?(args[:group_id]))
|
2016-12-23 00:08:35 +08:00
|
|
|
|
2016-11-08 11:43:54 +08:00
|
|
|
return if web_hook.category_ids.present? && (!args[:category_id].present? ||
|
|
|
|
!web_hook.category_ids.include?(args[:category_id]))
|
2016-12-23 00:08:35 +08:00
|
|
|
|
2018-12-05 17:14:06 +08:00
|
|
|
return if web_hook.tag_ids.present? && (args[:tag_ids].blank? ||
|
|
|
|
(web_hook.tag_ids & args[:tag_ids]).blank?)
|
|
|
|
|
2018-05-24 15:16:52 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:payload) unless args[:payload].present?
|
2018-05-21 16:23:09 +08:00
|
|
|
args[:payload] = JSON.parse(args[:payload])
|
2016-11-08 11:43:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
web_hook_request(args, web_hook)
|
2016-06-16 01:49:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-23 00:08:35 +08:00
|
|
|
def guardian
|
|
|
|
Guardian.new(Discourse.system_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ping_event?(event_type)
|
2018-05-21 16:23:09 +08:00
|
|
|
PING_EVENT == event_type.to_s
|
2016-12-23 00:08:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_web_hook_body(args, web_hook)
|
|
|
|
body = {}
|
|
|
|
event_type = args[:event_type].to_s
|
|
|
|
|
|
|
|
if ping_event?(event_type)
|
|
|
|
body[:ping] = 'OK'
|
|
|
|
else
|
|
|
|
body[event_type] = args[:payload]
|
|
|
|
end
|
2016-11-08 11:43:54 +08:00
|
|
|
|
2016-12-23 00:08:35 +08:00
|
|
|
new_body = Plugin::Filter.apply(:after_build_web_hook_body, self, body)
|
|
|
|
MultiJson.dump(new_body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def web_hook_request(args, web_hook)
|
2017-12-11 16:15:50 +08:00
|
|
|
uri = URI(web_hook.payload_url.strip)
|
2016-12-23 00:08:35 +08:00
|
|
|
|
|
|
|
conn = Excon.new(
|
|
|
|
uri.to_s,
|
|
|
|
ssl_verify_peer: web_hook.verify_certificate,
|
|
|
|
retry_limit: 0
|
|
|
|
)
|
2016-06-16 01:49:57 +08:00
|
|
|
|
2016-11-08 11:43:54 +08:00
|
|
|
body = build_web_hook_body(args, web_hook)
|
|
|
|
web_hook_event = WebHookEvent.create!(web_hook_id: web_hook.id)
|
2018-07-27 08:19:11 +08:00
|
|
|
response = nil
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
begin
|
2017-07-28 09:20:09 +08:00
|
|
|
content_type =
|
|
|
|
case web_hook.content_type
|
|
|
|
when WebHook.content_types['application/x-www-form-urlencoded']
|
|
|
|
'application/x-www-form-urlencoded'
|
|
|
|
else
|
|
|
|
'application/json'
|
|
|
|
end
|
2016-12-23 00:08:35 +08:00
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
headers = {
|
|
|
|
'Accept' => '*/*',
|
|
|
|
'Connection' => 'close',
|
2016-09-21 10:31:20 +08:00
|
|
|
'Content-Length' => body.bytesize,
|
2016-06-16 01:49:57 +08:00
|
|
|
'Content-Type' => content_type,
|
|
|
|
'Host' => uri.host,
|
2018-05-21 16:23:09 +08:00
|
|
|
'User-Agent' => "Discourse/#{Discourse::VERSION::STRING}",
|
2016-11-04 22:18:18 +08:00
|
|
|
'X-Discourse-Instance' => Discourse.base_url,
|
2016-06-16 01:49:57 +08:00
|
|
|
'X-Discourse-Event-Id' => web_hook_event.id,
|
2016-11-08 11:43:54 +08:00
|
|
|
'X-Discourse-Event-Type' => args[:event_type]
|
2016-06-16 01:49:57 +08:00
|
|
|
}
|
2016-12-23 00:08:35 +08:00
|
|
|
|
2016-11-08 11:43:54 +08:00
|
|
|
headers['X-Discourse-Event'] = args[:event_name].to_s if args[:event_name].present?
|
2016-06-16 01:49:57 +08:00
|
|
|
|
2016-11-08 11:43:54 +08:00
|
|
|
if web_hook.secret.present?
|
2018-05-21 16:23:09 +08:00
|
|
|
headers['X-Discourse-Event-Signature'] = "sha256=#{OpenSSL::HMAC.hexdigest("sha256", web_hook.secret, body)}"
|
2016-06-16 01:49:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
now = Time.zone.now
|
|
|
|
response = conn.post(headers: headers, body: body)
|
2016-12-23 00:08:35 +08:00
|
|
|
|
|
|
|
web_hook_event.update!(
|
|
|
|
headers: MultiJson.dump(headers),
|
|
|
|
payload: body,
|
|
|
|
status: response.status,
|
|
|
|
response_headers: MultiJson.dump(response.headers),
|
|
|
|
response_body: response.body,
|
|
|
|
duration: ((Time.zone.now - now) * 1000).to_i
|
|
|
|
)
|
2019-03-21 21:04:54 +08:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.error("Webhook event failed: #{e}")
|
2016-06-16 01:49:57 +08:00
|
|
|
end
|
2018-05-30 19:27:40 +08:00
|
|
|
|
2019-03-21 21:04:54 +08:00
|
|
|
MessageBus.publish("/web_hook_events/#{web_hook.id}", {
|
|
|
|
web_hook_event_id: web_hook_event.id,
|
|
|
|
event_type: args[:event_type]
|
|
|
|
}, user_ids: User.human_users.staff.pluck(:id))
|
|
|
|
|
2018-07-27 08:19:11 +08:00
|
|
|
retry_web_hook if response&.status != 200
|
2018-05-30 19:27:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def retry_web_hook
|
|
|
|
if SiteSetting.retry_web_hook_events?
|
|
|
|
@orig_args[:retry_count] = (@orig_args[:retry_count] || 0) + 1
|
|
|
|
return if @orig_args[:retry_count] > MAX_RETRY_COUNT
|
|
|
|
delay = RETRY_BACKOFF**(@orig_args[:retry_count] - 1)
|
|
|
|
Jobs.enqueue_in(delay.minutes, :emit_web_hook_event, @orig_args)
|
|
|
|
end
|
2016-06-16 01:49:57 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|