2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration
|
2017-08-30 19:11:06 -04:00
|
|
|
module Provider
|
|
|
|
module GitterProvider
|
2022-12-29 07:31:05 -05:00
|
|
|
PROVIDER_NAME = "gitter".freeze
|
2017-08-30 19:11:06 -04:00
|
|
|
PROVIDER_ENABLED_SETTING = :chat_integration_gitter_enabled
|
|
|
|
CHANNEL_PARAMETERS = [
|
|
|
|
{ key: "name", regex: '^\S+$', unique: true },
|
2022-12-29 07:31:05 -05:00
|
|
|
{
|
|
|
|
key: "webhook_url",
|
|
|
|
regex: '^https://webhooks\.gitter\.im/e/\S+$',
|
|
|
|
unique: true,
|
|
|
|
hidden: true,
|
|
|
|
},
|
2017-08-30 19:11:06 -04:00
|
|
|
]
|
|
|
|
|
2020-06-15 11:45:25 -04:00
|
|
|
def self.trigger_notification(post, channel, rule)
|
2017-08-30 19:11:06 -04:00
|
|
|
message = gitter_message(post)
|
2022-12-29 07:31:05 -05:00
|
|
|
response = Net::HTTP.post_form(URI(channel.data["webhook_url"]), message: message)
|
2017-08-30 19:11:06 -04:00
|
|
|
unless response.kind_of? Net::HTTPSuccess
|
|
|
|
error_key = nil
|
2022-12-29 07:31:05 -05:00
|
|
|
raise ::DiscourseChatIntegration::ProviderError.new info: {
|
|
|
|
error_key: error_key,
|
|
|
|
message: message,
|
|
|
|
response_body: response.body,
|
|
|
|
}
|
2017-08-30 19:11:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.gitter_message(post)
|
|
|
|
display_name = post.user.username
|
|
|
|
topic = post.topic
|
|
|
|
parent_category = topic.category.try :parent_category
|
2022-12-29 07:31:05 -05:00
|
|
|
category_name =
|
|
|
|
(
|
|
|
|
if parent_category
|
|
|
|
"[#{parent_category.name}/#{topic.category.name}]"
|
|
|
|
else
|
|
|
|
"[#{topic.category.name}]"
|
|
|
|
end
|
|
|
|
)
|
2017-08-30 19:11:06 -04:00
|
|
|
|
|
|
|
"[__#{display_name}__ - #{topic.title} - #{category_name}](#{post.full_url})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|