discourse-chat-integration/lib/discourse_chat/provider/mattermost/mattermost_command_controll...

69 lines
1.9 KiB
Ruby
Raw Normal View History

2017-07-26 08:09:05 -04:00
module DiscourseChat::Provider::MattermostProvider
class MattermostCommandController < DiscourseChat::Provider::HookController
requires_provider ::DiscourseChat::Provider::MattermostProvider::PROVIDER_NAME
before_action :mattermost_token_valid?, only: :command
2017-07-26 08:09:05 -04:00
skip_before_action :check_xhr,
2017-07-26 08:09:05 -04:00
:preload_json,
:verify_authenticity_token,
:redirect_to_login_if_required,
only: :command
def command
text = process_command(params)
2017-08-01 15:53:39 -04:00
render json: {
2017-07-26 08:09:05 -04:00
response_type: 'ephemeral',
2017-08-01 15:53:39 -04:00
text: text
2017-07-26 08:09:05 -04:00
}
end
def process_command(params)
tokens = params[:text].split(" ")
# channel name fix
channel_id =
case params[:channel_name]
when 'directmessage'
"@#{params[:user_name]}"
when 'privategroup'
params[:channel_id]
else
"##{params[:channel_name]}"
end
provider = DiscourseChat::Provider::MattermostProvider::PROVIDER_NAME
2017-08-01 15:53:39 -04:00
channel = DiscourseChat::Channel.with_provider(provider).with_data_value('identifier', channel_id).first
2017-07-26 08:09:05 -04:00
# Create channel if doesn't exist
2017-08-01 15:53:39 -04:00
channel ||= DiscourseChat::Channel.create!(provider: provider, data: { identifier: channel_id })
2017-07-26 08:09:05 -04:00
return ::DiscourseChat::Helper.process_command(channel, tokens)
2017-08-01 15:53:39 -04:00
2017-07-26 08:09:05 -04:00
end
def mattermost_token_valid?
params.require(:token)
if SiteSetting.chat_integration_mattermost_incoming_webhook_token.blank? ||
SiteSetting.chat_integration_mattermost_incoming_webhook_token != params[:token]
raise Discourse::InvalidAccess.new
end
end
end
class MattermostEngine < ::Rails::Engine
2017-08-01 15:53:39 -04:00
engine_name DiscourseChat::PLUGIN_NAME + "-mattermost"
2017-07-26 08:09:05 -04:00
isolate_namespace DiscourseChat::Provider::MattermostProvider
end
MattermostEngine.routes.draw do
post "command" => "mattermost_command#command"
end
end