diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 2553bc9..5ce7063 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -166,3 +166,15 @@ en: help: "The subject that these messages sent by the bot should be given" errors: does_not_exist: "That stream does not exist on Zulip" + + ####################################### + ######## ROCKET CHAT STRINGS ########## + ####################################### + rocketchat: + title: "Rocket.Chat" + param: + identifier: + title: Channel + help: "e.g. #channel, @username." + errors: + invalid_channel: "That channel does not exist on Rocket Chat" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 6bfce67..3ec459c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -59,12 +59,19 @@ en: ####################################### ########### ZULIP SETTINGS ############ ####################################### - chat_integration_zulip_enabled: "Enable the Matrix chat integration provider" + chat_integration_zulip_enabled: "Enable the Zulip chat integration provider" chat_integration_zulip_server: "The base URL for your Zulip server. Make sure to include http(s)://" chat_integration_zulip_bot_email_address: "The email address associated with your Zulip bot" chat_integration_zulip_bot_api_key: "The API key for your Zulip bot" chat_integration_zulip_excerpt_length: "Zulip post excerpt length" + ####################################### + ###### ROCKET CHAT SETTINGS ########### + ####################################### + chat_integration_rocketchat_enabled: "Enable the Rocket Chat chat integration provider" + chat_integration_rocketchat_webhook_url: "The URL for the Rocket Chat integration webhook" + chat_integration_rocketchat_excerpt_length: "Rocket Chat post excerpt length" + chat_integration: all_categories: "(all categories)" diff --git a/config/settings.yml b/config/settings.yml index f1ac881..fa5b04a 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -98,3 +98,13 @@ plugins: default: '' chat_integration_zulip_excerpt_length: default: 400 + +####################################### +######## ROCKET CHAT SETTINGS ######### +####################################### + chat_integration_rocketchat_enabled: + default: false + chat_integration_rocketchat_webhook_url: + default: '' + chat_integration_rocketchat_excerpt_length: + default: 400 diff --git a/lib/discourse_chat/provider.rb b/lib/discourse_chat/provider.rb index 3904810..981acd3 100644 --- a/lib/discourse_chat/provider.rb +++ b/lib/discourse_chat/provider.rb @@ -96,3 +96,4 @@ require_relative "provider/hipchat/hipchat_provider.rb" require_relative "provider/mattermost/mattermost_provider.rb" require_relative "provider/matrix/matrix_provider.rb" require_relative "provider/zulip/zulip_provider.rb" +require_relative "provider/rocketchat/rocketchat_provider.rb" diff --git a/lib/discourse_chat/provider/rocketchat/rocketchat_provider.rb b/lib/discourse_chat/provider/rocketchat/rocketchat_provider.rb new file mode 100644 index 0000000..cc45d52 --- /dev/null +++ b/lib/discourse_chat/provider/rocketchat/rocketchat_provider.rb @@ -0,0 +1,74 @@ +module DiscourseChat::Provider::RocketchatProvider + PROVIDER_NAME = "rocketchat".freeze + + PROVIDER_ENABLED_SETTING = :chat_integration_rocketchat_enabled + + CHANNEL_PARAMETERS = [ + { key: "identifier", regex: '^[@#]\S*$', unique: true } + ] + + def self.rocketchat_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 + + message = { + channel: channel, + 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_rocketchat_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true), + mrkdwn_in: ["text"], + 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) + + return message + end + + def self.send_via_webhook(message) + uri = URI(SiteSetting.chat_integration_rocketchat_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?('invalid-channel') + error_key = 'chat_integration.provider.rocketchat.errors.invalid_channel' + 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.trigger_notification(post, channel) + channel_id = channel.data['identifier'] + message = rocketchat_message(post, channel_id) + + self.send_via_webhook(message) + end +end diff --git a/spec/lib/discourse_chat/provider/rocketchat/rocketchat_provider_spec.rb b/spec/lib/discourse_chat/provider/rocketchat/rocketchat_provider_spec.rb new file mode 100644 index 0000000..fc3d831 --- /dev/null +++ b/spec/lib/discourse_chat/provider/rocketchat/rocketchat_provider_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +RSpec.describe DiscourseChat::Provider::RocketchatProvider do + let(:post) { Fabricate(:post) } + + describe '.trigger_notifications' do + before do + SiteSetting.chat_integration_rocketchat_enabled = true + SiteSetting.chat_integration_rocketchat_webhook_url = "https://example.com/abcd" + end + + let(:chan1) { DiscourseChat::Channel.create!(provider: 'rocketchat', data: { identifier: "#general" }) } + + it 'sends a webhook request' do + stub1 = stub_request(:post, 'https://example.com/abcd').to_return(body: "{\"success\":true}") + described_class.trigger_notification(post, chan1) + expect(stub1).to have_been_requested.once + end + + it 'handles errors correctly' do + stub1 = stub_request(:post, 'https://example.com/abcd').to_return(status: 400, body: "{}") + 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