2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration
|
2017-07-25 17:14:48 -04:00
|
|
|
module Provider
|
|
|
|
module MattermostProvider
|
|
|
|
PROVIDER_NAME = "mattermost".freeze
|
|
|
|
PROVIDER_ENABLED_SETTING = :chat_integration_mattermost_enabled
|
|
|
|
CHANNEL_PARAMETERS = [
|
2017-08-01 15:53:39 -04:00
|
|
|
{ key: "identifier", regex: '^[@#]\S*$', unique: true }
|
2017-07-25 17:14:48 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
def self.send_via_webhook(message)
|
|
|
|
|
|
|
|
uri = URI(SiteSetting.chat_integration_mattermost_webhook_url)
|
|
|
|
|
2022-11-01 13:36:56 -04:00
|
|
|
http = FinalDestination::HTTP.new(uri.host, uri.port)
|
2017-07-25 17:14:48 -04:00
|
|
|
http.use_ssl = (uri.scheme == 'https')
|
2017-08-01 15:53:39 -04:00
|
|
|
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
|
2017-07-25 17:14:48 -04: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'
|
|
|
|
else
|
|
|
|
error_key = nil
|
|
|
|
end
|
2021-07-13 15:36:16 -04:00
|
|
|
raise ::DiscourseChatIntegration::ProviderError.new info: { error_key: error_key, request: req.body, response_code: response.code, response_body: response.body }
|
2017-07-25 17:14:48 -04:00
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2017-07-25 17:14:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.mattermost_message(post, channel)
|
2022-05-30 12:13:55 -04:00
|
|
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
2017-07-25 17:14:48 -04:00
|
|
|
|
|
|
|
topic = post.topic
|
|
|
|
|
|
|
|
category = ''
|
2019-06-04 13:47:32 -04:00
|
|
|
if topic.category&.uncategorized?
|
|
|
|
category = "[#{I18n.t('uncategorized_category_name')}]"
|
|
|
|
elsif topic.category
|
2017-08-01 15:53:39 -04:00
|
|
|
category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]" : "[#{topic.category.name}]"
|
2017-07-25 17:14:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
icon_url =
|
2018-11-14 02:17:44 -05:00
|
|
|
if SiteSetting.chat_integration_mattermost_icon_url.present?
|
2017-07-25 17:14:48 -04:00
|
|
|
UrlHelper.absolute(SiteSetting.chat_integration_mattermost_icon_url)
|
2018-11-14 02:17:44 -05:00
|
|
|
elsif (url = (SiteSetting.try(:site_logo_small_url) || SiteSetting.logo_small_url)).present?
|
|
|
|
UrlHelper.absolute(url)
|
2017-07-25 17:14:48 -04:00
|
|
|
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),
|
2019-06-04 13:47:32 -04:00
|
|
|
title: "#{topic.title} #{category} #{topic.tags.present? ? topic.tags.map(&:name).join(', ') : ''}",
|
2017-07-25 17:14:48 -04:00
|
|
|
title_link: post.full_url,
|
|
|
|
}
|
|
|
|
|
|
|
|
message[:attachments].push(summary)
|
|
|
|
message
|
|
|
|
end
|
|
|
|
|
2020-06-15 11:45:25 -04:00
|
|
|
def self.trigger_notification(post, channel, rule)
|
2017-07-25 17:14:48 -04:00
|
|
|
channel_id = channel.data['identifier']
|
|
|
|
message = mattermost_message(post, channel_id)
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2017-07-25 17:14:48 -04:00
|
|
|
self.send_via_webhook(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2017-07-26 08:09:05 -04:00
|
|
|
end
|
|
|
|
|
2017-08-01 15:53:39 -04:00
|
|
|
require_relative "mattermost_command_controller.rb"
|