109 lines
3.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module DiscourseChatIntegration
2017-07-25 22:14:48 +01:00
module Provider
module MattermostProvider
PROVIDER_NAME = "mattermost".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_mattermost_enabled
Feature: add chat integration reference post (#216) * FEATURE: Add chat integration reference post This class works similar to a post but it is not a post. * DEV: change how `excerpt` method works * feature: Add `send_chat_integration_message` scriptable add `send_chat_integration_message` scriptable that uses the rules to send a message to the chat provider add locale strings for the new scriptable update `ChatIntegrationReferencePost` `excerpt` method add tests for `ChatIntegrationReferencePost` * DEV: Add `get_channel_by_name` to every provider This makes using `trigger_notification` easier with every provider as well. * DEV: Add `get_channel_name` to all providers This method gets the name of the channel based on how the provider identifies it. Updates channel_name in locales yaml Adds migrate_tag_added_filter_to_all_providers.rb to move all existing rules to use Automation * DEV: Add removal of old migration data Update small action locales with strings from core * DEV: solve review comments * DEV: update test locale strings * DEV: remove empty line to trigger lint * DEV: lint applied * DEV: Add tests for automation integration * DEV: add rails logger for when automatio error occurs * DEV: move migration to be SQL only Update provider helper to use hashes instead of dot notation * DEV: update migration with correct table names * DEV: Update migrate_tag_added_filter_to_all_providers to use smaller SQL queries Commented out migrate_tag_added_from_filter_to_automation.rb * DEV: update comments in migration file * DEV: update indentation in client.en.yml * DEV: update with review comments * Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * Update spec/integration/automation_spec.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * Update lib/discourse_chat_integration/chat_integration_reference_post.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * DEV: update specs with review comments * DEV: update typos in tests * DEV: inlined functions for getting channel name for provider in migration --------- Co-authored-by: Jarek Radosz <jradosz@gmail.com>
2024-09-11 10:42:52 -03:00
CHANNEL_IDENTIFIER_KEY = "identifier".freeze
CHANNEL_PARAMETERS = [{ key: "identifier", regex: '^[@#]\S*$', unique: true }]
2017-07-25 22:14:48 +01:00
def self.send_via_webhook(message)
uri = URI(SiteSetting.chat_integration_mattermost_webhook_url)
http = FinalDestination::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == "https")
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
2017-07-25 22:14:48 +01:00
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"
2017-07-25 22:14:48 +01:00
else
error_key = nil
end
raise ::DiscourseChatIntegration::ProviderError.new info: {
error_key: error_key,
request: req.body,
response_code: response.code,
response_body: response.body,
}
2017-07-25 22:14:48 +01:00
end
end
def self.mattermost_message(post, channel)
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
2017-07-25 22:14:48 +01:00
topic = post.topic
category = ""
if topic.category&.uncategorized?
category = "[#{I18n.t("uncategorized_category_name")}]"
elsif topic.category
category =
(
if (topic.category.parent_category)
"[#{topic.category.parent_category.name}/#{topic.category.name}]"
else
"[#{topic.category.name}]"
end
)
2017-07-25 22:14:48 +01:00
end
icon_url =
if SiteSetting.chat_integration_mattermost_icon_url.present?
2017-07-25 22:14:48 +01:00
UrlHelper.absolute(SiteSetting.chat_integration_mattermost_icon_url)
elsif (
url = (SiteSetting.try(:site_logo_small_url) || SiteSetting.logo_small_url)
).present?
UrlHelper.absolute(url)
2017-07-25 22:14:48 +01:00
end
message = {
channel: channel,
username: SiteSetting.title || "Discourse",
icon_url: icon_url,
attachments: [],
2017-07-25 22:14:48 +01:00
}
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} #{topic.tags.present? ? topic.tags.map(&:name).join(", ") : ""}",
2017-07-25 22:14:48 +01:00
title_link: post.full_url,
}
message[:attachments].push(summary)
message
end
def self.trigger_notification(post, channel, rule)
channel_id = channel.data["identifier"]
2017-07-25 22:14:48 +01:00
message = mattermost_message(post, channel_id)
2017-08-01 20:53:39 +01:00
2017-07-25 22:14:48 +01:00
self.send_via_webhook(message)
end
Feature: add chat integration reference post (#216) * FEATURE: Add chat integration reference post This class works similar to a post but it is not a post. * DEV: change how `excerpt` method works * feature: Add `send_chat_integration_message` scriptable add `send_chat_integration_message` scriptable that uses the rules to send a message to the chat provider add locale strings for the new scriptable update `ChatIntegrationReferencePost` `excerpt` method add tests for `ChatIntegrationReferencePost` * DEV: Add `get_channel_by_name` to every provider This makes using `trigger_notification` easier with every provider as well. * DEV: Add `get_channel_name` to all providers This method gets the name of the channel based on how the provider identifies it. Updates channel_name in locales yaml Adds migrate_tag_added_filter_to_all_providers.rb to move all existing rules to use Automation * DEV: Add removal of old migration data Update small action locales with strings from core * DEV: solve review comments * DEV: update test locale strings * DEV: remove empty line to trigger lint * DEV: lint applied * DEV: Add tests for automation integration * DEV: add rails logger for when automatio error occurs * DEV: move migration to be SQL only Update provider helper to use hashes instead of dot notation * DEV: update migration with correct table names * DEV: Update migrate_tag_added_filter_to_all_providers to use smaller SQL queries Commented out migrate_tag_added_from_filter_to_automation.rb * DEV: update comments in migration file * DEV: update indentation in client.en.yml * DEV: update with review comments * Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * Update spec/lib/discourse_chat_integration/chat_integration_reference_post_spec.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * Update spec/integration/automation_spec.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * Update lib/discourse_chat_integration/chat_integration_reference_post.rb Co-authored-by: Jarek Radosz <jradosz@gmail.com> * DEV: update specs with review comments * DEV: update typos in tests * DEV: inlined functions for getting channel name for provider in migration --------- Co-authored-by: Jarek Radosz <jradosz@gmail.com>
2024-09-11 10:42:52 -03:00
def self.get_channel_by_name(name)
DiscourseChatIntegration::Channel
.with_provider(PROVIDER_NAME)
.with_data_value(CHANNEL_IDENTIFIER_KEY, name)
.first
end
2017-07-25 22:14:48 +01:00
end
end
2017-07-26 13:09:05 +01:00
end
2017-08-01 20:53:39 +01:00
require_relative "mattermost_command_controller.rb"