From 5a4faa637f9a2a61895bd91647d21513d4aa4fdb Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 27 Jul 2017 17:09:44 +0100 Subject: [PATCH] Add matrix support --- config/locales/client.en.yml | 16 ++++ config/locales/server.en.yml | 22 ++++- config/settings.yml | 12 +++ lib/discourse_chat/provider.rb | 1 + .../provider/matrix/matrix_provider.rb | 86 +++++++++++++++++++ .../provider/matrix/matrix_provider_spec.rb | 29 +++++++ 6 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 lib/discourse_chat/provider/matrix/matrix_provider.rb create mode 100644 spec/lib/discourse_chat/provider/matrix/matrix_provider_spec.rb diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 8e0baf6..72319f9 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -111,3 +111,19 @@ en: help: "e.g. #channel, @username." errors: channel_not_found: "The specified channel does not exist on Mattermost" + + ####################################### + ############ MATRIX STRINGS ########### + ####################################### + matrix: + title: "Matrix" + param: + name: + title: "Name" + help: "A name to describe the channel. It is not used for the connection to Matrix." + room_id: + title: "Room ID" + help: "The 'private identifier' for the room. It should look something like !abcdefg:matrix.org" + errors: + unknown_token: "Access token is invalid" + unknown_room: "Room ID is invalid" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 0516f0d..559b491 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -47,7 +47,15 @@ en: chat_integration_mattermost_incoming_webhook_token: 'The verification token used to authenticate incoming requests' chat_integration_mattermost_icon_url: "Icon for posts to mattermost (defaults to forum logo)" chat_integration_mattermost_excerpt_length: "Mattermost post excerpt length" - + + ####################################### + ############ MATRIX SETTINGS ########## + ####################################### + chat_integration_matrix_enabled: "Enable the Matrix chat integration provider" + chat_integration_matrix_homeserver: "Homeserver to connect to. Make sure to include the protocol" + chat_integration_matrix_access_token: "Access token for the bot's matrix account" + chat_integration_matrix_excerpt_length: "Matrix post excerpt length" + chat_integration: all_categories: "(all categories)" @@ -182,3 +190,15 @@ en: *List rules:* `/discourse status` *Help:* `/discourse help` + + ####################################### + ############ MATRIX STRINGS ########### + ####################################### + matrix: + text_message: "%{user} posted in %{title} - %{post_url}" + formatted_message: | + %{user} posted in %{title} +
+ %{excerpt} +
+ diff --git a/config/settings.yml b/config/settings.yml index 9ebffb2..35f8966 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -72,3 +72,15 @@ plugins: default: '' chat_integration_mattermost_excerpt_length: default: 400 + +####################################### +########## MATRIX SETTINGS ############ +####################################### + chat_integration_matrix_enabled: + default: false + chat_integration_matrix_homeserver: + default: 'https://matrix.org' + chat_integration_matrix_access_token: + default: '' + chat_integration_matrix_excerpt_length: + default: 400 \ No newline at end of file diff --git a/lib/discourse_chat/provider.rb b/lib/discourse_chat/provider.rb index 4c0dd8f..788cb0a 100644 --- a/lib/discourse_chat/provider.rb +++ b/lib/discourse_chat/provider.rb @@ -94,3 +94,4 @@ require_relative "provider/telegram/telegram_provider.rb" require_relative "provider/discord/discord_provider.rb" require_relative "provider/hipchat/hipchat_provider.rb" require_relative "provider/mattermost/mattermost_provider.rb" +require_relative "provider/matrix/matrix_provider.rb" diff --git a/lib/discourse_chat/provider/matrix/matrix_provider.rb b/lib/discourse_chat/provider/matrix/matrix_provider.rb new file mode 100644 index 0000000..feb0304 --- /dev/null +++ b/lib/discourse_chat/provider/matrix/matrix_provider.rb @@ -0,0 +1,86 @@ +module DiscourseChat + module Provider + module MatrixProvider + PROVIDER_NAME = "matrix".freeze + PROVIDER_ENABLED_SETTING = :chat_integration_matrix_enabled + CHANNEL_PARAMETERS = [ + {key: "name", regex: '^\S+'}, + {key: "room_id", regex: '^\!\S+:\S+$'} + ] + + def self.send_message(room_id, message) + homeserver = SiteSetting.chat_integration_matrix_homeserver + event_type = 'm.room.message' + uid = Time.now.to_i + + url_params = URI.encode_www_form({ + access_token: SiteSetting.chat_integration_matrix_access_token + }) + + url = "#{homeserver}/_matrix/client/r0/rooms/#{CGI::escape(room_id)}/send/#{event_type}/#{uid}" + + uri = URI([url,url_params].join('?')) + + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + + req = Net::HTTP::Put.new(uri, 'Content-Type' =>'application/json') + req.body = message.to_json + response = http.request(req) + + return response + end + + def self.generate_matrix_message(post) + + 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 = { + msgtype: 'm.notice', + body: I18n.t('chat_integration.provider.matrix.text_message', { + user: display_name, + post_url: post.full_url, + title: post.topic.title + }), + format: 'org.matrix.custom.html', + formatted_body: I18n.t('chat_integration.provider.matrix.formatted_message', { + user: display_name, + post_url: post.full_url, + title: post.topic.title, + excerpt: post.excerpt(SiteSetting.chat_integration_discord_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true), + }) + + } + + return message + end + + def self.trigger_notification(post, channel) + message = generate_matrix_message(post) + + response = send_message(channel.data['room_id'], message) + + if not response.kind_of? Net::HTTPSuccess + error_key = nil + begin + responseData = JSON.parse(response.body) + if responseData['errcode'] == "M_UNKNOWN_TOKEN" + error_key = 'chat_integration.provider.matrix.errors.unknown_token' + elsif responseData['errcode'] == "M_UNKNOWN" + error_key = 'chat_integration.provider.matrix.errors.unknown_room' + end + ensure + raise ::DiscourseChat::ProviderError.new info: {error_key: error_key, message: message, response_body:response.body} + end + end + + end + + end + end +end \ No newline at end of file diff --git a/spec/lib/discourse_chat/provider/matrix/matrix_provider_spec.rb b/spec/lib/discourse_chat/provider/matrix/matrix_provider_spec.rb new file mode 100644 index 0000000..287d3b3 --- /dev/null +++ b/spec/lib/discourse_chat/provider/matrix/matrix_provider_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +RSpec.describe DiscourseChat::Provider::MatrixProvider do + let(:post) { Fabricate(:post) } + + describe '.trigger_notifications' do + before do + SiteSetting.chat_integration_matrix_enabled = true + SiteSetting.chat_integration_matrix_access_token = 'abcd' + end + + let(:chan1){DiscourseChat::Channel.create!(provider:'matrix', data:{name: "Awesome Channel", room_id: '!blah:matrix.org'})} + + it 'sends the message' do + stub1 = stub_request(:put, %r{https://matrix.org/_matrix/client/r0/rooms/!blah:matrix.org/send/m.room.message/*}).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(:put, %r{https://matrix.org/_matrix/client/r0/rooms/!blah:matrix.org/send/m.room.message/*}).to_return(status: 400, body:'{"errmsg":"M_UNKNOWN"}') + 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