From 11bf892bcc2adbf7b354715000e36982c84894f6 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 9 Jul 2020 12:19:36 +1000 Subject: [PATCH 1/4] Add Create Category action and Create Group serverside --- .../components/wizard-custom-action.js.es6 | 1 + .../discourse/lib/wizard-schema.js.es6 | 16 ++- .../components/wizard-custom-action.hbs | 119 ++++++++++++++++++ assets/stylesheets/common/wizard-admin.scss | 4 - config/locales/client.en.yml | 8 ++ controllers/custom_wizard/admin/wizard.rb | 8 +- lib/custom_wizard/action.rb | 102 +++++++++++++++ 7 files changed, 252 insertions(+), 6 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index 196bf328..af80d30c 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -20,6 +20,7 @@ export default Component.extend(UndoChanges, { sendToApi: equal('action.type', 'send_to_api'), addToGroup: equal('action.type', 'add_to_group'), routeTo: equal('action.type', 'route_to'), + createCategory: equal('action.type', 'create_category'), apiEmpty: empty('action.api'), groupPropertyTypes: selectKitContent(['id', 'name']), hasAdvanced: or('hasCustomFields', 'routeTo'), diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index 1b480471..b21142d2 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -157,6 +157,14 @@ const action = { route_to: { url: null, code: null + }, + create_category: { + name: null, + slug: null, + color: null, + text_color: "FFFFFF", + parent_category_id: null, + permissions: null } }, mapped: [ @@ -170,7 +178,13 @@ const action = { 'group', 'url', 'categories', - 'mute_remainder' + 'mute_remainder', + 'name', + 'slug', + 'color', + 'text_color', + 'parent_category_id', + 'permittions' ], advanced: [ 'code', diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index c52f6cd1..3f69f8f1 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -340,6 +340,125 @@ {{/if}} +{{#if createCategory}} +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.name + property='name' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection='key,value' + wizardFieldSelection=true + userFieldSelection='key,value' + context='action' + )}} +
+
+ +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.slug + property='slug' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection='key,value' + context='action' + )}} +
+
+ +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.color + property='color' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection='key,value' + context='action' + )}} +
+
+ +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.text_color + property='text_color' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection='key,value' + context='action' + )}} +
+
+ +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.parent_category_id + property='parent_category_id' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection='key,value' + wizardFieldSelection=true + userFieldSelection='key,value' + categorySelection='output' + context='action' + )}} +
+
+ +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.permissions + property='permissions' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + inputTypes='association' + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + groupSelection='key' + context='action' + )}} +
+
+{{/if}} + {{#if showAdvanced}} {{wizard-advanced-toggle showAdvanced=action.showAdvanced}} diff --git a/assets/stylesheets/common/wizard-admin.scss b/assets/stylesheets/common/wizard-admin.scss index 5eacc613..6d26e13b 100644 --- a/assets/stylesheets/common/wizard-admin.scss +++ b/assets/stylesheets/common/wizard-admin.scss @@ -35,10 +35,6 @@ & + div { margin-top: 30px; - - &+ div.setting { - margin-top: 30px; - } } } diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 5d58200f..ed522795 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -241,6 +241,14 @@ en: select_an_endpoint: "Select an endpoint" body: "Body" body_placeholder: "JSON" + create_category: + label: "Create Category" + name: Name + slug: Slug + color: Color + text_color: Text color + parent_category: Parent Category + permissions: Permissions submissions: nav_label: "Submissions" diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index dcf6fa00..865821ad 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -130,7 +130,13 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController mute_remainder: mapped_params, profile_updates: mapped_params, group: mapped_params, - url: mapped_params + url: mapped_params, + name: mapped_params, + slug: mapped_params, + color: mapped_params, + text_color: mapped_params, + parent_category_id: mapped_params, + permissions: mapped_params ] ) end diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index a33a244f..26346741 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -2,12 +2,14 @@ class CustomWizard::Action attr_accessor :data, :action, :user, + :guardian, :result def initialize(params) @wizard = params[:wizard] @action = params[:action] @user = params[:user] + @guardian = Guardian.new(@user) @data = params[:data] @log = [] @result = CustomWizard::ActionResult.new @@ -274,6 +276,44 @@ class CustomWizard::Action log_info("route: #{route_to}") end + def create_group + guardian.ensure_can_create!(Group) + + group = + begin + Group.new(new_group_params.merge(user: user)) + rescue ArgumentError => e + raise Discourse::InvalidParameters, "Invalid group params" + end + + if group.update(new_group_params) + GroupActionLogger.new(user, group).log_change_group_settings + log_success("Group created", group.name) + else + log_error("Group creation failed") + end + end + + def create_category + guardian.ensure_can_create!(Category) + + byebug + + category = + begin + Category.new(new_category_params.merge(user: user)) + rescue ArgumentError => e + raise Discourse::InvalidParameters, "Invalid category params" + end + + if category.save + StaffActionLogger.new(user).log_category_creation(category) + log_success("Category created", category.name) + else + log_error("Category creation failed") + end + end + private def action_category @@ -350,6 +390,68 @@ class CustomWizard::Action add_custom_fields(params) end + def new_group_params + params = {} + + %w( + name + full_name + title + mentionable_level + messageable_level + visibility_level + members_visibility_level + grant_trust_level + ).each do |attr| + if action["group_#{attr}"].present? + params[attr.to_sym] = CustomWizard::Mapper.new( + inputs: action["group_#{attr}"], + data: data, + user: user + ).perform + end + end + + add_custom_fields(params) + end + + def new_category_params + params = {} + + %w( + name + slug + color + text_color + parent_category_id + permissions + ).each do |attr| + if action[attr].present? + params[attr.to_sym] = CustomWizard::Mapper.new( + inputs: action[attr], + data: data, + user: user + ).perform + end + end + + if params[:parent_category_id].present? + params[:parent_category_id] = params[:parent_category_id][0] + end + + if params[:permissions].present? + permissions = {} + params[:permissions].each do |p| + if group = Group.find_by(id: p[:key][0]) + permissions[group.name] = p[:value].to_i + end + end + params[:permissions] = permissions + end + + add_custom_fields(params) + end + def creates_post? [:create_topic, :send_message].include?(action['type'].to_sym) end From 8fb44749f5229ccfbe261ca1fa46927a6eda5ca0 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 16 Jul 2020 15:25:06 +1000 Subject: [PATCH 2/4] Add create_group --- .../components/wizard-custom-action.js.es6 | 1 + .../components/wizard-custom-field.js.es6 | 2 +- .../discourse/lib/wizard-schema.js.es6 | 11 ++ .../components/wizard-custom-action.hbs | 165 ++++++++++++++++++ config/locales/client.en.yml | 11 ++ controllers/custom_wizard/admin/wizard.rb | 11 +- lib/custom_wizard/action.rb | 2 - 7 files changed, 199 insertions(+), 4 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index af80d30c..c661db3e 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -21,6 +21,7 @@ export default Component.extend(UndoChanges, { addToGroup: equal('action.type', 'add_to_group'), routeTo: equal('action.type', 'route_to'), createCategory: equal('action.type', 'create_category'), + createGroup: equal('action.type', 'create_group'), apiEmpty: empty('action.api'), groupPropertyTypes: selectKitContent(['id', 'name']), hasAdvanced: or('hasCustomFields', 'routeTo'), diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 9251f433..bd44fde0 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -17,7 +17,7 @@ export default Component.extend(UndoChanges, { isText: equal('field.type', 'text'), isTextarea: equal('field.type', 'textarea'), isUrl: equal('field.type', 'url'), - showPrefill: or('isCategory', 'isTag', 'isGroup', 'isDropdown'), + showPrefill: or('isText', 'isCategory', 'isTag', 'isGroup', 'isDropdown'), showContent: or('isCategory', 'isTag', 'isGroup', 'isDropdown'), showLimit: or('isCategory', 'isTag'), showMinLength: or('isText', 'isTextarea', 'isUrl', 'isComposer'), diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index b21142d2..c377d3b9 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -165,6 +165,17 @@ const action = { text_color: "FFFFFF", parent_category_id: null, permissions: null + }, + create_group: { + name: null, + full_name: null, + title: null, + bio_raw: null, + grant_trust_level: null, + mentionable_level: null, + messageable_level: null, + visibility_level: null, + members_visibility_level: null } }, mapped: [ diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 3f69f8f1..2cf5e446 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -340,6 +340,171 @@ {{/if}} +{{#if createGroup}} +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.name + property='name' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.full_name + property='full_name' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.title + property='title' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.bio_raw + property='bio_raw' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.grant_trust_level + property='grant_trust_level' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.mentionable_level + property='mentionable_level' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.messageable_level + property='messageable_level' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.visibility_level + property='visibility_level' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.members_visibility_level + property='members_visibility_level' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
+{{/if}} + {{#if createCategory}}
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index ed522795..9a7503f5 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -249,6 +249,17 @@ en: text_color: Text color parent_category: Parent Category permissions: Permissions + create_group: + label: Create Group + name: Name + full_name: Full Name + title: Title + bio_raw: About + grant_trust_level: Automatic Trust Level + mentionable_level: Mentionable Level + messageable_level: Messageable Level + visibility_level: Visibility Level + members_visibility_level: Members Visibility Level submissions: nav_label: "Submissions" diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index 865821ad..70ec764a 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -136,7 +136,16 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController color: mapped_params, text_color: mapped_params, parent_category_id: mapped_params, - permissions: mapped_params + permissions: mapped_params, + full_name: mapped_params, + bio_raw: mapped_params, + title: mapped_params, + bio_raw: mapped_params, + grant_trust_level: mapped_params, + mentionable_level: mapped_params, + messageable_level: mapped_params, + visibility_level: mapped_params, + members_visibility_level: mapped_params ] ) end diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 26346741..8ee1f8a4 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -297,8 +297,6 @@ class CustomWizard::Action def create_category guardian.ensure_can_create!(Category) - byebug - category = begin Category.new(new_category_params.merge(user: user)) From 1e4348b67a98c3e5233cf011bb32ba3d9441b1c2 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 16 Jul 2020 17:50:09 +1000 Subject: [PATCH 3/4] Complete new group category actions --- .../discourse/lib/wizard-schema.js.es6 | 13 +++- .../components/wizard-custom-action.hbs | 38 ++++++++++++ config/locales/client.en.yml | 2 + controllers/custom_wizard/admin/wizard.rb | 3 +- lib/custom_wizard/action.rb | 61 ++++++++++++------- lib/custom_wizard/field.rb | 6 +- 6 files changed, 98 insertions(+), 25 deletions(-) diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index c377d3b9..4a3d26ef 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -171,6 +171,8 @@ const action = { full_name: null, title: null, bio_raw: null, + owner_usernames: null, + usernames: null, grant_trust_level: null, mentionable_level: null, messageable_level: null, @@ -195,7 +197,16 @@ const action = { 'color', 'text_color', 'parent_category_id', - 'permittions' + 'permissions', + 'full_name', + 'bio_raw', + 'owner_usernames', + 'usernames', + 'grant_trust_level', + 'mentionable_level', + 'messageable_level', + 'visibility_level', + 'members_visibility_level' ], advanced: [ 'code', diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 2cf5e446..6aa2eb49 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -413,6 +413,44 @@ )}}
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.owner_usernames + property='owner_usernames' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + userSelection='output' + context='action' + )}} +
+
+
+
+ +
+ +
+ {{wizard-mapper + inputs=action.usernames + property='usernames' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + textSelection=true + wizardFieldSelection=true + userFieldSelection=true + userSelection='output' + context='action' + )}} +
+
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 9a7503f5..07f3f67b 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -255,6 +255,8 @@ en: full_name: Full Name title: Title bio_raw: About + owner_usernames: Owners + usernames: Members grant_trust_level: Automatic Trust Level mentionable_level: Mentionable Level messageable_level: Messageable Level diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index 70ec764a..f095e39a 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -138,9 +138,10 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController parent_category_id: mapped_params, permissions: mapped_params, full_name: mapped_params, - bio_raw: mapped_params, title: mapped_params, bio_raw: mapped_params, + usernames: mapped_params, + owner_usernames: mapped_params, grant_trust_level: mapped_params, mentionable_level: mapped_params, messageable_level: mapped_params, diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 8ee1f8a4..ee064e0d 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -281,12 +281,12 @@ class CustomWizard::Action group = begin - Group.new(new_group_params.merge(user: user)) + Group.new(new_group_params) rescue ArgumentError => e raise Discourse::InvalidParameters, "Invalid group params" end - - if group.update(new_group_params) + + if group.save GroupActionLogger.new(user, group).log_change_group_settings log_success("Group created", group.name) else @@ -395,18 +395,26 @@ class CustomWizard::Action name full_name title + bio_raw + owner_usernames + usernames mentionable_level messageable_level visibility_level members_visibility_level grant_trust_level ).each do |attr| - if action["group_#{attr}"].present? - params[attr.to_sym] = CustomWizard::Mapper.new( - inputs: action["group_#{attr}"], + if action[attr].present? + value = CustomWizard::Mapper.new( + inputs: action[attr], data: data, user: user ).perform + + value = value.parameterize(separator: '_') if attr === "name" + value = value.to_i if attr.include?("_level") + + params[attr.to_sym] = value end end @@ -424,27 +432,38 @@ class CustomWizard::Action parent_category_id permissions ).each do |attr| - if action[attr].present? - params[attr.to_sym] = CustomWizard::Mapper.new( + if action[attr].present? + value = CustomWizard::Mapper.new( inputs: action[attr], data: data, user: user ).perform - end - end - - if params[:parent_category_id].present? - params[:parent_category_id] = params[:parent_category_id][0] - end - - if params[:permissions].present? - permissions = {} - params[:permissions].each do |p| - if group = Group.find_by(id: p[:key][0]) - permissions[group.name] = p[:value].to_i + + if attr === "parent_category_id" && value.is_a?(Array) + value = value[0] end + + if attr === "permissions" && value.is_a?(Array) + permissions = value + value = {} + + permissions.each do |p| + k = p[:key] + v = p[:value].to_i + + if k.is_a?(Array) + group = Group.find_by(id: k[0]) + k = group.name + else + k = k.parameterize(separator: '_') + end + + value[k] = v + end + end + + params[attr.to_sym] = value end - params[:permissions] = permissions end add_custom_fields(params) diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index dff610ce..9e7fe5bb 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -2,10 +2,12 @@ class CustomWizard::Field def self.types @types ||= { text: { - min_length: nil + min_length: nil, + prefill: nil }, textarea: { - min_length: nil + min_length: nil, + prefill: nil }, composer: { min_length: nil From 978dc7c2358baf7fb122e82501d201e91fd05ea0 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 17 Jul 2020 11:08:25 +1000 Subject: [PATCH 4/4] Use name input for full_name if blank --- lib/custom_wizard/action.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index ee064e0d..45bdbd35 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -404,9 +404,19 @@ class CustomWizard::Action members_visibility_level grant_trust_level ).each do |attr| - if action[attr].present? + input = action[attr] + + if attr === "name" && input.blank? + raise ArgumentError.new + end + + if attr === "full_name" && input.blank? + input = action["name"] + end + + if input.present? value = CustomWizard::Mapper.new( - inputs: action[attr], + inputs: input, data: data, user: user ).perform