discourse-chat-integration/lib/discourse_chat_integration/provider/flowdock/flowdock_provider.rb

57 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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 = FinalDestination::HTTP.new(uri.host, uri.port)
2017-10-16 10:44:21 -04:00
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 = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
2017-10-16 10:44:21 -04:00
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
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
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