diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 565d0b7..788e967 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -112,6 +112,19 @@ en: webhook_url: title: Webhook URL help: The webhook URL created in your Discord server settings + + ####################################### + ########### GUILDED STRINGS ########### + ####################################### + guilded: + title: "Guilded" + param: + name: + title: "Name" + help: "A name to describe the channel. It is not used for the connection to Guilded" + webhook_url: + title: Webhook URL + help: The webhook URL created in your Guilded server settings ####################################### ######### MATTERMOST STRINGS ########## diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index c16dd3d..ff7a98a 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -33,6 +33,12 @@ en: chat_integration_discord_enabled: "Enable the Discord chat-integration provider" chat_integration_discord_message_content: "The message to include above the summary when sending a notification to Discord" chat_integration_discord_excerpt_length: "Discord post excerpt length" + + ####################################### + ########## GUILDED SETTINGS ########### + ####################################### + chat_integration_guilded_enabled: "Enable the Guilded chat-integration provider" + chat_integration_guilded_excerpt_length: "Guilded post excerpt length" ####################################### ######## MATTERMOST SETTINGS ########## diff --git a/config/settings.yml b/config/settings.yml index 66502fa..f80cf30 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -53,6 +53,15 @@ chat_integration: default: "" chat_integration_discord_excerpt_length: default: 400 + +####################################### +########## GUILDED SETTINGS ########### +####################################### + chat_integration_guilded_enabled: + default: false + chat_integration_guilded_excerpt_length: + default: 400 + ####################################### ######## MATTERMOST SETTINGS ########## diff --git a/lib/discourse_chat_integration/provider.rb b/lib/discourse_chat_integration/provider.rb index 2272014..bb6859a 100644 --- a/lib/discourse_chat_integration/provider.rb +++ b/lib/discourse_chat_integration/provider.rb @@ -104,3 +104,4 @@ require_relative "provider/groupme/groupme_provider" require_relative "provider/teams/teams_provider" require_relative "provider/webex/webex_provider" require_relative "provider/google/google_provider" +require_relative "provider/guilded/guilded_provider" diff --git a/lib/discourse_chat_integration/provider/guilded/guilded_provider.rb b/lib/discourse_chat_integration/provider/guilded/guilded_provider.rb new file mode 100644 index 0000000..fb5aa14 --- /dev/null +++ b/lib/discourse_chat_integration/provider/guilded/guilded_provider.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +module DiscourseChatIntegration + module Provider + module GuildedProvider + PROVIDER_NAME = "guilded".freeze + PROVIDER_ENABLED_SETTING = :chat_integration_guilded_enabled + + CHANNEL_PARAMETERS = [ + { key: "name", regex: '^\S+' }, + { key: "webhook_url", regex: '^https:\/\/media\.guilded\.gg\/webhooks\/', unique: true, hidden: true } + ].freeze + + def self.trigger_notification(post, channel, rule) + webhook_url = channel.data['webhook_url'] + message = generate_guilded_message(post) + response = send_message(webhook_url, message) + + if !response.kind_of?(Net::HTTPSuccess) + raise ::DiscourseChatIntegration::ProviderError.new(info: { + error_key: nil, message: message, response_body: response.body + }) + end + end + + def self.generate_guilded_message(post) + topic = post.topic + category = '' + if topic.category + category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]" : "[#{topic.category.name}]" + end + display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user) + + icon_url = + if (url = (SiteSetting.try(:site_logo_small_url) || SiteSetting.logo_small_url)).present? + "#{Discourse.base_url}#{url}" + end + + message = { + embeds: [{ + title: "#{topic.title} #{(category == '[uncategorized]') ? '' : category} #{topic.tags.present? ? topic.tags.map(&:name).join(', ') : ''}", + url: post.full_url, + description: post.excerpt(SiteSetting.chat_integration_guilded_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true), + footer: { + icon_url: ensure_protocol(post.user.small_avatar_url), + text: "#{display_name} | #{post.created_at}" + } + }] + } + + message + end + + def self.send_message(url, message) + uri = URI(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) + + response + end + + def self.ensure_protocol(url) + return url if !url.start_with?('//') + "http:#{url}" + end + + end + end +end diff --git a/spec/lib/discourse_chat_integration/provider/guilded/guilded_provider_spec.rb b/spec/lib/discourse_chat_integration/provider/guilded/guilded_provider_spec.rb new file mode 100644 index 0000000..98ab3e2 --- /dev/null +++ b/spec/lib/discourse_chat_integration/provider/guilded/guilded_provider_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe DiscourseChatIntegration::Provider::GuildedProvider do + let(:post) { Fabricate(:post) } + + describe '.trigger_notifications' do + before do + SiteSetting.chat_integration_guilded_enabled = true + end + + let(:chan1) { DiscourseChatIntegration::Channel.create!(provider: 'guilded', data: { name: "Awesome Channel", webhook_url: 'https://media.guilded.gg/webhooks/1234/abcd' }) } + + it 'sends a webhook request' do + stub1 = stub_request(:post, 'https://media.guilded.gg/webhooks/1234/abcd').to_return(status: 200) + described_class.trigger_notification(post, chan1, nil) + expect(stub1).to have_been_requested.once + end + + it 'handles errors correctly' do + stub1 = stub_request(:post, "https://media.guilded.gg/webhooks/1234/abcd").to_return(status: 400) + expect(stub1).to have_been_requested.times(0) + expect { described_class.trigger_notification(post, chan1, nil) }.to raise_exception(::DiscourseChatIntegration::ProviderError) + expect(stub1).to have_been_requested.once + end + + end + +end