diff --git a/assets/javascripts/admin/models/rule.js.es6 b/assets/javascripts/admin/models/rule.js.es6 index 39db740..c58fc04 100644 --- a/assets/javascripts/admin/models/rule.js.es6 +++ b/assets/javascripts/admin/models/rule.js.es6 @@ -4,9 +4,9 @@ import computed from "ember-addons/ember-computed-decorators"; export default RestModel.extend({ available_filters: [ - { id: 'watch', name: I18n.t('chat.filter.watch'), icon: 'exclamation-circle' }, - { id: 'follow', name: I18n.t('chat.filter.follow'), icon: 'circle'}, - { id: 'mute', name: I18n.t('chat.filter.mute'), icon: 'times-circle' } + { id: 'watch', name: I18n.t('chat_integration.filter.watch'), icon: 'exclamation-circle' }, + { id: 'follow', name: I18n.t('chat_integration.filter.follow'), icon: 'circle'}, + { id: 'mute', name: I18n.t('chat_integration.filter.mute'), icon: 'times-circle' } ], category_id: null, @@ -26,7 +26,7 @@ export default RestModel.extend({ @computed('filter') filterName(filter) { - return I18n.t(`slack.present.${filter}`); + return I18n.t(`chat_integration.filter.${filter}`); }, updateProperties() { diff --git a/assets/javascripts/admin/routes/admin-plugins-chat-index.js.es6 b/assets/javascripts/admin/routes/admin-plugins-chat-index.js.es6 index 8bd5796..8889117 100644 --- a/assets/javascripts/admin/routes/admin-plugins-chat-index.js.es6 +++ b/assets/javascripts/admin/routes/admin-plugins-chat-index.js.es6 @@ -1,7 +1,10 @@ import AdminPluginsChatProvider from 'discourse/plugins/discourse-chat-integration/admin/routes/admin-plugins-chat-provider' export default Discourse.Route.extend({ - afterModel(model, transition) { - this.transitionTo('adminPlugins.chat.provider', model.get('firstObject').name); + afterModel(model, transition) { + + if(model.totalRows > 0){ + this.transitionTo('adminPlugins.chat.provider', model.get('firstObject').name); + } } }); diff --git a/assets/javascripts/admin/routes/admin-plugins-chat.js.es6 b/assets/javascripts/admin/routes/admin-plugins-chat.js.es6 index 20475d5..d1913f9 100644 --- a/assets/javascripts/admin/routes/admin-plugins-chat.js.es6 +++ b/assets/javascripts/admin/routes/admin-plugins-chat.js.es6 @@ -3,5 +3,13 @@ import AdminPluginsChatProvider from 'discourse/plugins/discourse-chat-integrati export default Discourse.Route.extend({ model(params, transition) { return this.store.findAll('provider'); - } + }, + + actions: { + showSettings: function(){ + this.transitionTo('adminSiteSettingsCategory', 'plugins', { + queryParams: { filter: 'chat_integration'} + }); + } + } }); diff --git a/assets/javascripts/admin/templates/modal/admin-plugins-chat-edit-rule.hbs b/assets/javascripts/admin/templates/modal/admin-plugins-chat-edit-rule.hbs index 6358cdc..a3b960e 100644 --- a/assets/javascripts/admin/templates/modal/admin-plugins-chat-edit-rule.hbs +++ b/assets/javascripts/admin/templates/modal/admin-plugins-chat-edit-rule.hbs @@ -1,12 +1,12 @@ -{{#d-modal-body title="chat.edit_rule_modal_title"}} +{{#d-modal-body title="chat_integration.edit_rule_modal.title"}}
- Rule for provider {{model.provider}} + {{i18n 'chat_integration.edit_rule_modal.provider'}}: {{i18n (concat 'chat_integration.provider.' model.provider '.title')}}
- + {{category-chooser name="category" value=model.category_id @@ -18,22 +18,22 @@ {{#if siteSettings.tagging_enabled}}
- - {{tag-chooser name="tags" tags=model.tags placeholderKey="slack.choose.tags"}} + + {{tag-chooser name="tags" tags=model.tags placeholderKey="chat_integration.edit_rule_modal.placeholders.tags"}}
{{/if}}
- + {{text-field name="channel" value=model.channel - placeholderKey="slack.choose.channel" + placeholderKey="chat_integration.edit_rule_modal.placeholders.channel" class="channel"}}
- + {{combo-box name="filter" content=model.available_filters value=model.filter}}
@@ -42,6 +42,8 @@ {{/d-modal-body}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs b/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs index eb5a49b..d784c57 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs @@ -1,15 +1,14 @@ - - - + + - + {{#if siteSettings.tagging_enabled}} - + {{/if}} @@ -27,7 +26,7 @@ {{#if rule.category}} {{category-link rule.category allowUncategorized="true" link="false"}} {{else}} - {{i18n "slack.choose.all_categories"}} + {{i18n "chat_integration.all_categories"}} {{/if}} @@ -38,27 +37,16 @@ {{/each}}
{{i18n "chat.channel"}}{{i18n "chat.filter"}}{{i18n "chat_integration.rule_table.channel"}}{{i18n "chat_integration.rule_table.filter"}}{{i18n "chat.category"}}{{i18n "chat_integration.rule_table.category"}}{{i18n "chat.tags"}}{{i18n "chat_integration.rule_table.tags"}} - {{d-button action="edit" actionParam=rule icon="pencil" class="edit" title="chat.edit"}} - {{d-button action="delete" actionParam=rule icon="trash-o" class="delete btn-danger" title="chat.delete"}} + {{d-button action="edit" actionParam=rule icon="pencil" class="edit" title="chat_integration.rule_table.edit_rule"}} + {{d-button action="delete" actionParam=rule icon="trash-o" class="delete btn-danger" title="chat_integration.rule_table.delete_rule"}}
-
- {{d-button action="testNotification" - icon="rocket" - disabled=testingNotification - title="chat.test_notification" - label="chat.test_notification"}} - - {{#d-button - action="resetSettings" - icon="trash" - title="chat.reset_settings" - label="chat.reset_settings"}} - {{/d-button}} -
- {{d-button action="create" actionParam=model.provider icon="plus" title="chat.create_rule" label="chat.create_rule"}} -
+
+
+ {{d-button action="create" actionParam=model.provider icon="plus" title="chat_integration.create_rule" label="chat_integration.create_rule"}} +
+
\ No newline at end of file diff --git a/assets/javascripts/discourse/templates/admin/plugins-chat.hbs b/assets/javascripts/discourse/templates/admin/plugins-chat.hbs index 77f5a7f..8b1aec5 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-chat.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-chat.hbs @@ -1,14 +1,27 @@ -
+
+
- +
+ {{#d-button + action="showSettings" + icon="gear" + title="chat_integration.settings" + label="chat_integration.settings"}} + {{/d-button}} +
+ {{#if model.totalRows}} + {{else}} + {{i18n "chat_integration.no_providers"}} + {{/if}} + {{outlet}}
\ No newline at end of file diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index a6bef80..0ed3c38 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1,17 +1,35 @@ en: js: - chat: + chat_integration: menu_title: "Chat Integrations" - channel: "Channel" - filter: "Filter" - category: "Category" - tags: "Tags" - edit: "Edit" - delete: "Delete" - test_notification: "Test Notification" - reset_settings: "Reset Settings" - edit_rule_modal_title: "Edit Rule" - integration: + settings: "Settings" + no_providers: "You need to enable some providers in the plugin settings" + all_categories: "All Categories" + create_rule: "Create Rule" + rule_table: + channel: "Channel" + filter: "Filter" + category: "Category" + tags: "Tags" + edit: "Edit" + delete: "Delete" + filter: + watch: "Watch" + follow: "Follow" + mute: "Mute" + edit_rule_modal: + title: "Edit Rule" + save: "Save Rule" + cancel: "Cancel" + provider: "Provider" + category: "Category" + tags: "Tags" + channel: "Channel" + filter: "Filter" + placeholders: + tags: "Choose tags" + channel: "#channel, @user or Channel ID" + provider: telegram: title: "Telegram" slack: diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 33db6ae..8d6a2a1 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1,3 +1,20 @@ en: site_settings: - discourse_chat_enabled: 'Enable the discourse-chat plugin' \ No newline at end of file + chat_integration_enabled: 'Enable the discourse-chat-integration plugin' + chat_integration_discourse_username: 'Username of user to act as when fetching content.' + chat_integration_delay_seconds: 'Number of seconds to wait after post creation before sending chat notitifications' + + + ####################################### + ########## SLACK SETTINGS ############# + ####################################### + chat_integration_slack_enabled: 'Enable the slack chat-integration provider' + chat_integration_slack_outbound_webhook_url: 'URL for outbound slack requests' + chat_integration_slack_excerpt_length: 'Post excerpt length' + chat_integration_slack_icon_url: 'Icon to post to slack with (defaults to forum logo)' + chat_integration_slack_access_token: 'Token if you are using the Web API instead of webhooks' + + ####################################### + ######### TELEGRAM SETTINGS ########### + ####################################### + chat_integration_telegram_enabled: "Enable the telegram chat-integration provider" \ No newline at end of file diff --git a/config/settings.yml b/config/settings.yml index 3059834..25216f8 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -3,6 +3,7 @@ plugins: default: false chat_integration_discourse_username: default: system + type: username chat_integration_delay_seconds: default: 20 @@ -13,7 +14,7 @@ plugins: default: false chat_integration_slack_outbound_webhook_url: default: '' - chat_integration_slack_discourse_excerpt_length: + chat_integration_slack_excerpt_length: default: 400 chat_integration_slack_icon_url: default: '' diff --git a/lib/discourse_chat/manager.rb b/lib/discourse_chat/manager.rb index a1c1a85..b9952d9 100644 --- a/lib/discourse_chat/manager.rb +++ b/lib/discourse_chat/manager.rb @@ -63,10 +63,12 @@ module DiscourseChat matching_rules.each do |rule| Rails.logger.info("Sending notification to provider #{rule.provider}, channel #{rule.channel}") provider = ::DiscourseChat::Provider.get_by_name(rule.provider) - if provider + is_enabled = ::DiscourseChat::Provider.is_enabled(provider) + if provider and is_enabled provider.trigger_notification(post, rule.channel) + elsif provider + # Provider is disabled, don't do anything else - puts "Can't find provider" # TODO: Handle when the provider does not exist end end diff --git a/lib/discourse_chat/provider.rb b/lib/discourse_chat/provider.rb index 50ad77f..41119a4 100644 --- a/lib/discourse_chat/provider.rb +++ b/lib/discourse_chat/provider.rb @@ -6,10 +6,24 @@ module DiscourseChat end.map(&method(:const_get)) end + def self.enabled_providers + self.providers.select do |provider| + self.is_enabled(provider) + end + end + def self.get_by_name(name) self.providers.find{|p| p::PROVIDER_NAME == name} end + def self.is_enabled(provider) + if defined? provider::PROVIDER_ENABLED_SETTING + SiteSetting.send(provider::PROVIDER_ENABLED_SETTING) + else + false + end + end + end end diff --git a/lib/discourse_chat/provider/slack/slack_provider.rb b/lib/discourse_chat/provider/slack/slack_provider.rb index 329640b..ca399d8 100644 --- a/lib/discourse_chat/provider/slack/slack_provider.rb +++ b/lib/discourse_chat/provider/slack/slack_provider.rb @@ -3,7 +3,9 @@ require_relative "slack_message_formatter.rb" module DiscourseChat::Provider::SlackProvider PROVIDER_NAME = "slack".freeze - def self.excerpt(post, max_length = SiteSetting.chat_integration_slack_discourse_excerpt_length) + PROVIDER_ENABLED_SETTING = :chat_integration_slack_enabled + + def self.excerpt(post, max_length = SiteSetting.chat_integration_slack_excerpt_length) doc = Nokogiri::HTML.fragment(post.excerpt(max_length, remap_emoji: true, keep_onebox_source: true diff --git a/lib/discourse_chat/provider/telegram/telegram_provider.rb b/lib/discourse_chat/provider/telegram/telegram_provider.rb index 61db74b..5015260 100644 --- a/lib/discourse_chat/provider/telegram/telegram_provider.rb +++ b/lib/discourse_chat/provider/telegram/telegram_provider.rb @@ -1,7 +1,10 @@ module DiscourseChat module Provider module TelegramProvider + include Provider + PROVIDER_NAME = "telegram".freeze + PROVIDER_ENABLED_SETTING = :chat_integration_telegram_enabled end end end \ No newline at end of file diff --git a/plugin.rb b/plugin.rb index 6d6a504..1c713df 100644 --- a/plugin.rb +++ b/plugin.rb @@ -65,18 +65,16 @@ after_initialize do end def list_providers - providers = ::DiscourseChat::Provider.providers.map {|x| {name: x::PROVIDER_NAME, id: x::PROVIDER_NAME}} + providers = ::DiscourseChat::Provider.enabled_providers.map {|x| {name: x::PROVIDER_NAME, id: x::PROVIDER_NAME}} render json:providers, root: 'providers' end def list_rules - providers = ::DiscourseChat::Provider.providers.map {|x| x::PROVIDER_NAME} + providers = ::DiscourseChat::Provider.enabled_providers.map {|x| x::PROVIDER_NAME} requested_provider = params[:provider] - if requested_provider.nil? - rules = DiscourseChat::Rule.all - elsif providers.include? requested_provider + if providers.include? requested_provider rules = DiscourseChat::Rule.all_for_provider(requested_provider) else raise Discourse::NotFound @@ -119,7 +117,7 @@ after_initialize do require_dependency 'admin_constraint' - add_admin_route 'chat.menu_title', 'chat' + add_admin_route 'chat_integration.menu_title', 'chat' DiscourseChat::Engine.routes.draw do get "" => "chat#respond" diff --git a/spec/lib/discourse_chat/manager_spec.rb b/spec/lib/discourse_chat/manager_spec.rb index 9ad11d6..9eee83d 100644 --- a/spec/lib/discourse_chat/manager_spec.rb +++ b/spec/lib/discourse_chat/manager_spec.rb @@ -10,9 +10,14 @@ RSpec.describe DiscourseChat::Manager do let(:second_post) {Fabricate(:post, topic: topic, post_number:2)} describe '.trigger_notifications' do + before do + SiteSetting.chat_integration_enabled = true + end + before(:each) do module ::DiscourseChat::Provider::DummyProvider PROVIDER_NAME = "dummy".freeze + PROVIDER_ENABLED_SETTING = :chat_integration_enabled # Tie to main plugin enabled setting @@sent_messages = [] def self.trigger_notification(post, channel) @@ -35,6 +40,15 @@ RSpec.describe DiscourseChat::Manager do DiscourseChat::Rule.new({provider: provider, channel: channel, filter:filter, category_id:category_id, tags:tags}).save end + it "should only send notifications when provider is enabled" do + SiteSetting.chat_integration_enabled = false + create_rule('dummy', 'chan1', 'watch', category.id, nil) + + manager.trigger_notifications(first_post.id) + + expect(provider.sent_messages.map{|x| x[:channel]}).to contain_exactly() + end + it "should send a notification to watched and following channels for new topic" do create_rule('dummy', 'chan1', 'watch', category.id, nil)