From 92d6a6ba0e89025293e63638d19023d516665bd7 Mon Sep 17 00:00:00 2001 From: Maja Komel Date: Mon, 16 Oct 2017 16:44:21 +0200 Subject: [PATCH 1/2] Add notification support for Flowdock --- config/locales/client.en.yml | 11 ++++ config/locales/server.en.yml | 12 ++++ config/settings.yml | 8 +++ lib/discourse_chat/provider.rb | 1 + .../provider/flowdock/flowdock_provider.rb | 59 +++++++++++++++++++ .../flowdock/flowdock_provider_spec.rb | 26 ++++++++ 6 files changed, 117 insertions(+) create mode 100644 lib/discourse_chat/provider/flowdock/flowdock_provider.rb create mode 100644 spec/lib/discourse_chat/provider/flowdock/flowdock_provider_spec.rb 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 3bbfb52..df4a16c 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -116,3 +116,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..de0b626 --- /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..b9cdccc --- /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 From 7889691cdb395baddf247b639d3f80f1fc8c44b8 Mon Sep 17 00:00:00 2001 From: Maja Komel Date: Tue, 17 Oct 2017 12:21:36 +0200 Subject: [PATCH 2/2] fix rubocop offenses --- .../provider/flowdock/flowdock_provider.rb | 10 +++++----- .../provider/flowdock/flowdock_provider_spec.rb | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/discourse_chat/provider/flowdock/flowdock_provider.rb b/lib/discourse_chat/provider/flowdock/flowdock_provider.rb index de0b626..765eb4d 100644 --- a/lib/discourse_chat/provider/flowdock/flowdock_provider.rb +++ b/lib/discourse_chat/provider/flowdock/flowdock_provider.rb @@ -30,11 +30,11 @@ module DiscourseChat::Provider::FlowdockProvider message = { flow_token: flow_token, event: "discussion", - author: { - name: display_name, + author: { + name: display_name, avatar: post.user.small_avatar_url - }, - title: I18n.t("chat_integration.provider.flowdock.message_title"), + }, + 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: { @@ -56,4 +56,4 @@ module DiscourseChat::Provider::FlowdockProvider raise ::DiscourseChat::ProviderError.new info: { error_key: error_key, message: message, response_body: response.body } end 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 index b9cdccc..fbba268 100644 --- a/spec/lib/discourse_chat/provider/flowdock/flowdock_provider_spec.rb +++ b/spec/lib/discourse_chat/provider/flowdock/flowdock_provider_spec.rb @@ -8,7 +8,7 @@ RSpec.describe DiscourseChat::Provider::FlowdockProvider do SiteSetting.chat_integration_flowdock_enabled = true end - let(:chan1) { DiscourseChat::Channel.create!(provider: 'flowdock', data: { flow_token: '5d1fe04cf66e078d6a2b579ddb8a465b'}) } + 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)