From 36c0281b2fcea4b5803ba99a65cc7195491c7d86 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 20 Jul 2020 13:06:36 +1000 Subject: [PATCH] Add watch category users and migration --- .../discourse/lib/wizard-schema.js.es6 | 4 +- .../components/wizard-custom-action.hbs | 30 ++++++++++ config/locales/client.en.yml | 2 + controllers/custom_wizard/admin/wizard.rb | 1 + ...18014105_update_watch_categories_action.rb | 27 +++++++++ lib/custom_wizard/action.rb | 57 ++++++++++++++++--- 6 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20200718014105_update_watch_categories_action.rb diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index cd0b950c..e75339bd 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -144,7 +144,9 @@ const action = { watch_categories: { categories: null, notification_level: null, - mute_remainder: null + mute_remainder: null, + wizard_user: true, + usernames: null }, send_to_api: { api: null, diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 8f331772..f46be9ad 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -297,6 +297,7 @@ options=(hash textSelection='key,value' wizardFieldSelection=true + wizardActionSelection=true userFieldSelection='key,value' categorySelection='output' context='action' @@ -338,6 +339,35 @@ )}} + +
+
+ +
+ +
+ {{input type="checkbox" checked=action.wizard_user}} +
+
+ +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.usernames + property='usernames' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + context='action' + wizardFieldSelection=true + userFieldSelection='key,value' + userSelection='output' + )}} +
+
{{/if}} {{#if createGroup}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 8ebf661e..0b9587ea 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -223,6 +223,8 @@ en: watching_first_post: "Watching First Post" muted: "Muted" select_a_notification_level: "Select level" + wizard_user: "Wizard User" + usernames: "Users" post_builder: checkbox: "Post Builder" label: "Builder" diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index f095e39a..c17de1cb 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -120,6 +120,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController :api, :api_endpoint, :api_body, + :wizard_user, title: mapped_params, category: mapped_params, tags: mapped_params, diff --git a/db/migrate/20200718014105_update_watch_categories_action.rb b/db/migrate/20200718014105_update_watch_categories_action.rb new file mode 100644 index 00000000..bf24004d --- /dev/null +++ b/db/migrate/20200718014105_update_watch_categories_action.rb @@ -0,0 +1,27 @@ +class UpdateWatchCategoriesAction < ActiveRecord::Migration[6.0] + def change + watch_category_wizards = PluginStoreRow.where(" + plugin_name = 'custom_wizard' AND + value::jsonb -> 'actions' @> '[{ \"type\" : \"watch_categories\" }]'::jsonb + ") + + if watch_category_wizards.exists? + watch_category_wizards.each do |row| + begin + wizard_json = JSON.parse(row.value) + rescue TypeError, JSON::ParserError + next + end + + wizard_json['actions'].each do |a| + if a['type'] === "watch_categories" && a['wizard_user'] == nil + a['wizard_user'] = true + end + end + + row.value = wizard_json.to_json + row.save + end + end + end +end \ No newline at end of file diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index dcd44f88..fe8462ed 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -143,17 +143,20 @@ class CustomWizard::Action end def watch_categories - watched_categories = CustomWizard::Mapper.new( inputs: action['categories'], data: data, user: user ).perform + + unless watched_categories.is_a?(Array) + watched_categories = [watched_categories] + end notification_level = action['notification_level'] if notification_level.blank? - log_error("Notifcation Level was not set! Exiting wizard action") + log_error("Notifcation Level was not set. Exiting wizard action") return end @@ -162,12 +165,52 @@ class CustomWizard::Action data: data, user: user ).perform + + users = [] + + if action['usernames'] + mapped_users = CustomWizard::Mapper.new( + inputs: action['usernames'], + data: data, + user: user + ).perform + + if mapped_users.present? + mapped_users = mapped_users.split(',') + .map { |username| User.find_by(username: username) } + users.push(*mapped_users) + end + end + + if ActiveRecord::Type::Boolean.new.cast(action['wizard_user']) + users.push(user) + end - Category.all.each do |category| - if watched_categories.present? && watched_categories.include?(category.id.to_s) - CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[notification_level.to_sym], category.id) - elsif mute_remainder - CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[:muted], category.id) + category_ids = Category.all.pluck(:id) + set_level = CategoryUser.notification_levels[notification_level.to_sym] + mute_level = CategoryUser.notification_levels[:muted] + + users.each do |user| + category_ids.each do |category_id| + new_level = nil + + if watched_categories.include?(category_id) && set_level != nil + new_level = set_level + elsif mute_remainder + new_level = mute_level + end + + if new_level + CategoryUser.set_notification_level_for_category(user, new_level, category_id) + end + end + + if watched_categories.any? + log_success("#{user.username} notifications for #{watched_categories} set to #{set_level}") + end + + if mute_remainder + log_success("#{user.username} notifications for all other categories muted") end end end