2016-06-15 13:49:57 -04:00
|
|
|
require 'excon'
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class EmitWebHookEvent < Jobs::Base
|
|
|
|
def execute(args)
|
2016-12-22 11:08:35 -05:00
|
|
|
[:web_hook_id, :event_type].each do |key|
|
|
|
|
raise Discourse::InvalidParameters.new(key) unless args[key].present?
|
2016-11-07 22:43:54 -05:00
|
|
|
end
|
|
|
|
|
2016-12-22 11:08:35 -05:00
|
|
|
web_hook = WebHook.find_by(id: args[:web_hook_id])
|
2017-10-02 00:50:22 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:web_hook_id) if web_hook.blank?
|
2016-11-07 22:43:54 -05:00
|
|
|
|
2016-12-22 11:08:35 -05:00
|
|
|
unless ping_event?(args[:event_type])
|
2016-11-07 22:43:54 -05:00
|
|
|
return unless web_hook.active?
|
2016-12-22 11:08:35 -05:00
|
|
|
|
2016-11-07 22:43:54 -05:00
|
|
|
return if web_hook.group_ids.present? && (args[:group_id].present? ||
|
|
|
|
!web_hook.group_ids.include?(args[:group_id]))
|
2016-12-22 11:08:35 -05:00
|
|
|
|
2016-11-07 22:43:54 -05:00
|
|
|
return if web_hook.category_ids.present? && (!args[:category_id].present? ||
|
|
|
|
!web_hook.category_ids.include?(args[:category_id]))
|
2016-12-22 11:08:35 -05:00
|
|
|
|
|
|
|
event_type = args[:event_type].to_s
|
2017-03-15 22:22:15 -04:00
|
|
|
return unless self.send("setup_#{event_type}", args)
|
2016-11-07 22:43:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
web_hook_request(args, web_hook)
|
2016-06-15 13:49:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-22 11:08:35 -05:00
|
|
|
def guardian
|
|
|
|
Guardian.new(Discourse.system_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_post(args)
|
2018-02-12 15:06:56 -05:00
|
|
|
post = Post.with_deleted.find_by(id: args[:post_id])
|
2016-12-22 11:08:35 -05:00
|
|
|
return if post.blank?
|
|
|
|
args[:payload] = WebHookPostSerializer.new(post, scope: guardian, root: false).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_topic(args)
|
2018-03-28 04:20:08 -04:00
|
|
|
topic_view = TopicView.new(args[:topic_id], Discourse.system_user)
|
2016-12-22 11:08:35 -05:00
|
|
|
return if topic_view.blank?
|
2017-03-15 22:22:15 -04:00
|
|
|
args[:payload] = WebHookTopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
|
2016-12-22 11:08:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_user(args)
|
|
|
|
user = User.find_by(id: args[:user_id])
|
|
|
|
return if user.blank?
|
2017-03-15 22:22:15 -04:00
|
|
|
args[:payload] = WebHookUserSerializer.new(user, scope: guardian, root: false).as_json
|
2016-12-22 11:08:35 -05:00
|
|
|
end
|
|
|
|
|
2018-03-28 08:10:29 -04:00
|
|
|
def setup_group(args)
|
|
|
|
group = Group.find(args[:group_id])
|
|
|
|
return if group.blank?
|
|
|
|
args[:payload] = WebHookGroupSerializer.new(group, scope: guardian, root: false).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_category(args)
|
|
|
|
category = Category.find(args[:category_id])
|
|
|
|
return if category.blank?
|
|
|
|
args[:payload] = WebHookCategorySerializer.new(category, scope: guardian, root: false).as_json
|
|
|
|
end
|
|
|
|
|
2018-04-04 08:19:20 -04:00
|
|
|
def setup_tag(args)
|
|
|
|
tag = Tag.find(args[:tag_id])
|
|
|
|
return if tag.blank?
|
|
|
|
args[:payload] = TagSerializer.new(tag, scope: guardian, root: false).as_json
|
|
|
|
end
|
|
|
|
|
2016-12-22 11:08:35 -05:00
|
|
|
def ping_event?(event_type)
|
|
|
|
event_type.to_s == 'ping'.freeze
|
|
|
|
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-07 22:43:54 -05:00
|
|
|
|
2016-12-22 11:08:35 -05: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 03:15:50 -05:00
|
|
|
uri = URI(web_hook.payload_url.strip)
|
2016-12-22 11:08:35 -05:00
|
|
|
|
|
|
|
conn = Excon.new(
|
|
|
|
uri.to_s,
|
|
|
|
ssl_verify_peer: web_hook.verify_certificate,
|
|
|
|
retry_limit: 0
|
|
|
|
)
|
2016-06-15 13:49:57 -04:00
|
|
|
|
2016-11-07 22:43:54 -05:00
|
|
|
body = build_web_hook_body(args, web_hook)
|
|
|
|
web_hook_event = WebHookEvent.create!(web_hook_id: web_hook.id)
|
2016-06-15 13:49:57 -04:00
|
|
|
|
|
|
|
begin
|
2017-07-27 21:20:09 -04: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-22 11:08:35 -05:00
|
|
|
|
2016-06-15 13:49:57 -04:00
|
|
|
headers = {
|
|
|
|
'Accept' => '*/*',
|
|
|
|
'Connection' => 'close',
|
2016-09-20 22:31:20 -04:00
|
|
|
'Content-Length' => body.bytesize,
|
2016-06-15 13:49:57 -04:00
|
|
|
'Content-Type' => content_type,
|
|
|
|
'Host' => uri.host,
|
|
|
|
'User-Agent' => "Discourse/" + Discourse::VERSION::STRING,
|
2016-11-04 10:18:18 -04:00
|
|
|
'X-Discourse-Instance' => Discourse.base_url,
|
2016-06-15 13:49:57 -04:00
|
|
|
'X-Discourse-Event-Id' => web_hook_event.id,
|
2016-11-07 22:43:54 -05:00
|
|
|
'X-Discourse-Event-Type' => args[:event_type]
|
2016-06-15 13:49:57 -04:00
|
|
|
}
|
2016-12-22 11:08:35 -05:00
|
|
|
|
2016-11-07 22:43:54 -05:00
|
|
|
headers['X-Discourse-Event'] = args[:event_name].to_s if args[:event_name].present?
|
2016-06-15 13:49:57 -04:00
|
|
|
|
2016-11-07 22:43:54 -05:00
|
|
|
if web_hook.secret.present?
|
|
|
|
headers['X-Discourse-Event-Signature'] = "sha256=" + OpenSSL::HMAC.hexdigest("sha256", web_hook.secret, body)
|
2016-06-15 13:49:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
now = Time.zone.now
|
|
|
|
response = conn.post(headers: headers, body: body)
|
2016-12-22 11:08:35 -05: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
|
|
|
|
)
|
|
|
|
|
|
|
|
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))
|
2016-06-15 13:49:57 -04:00
|
|
|
rescue
|
|
|
|
web_hook_event.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|