diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 9e5c44d..8e0baf6 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -98,4 +98,16 @@ en: help: The webhook URL created in your HipChat integration color: title: Color - help: The colour of the message in HipChat. Must be one of yellow,green,red,purple,gray,random \ No newline at end of file + help: The colour of the message in HipChat. Must be one of yellow,green,red,purple,gray,random + + ####################################### + ######### MATTERMOST STRINGS ########## + ####################################### + mattermost: + title: "Mattermost" + param: + identifier: + title: Channel + help: "e.g. #channel, @username." + errors: + channel_not_found: "The specified channel does not exist on Mattermost" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 11f6325..a252660 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -36,8 +36,16 @@ en: ########## HIPCHAT SETTINGS ########### ####################################### chat_integration_hipchat_enabled: "Enable the Hipchat chat-integration provider" - chat_integration_icon_url: "Icon for posts to hipchat (defaults to forum logo)" + chat_integration_hipchat_icon_url: "Icon for posts to hipchat (defaults to forum logo)" chat_integration_hipchat_excerpt_length: "Hipchat post excerpt length" + + ####################################### + ######## MATTERMOST SETTINGS ########## + ####################################### + chat_integration_mattermost_enabled: "Enable the Mattermost chat-integration provider" + chat_integration_mattermost_webhook_url: 'URL for the Mattermost webhook' + chat_integration_mattermost_icon_url: "Icon for posts to mattermost (defaults to forum logo)" + chat_integration_mattermost_excerpt_length: "Mattermost post excerpt length" chat_integration: diff --git a/config/settings.yml b/config/settings.yml index d75535b..20a09f1 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -58,3 +58,15 @@ plugins: default: '' chat_integration_hipchat_excerpt_length: default: 400 + +####################################### +######## MATTERMOST SETTINGS ########## +####################################### + chat_integration_mattermost_enabled: + default: false + chat_integration_mattermost_webhook_url: + default: '' + chat_integration_mattermost_icon_url: + default: '' + chat_integration_mattermost_excerpt_length: + default: 400 diff --git a/lib/discourse_chat/provider.rb b/lib/discourse_chat/provider.rb index 2dc2b01..4c0dd8f 100644 --- a/lib/discourse_chat/provider.rb +++ b/lib/discourse_chat/provider.rb @@ -93,3 +93,4 @@ require_relative "provider/slack/slack_provider.rb" 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" diff --git a/lib/discourse_chat/provider/discord/discord_provider.rb b/lib/discourse_chat/provider/discord/discord_provider.rb index 3d48270..499b61e 100644 --- a/lib/discourse_chat/provider/discord/discord_provider.rb +++ b/lib/discourse_chat/provider/discord/discord_provider.rb @@ -47,6 +47,7 @@ module DiscourseChat end def self.trigger_notification(post, channel) + # Adding ?wait=true means that we actually get a success/failure response, rather than returning asynchronously webhook_url = channel.data['webhook_url']+'?wait=true' message = generate_discord_message(post) diff --git a/lib/discourse_chat/provider/mattermost/mattermost_provider.rb b/lib/discourse_chat/provider/mattermost/mattermost_provider.rb new file mode 100644 index 0000000..9806349 --- /dev/null +++ b/lib/discourse_chat/provider/mattermost/mattermost_provider.rb @@ -0,0 +1,84 @@ +module DiscourseChat + module Provider + module MattermostProvider + PROVIDER_NAME = "mattermost".freeze + PROVIDER_ENABLED_SETTING = :chat_integration_mattermost_enabled + CHANNEL_PARAMETERS = [ + {key: "identifier", regex: '^[@#]\S*$'} + ] + + def self.send_via_webhook(message) + + uri = URI(SiteSetting.chat_integration_mattermost_webhook_url) + + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = (uri.scheme == 'https') + req = Net::HTTP::Post.new(uri, 'Content-Type' =>'application/json') + req.body = message.to_json + response = http.request(req) + + unless response.kind_of? Net::HTTPSuccess + if response.body.include? "Couldn't find the channel" + error_key = 'chat_integration.provider.mattermost.errors.channel_not_found' + else + error_key = nil + end + raise ::DiscourseChat::ProviderError.new info: {error_key: error_key, request: req.body, response_code:response.code, response_body:response.body} + end + + end + + def self.mattermost_message(post, channel) + 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 + + topic = post.topic + + category = '' + if topic.category + category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]": "[#{topic.category.name}]" + end + + icon_url = + if !SiteSetting.chat_integration_slack_icon_url.blank? + UrlHelper.absolute(SiteSetting.chat_integration_mattermost_icon_url) + elsif !SiteSetting.logo_small_url.blank? + UrlHelper.absolute(SiteSetting.logo_small_url) + end + + message = { + channel: channel, + username: SiteSetting.title || "Discourse", + icon_url: icon_url, + attachments: [] + } + + summary = { + fallback: "#{topic.title} - #{display_name}", + author_name: display_name, + author_icon: post.user.small_avatar_url, + color: topic.category ? "##{topic.category.color}" : nil, + text: post.excerpt(SiteSetting.chat_integration_mattermost_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true), + title: "#{topic.title} #{(category == '[uncategorized]')? '' : category} #{topic.tags.present? ? topic.tags.map(&:name).join(', ') : ''}", + title_link: post.full_url, + thumb_url: post.full_url, + } + + message[:attachments].push(summary) + message + end + + def self.trigger_notification(post, channel) + channel_id = channel.data['identifier'] + message = mattermost_message(post, channel_id) + + self.send_via_webhook(message) + end + + end + end +end \ No newline at end of file diff --git a/spec/lib/discourse_chat/provider/mattermost/mattermost_provider_spec.rb b/spec/lib/discourse_chat/provider/mattermost/mattermost_provider_spec.rb new file mode 100644 index 0000000..6ee6377 --- /dev/null +++ b/spec/lib/discourse_chat/provider/mattermost/mattermost_provider_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +RSpec.describe DiscourseChat::Provider::MattermostProvider do + let(:post) { Fabricate(:post) } + + describe '.trigger_notifications' do + before do + SiteSetting.chat_integration_mattermost_enabled = true + SiteSetting.chat_integration_mattermost_webhook_url = "https://mattermost.blah/hook/abcd" + end + + let(:chan1){DiscourseChat::Channel.create!(provider:'mattermost', data:{identifier: "#awesomechannel"})} + + it 'sends a webhook request' do + stub1 = stub_request(:post, 'https://mattermost.blah/hook/abcd').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://mattermost.blah/hook/abcd").to_return(status: 500, body: "error") + 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