Add matrix support

This commit is contained in:
David Taylor 2017-07-27 17:09:44 +01:00
parent 4add044f86
commit 5a4faa637f
6 changed files with 165 additions and 1 deletions

View File

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

View File

@ -48,6 +48,14 @@ en:
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: |
<b>%{user}</b> posted in <b><a href='%{post_url}'>%{title}</a></b>
<blockquote>
%{excerpt}
</blockquote>

View File

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

View File

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

View File

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

View File

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