Move slack command processor into common helper so it can be used by other providers
This commit is contained in:
parent
2e94f23fbe
commit
694bf919ce
|
@ -1,6 +1,77 @@
|
||||||
module DiscourseChat
|
module DiscourseChat
|
||||||
module Helper
|
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
|
# Produce a string with a list of all rules associated with a channel
|
||||||
def self.status_for_channel(channel)
|
def self.status_for_channel(channel)
|
||||||
rules = channel.rules.order_by_precedence
|
rules = channel.rules.order_by_precedence
|
||||||
|
|
|
@ -17,7 +17,6 @@ module DiscourseChat::Provider::SlackProvider
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_command(params)
|
def process_command(params)
|
||||||
guardian = DiscourseChat::Manager.guardian
|
|
||||||
|
|
||||||
tokens = params[:text].split(" ")
|
tokens = params[:text].split(" ")
|
||||||
|
|
||||||
|
@ -39,70 +38,7 @@ module DiscourseChat::Provider::SlackProvider
|
||||||
# Create channel if doesn't exist
|
# Create channel if doesn't exist
|
||||||
channel ||= DiscourseChat::Channel.create!(provider:provider, data:{identifier: channel_id})
|
channel ||= DiscourseChat::Channel.create!(provider:provider, data:{identifier: channel_id})
|
||||||
|
|
||||||
cmd = tokens.shift if tokens.size >= 1
|
return ::DiscourseChat::Helper.process_command(channel, tokens)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue