102 lines
3.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module DiscourseChatIntegration
2017-07-27 17:09:44 +01:00
module Provider
module MatrixProvider
PROVIDER_NAME = "matrix".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_matrix_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 = "name".freeze
2017-07-27 17:09:44 +01:00
CHANNEL_PARAMETERS = [
{ key: "name", regex: '^\S+' },
{ key: "room_id", regex: '^\!\S+:\S+$', unique: true, hidden: true },
]
2017-07-27 17:09:44 +01:00
def self.send_message(room_id, message)
homeserver = SiteSetting.chat_integration_matrix_homeserver
event_type = "m.room.message"
2017-07-27 17:09:44 +01:00
uid = Time.now.to_i
url_params =
URI.encode_www_form(access_token: SiteSetting.chat_integration_matrix_access_token)
2017-07-27 17:09:44 +01:00
url =
"#{homeserver}/_matrix/client/r0/rooms/#{CGI.escape(room_id)}/send/#{event_type}/#{uid}"
2017-07-27 17:09:44 +01:00
uri = URI([url, url_params].join("?"))
2017-07-27 17:09:44 +01:00
http = FinalDestination::HTTP.new(uri.host, uri.port)
2017-07-27 17:09:44 +01:00
http.use_ssl = true
req = Net::HTTP::Put.new(uri, "Content-Type" => "application/json")
2017-07-27 17:09:44 +01:00
req.body = message.to_json
response = http.request(req)
2019-11-14 15:03:49 -05:00
response
2017-07-27 17:09:44 +01:00
end
def self.generate_matrix_message(post)
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
2017-07-27 17:09:44 +01:00
message = {
msgtype: SiteSetting.chat_integration_matrix_use_notice ? "m.notice" : "m.text",
body:
I18n.t(
"chat_integration.provider.matrix.text_message",
user: display_name,
post_url: post.full_url,
title: post.topic.title,
),
format: "org.matrix.custom.html",
formatted_body:
I18n.t(
"chat_integration.provider.matrix.formatted_message",
user: display_name,
post_url: post.full_url,
title: post.topic.title,
excerpt:
post.excerpt(
SiteSetting.chat_integration_matrix_excerpt_length,
text_entities: true,
strip_links: true,
remap_emoji: true,
),
),
2017-07-27 17:09:44 +01:00
}
2019-11-14 15:03:49 -05:00
message
2017-07-27 17:09:44 +01:00
end
def self.trigger_notification(post, channel, rule)
2017-07-27 17:09:44 +01:00
message = generate_matrix_message(post)
response = send_message(channel.data["room_id"], message)
2017-07-27 17:09:44 +01:00
2017-10-03 17:42:07 +08:00
if !response.kind_of?(Net::HTTPSuccess)
2017-07-27 17:09:44 +01:00
error_key = nil
begin
responseData = JSON.parse(response.body)
if responseData["errcode"] == "M_UNKNOWN_TOKEN"
error_key = "chat_integration.provider.matrix.errors.unknown_token"
elsif responseData["errcode"] == "M_UNKNOWN"
error_key = "chat_integration.provider.matrix.errors.unknown_room"
2017-07-27 17:09:44 +01:00
end
ensure
raise ::DiscourseChatIntegration::ProviderError.new info: {
error_key: error_key,
message: message,
response_body: response.body,
}
2017-07-27 17:09:44 +01:00
end
end
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-27 17:09:44 +01:00
end
end
2017-08-01 20:53:39 +01:00
end