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:
parent
95344c348f
commit
e4ff1997c8
|
@ -20,4 +20,37 @@ en:
|
||||||
#######################################
|
#######################################
|
||||||
######### TELEGRAM SETTINGS ###########
|
######### TELEGRAM SETTINGS ###########
|
||||||
#######################################
|
#######################################
|
||||||
chat_integration_telegram_enabled: "Enable the telegram chat-integration provider"
|
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`
|
|
@ -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
|
||||||
|
|
|
@ -40,6 +40,7 @@ after_initialize do
|
||||||
require_relative "lib/discourse_chat/provider"
|
require_relative "lib/discourse_chat/provider"
|
||||||
require_relative "lib/discourse_chat/manager"
|
require_relative "lib/discourse_chat/manager"
|
||||||
require_relative "lib/discourse_chat/rule"
|
require_relative "lib/discourse_chat/rule"
|
||||||
|
require_relative "lib/discourse_chat/helper"
|
||||||
|
|
||||||
module ::Jobs
|
module ::Jobs
|
||||||
class NotifyChats < Jobs::Base
|
class NotifyChats < Jobs::Base
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue