2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration::Provider::FlowdockProvider
|
2017-10-16 10:44:21 -04:00
|
|
|
|
|
|
|
PROVIDER_NAME = "flowdock".freeze
|
|
|
|
PROVIDER_ENABLED_SETTING = :chat_integration_flowdock_enabled
|
|
|
|
CHANNEL_PARAMETERS = [
|
|
|
|
{ key: "flow_token", regex: '^\S+', unique: true, hidden: true },
|
|
|
|
]
|
|
|
|
|
|
|
|
def self.send_message(url, message)
|
|
|
|
uri = URI(url)
|
|
|
|
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
http.use_ssl = true
|
|
|
|
|
|
|
|
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
|
|
|
|
req.body = message.to_json
|
|
|
|
response = http.request(req)
|
|
|
|
|
2019-11-14 15:03:49 -05:00
|
|
|
response
|
2017-10-16 10:44:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.generate_flowdock_message(post, flow_token)
|
|
|
|
display_name = "@#{post.user.username}"
|
|
|
|
full_name = post.user.name || ""
|
|
|
|
|
|
|
|
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
|
|
|
|
display_name = "#{full_name} @#{post.user.username}"
|
|
|
|
end
|
|
|
|
|
|
|
|
message = {
|
|
|
|
flow_token: flow_token,
|
|
|
|
event: "discussion",
|
2017-10-17 06:21:36 -04:00
|
|
|
author: {
|
|
|
|
name: display_name,
|
2017-10-16 10:44:21 -04:00
|
|
|
avatar: post.user.small_avatar_url
|
2017-10-17 06:21:36 -04:00
|
|
|
},
|
|
|
|
title: I18n.t("chat_integration.provider.flowdock.message_title"),
|
2017-10-16 10:44:21 -04:00
|
|
|
external_thread_id: post.topic.id,
|
|
|
|
body: post.excerpt(SiteSetting.chat_integration_flowdock_excerpt_length, text_entities: true, strip_links: false, remap_emoji: true),
|
|
|
|
thread: {
|
|
|
|
title: post.topic.title,
|
|
|
|
external_url: post.full_url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 15:03:49 -05:00
|
|
|
message
|
2017-10-16 10:44:21 -04:00
|
|
|
end
|
|
|
|
|
2020-06-15 11:45:25 -04:00
|
|
|
def self.trigger_notification(post, channel, rule)
|
2017-10-16 10:44:21 -04:00
|
|
|
flow_token = channel.data["flow_token"]
|
|
|
|
message = generate_flowdock_message(post, flow_token)
|
|
|
|
response = send_message("https://api.flowdock.com/messages", message)
|
|
|
|
|
|
|
|
unless response.kind_of?(Net::HTTPSuccess)
|
|
|
|
error_key = nil
|
2021-07-13 15:36:16 -04:00
|
|
|
raise ::DiscourseChatIntegration::ProviderError.new info: { error_key: error_key, message: message, response_body: response.body }
|
2017-10-16 10:44:21 -04:00
|
|
|
end
|
|
|
|
end
|
2017-10-17 06:21:36 -04:00
|
|
|
end
|