2017-07-05 10:03:02 -04:00
|
|
|
module DiscourseChat::Provider::SlackProvider
|
|
|
|
class SlackCommandController < DiscourseChat::Provider::HookController
|
|
|
|
requires_provider ::DiscourseChat::Provider::SlackProvider::PROVIDER_NAME
|
|
|
|
|
2017-07-05 18:01:46 -04:00
|
|
|
before_filter :slack_token_valid?, only: :command
|
2017-07-05 10:03:02 -04:00
|
|
|
|
2017-07-05 18:01:46 -04:00
|
|
|
skip_before_filter :check_xhr,
|
|
|
|
:preload_json,
|
|
|
|
:verify_authenticity_token,
|
|
|
|
:redirect_to_login_if_required,
|
|
|
|
only: :command
|
|
|
|
|
|
|
|
def command
|
|
|
|
text = process_command(params)
|
|
|
|
|
|
|
|
render json: { text: text }
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_command(params)
|
|
|
|
guardian = DiscourseChat::Manager.guardian
|
|
|
|
|
|
|
|
tokens = params[:text].split(" ")
|
|
|
|
|
|
|
|
# channel name fix
|
2017-07-13 18:21:15 -04:00
|
|
|
channel_id =
|
2017-07-05 18:01:46 -04:00
|
|
|
case params[:channel_name]
|
|
|
|
when 'directmessage'
|
|
|
|
"@#{params[:user_name]}"
|
|
|
|
when 'privategroup'
|
|
|
|
params[:channel_id]
|
|
|
|
else
|
|
|
|
"##{params[:channel_name]}"
|
|
|
|
end
|
|
|
|
|
2017-07-06 16:42:37 -04:00
|
|
|
provider = DiscourseChat::Provider::SlackProvider::PROVIDER_NAME
|
|
|
|
|
2017-07-13 18:21:15 -04:00
|
|
|
channel = DiscourseChat::Channel.with_provider(provider).with_data_value('identifier',channel_id).first
|
|
|
|
|
|
|
|
# Create channel if doesn't exist
|
|
|
|
channel ||= DiscourseChat::Channel.create!(provider:provider, data:{identifier: channel_id})
|
|
|
|
|
2017-07-05 18:01:46 -04:00
|
|
|
cmd = tokens.shift if tokens.size >= 1
|
|
|
|
|
2017-07-06 16:42:37 -04:00
|
|
|
error_text = I18n.t("chat_integration.provider.slack.parse_error")
|
2017-07-05 18:01:46 -04:00
|
|
|
|
|
|
|
case cmd
|
|
|
|
when "watch", "follow", "mute"
|
|
|
|
return error_text if tokens.empty?
|
|
|
|
# If the first token in the command is a tag, this rule applies to all categories
|
|
|
|
category_name = tokens[0].start_with?('tag:') ? nil : tokens.shift
|
|
|
|
|
|
|
|
if category_name
|
|
|
|
category = Category.find_by(slug: category_name)
|
|
|
|
unless category
|
|
|
|
cat_list = (CategoryList.new(guardian).categories.map(&:slug)).join(', ')
|
|
|
|
return I18n.t("chat_integration.provider.slack.not_found.category", name: category_name, list:cat_list)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
category = nil # All categories
|
|
|
|
end
|
|
|
|
|
|
|
|
tags = []
|
|
|
|
# Every remaining token must be a tag. If not, abort and send help text
|
|
|
|
while tokens.size > 0
|
|
|
|
token = tokens.shift
|
|
|
|
if token.start_with?('tag:')
|
|
|
|
tag_name = token.sub(/^tag:/, '')
|
|
|
|
else
|
|
|
|
return error_text # Abort and send help text
|
|
|
|
end
|
|
|
|
|
|
|
|
tag = Tag.find_by(name: tag_name)
|
|
|
|
unless tag # If tag doesn't exist, abort
|
|
|
|
return I18n.t("chat_integration.provider.slack.not_found.tag", name: tag_name)
|
|
|
|
end
|
2017-07-06 16:42:37 -04:00
|
|
|
tags.push(tag.name)
|
2017-07-05 18:01:46 -04:00
|
|
|
end
|
|
|
|
|
2017-07-06 16:42:37 -04:00
|
|
|
category_id = category.nil? ? nil : category.id
|
2017-07-13 18:21:15 -04:00
|
|
|
case DiscourseChat::Helper.smart_create_rule(channel:channel, filter:cmd, category_id: category_id, tags:tags)
|
2017-07-06 16:42:37 -04:00
|
|
|
when :created
|
|
|
|
return I18n.t("chat_integration.provider.slack.create.created")
|
|
|
|
when :updated
|
|
|
|
return I18n.t("chat_integration.provider.slack.create.updated")
|
|
|
|
else
|
|
|
|
return I18n.t("chat_integration.provider.slack.create.error")
|
|
|
|
end
|
2017-07-05 18:01:46 -04:00
|
|
|
when "remove"
|
2017-07-06 16:42:37 -04:00
|
|
|
return error_text unless tokens.size == 1
|
|
|
|
|
|
|
|
rule_number = tokens[0].to_i
|
|
|
|
return error_text unless rule_number.to_s == tokens[0] # Check we were given a number
|
|
|
|
|
2017-07-13 18:21:15 -04:00
|
|
|
if DiscourseChat::Helper.delete_by_index(channel, rule_number)
|
2017-07-06 16:42:37 -04:00
|
|
|
return I18n.t("chat_integration.provider.slack.delete.success")
|
|
|
|
else
|
|
|
|
return I18n.t("chat_integration.provider.slack.delete.error")
|
|
|
|
end
|
2017-07-05 18:01:46 -04:00
|
|
|
when "status"
|
2017-07-13 18:21:15 -04:00
|
|
|
return DiscourseChat::Helper.status_for_channel(channel)
|
2017-07-05 18:01:46 -04:00
|
|
|
when "help"
|
|
|
|
return I18n.t("chat_integration.provider.slack.help")
|
|
|
|
else
|
|
|
|
return error_text
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def slack_token_valid?
|
|
|
|
params.require(:token)
|
|
|
|
|
|
|
|
if SiteSetting.chat_integration_slack_incoming_webhook_token.blank? ||
|
|
|
|
SiteSetting.chat_integration_slack_incoming_webhook_token != params[:token]
|
|
|
|
|
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SlackEngine < ::Rails::Engine
|
|
|
|
engine_name DiscourseChat::PLUGIN_NAME+"-slack"
|
|
|
|
isolate_namespace DiscourseChat::Provider::SlackProvider
|
|
|
|
end
|
|
|
|
|
|
|
|
SlackEngine.routes.draw do
|
2017-07-05 18:01:46 -04:00
|
|
|
post "command" => "slack_command#command"
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|