From 5f6484a002b9506a0f78513157b1ba31b63a85bc Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sat, 7 Nov 2026 16:31:37 +0800 Subject: [PATCH] FIX: Accept category slugs and group field values in actions A category field can store slugs. Category settings turned values into numbers, so a slug became 0: watch categories with mute remainder muted every category, and the topic, composer and parent category ignored or rejected it. Category values are now looked up by id or slug in one place. Watch categories logs an error and changes nothing when none of the given categories exist. Rebuilding a wizard after a group field was saved crashed, as the saved group id was treated as a list. A group id used as a message recipient also crashed the action. Both now work. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DSz8MWXii43sifu8gH46aE --- lib/custom_wizard/action.rb | 48 ++++++---- lib/custom_wizard/builder.rb | 4 +- plugin.rb | 2 +- spec/components/custom_wizard/action_spec.rb | 96 ++++++++++++++++---- 4 files changed, 110 insertions(+), 40 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 3028197e1f..0f7416752e 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -74,7 +74,7 @@ def send_message params = basic_topic_params - targets = map_list(action["recipient"]).uniq(&:downcase) + targets = map_list(action["recipient"]).uniq { |target| target.to_s.downcase } if targets.blank? log_error("no recipients", "send_message has no recipients") @@ -85,11 +85,11 @@ def send_message params[:target_usernames] = [] params[:target_emails] = [] targets.each do |target| - if Group.find_by(name: target) - params[:target_group_names] << target - elsif User.find_by_username(target) - params[:target_usernames] << target - elsif target.match(/@/) # Compare discourse/discourse/app/controllers/posts_controller.rb#L922-L923 + if group = target.is_a?(Integer) ? Group.find_by(id: target) : Group.find_by(name: target) + params[:target_group_names] << group.name + elsif recipient = User.find_by_username(target) + params[:target_usernames] << recipient.username + elsif target.to_s.match(/@/) # Compare discourse/discourse/app/controllers/posts_controller.rb#L922-L923 params[:target_emails] << target end end @@ -169,7 +169,13 @@ def watch_tags end def watch_categories - watched_categories = map_list(action["categories"]).map(&:to_i) + values = map_list(action["categories"]) + watched_categories = find_category_ids(values) + + if values.present? && watched_categories.empty? + log_error("categories not found", "categories: #{values.join(",")}") + return + end notification_level = action["notification_level"] @@ -390,13 +396,20 @@ def action_category return false if output.blank? - if output.is_a?(Array) - output.first - elsif output.is_a?(Integer) - output - elsif output.is_a?(String) - output.to_i - end + find_category_id(output) || false + end + + def find_category_id(value) + find_category_ids(Array.wrap(value).first).first + end + + def find_category_ids(values) + values = Array.wrap(values).map { |value| value.to_s.strip }.compact_blank + Category + .where(id: values.grep(/\A\d+\z/)) + .or(Category.where(slug: values)) + .order(:id) + .pluck(:id) end def action_tags @@ -528,8 +541,7 @@ def basic_topic_params def public_topic_params params = {} - # Like TopicCreator, ignore a category that doesn't exist. - if (category = action_category) && Category.exists?(id: category) + if category = action_category params[:category] = category end @@ -695,7 +707,9 @@ def new_category_params CustomWizard::Mapper.new(inputs: action[attr], data: mapper_data, user: user).perform if value - value = value[0] if attr === "parent_category_id" && value.is_a?(Array) + if attr === "parent_category_id" + value = find_category_id(value) || Array.wrap(value).first + end if attr === "permissions" && value.is_a?(Array) permissions = value diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index cff77230a4..83fcd2f876 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -123,9 +123,7 @@ def append_field(step, step_template, field_template, build_opts) ] end - if field_template["type"] === "group" && params[:value].present? - params[:value] = params[:value].first - end + params[:value] = Array.wrap(params[:value]).first if field_template["type"] === "group" params[:value] = standardise_boolean(params[:value]) if field_template["type"] === "checkbox" diff --git a/plugin.rb b/plugin.rb index 8efeda0ed0..3ee444b13f 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.18.13 +# version: 2.18.14 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index 168589a9f3..366b14d250 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -97,11 +97,15 @@ def complete_wizard(body: "topic body", **last_step_fields) expect(submission.redirect_on_complete).to eq(Topic.find_by(title: "Topic Title").url) end - it "ignores a category that doesn't exist" do + def assign_category(output) wizard_template[:actions][0]["category"] = [ - { type: "assignment", output_type: "text", output_connector: "set", output: "missing" }, + { type: "assignment", output_type: "text", output_connector: "set", output: output }, ] update_template(wizard_template) + end + + it "ignores a category that doesn't exist" do + assign_category("missing") complete_wizard @@ -109,6 +113,15 @@ def complete_wizard(body: "topic body", **last_step_fields) expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id) end + it "uses a category given by slug" do + assign_category(category.slug) + + complete_wizard + + topic = Topic.find_by(title: "Topic Title") + expect(topic.category_id).to eq(category.id) + end + context "with tags" do fab!(:tag2) { Fabricate(:tag, name: "tag2") } @@ -484,17 +497,43 @@ def composer_query ) end - it "watches categories" do - watch_categories[:categories][0][:output] = category.id + def run_watch_categories wizard_template[:actions] << watch_categories update_template(wizard_template) wizard = CustomWizard::Builder.new(@template[:id], user).build wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update + end - expect( - CategoryUser.where(category_id: category.id, user_id: user.id).first.notification_level, - ).to eq(2) + def category_level(category) + CategoryUser.find_by(category: category, user: user)&.notification_level + end + + it "watches categories" do + watch_categories[:categories][0][:output] = category.id + run_watch_categories + + expect(category_level(category)).to eq(CategoryUser.notification_levels[:tracking]) + end + + it "watches categories given by slug" do + watch_categories[:categories][0][:output] = category.slug + run_watch_categories + + expect(category_level(category)).to eq(CategoryUser.notification_levels[:tracking]) + end + + it "mutes the other categories when no category is given" do + run_watch_categories + + expect(category_level(category)).to eq(CategoryUser.notification_levels[:muted]) + end + + it "doesn't mute categories when no category is found" do + watch_categories[:categories][0][:output] = "missing" + run_watch_categories + + expect(CategoryUser.where(user: user)).to be_empty end it "watches categories for the users in the action" do @@ -510,11 +549,7 @@ def composer_query output: [user1.username, user2.username], }, ] - wizard_template[:actions] << watch_categories - update_template(wizard_template) - - wizard = CustomWizard::Builder.new(@template[:id], user).build - wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update + run_watch_categories expect( CategoryUser.where(category_id: category.id, notification_level: 2).pluck(:user_id), @@ -532,11 +567,7 @@ def composer_query watch_categories[:usernames] = [user1, user2].map do |u| { type: "assignment", output_type: "user", output_connector: "set", output: [u.username] } end - wizard_template[:actions] << watch_categories - update_template(wizard_template) - - wizard = CustomWizard::Builder.new(@template[:id], user).build - wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update + run_watch_categories expect( CategoryUser.where(notification_level: 2).pluck(:user_id, :category_id), @@ -645,6 +676,21 @@ def composer_query expect(topic.allowed_groups).to contain_exactly(group) end + it "#send_message sends to the group in a group field" do + wizard_template[:steps][0][:fields] << { id: "step_1_field_5", type: "group" }.as_json + send_message["recipient"] = [step_1_field_1_input.merge(output: "step_1_field_5")] + wizard_template["actions"] << send_message + update_template(wizard_template) + + wizard = CustomWizard::Builder.new(@template[:id], user).build + wizard.create_updater(wizard.steps[0].id, step_1_field_5: group.id).update + wizard = CustomWizard::Builder.new(@template[:id], user).build + wizard.create_updater(wizard.steps[1].id, {}).update + + topic = Topic.find_by!(archetype: Archetype.private_message, title: "Message title") + expect(topic.allowed_groups).to contain_exactly(group) + end + it "#send_message sends to each recipient in a comma separated value" do send_message["recipient"] = [step_1_field_1_input] wizard_template["actions"] << send_message @@ -816,7 +862,7 @@ def complete_as_guest(email) end context "business actions" do - it "#create_category" do + def run_create_category wizard_template["actions"] << create_category wizard_template["actions"] << create_group update_template(wizard_template) @@ -825,7 +871,19 @@ def complete_as_guest(email) wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update wizard.create_updater(wizard.steps[1].id, {}).update - expect(Category.where(id: wizard.current_submission.fields["action_8"]).exists?).to eq(true) + Category.find_by(id: wizard.current_submission.fields["action_8"]) + end + + it "#create_category" do + expect(run_create_category).to be_present + end + + it "#create_category uses a parent category given by slug" do + create_category["parent_category_id"] = [ + { type: "assignment", output_type: "text", output_connector: "set", output: category.slug }, + ] + + expect(run_create_category.parent_category_id).to eq(category.id) end it "#create_group" do