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

33 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-08-30 19:11:06 -04:00
module DiscourseChat
module Provider
module GitterProvider
PROVIDER_NAME = 'gitter'.freeze
PROVIDER_ENABLED_SETTING = :chat_integration_gitter_enabled
CHANNEL_PARAMETERS = [
{ key: "name", regex: '^\S+$', unique: true },
2017-08-30 20:51:28 -04:00
{ 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)
message = gitter_message(post)
response = Net::HTTP.post_form(URI(channel.data['webhook_url']), message: message)
unless response.kind_of? Net::HTTPSuccess
error_key = nil
raise ::DiscourseChat::ProviderError.new info: { error_key: error_key, message: message, response_body: response.body }
end
end
def self.gitter_message(post)
display_name = post.user.username
topic = post.topic
parent_category = topic.category.try :parent_category
category_name = parent_category ? "[#{parent_category.name}/#{topic.category.name}]" : "[#{topic.category.name}]"
"[__#{display_name}__ - #{topic.title} - #{category_name}](#{post.full_url})"
end
end
end
end