Move slack command processor into common helper so it can be used by other providers

This commit is contained in:
David Taylor 2017-07-19 16:57:33 +01:00
parent 2e94f23fbe
commit 694bf919ce
2 changed files with 72 additions and 65 deletions

View File

@ -1,6 +1,77 @@
module DiscourseChat
module Helper
def self.process_command(channel, tokens)
guardian = DiscourseChat::Manager.guardian
provider = channel.provider
cmd = tokens.shift if tokens.size >= 1
error_text = I18n.t("chat_integration.provider.#{provider}.parse_error")
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.#{provider}.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.#{provider}.not_found.tag", name: tag_name)
end
tags.push(tag.name)
end
category_id = category.nil? ? nil : category.id
case DiscourseChat::Helper.smart_create_rule(channel:channel, filter:cmd, category_id: category_id, tags:tags)
when :created
return I18n.t("chat_integration.provider.#{provider}.create.created")
when :updated
return I18n.t("chat_integration.provider.#{provider}.create.updated")
else
return I18n.t("chat_integration.provider.#{provider}.create.error")
end
when "remove"
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
if DiscourseChat::Helper.delete_by_index(channel, rule_number)
return I18n.t("chat_integration.provider.#{provider}.delete.success")
else
return I18n.t("chat_integration.provider.#{provider}.delete.error")
end
when "status"
return DiscourseChat::Helper.status_for_channel(channel)
when "help"
return I18n.t("chat_integration.provider.#{provider}.help")
else
return error_text
end
end
# Produce a string with a list of all rules associated with a channel
def self.status_for_channel(channel)
rules = channel.rules.order_by_precedence

View File

@ -17,7 +17,6 @@ module DiscourseChat::Provider::SlackProvider
end
def process_command(params)
guardian = DiscourseChat::Manager.guardian
tokens = params[:text].split(" ")
@ -39,70 +38,7 @@ module DiscourseChat::Provider::SlackProvider
# Create channel if doesn't exist
channel ||= DiscourseChat::Channel.create!(provider:provider, data:{identifier: channel_id})
cmd = tokens.shift if tokens.size >= 1
error_text = I18n.t("chat_integration.provider.slack.parse_error")
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
tags.push(tag.name)
end
category_id = category.nil? ? nil : category.id
case DiscourseChat::Helper.smart_create_rule(channel:channel, filter:cmd, category_id: category_id, tags:tags)
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
when "remove"
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
if DiscourseChat::Helper.delete_by_index(channel, rule_number)
return I18n.t("chat_integration.provider.slack.delete.success")
else
return I18n.t("chat_integration.provider.slack.delete.error")
end
when "status"
return DiscourseChat::Helper.status_for_channel(channel)
when "help"
return I18n.t("chat_integration.provider.slack.help")
else
return error_text
end
return ::DiscourseChat::Helper.process_command(channel, tokens)
end