diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 70934e9..72be008 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -191,3 +191,14 @@ en: webhook_url: title: "Webhook URL" help: "The URL provided when you create a new integration in a Gitter room." + + ####################################### + ########## FLOWDOCK STRINGS ########### + ####################################### + flowdock: + title: "Flowdock" + param: + flow_token: + title: "Flow Token" + help: "The flow token provided after creating a source for a flow into which you want to send messages." + diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 61169b6..a4785ea 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -78,6 +78,12 @@ en: ####################################### chat_integration_gitter_enabled: "Enable the Gitter chat integration provider" + ####################################### + ########## FLOWDOCK SETTINGS ########## + ####################################### + chat_integration_flowdock_enabled: "Enable the Flowdock chat integration provider" + chat_integration_flowdock_excerpt_length: "Flowdock post excerpt length" + chat_integration: all_categories: "(all categories)" @@ -244,3 +250,9 @@ en: ~~~quote %{excerpt} ~~~ + + ####################################### + ########### FLOWDOCK STRINGS ########## + ####################################### + flowdock: + message_title: "posted" diff --git a/config/settings.yml b/config/settings.yml index 93e476b..1bb9aff 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -115,3 +115,11 @@ plugins: ####################################### chat_integration_gitter_enabled: default: false + +####################################### +########## FLOWDOCK SETTINGS ########## +####################################### + chat_integration_flowdock_enabled: + default: false + chat_integration_flowdock_excerpt_length: + default: 400 diff --git a/lib/discourse_chat/provider.rb b/lib/discourse_chat/provider.rb index 2f907cb..648feb9 100644 --- a/lib/discourse_chat/provider.rb +++ b/lib/discourse_chat/provider.rb @@ -98,3 +98,4 @@ require_relative "provider/matrix/matrix_provider" require_relative "provider/zulip/zulip_provider" require_relative "provider/rocketchat/rocketchat_provider" require_relative "provider/gitter/gitter_provider" +require_relative "provider/flowdock/flowdock_provider" diff --git a/lib/discourse_chat/provider/flowdock/flowdock_provider.rb b/lib/discourse_chat/provider/flowdock/flowdock_provider.rb new file mode 100644 index 0000000..765eb4d --- /dev/null +++ b/lib/discourse_chat/provider/flowdock/flowdock_provider.rb @@ -0,0 +1,59 @@ +module DiscourseChat::Provider::FlowdockProvider + + 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) + + return response + 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", + author: { + name: display_name, + avatar: post.user.small_avatar_url + }, + title: I18n.t("chat_integration.provider.flowdock.message_title"), + 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 + } + } + + return message + end + + def self.trigger_notification(post, channel) + 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 ::DiscourseChat::ProviderError.new info: { error_key: error_key, message: message, response_body: response.body } + end + end +end diff --git a/spec/lib/discourse_chat/provider/flowdock/flowdock_provider_spec.rb b/spec/lib/discourse_chat/provider/flowdock/flowdock_provider_spec.rb new file mode 100644 index 0000000..fbba268 --- /dev/null +++ b/spec/lib/discourse_chat/provider/flowdock/flowdock_provider_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe DiscourseChat::Provider::FlowdockProvider do + let(:post) { Fabricate(:post) } + + describe '.trigger_notifications' do + before do + SiteSetting.chat_integration_flowdock_enabled = true + end + + let(:chan1) { DiscourseChat::Channel.create!(provider: 'flowdock', data: { flow_token: '5d1fe04cf66e078d6a2b579ddb8a465b' }) } + + it 'sends a request' do + stub1 = stub_request(:post, "https://api.flowdock.com/messages").to_return(status: 200) + described_class.trigger_notification(post, chan1) + expect(stub1).to have_been_requested.once + end + + it 'handles errors correctly' do + stub1 = stub_request(:post, "https://api.flowdock.com/messages").to_return(status: 404, body: "{ \"error\": \"Not Found\"}") + expect(stub1).to have_been_requested.times(0) + expect { described_class.trigger_notification(post, chan1) }.to raise_exception(::DiscourseChat::ProviderError) + expect(stub1).to have_been_requested.once + end + end +end