From 940217805b339421fc734ab37860f8954fd7dd00 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sat, 7 Nov 2026 15:25:53 +0800 Subject: [PATCH] FIX: Use every username input when creating a group The create group action crashed when its members setting was text or a wizard field, such as a user selector field. Members and owners also used only their first input and matched usernames case sensitively. Members and owners now follow the same rule as other list settings: plain inputs add up, conditional inputs keep first match wins, text values are split, and usernames match in any case. Templates with several plain member or owner inputs now use all of them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DSz8MWXii43sifu8gH46aE --- lib/custom_wizard/action.rb | 41 ++++++++------------ plugin.rb | 2 +- spec/components/custom_wizard/action_spec.rb | 27 +++++++++++++ 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index ccfe280d7..c8a091f29 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -331,29 +331,22 @@ def route_to def create_group group = begin - Group.new(new_group_params.except(:usernames, :owner_usernames)) + Group.new(new_group_params) rescue ArgumentError => e raise Discourse::InvalidParameters, "Invalid group params" end if group.save - def get_user_ids(username_string) - User.where(username: username_string.split(",")).pluck(:id) - end + owner_ids = User.where(username_lower: map_usernames(action["owner_usernames"])).pluck(:id) + usernames = map_usernames(action["usernames"]) + user_ids = User.where(username_lower: usernames).pluck(:id) - if new_group_params[:owner_usernames].present? - owner_ids = get_user_ids(new_group_params[:owner_usernames]) - owner_ids.each { |user_id| group.group_users.build(user_id: user_id, owner: true) } + if user_ids.size < usernames.size + log_error("Warning, group creation: some users were not found!") end - if new_group_params[:usernames].present? - user_ids = get_user_ids(new_group_params[:usernames]) - if user_ids.count < new_group_params[:usernames].count - log_error("Warning, group creation: some users were not found!") - end - user_ids -= owner_ids if owner_ids - user_ids.each { |user_id| group.group_users.build(user_id: user_id) } - end + owner_ids.each { |user_id| group.group_users.build(user_id: user_id, owner: true) } + (user_ids - owner_ids).each { |user_id| group.group_users.build(user_id: user_id) } log_success("Group created", group.name) if group.save @@ -411,13 +404,15 @@ def action_tags end def map_list(inputs) + return [] if inputs.blank? + output = CustomWizard::Mapper.new( inputs: inputs, data: mapper_data, user: user, opts: { - multiple: inputs&.all? { |input| input["type"] == "assignment" }, + multiple: inputs.all? { |input| input["type"] == "assignment" }, }, ).perform @@ -428,14 +423,12 @@ def map_list(inputs) .compact_blank end - def action_users - users = [] - - if action["usernames"].present? - usernames = map_list(action["usernames"]).map { |value| User.normalize_username(value) } - users.push(*User.where(username_lower: usernames)) - end + def map_usernames(inputs) + map_list(inputs).map { |value| User.normalize_username(value) }.uniq + end + def action_users + users = User.where(username_lower: map_usernames(action["usernames"])).to_a users.push(user) if ActiveRecord::Type::Boolean.new.cast(action["wizard_user"]) users.uniq @@ -666,8 +659,6 @@ def new_group_params full_name title bio_raw - owner_usernames - usernames mentionable_level messageable_level visibility_level diff --git a/plugin.rb b/plugin.rb index 340a37e35..aef351273 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.11 +# version: 2.18.12 # 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 e1a983b4e..4cc552e85 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -843,6 +843,33 @@ def complete_as_guest(email) expect(GroupUser.where(group_id: group_id).count).to eq(1) end + it "#create_group adds the users of every username input" do + user2 = Fabricate(:user) + create_group["usernames"] = [ + { + type: "assignment", + output_type: "text", + output_connector: "set", + output: user2.username.upcase, + }, + { + type: "assignment", + output_type: "user", + output_connector: "set", + output: [user1.username], + }, + ] + wizard_template["actions"] << create_group + 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 + + group = Group.find_by!(name: wizard.current_submission.fields["action_9"]) + expect(group.users).to contain_exactly(user, user1, user2) + expect(group.group_users.where(owner: true).pluck(:user_id)).to eq([user.id]) + end + it "#add_to_group" do wizard_template["actions"] << create_group wizard_template["actions"] << add_to_group