diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 830fe30..2553bc9 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -151,3 +151,18 @@ en: errors: unknown_token: "Access token is invalid" unknown_room: "Room ID is invalid" + + ####################################### + ############# ZULIP STRINGS ########### + ####################################### + zulip: + title: "Zulip" + param: + stream: + title: "Stream" + help: "The name of the Zulip stream the message should be sent to. e.g. 'general'" + subject: + title: "Subject" + help: "The subject that these messages sent by the bot should be given" + errors: + does_not_exist: "That stream does not exist on Zulip" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 78b1416..14ff433 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -56,6 +56,14 @@ en: chat_integration_matrix_access_token: "Access token for the bot's matrix account" chat_integration_matrix_excerpt_length: "Matrix post excerpt length" + ####################################### + ########### ZULIP SETTINGS ############ + ####################################### + chat_integration_zulip_enabled: "Enable the Matrix chat integration provider" + chat_integration_zulip_server: "The base URL for your Zulip server. Make sure to include http(s)://" + chat_integration_zulip_bot_api_key: "The API key for your Zulip bot" + chat_integration_zulip_excerpt_length: "Zulip post excerpt length" + chat_integration: all_categories: "(all categories)" @@ -212,3 +220,13 @@ en: %{excerpt} + ####################################### + ############# ZULIP STRINGS ########### + ####################################### + zulip: + message: | + **%{user}** posted in **[%{title}](%{post_url})** + ~~~quote + %{excerpt} + ~~~ + diff --git a/config/settings.yml b/config/settings.yml index d93f411..0e87e8b 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -84,3 +84,15 @@ plugins: default: '' chat_integration_matrix_excerpt_length: default: 400 + +####################################### +########### ZULIP SETTINGS ############ +####################################### + chat_integration_zulip_enabled: + default: false + chat_integration_zulip_server: + default: '' + chat_integration_zulip_bot_api_key: + default: '' + chat_integration_zulip_excerpt_length: + default: 400 diff --git a/lib/discourse_chat/provider.rb b/lib/discourse_chat/provider.rb index e878cf2..3904810 100644 --- a/lib/discourse_chat/provider.rb +++ b/lib/discourse_chat/provider.rb @@ -95,3 +95,4 @@ 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" +require_relative "provider/zulip/zulip_provider.rb" diff --git a/lib/discourse_chat/provider/zulip/zulip_provider.rb b/lib/discourse_chat/provider/zulip/zulip_provider.rb new file mode 100644 index 0000000..d115bb8 --- /dev/null +++ b/lib/discourse_chat/provider/zulip/zulip_provider.rb @@ -0,0 +1,64 @@ +module DiscourseChat + module Provider + module ZulipProvider + PROVIDER_NAME = "zulip".freeze + PROVIDER_ENABLED_SETTING = :chat_integration_zulip_enabled + CHANNEL_PARAMETERS = [ + { key: "stream", unique: true, regex: '^\S+' }, + { key: "subject", unique: true, regex: '^\S+' }, + ] + + def self.send_message(message) + uri = URI("#{SiteSetting.chat_integration_zulip_server}/api/v1/external/discourse?api_key=#{SiteSetting.chat_integration_zulip_bot_api_key}") + + 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) + + return response + end + + def self.generate_zulip_message(post, stream, subject) + 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 = I18n.t('chat_integration.provider.zulip.message', user: display_name, + post_url: post.full_url, + title: post.topic.title, + excerpt: post.excerpt(SiteSetting.chat_integration_zulip_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true)) + + data = { + stream: stream, + subject: subject, + message: message + } + end + + def self.trigger_notification(post, channel) + + stream = channel.data['stream'] + subject = channel.data['subject'] + + message = self.generate_zulip_message(post, stream, subject) + + response = send_message(message) + + if not response.kind_of? Net::HTTPSuccess + error_key = nil + error_key = 'chat_integration.provider.zulip.errors.does_not_exist' if response.body.include?('does not exist') + raise ::DiscourseChat::ProviderError.new info: { error_key: error_key, message: message, response_code: response.code, response_body: response.body } + end + + end + + end + end +end diff --git a/spec/lib/discourse_chat/provider/zulip/zulip_provider_spec.rb b/spec/lib/discourse_chat/provider/zulip/zulip_provider_spec.rb new file mode 100644 index 0000000..88205d0 --- /dev/null +++ b/spec/lib/discourse_chat/provider/zulip/zulip_provider_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +RSpec.describe DiscourseChat::Provider::ZulipProvider do + let(:post) { Fabricate(:post) } + + describe '.trigger_notifications' do + before do + SiteSetting.chat_integration_zulip_enabled = true + SiteSetting.chat_integration_zulip_server = "https://hello.world" + SiteSetting.chat_integration_zulip_bot_api_key = "secret" + end + + let(:chan1) { DiscourseChat::Channel.create!(provider: 'zulip', data: { stream: "general", subject: "Discourse Notifications" }) } + + it 'sends a webhook request' do + stub1 = stub_request(:post, 'https://hello.world/api/v1/external/discourse?api_key=secret').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://hello.world/api/v1/external/discourse?api_key=secret').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