diff --git a/.discourse-compatibility b/.discourse-compatibility index 0b4de0ee..e42b8a51 100644 --- a/.discourse-compatibility +++ b/.discourse-compatibility @@ -1 +1 @@ -2.6.0.beta1: bb85b3a0d2c0ab6b59bcb405731c39089ec6731c \ No newline at end of file +2.5.0: bb85b3a0d2c0ab6b59bcb405731c39089ec6731c \ No newline at end of file diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index 196bf328..8fcca367 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -20,11 +20,13 @@ 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'), + createGroup: equal('action.type', 'create_group'), apiEmpty: empty('action.api'), groupPropertyTypes: selectKitContent(['id', 'name']), hasAdvanced: or('hasCustomFields', 'routeTo'), showAdvanced: and('hasAdvanced', 'action.type'), - hasCustomFields: or('basicTopicFields', 'updateProfile'), + hasCustomFields: or('basicTopicFields', 'updateProfile', 'createGroup', 'createCategory'), basicTopicFields: or('createTopic', 'sendMessage', 'openComposer'), publicTopicFields: or('createTopic', 'openComposer'), showSkipRedirect: or('createTopic', 'sendMessage'), diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 8c91e95b..7ddd34b2 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/components/wizard-mapper-selector.js.es6 b/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 index 91af570b..7d37f74f 100644 --- a/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 +++ b/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 @@ -13,6 +13,7 @@ export default Component.extend({ showText: computed('activeType', function() { return this.showInput('text') }), showWizardField: computed('activeType', function() { return this.showInput('wizardField') }), + showWizardAction: computed('activeType', function() { return this.showInput('wizardAction') }), showUserField: computed('activeType', function() { return this.showInput('userField') }), showUserFieldOptions: computed('activeType', function() { return this.showInput('userFieldOptions') }), showCategory: computed('activeType', function() { return this.showInput('category') }), @@ -22,6 +23,7 @@ export default Component.extend({ showList: computed('activeType', function() { return this.showInput('list') }), textEnabled: computed('options.textSelection', 'inputType', function() { return this.optionEnabled('textSelection') }), wizardFieldEnabled: computed('options.wizardFieldSelection', 'inputType', function() { return this.optionEnabled('wizardFieldSelection') }), + wizardActionEnabled: computed('options.wizardActionSelection', 'inputType', function() { return this.optionEnabled('wizardActionSelection') }), userFieldEnabled: computed('options.userFieldSelection', 'inputType', function() { return this.optionEnabled('userFieldSelection') }), userFieldOptionsEnabled: computed('options.userFieldOptionsSelection', 'inputType', function() { return this.optionEnabled('userFieldOptionsSelection') }), categoryEnabled: computed('options.categorySelection', 'inputType', function() { return this.optionEnabled('categorySelection') }), @@ -32,7 +34,7 @@ export default Component.extend({ groups: alias('site.groups'), categories: alias('site.categories'), - showComboBox: or('showWizardField', 'showUserField', 'showUserFieldOptions'), + showComboBox: or('showWizardField', 'showWizardAction', 'showUserField', 'showUserFieldOptions'), showMultiSelect: or('showCategory', 'showGroup'), hasTypes: gt('selectorTypes.length', 1), showTypes: false, @@ -73,20 +75,48 @@ export default Component.extend({ return type ? I18n.t(`admin.wizard.selector.label.${snakeCase(type)}`) : null; }, - comboBoxAllowAny: alias('showWizardField'), + comboBoxAllowAny: or('showWizardField', 'showWizardAction'), - @discourseComputed('activeType') - comboBoxContent(activeType) { - const controller = getOwner(this).lookup('controller:admin-wizards-wizard-show'); - const wizardFields = controller.wizardFields; - const userFields = controller.userFields; + @discourseComputed + showController() { + return getOwner(this).lookup('controller:admin-wizards-wizard-show'); + }, + + @discourseComputed( + 'activeType', + 'showController.wizardFields.[]', + 'showController.wizard.actions.[]', + 'showController.userFields.[]', + 'showController.currentField.id', + 'showController.currentAction.id' + ) + comboBoxContent( + activeType, + wizardFields, + wizardActions, + userFields, + currentFieldId, + currentActionId + ) { let content; if (activeType === 'wizardField') { content = wizardFields; if (this.options.context === 'field') { - content = content.filter(field => field.id !== controller.currentField.id); + content = content.filter(field => field.id !== currentFieldId); + } + } + + if (activeType === 'wizardAction') { + content = wizardActions.map(a => ({ + id: a.id, + label: `${generateName(a.type)} (${a.id})`, + type: a.type + })); + + if (this.options.context === 'action') { + content = content.filter(a => a.id !== currentActionId); } } diff --git a/assets/javascripts/discourse/components/wizard-text-editor.js.es6 b/assets/javascripts/discourse/components/wizard-text-editor.js.es6 index 25cf05e8..6b95eee9 100644 --- a/assets/javascripts/discourse/components/wizard-text-editor.js.es6 +++ b/assets/javascripts/discourse/components/wizard-text-editor.js.es6 @@ -11,6 +11,7 @@ export default Component.extend({ previewEnabled: true, fieldsEnabled: true, hasWizardFields: notEmpty('wizardFieldList'), + hasWizardActions: notEmpty('wizardActionList'), didReceiveAttrs() { this._super(...arguments); @@ -46,6 +47,11 @@ export default Component.extend({ return wizardFields.map((f) => ` w{${f.id}}`); }, + @discourseComputed('wizardActions') + wizardActionList(wizardActions) { + return wizardActions.map((a) => ` w{${a.id}}`); + }, + actions: { togglePreview() { this.toggleProperty('forcePreview'); diff --git a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 index fd01f605..fd8470e4 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 @@ -46,7 +46,7 @@ export default Controller.extend({ I18n.t('admin.wizard.after_time_time_label'); }, - @discourseComputed('currentStep.id', 'wizard.save_submissions', 'wizard.steps.@each.fields[]') + @discourseComputed('currentStep.id', 'wizard.save_submissions', 'currentStep.fields.@each.label') wizardFields(currentStepId, saveSubmissions) { let steps = this.wizard.steps; if (!saveSubmissions) { diff --git a/assets/javascripts/discourse/lib/wizard-mapper.js.es6 b/assets/javascripts/discourse/lib/wizard-mapper.js.es6 index 6d179ef6..470a78c4 100644 --- a/assets/javascripts/discourse/lib/wizard-mapper.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-mapper.js.es6 @@ -80,6 +80,7 @@ const selectionTypes = [ 'text', 'list', 'wizardField', + 'wizardAction', 'userField', 'userFieldOptions', 'group', diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index 1b480471..840b84a5 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -115,6 +115,7 @@ const action = { post_template: null, category: null, tags: null, + visible: null, custom_fields: null, skip_redirect: null }, @@ -144,7 +145,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, @@ -157,12 +160,36 @@ 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, + custom_fields: null + }, + create_group: { + name: null, + full_name: null, + title: null, + bio_raw: null, + owner_usernames: null, + usernames: null, + grant_trust_level: null, + mentionable_level: null, + messageable_level: null, + visibility_level: null, + members_visibility_level: null, + custom_fields: null } }, mapped: [ 'title', 'category', 'tags', + 'visible', 'custom_fields', 'required', 'recipient', @@ -170,7 +197,22 @@ const action = { 'group', 'url', 'categories', - 'mute_remainder' + 'mute_remainder', + 'name', + 'slug', + 'color', + 'text_color', + 'parent_category_id', + '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 c52f6cd1..272c3155 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -139,6 +139,24 @@ )}} + +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.visible + property='visible' + onUpdate=(action 'mappedFieldUpdated') + options=(hash + wizardFieldSelection=true + userFieldSelection=true + context='action' + )}} +
+
{{/if}} {{#if sendMessage}} @@ -297,6 +315,7 @@ options=(hash textSelection='key,value' wizardFieldSelection=true + wizardActionSelection=true userFieldSelection='key,value' categorySelection='output' context='action' @@ -338,6 +357,358 @@ )}} + +
+
+ +
+ +
+ {{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}} +
+
+ +
+ +
+ {{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.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' + )}} +
+
+
+
+ +
+ +
+ {{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}} +
+
+ +
+ +
+ {{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 + wizardActionSelection='key' + userFieldSelection=true + groupSelection='key' + context='action' + )}} +
+
{{/if}} {{#if showAdvanced}} diff --git a/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs b/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs index f9f6cc9b..36be715f 100644 --- a/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs @@ -28,6 +28,13 @@ {{wizardFieldList}} {{/if}} + + {{#if hasWizardActions}} + + {{/if}} {{/if}} {{/if}} 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 c60ecec9..65e31c96 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -94,6 +94,7 @@ en: label: text: "text" wizard_field: "wizard field" + wizard_action: "wizard action" user_field: "user field" user_field_options: "user field options" user: "user" @@ -106,6 +107,7 @@ en: text: "Enter text" property: "Select property" wizard_field: "Select field" + wizard_action: "Select action" user_field: "Select field" user_field_options: "Select field" user: "Select user" @@ -208,6 +210,7 @@ en: label: "Create Topic" category: "Category" tags: "Tags" + visible: "Visible" open_composer: label: "Open Composer" update_profile: @@ -226,11 +229,14 @@ 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" user_properties: "User Properties" wizard_fields: "Wizard Fields" + wizard_actions: "Wizard Actions" placeholder: "Insert wizard fields using the field_id in w{}. Insert user properties using property in u{}." add_to_group: label: "Add to Group" @@ -246,6 +252,27 @@ 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 + create_group: + label: Create Group + name: Name + 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 + 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 3930d75d..84b09595 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -121,17 +121,35 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController :api, :api_endpoint, :api_body, + :wizard_user, title: mapped_params, category: mapped_params, tags: mapped_params, custom_fields: mapped_params, + visible: mapped_params, required: mapped_params, recipient: mapped_params, categories: mapped_params, 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, + full_name: 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, + visibility_level: mapped_params, + members_visibility_level: mapped_params ] ) end 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 a33a244f..a916d847 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 @@ -22,6 +24,10 @@ class CustomWizard::Action @result.handler.enqueue_jobs end + if @result.success? && @result.output.present? + data[action['id']] = @result.output + end + save_log end @@ -30,12 +36,9 @@ class CustomWizard::Action end def create_topic - params = basic_topic_params + params = basic_topic_params.merge(public_topic_params) if params[:title].present? && params[:raw].present? - params[:category] = action_category - params[:tags] = action_tags - creator = PostCreator.new(user, params) post = creator.create @@ -49,6 +52,7 @@ class CustomWizard::Action if creator.errors.blank? log_success("created topic", "id: #{post.topic.id}") result.handler = creator + result.output = post.topic.id end else log_error("invalid topic params", "title: #{params[:title]}; post: #{params[:raw]}") @@ -87,6 +91,7 @@ class CustomWizard::Action if creator.errors.blank? log_success("created message", "id: #{post.topic.id}") result.handler = creator + result.output = post.topic.id end else log_error( @@ -135,17 +140,18 @@ class CustomWizard::Action end def watch_categories - watched_categories = CustomWizard::Mapper.new( inputs: action['categories'], data: data, user: user ).perform + + watched_categories = [*watched_categories].map(&:to_i) 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 @@ -154,12 +160,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 @@ -274,6 +320,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) + rescue ArgumentError => e + raise Discourse::InvalidParameters, "Invalid group params" + end + + if group.save + GroupActionLogger.new(user, group).log_change_group_settings + log_success("Group created", group.name) + result.output = group.name + else + log_error("Group creation failed") + end + end + + def create_category + guardian.ensure_can_create!(Category) + + 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) + result.output = category.id + else + log_error("Category creation failed") + end + end + private def action_category @@ -350,10 +434,131 @@ class CustomWizard::Action add_custom_fields(params) end + def public_topic_params + params = {} + + if (category = action_category) + params[:category] = category + end + + if (tags = action_tags) + params[:tags] = tags + end + + if public_topic_fields.any? + public_topic_fields.each do |field| + unless action[field].nil? || action[field] == "" + params[field.to_sym] = CustomWizard::Mapper.new( + inputs: action[field], + data: data, + user: user + ).perform + end + end + end + + params + end + + def new_group_params + params = {} + + %w( + name + full_name + title + bio_raw + owner_usernames + usernames + mentionable_level + messageable_level + visibility_level + members_visibility_level + grant_trust_level + ).each do |attr| + 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: input, + 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 + + 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? + value = CustomWizard::Mapper.new( + inputs: action[attr], + data: data, + user: user + ).perform + + 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 + end + + add_custom_fields(params) + end + def creates_post? [:create_topic, :send_message].include?(action['type'].to_sym) end + def public_topic_fields + ['visible'] + end + def profile_url_fields ['profile_background', 'card_background'] end diff --git a/lib/custom_wizard/action_result.rb b/lib/custom_wizard/action_result.rb index c731e752..80f2b55d 100644 --- a/lib/custom_wizard/action_result.rb +++ b/lib/custom_wizard/action_result.rb @@ -1,5 +1,5 @@ class CustomWizard::ActionResult - attr_accessor :success, :handler + attr_accessor :success, :handler, :output def initialize @success = false diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index e1dd2182..534e4824 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 diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index 44ccf2f5..e06a9da9 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -187,6 +187,10 @@ class CustomWizard::Mapper def map_wizard_field(value) data && !data.key?("submitted_at") && data[value] end + + def map_wizard_action(value) + data && !data.key?("submitted_at") && data[value] + end def map_user_field(value) if value.include?(User::USER_FIELD_PREFIX)