From d8e5494d4ca6b55efa33cdbe918e2eb48ef4555b Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 28 Jun 2017 15:12:37 +0100 Subject: [PATCH] Add (currently read-only) UI to the admin panel, displaying all existing rules. Dynamic routes created for each registered provider. --- .../javascripts/admin/chat-route-map.js.es6 | 10 +++ .../admin-plugins-chat-provider.js.es6 | 14 +++++ .../admin/models/filter-rule.js.es6 | 24 +++++++ .../routes/admin-plugins-chat-index.js.es6 | 7 +++ .../routes/admin-plugins-chat-provider.js.es6 | 24 +++++++ .../discourse/chat-route-map.js.es6 | 7 --- .../controllers/admin-plugins-chat.js.es6 | 3 - .../routes/admin-plugins-chat.js.es6 | 9 --- .../templates/admin/plugins-chat-provider.hbs | 63 +++++++++++++++++++ .../templates/admin/plugins-chat.hbs | 12 ---- config/locales/client.en.yml | 9 ++- lib/manager.rb | 30 +++++++++ plugin.rb | 29 ++++++--- 13 files changed, 201 insertions(+), 40 deletions(-) create mode 100644 assets/javascripts/admin/chat-route-map.js.es6 create mode 100644 assets/javascripts/admin/controllers/admin-plugins-chat-provider.js.es6 create mode 100644 assets/javascripts/admin/models/filter-rule.js.es6 create mode 100644 assets/javascripts/admin/routes/admin-plugins-chat-index.js.es6 create mode 100644 assets/javascripts/admin/routes/admin-plugins-chat-provider.js.es6 delete mode 100644 assets/javascripts/discourse/chat-route-map.js.es6 delete mode 100644 assets/javascripts/discourse/controllers/admin-plugins-chat.js.es6 delete mode 100644 assets/javascripts/discourse/routes/admin-plugins-chat.js.es6 create mode 100644 assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs delete mode 100644 assets/javascripts/discourse/templates/admin/plugins-chat.hbs diff --git a/assets/javascripts/admin/chat-route-map.js.es6 b/assets/javascripts/admin/chat-route-map.js.es6 new file mode 100644 index 0000000..4a3bd7d --- /dev/null +++ b/assets/javascripts/admin/chat-route-map.js.es6 @@ -0,0 +1,10 @@ +export default { + resource: 'admin.adminPlugins', + path: '/plugins', + map() { + this.route('chat', function(){ + this.route('index', { 'path': '/' }); + this.route('provider', {path: '/:provider'}); + }); + } +}; \ No newline at end of file diff --git a/assets/javascripts/admin/controllers/admin-plugins-chat-provider.js.es6 b/assets/javascripts/admin/controllers/admin-plugins-chat-provider.js.es6 new file mode 100644 index 0000000..9c88b03 --- /dev/null +++ b/assets/javascripts/admin/controllers/admin-plugins-chat-provider.js.es6 @@ -0,0 +1,14 @@ +import FilterRule from 'discourse/plugins/discourse-chat/admin/models/filter-rule' +import { ajax } from 'discourse/lib/ajax'; +import computed from "ember-addons/ember-computed-decorators"; + +export default Ember.Controller.extend({ + 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' } + ], + + editing: FilterRule.create({}), + +}); \ No newline at end of file diff --git a/assets/javascripts/admin/models/filter-rule.js.es6 b/assets/javascripts/admin/models/filter-rule.js.es6 new file mode 100644 index 0000000..c2bd045 --- /dev/null +++ b/assets/javascripts/admin/models/filter-rule.js.es6 @@ -0,0 +1,24 @@ +import RestModel from 'discourse/models/rest'; +import Category from 'discourse/models/category'; +import computed from "ember-addons/ember-computed-decorators"; + +export default RestModel.extend({ + category_id: null, + provider: '', + channel: '', + filter: null, + + @computed('category_id') + categoryName(categoryId) { + if (categoryId) + return Category.findById(categoryId).get('name'); + else { + return I18n.t('slack.choose.all_categories'); + } + }, + + @computed('filter') + filterName(filter) { + return I18n.t(`slack.present.${filter}`); + } +}); diff --git a/assets/javascripts/admin/routes/admin-plugins-chat-index.js.es6 b/assets/javascripts/admin/routes/admin-plugins-chat-index.js.es6 new file mode 100644 index 0000000..1e97fb0 --- /dev/null +++ b/assets/javascripts/admin/routes/admin-plugins-chat-index.js.es6 @@ -0,0 +1,7 @@ +import AdminPluginsChatProvider from 'discourse/plugins/discourse-chat/admin/routes/admin-plugins-chat-provider' + +export default AdminPluginsChatProvider.extend({ + afterModel(model, transition) { + this.transitionTo('adminPlugins.chat.provider', model); + } +}); diff --git a/assets/javascripts/admin/routes/admin-plugins-chat-provider.js.es6 b/assets/javascripts/admin/routes/admin-plugins-chat-provider.js.es6 new file mode 100644 index 0000000..914b382 --- /dev/null +++ b/assets/javascripts/admin/routes/admin-plugins-chat-provider.js.es6 @@ -0,0 +1,24 @@ +import FilterRule from 'discourse/plugins/discourse-chat/admin/models/filter-rule' +import { ajax } from 'discourse/lib/ajax'; + +export default Discourse.Route.extend({ + + model(params, transition) { + + var url = '/admin/plugins/chat' + if(params.provider !== undefined){ + url += `/${params.provider}` + } + url += '.json' + + return ajax(url).then(result => { + var rules = result.rules.map(v => FilterRule.create(v)) + var providers = result.providers + return {provider: result.provider, rules: rules, providers: providers}; + }); + }, + + serialize: function(model, params) { + return { provider: model['provider']}; + } +}); diff --git a/assets/javascripts/discourse/chat-route-map.js.es6 b/assets/javascripts/discourse/chat-route-map.js.es6 deleted file mode 100644 index 9458f36..0000000 --- a/assets/javascripts/discourse/chat-route-map.js.es6 +++ /dev/null @@ -1,7 +0,0 @@ -export default { - resource: 'admin.adminPlugins', - path: '/plugins', - map() { - this.route('chat'); - } -}; \ No newline at end of file diff --git a/assets/javascripts/discourse/controllers/admin-plugins-chat.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-chat.js.es6 deleted file mode 100644 index 4ee344f..0000000 --- a/assets/javascripts/discourse/controllers/admin-plugins-chat.js.es6 +++ /dev/null @@ -1,3 +0,0 @@ -export default Ember.Controller.extend({ - -}); \ No newline at end of file diff --git a/assets/javascripts/discourse/routes/admin-plugins-chat.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-chat.js.es6 deleted file mode 100644 index 2d3861d..0000000 --- a/assets/javascripts/discourse/routes/admin-plugins-chat.js.es6 +++ /dev/null @@ -1,9 +0,0 @@ -import { ajax } from 'discourse/lib/ajax'; - -export default Discourse.Route.extend({ - model() { - return ajax("/chat/list-providers.json").then(result => { - return result.chat; - }); - } -}); diff --git a/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs b/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs new file mode 100644 index 0000000..f031eb4 --- /dev/null +++ b/assets/javascripts/discourse/templates/admin/plugins-chat-provider.hbs @@ -0,0 +1,63 @@ + +
+
+
+ +
+
+ + + + + + + + + {{#if siteSettings.tagging_enabled}} + + {{/if}} + + + + + + + {{#each model.rules as |rule|}} + + + + + + + + {{#if siteSettings.tagging_enabled}} + + {{/if}} + + + + + {{/each}} + +
{{i18n "chat.channel"}}{{i18n "chat.filter"}}{{i18n "chat.category"}}{{i18n "chat.tags"}}
{{rule.channel}}{{rule.filterName}}{{rule.categoryName}}{{rule.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="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}} +
diff --git a/assets/javascripts/discourse/templates/admin/plugins-chat.hbs b/assets/javascripts/discourse/templates/admin/plugins-chat.hbs deleted file mode 100644 index 8669772..0000000 --- a/assets/javascripts/discourse/templates/admin/plugins-chat.hbs +++ /dev/null @@ -1,12 +0,0 @@ -
-
-
- - -
-
-
\ No newline at end of file diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 6cafab4..9cb896f 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2,7 +2,14 @@ en: js: chat: menu_title: "Chat Integrations" - + channel: "Channel" + filter: "Filter" + category: "Category" + tags: "Tags" + edit: "Edit" + delete: "Delete" + test_notification: "Test Notification" + reset_settings: "Reset Settings" integration: telegram: title: "Telegram" diff --git a/lib/manager.rb b/lib/manager.rb index 9f08222..b48fb3b 100644 --- a/lib/manager.rb +++ b/lib/manager.rb @@ -10,6 +10,36 @@ module DiscourseChat "#{KEY_PREFIX}#{cat_id.present? ? cat_id : '*'}" end + def self.get_all_rules + rules = [] + PluginStoreRow.where(plugin_name: DiscourseChat::PLUGIN_NAME) + .where("key ~* :pattern", pattern: "^#{DiscourseChat::Manager::KEY_PREFIX}.*") + .each do |row| + PluginStore.cast_value(row.type_name, row.value).each do |rule| + category_id = + if row.key == DiscourseChat::Manager.get_store_key + nil + else + row.key.gsub!(DiscourseChat::Manager::KEY_PREFIX, '') + row.key + end + + rules << { + provider: rule[:provider], + channel: rule[:channel], + filter: rule[:filter], + category_id: category_id, + tags: rule[:tags] + } + end + end + return rules + end + + def self.get_rules_for_provider(provider) + get_all_rules.select { |rule| rule[:provider] == provider } + end + def self.get_rules_for_category(cat_id = nil) PluginStore.get(DiscourseChat::PLUGIN_NAME, get_store_key(cat_id)) || [] end diff --git a/plugin.rb b/plugin.rb index 48ed11a..f383fda 100644 --- a/plugin.rb +++ b/plugin.rb @@ -28,8 +28,24 @@ after_initialize do class ::DiscourseChat::ChatController < ::ApplicationController requires_plugin DiscourseChat::PLUGIN_NAME - def list_providers - render json: ::DiscourseChat::Provider.providers.map {|x| x::PROVIDER_NAME} + def list + requested_provider = params[:provider] + + providers = ::DiscourseChat::Provider.providers.map {|x| x::PROVIDER_NAME} + + if requested_provider.nil? + requested_provider = providers[0] + end + + if not providers.include? requested_provider + raise Discourse::NotFound + end + + rules = DiscourseChat::Manager.get_rules_for_provider(requested_provider) + + out = {provider: requested_provider, providers: providers, rules: rules} + + render json: out end end @@ -40,15 +56,12 @@ after_initialize do add_admin_route 'chat.menu_title', 'chat' DiscourseChat::Engine.routes.draw do - get "/list-providers" => "chat#list_providers", constraints: AdminConstraint.new - end - - Discourse::Application.routes.prepend do - mount ::DiscourseChat::Engine, at: "/chat" + get "(.:format)" => "chat#list" # TODO: Fix this hack + get "/:provider" => "chat#list" end Discourse::Application.routes.append do - get '/admin/plugins/chat' => 'admin/plugins#index', constraints: StaffConstraint.new + mount ::DiscourseChat::Engine, at: '/admin/plugins/chat', constraints: AdminConstraint.new end end