Add notification support for Flowdock

This commit is contained in:
Maja Komel 2017-10-16 16:44:21 +02:00
parent 0e52edd1fd
commit 92d6a6ba0e
6 changed files with 117 additions and 0 deletions

View File

@ -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."

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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