Add a helper class for common provider functionality

Initially implemented a ‘status_for_channel’ function, based on the existing slack plugin
This commit is contained in:
David Taylor 2017-07-05 23:00:58 +01:00
parent 95344c348f
commit e4ff1997c8
4 changed files with 137 additions and 1 deletions

View File

@ -21,3 +21,36 @@ en:
######### TELEGRAM SETTINGS ###########
#######################################
chat_integration_telegram_enabled: "Enable the telegram chat-integration provider"
chat_integration:
all_categories: "(all categories)"
deleted_category: "(deleted category)"
provider:
slack:
status:
header: |
*Rules for this channel*
(if multiple rules match a post, the topmost rule is executed)
no_rules: "There are no rules set up for this channel. Run `/discourse help` for instructions."
rule_string: "*%{index})* *%{filter}* posts in *%{category}*"
rule_string_tags_suffix: " with tags: *%{tags}*"
error: "Sorry, I didn't understand that. Run `/discourse help` for instructions."
not_found:
tag: "The *%{name}* tag cannot be found."
category: "The *%{name}* category cannot be found. Available categories: *%{list}*"
help: |
*New rule:* `/discourse [watch|follow|mute] [category] [tag:name]`
(you must specify a rule type and at least one category or tag)
- *watch* notify this channel for new topics and new replies
- *follow* notify this channel for new topics
- *mute* block notifications to this channel
*Remove rule:* `/discourse remove [rule number]`
(`[rule number]` can be found by running `/discourse status`)
*List rules:* `/discourse status`
*Help:* `/discourse help`

View File

@ -0,0 +1,45 @@
module DiscourseChat
module Helper
def self.status_for_channel(provider, channel)
rules = DiscourseChat::Rule.all_for_channel(provider, channel)
text = I18n.t("chat_integration.provider.#{provider}.status.header") + "\n"
i = 1
rules.each do |rule|
category_id = rule.category_id
if category_id.nil?
category_name = I18n.t("chat_integration.all_categories")
else
category = Category.find_by(id: category_id)
if category
category_name = category.slug
else
category_name = I18n.t("chat_integration.deleted_category")
end
end
text << I18n.t("chat_integration.provider.#{provider}.status.rule_string",
index: i,
filter: rule.filter,
category: category_name
)
if SiteSetting.tagging_enabled and not rule.tags.nil?
text << I18n.t("chat_integration.provider.#{provider}.status.rule_string_tags_suffix", tags: rule.tags.join(', '))
end
text << "\n"
i += 1
end
if rules.size == 0
text << I18n.t("chat_integration.provider.#{provider}.status.no_rules")
end
return text
end
end
end

View File

@ -40,6 +40,7 @@ after_initialize do
require_relative "lib/discourse_chat/provider"
require_relative "lib/discourse_chat/manager"
require_relative "lib/discourse_chat/rule"
require_relative "lib/discourse_chat/helper"
module ::Jobs
class NotifyChats < Jobs::Base

View File

@ -0,0 +1,57 @@
require 'rails_helper'
RSpec.describe DiscourseChat::Manager do
let(:category) {Fabricate(:category)}
let(:tag1){Fabricate(:tag)}
describe '.status_for_channel' do
context 'with no rules' do
it 'includes the heading' do
string = DiscourseChat::Helper.status_for_channel('slack','#general')
expect(string).to include('Rules for this channel')
end
it 'includes the no_rules string' do
string = DiscourseChat::Helper.status_for_channel('slack','#general')
expect(string).to include('no rules')
end
end
context 'with some rules' do
before do
DiscourseChat::Rule.new({provider: 'slack', channel: '#general', filter:'watch', category_id:category.id, tags:nil}).save!
DiscourseChat::Rule.new({provider: 'slack', channel: '#general', filter:'mute', category_id:nil, tags:nil}).save!
DiscourseChat::Rule.new({provider: 'slack', channel: '#general', filter:'follow', category_id:nil, tags:[tag1.name]}).save!
DiscourseChat::Rule.new({provider: 'slack', channel: '#otherchannel', filter:'watch', category_id:1, tags:nil}).save!
end
it 'displays the correct rules' do
string = DiscourseChat::Helper.status_for_channel('slack','#general')
expect(string.scan('watch').size).to eq(1)
expect(string.scan('mute').size).to eq(1)
expect(string.scan('follow').size).to eq(1)
end
it 'enumerates the rules correctly' do
string = DiscourseChat::Helper.status_for_channel('slack','#general')
expect(string.scan('1)').size).to eq(1)
expect(string.scan('2)').size).to eq(1)
expect(string.scan('3)').size).to eq(1)
end
it 'only displays tags for rules with tags' do
string = DiscourseChat::Helper.status_for_channel('slack','#general')
expect(string.scan('with tags').size).to eq(0)
SiteSetting.tagging_enabled = true
string = DiscourseChat::Helper.status_for_channel('slack','#general')
expect(string.scan('with tags').size).to eq(1)
end
end
end
end