discourse-chat-integration/lib/discourse_chat_integration/provider/gitter/gitter_provider.rb

49 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module DiscourseChatIntegration
2017-08-30 19:11:06 -04:00
module Provider
module GitterProvider
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 },
{
key: "webhook_url",
regex: '^https://webhooks\.gitter\.im/e/\S+$',
unique: true,
hidden: true,
},
2017-08-30 19:11:06 -04:00
]
def self.trigger_notification(post, channel, rule)
2017-08-30 19:11:06 -04:00
message = gitter_message(post)
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
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
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