From b023197d0e11d4474427d150b22df28b0a3cbce3 Mon Sep 17 00:00:00 2001 From: "s.sisti@elogic.it" Date: Sat, 21 Nov 2026 23:45:36 +0100 Subject: [PATCH 1/4] FEATURE: Reopen the wizard when a moderator asks to revise a queued topic When a wizard topic needs approval and a moderator chooses "Revise", core rejects it and sends the author the raw post, to be posted again with the composer. That loses the wizard's structure, so the author has to fill the whole wizard in again. Instead, copy the submitted answers into a new draft of the wizard and send a message with the moderator's reason and feedback and a link to the wizard, which opens prefilled. Submitting it queues the corrected topic again. Falls back to the core message when the wizard can't be reopened (e.g. it doesn't allow multiple submissions or doesn't save submissions). Co-Authored-By: Claude Opus 5.5 --- config/locales/server.en.yml | 18 +++ config/locales/server.it.yml | 18 +++ lib/custom_wizard/action.rb | 2 + .../extensions/reviewable_queued_post.rb | 47 +++++++ lib/custom_wizard/submission.rb | 30 +++++ plugin.rb | 4 +- .../extensions/reviewable_queued_post_spec.rb | 126 ++++++++++++++++++ 7 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 lib/custom_wizard/extensions/reviewable_queued_post.rb create mode 100644 spec/extensions/reviewable_queued_post_spec.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index da24e7d8f..4a168f6c1 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -61,6 +61,24 @@ en: error_messages: wizard_replacing_composer: "Category not allowed for topic creation." + system_messages: + custom_wizard_revise_queued_topic: + title: "Feedback on your topic" + subject_template: 'Feedback on new topic titled "%{topic_title}"' + text_body_template: | + Hi %{username}, + + We've reviewed your new topic titled "%{topic_title}" and have some feedback for you. + + Reason: %{reason} + + Feedback: %{feedback} + + Your answers have been saved: [open the "%{wizard_name}" wizard](%{wizard_url}), make the suggested changes and submit it again. You don't need to fill it in from scratch. + + Thanks, + %{site_name} Moderators + site_settings: custom_wizard_enabled: "Enable custom wizards." wizard_redirect_exclude_paths: "Routes excluded from wizard redirects." diff --git a/config/locales/server.it.yml b/config/locales/server.it.yml index 9b59829c7..b74c47448 100644 --- a/config/locales/server.it.yml +++ b/config/locales/server.it.yml @@ -61,6 +61,24 @@ it: error_messages: wizard_replacing_composer: "Categoria non consentita per la creazione di argomenti." + system_messages: + custom_wizard_revise_queued_topic: + title: "Feedback sul tuo argomento" + subject_template: 'Feedback sul nuovo argomento intitolato "%{topic_title}"' + text_body_template: | + Ciao %{username}, + + Abbiamo esaminato il tuo nuovo argomento intitolato "%{topic_title}" e abbiamo un feedback per te. + + Motivo: %{reason} + + Feedback: %{feedback} + + Le tue risposte sono state salvate: [apri il wizard "%{wizard_name}"](%{wizard_url}), apporta le modifiche suggerite e invialo di nuovo. Non devi compilarlo da capo. + + Grazie, + I moderatori di %{site_name} + site_settings: custom_wizard_enabled: "Abilita i wizard personalizzati." wizard_redirect_exclude_paths: "Percorsi esclusi dai reindirizzamenti dei wizard." diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 9c6e4680f..ae88dfe39 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -613,6 +613,8 @@ def queue_topic(params, reason) topic: params.dig(:topic_opts, :custom_fields), post: params[:custom_fields], }, + # Lets a moderator's "revise" reopen this wizard (see CustomWizardReviewableQueuedPost). + wizard_id: @wizard.id, ), ) diff --git a/lib/custom_wizard/extensions/reviewable_queued_post.rb b/lib/custom_wizard/extensions/reviewable_queued_post.rb new file mode 100644 index 000000000..6d720f67c --- /dev/null +++ b/lib/custom_wizard/extensions/reviewable_queued_post.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +# When a moderator asks the author to revise a queued wizard topic, core sends the raw post +# and asks the author to post it again with the composer, which loses the wizard's structure. +# Instead, reopen the wizard prefilled with the author's answers and link to it. +module CustomWizardReviewableQueuedPost + def perform_revise_and_reject_post(performed_by, args) + draft = custom_wizard_revision_draft + return super if draft.blank? + + wizard = draft.wizard + message_options = + defined?(Dsa::AppealInstructions) ? Dsa::AppealInstructions.message_options : {} + + SystemMessage.create( + target_created_by, + :custom_wizard_revise_queued_topic, + **message_options, + topic_title: payload["title"], + reason: args[:revise_custom_reason].presence || args[:revise_reason], + feedback: args[:revise_feedback], + wizard_name: wizard.name, + wizard_url: "#{Discourse.base_url}/w/#{wizard.id.dasherize}", + ) + + StaffActionLogger.new(performed_by).log_post_rejected(self, DateTime.now) if performed_by.staff? + create_result(:success, :rejected) + end + + private + + def custom_wizard_revision_draft + return if !SiteSetting.custom_wizard_enabled || topic.present? || target_created_by.blank? + + submission_id = payload&.dig("wizard_custom_fields", "topic", "wizard_submission_id") + return if submission_id.blank? + + CustomWizard::Submission.reopen( + target_created_by, + submission_id, + wizard_id: payload["wizard_id"], + ) + rescue StandardError => e + Rails.logger.warn("Custom Wizard: failed to reopen wizard for review #{id}: #{e.message}") + nil + end +end diff --git a/lib/custom_wizard/submission.rb b/lib/custom_wizard/submission.rb index 98028741e..22dbdec36 100644 --- a/lib/custom_wizard/submission.rb +++ b/lib/custom_wizard/submission.rb @@ -92,6 +92,36 @@ def remove end end + # Copies the answers of a submitted wizard into a new draft, so the wizard opens prefilled + # and the user can correct and resubmit it. Replaces the user's other draft of the wizard, + # if any. Returns the draft, or nil if the wizard can't be reopened for the user. + def self.reopen(user, submission_id, wizard_id: nil) + wizard_id ||= find_wizard_id(user, submission_id) + return if wizard_id.blank? + + wizard = CustomWizard::Wizard.create(wizard_id, user) + return if !wizard || !wizard.save_submissions || !wizard.can_access? + + submissions = list(wizard).submissions + original = submissions.find { |submission| submission.id == submission_id } + return if original.blank? + + submissions.reject(&:submitted?).each(&:remove) + + draft = new(wizard, original.fields.slice(*wizard.field_ids)) + draft.save + draft + end + + def self.find_wizard_id(user, submission_id) + PluginStoreRow + .where("plugin_name LIKE ?", "%\\_#{KEY}") + .where(key: user.id.to_s) + .find { |row| ::JSON.parse(row.value).any? { |data| data["id"] == submission_id } } + &.plugin_name + &.delete_suffix("_#{KEY}") + end + def self.cleanup_incomplete_submissions(wizard) all_submissions = list(wizard) sorted_submissions = diff --git a/plugin.rb b/plugin.rb index ed511c6b5..648e86587 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.18 +# version: 2.18.19 # 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 @@ -92,6 +92,7 @@ require_relative "lib/custom_wizard/extensions/invites_controller.rb" require_relative "lib/custom_wizard/extensions/users_controller.rb" require_relative "lib/custom_wizard/extensions/guardian.rb" + require_relative "lib/custom_wizard/extensions/reviewable_queued_post.rb" require_relative "lib/custom_wizard/extensions/custom_field/preloader.rb" require_relative "lib/custom_wizard/extensions/custom_field/serializer.rb" require_relative "lib/custom_wizard/extensions/custom_field/extension.rb" @@ -183,6 +184,7 @@ ::InvitesController.prepend InvitesControllerCustomWizard ::UsersController.prepend CustomWizardUsersController ::Guardian.prepend CustomWizardGuardian + ::ReviewableQueuedPost.prepend CustomWizardReviewableQueuedPost full_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets/stylesheets/wizard/wizard_custom.scss" diff --git a/spec/extensions/reviewable_queued_post_spec.rb b/spec/extensions/reviewable_queued_post_spec.rb new file mode 100644 index 000000000..15cb5df12 --- /dev/null +++ b/spec/extensions/reviewable_queued_post_spec.rb @@ -0,0 +1,126 @@ +# frozen_string_literal: true + +describe CustomWizardReviewableQueuedPost do + fab!(:user) { Fabricate(:user, trust_level: TrustLevel[2]) } + fab!(:moderator) + fab!(:category) + + let(:wizard_template) { get_wizard_fixture("wizard") } + + before do + category.update!(topic_posting_review_mode: :everyone) + wizard_template["multiple_submissions"] = true + wizard_template["actions"][0]["visible"][0]["output"] = "false" + CustomWizard::Template.save(wizard_template, skip_jobs: true) + end + + def build_wizard + CustomWizard::Builder.new("super_mega_fun_wizard", user).build + end + + def complete_wizard + wizard = build_wizard + wizard.create_updater( + wizard.steps.first.id, + step_1_field_1: "Topic Title", + step_1_field_2: "topic body", + ).update + wizard.create_updater(wizard.steps.second.id, {}).update + wizard.create_updater(wizard.steps.last.id, step_3_field_3: category.id).update + end + + def revise(reviewable) + reviewable.perform( + moderator, + :revise_and_reject_post, + revise_reason: "Needs more detail", + revise_feedback: "Please describe the outcome.", + ) + end + + def field_value(wizard, field_id) + wizard.steps.flat_map(&:fields).find { |field| field.id == field_id }.value + end + + def last_message + Topic.private_messages_for_user(user).order(:created_at).last&.first_post&.raw + end + + it "reopens the wizard with the submitted answers and links to it" do + complete_wizard + reviewable = ReviewableQueuedPost.find_by(target_created_by: user) + expect(reviewable.payload["wizard_id"]).to eq("super_mega_fun_wizard") + + result = revise(reviewable) + + expect(result.success?).to eq(true) + expect(reviewable.reload.status).to eq("rejected") + expect(last_message).to include("Please describe the outcome.") + expect(last_message).to include("#{Discourse.base_url}/w/super-mega-fun-wizard") + + wizard = build_wizard + expect(wizard.start).to eq(wizard.steps.first.id) + expect(field_value(wizard, "step_1_field_1")).to eq("Topic Title") + expect(field_value(wizard, "step_1_field_2")).to eq("topic body") + end + + it "queues the corrected topic again when the reopened wizard is submitted" do + complete_wizard + revise(ReviewableQueuedPost.find_by(target_created_by: user)) + + # The client sends every field of a step, prefilled ones included. + wizard = build_wizard + wizard.create_updater( + wizard.steps.first.id, + step_1_field_1: field_value(wizard, "step_1_field_1"), + step_1_field_2: "corrected body", + ).update + wizard.create_updater(wizard.steps.second.id, {}).update + wizard.create_updater(wizard.steps.last.id, step_3_field_3: category.id).update + + pending = ReviewableQueuedPost.where(target_created_by: user, status: :pending) + expect(pending.count).to eq(1) + expect(pending.first.payload["title"]).to eq("Topic Title") + expect(pending.first.payload["raw"]).to eq("corrected body") + expect(pending.first.category_id).to eq(category.id) + end + + it "finds the wizard of a topic queued without its id" do + complete_wizard + reviewable = ReviewableQueuedPost.find_by(target_created_by: user) + reviewable.update!(payload: reviewable.payload.except("wizard_id")) + + revise(reviewable) + + expect(last_message).to include("/w/super-mega-fun-wizard") + expect(field_value(build_wizard, "step_1_field_1")).to eq("Topic Title") + end + + it "sends the core message when the wizard can't be submitted again" do + wizard_template["multiple_submissions"] = false + CustomWizard::Template.save(wizard_template, skip_jobs: true) + complete_wizard + + revise(ReviewableQueuedPost.find_by(target_created_by: user)) + + expect(last_message).to include("Please describe the outcome.") + expect(last_message).to include("topic body") + expect(last_message).not_to include("/w/super-mega-fun-wizard") + end + + it "leaves other queued topics to core" do + manager = + NewPostManager.new( + user, + title: "A regular topic title", + raw: "regular body " * 5, + category: category.id, + ) + reviewable = manager.perform.reviewable + + revise(reviewable) + + expect(last_message).to include("regular body") + expect(last_message).not_to include("/w/") + end +end From d3f5d97febc793334e5d84856a0a525a0a683b49 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 24 Nov 2026 09:56:59 +0800 Subject: [PATCH 2/4] DEV: Make reopening the wizard on revise opt-in and safer - Add the custom_wizard_reopen_on_revise site setting (off by default). - Never overwrite a draft with answers; send the core message instead. - Require the wizard id in the payload; drop the plugin store lookup. - Use Dsa::AppealInstructions directly and Discourse.warn_exception. - Test through the step endpoints so submissions are actually submitted. Co-Authored-By: Claude Opus 5.5 --- config/locales/server.en.yml | 1 + config/locales/server.it.yml | 1 + config/settings.yml | 2 + .../extensions/reviewable_queued_post.rb | 25 ++-- lib/custom_wizard/submission.rb | 26 ++-- .../extensions/reviewable_queued_post_spec.rb | 119 +++++++++++------- 6 files changed, 97 insertions(+), 77 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 4a168f6c1..665a4facc 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -85,3 +85,4 @@ en: wizard_recognised_image_upload_formats: "File types which will result in upload displaying an image preview" wizard_apis_enabled: "Enable API features (experimental)." wizard_critical_notices_on_dashboard: "Show critical notices about the custom wizard plugin on the admin dashboard." + custom_wizard_reopen_on_revise: 'When a moderator chooses "Revise" on a queued wizard topic, reopen the wizard prefilled with the author''s answers instead of sending them the raw post. The wizard must save submissions and allow multiple submissions.' diff --git a/config/locales/server.it.yml b/config/locales/server.it.yml index b74c47448..d80a500c4 100644 --- a/config/locales/server.it.yml +++ b/config/locales/server.it.yml @@ -85,3 +85,4 @@ it: wizard_recognised_image_upload_formats: "Tipi di file per cui il caricamento mostra l'anteprima dell'immagine" wizard_apis_enabled: "Abilita le funzioni API (sperimentale)." wizard_critical_notices_on_dashboard: "Mostra nella dashboard di amministrazione gli avvisi critici sul plugin Custom Wizard." + custom_wizard_reopen_on_revise: 'Quando un moderatore sceglie "Revisione" su un argomento del wizard in coda, riapri il wizard precompilato con le risposte dell''autore invece di inviargli il post grezzo. Il wizard deve salvare gli invii e consentire invii multipli.' diff --git a/config/settings.yml b/config/settings.yml index 7698fd9cd..8211c5482 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -18,6 +18,8 @@ plugins: wizard_important_notices_on_dashboard: client: true default: true + custom_wizard_reopen_on_revise: + default: false wizard_subscription_product_key: hidden: true default: '' diff --git a/lib/custom_wizard/extensions/reviewable_queued_post.rb b/lib/custom_wizard/extensions/reviewable_queued_post.rb index 6d720f67c..3ca1546e9 100644 --- a/lib/custom_wizard/extensions/reviewable_queued_post.rb +++ b/lib/custom_wizard/extensions/reviewable_queued_post.rb @@ -2,20 +2,18 @@ # When a moderator asks the author to revise a queued wizard topic, core sends the raw post # and asks the author to post it again with the composer, which loses the wizard's structure. -# Instead, reopen the wizard prefilled with the author's answers and link to it. +# If enabled, reopen the wizard prefilled with the author's answers and link to it instead. module CustomWizardReviewableQueuedPost def perform_revise_and_reject_post(performed_by, args) draft = custom_wizard_revision_draft return super if draft.blank? wizard = draft.wizard - message_options = - defined?(Dsa::AppealInstructions) ? Dsa::AppealInstructions.message_options : {} SystemMessage.create( target_created_by, :custom_wizard_revise_queued_topic, - **message_options, + **Dsa::AppealInstructions.message_options, topic_title: payload["title"], reason: args[:revise_custom_reason].presence || args[:revise_reason], feedback: args[:revise_feedback], @@ -30,18 +28,19 @@ def perform_revise_and_reject_post(performed_by, args) private def custom_wizard_revision_draft - return if !SiteSetting.custom_wizard_enabled || topic.present? || target_created_by.blank? + return if !SiteSetting.custom_wizard_enabled || !SiteSetting.custom_wizard_reopen_on_revise + return if topic.present? || target_created_by.blank? - submission_id = payload&.dig("wizard_custom_fields", "topic", "wizard_submission_id") - return if submission_id.blank? + wizard_id = payload["wizard_id"] + submission_id = payload.dig("wizard_custom_fields", "topic", "wizard_submission_id") + return if wizard_id.blank? || submission_id.blank? - CustomWizard::Submission.reopen( - target_created_by, - submission_id, - wizard_id: payload["wizard_id"], - ) + CustomWizard::Submission.reopen(target_created_by, wizard_id, submission_id) rescue StandardError => e - Rails.logger.warn("Custom Wizard: failed to reopen wizard for review #{id}: #{e.message}") + Discourse.warn_exception( + e, + message: "Custom Wizard: failed to reopen wizard for reviewable #{id}", + ) nil end end diff --git a/lib/custom_wizard/submission.rb b/lib/custom_wizard/submission.rb index 22dbdec36..d0bc30d88 100644 --- a/lib/custom_wizard/submission.rb +++ b/lib/custom_wizard/submission.rb @@ -93,35 +93,27 @@ def remove end # Copies the answers of a submitted wizard into a new draft, so the wizard opens prefilled - # and the user can correct and resubmit it. Replaces the user's other draft of the wizard, - # if any. Returns the draft, or nil if the wizard can't be reopened for the user. - def self.reopen(user, submission_id, wizard_id: nil) - wizard_id ||= find_wizard_id(user, submission_id) - return if wizard_id.blank? - + # and the user can correct and resubmit it. Returns the draft, or nil if the wizard can't be + # reopened for the user, or if the user already has a draft with answers, which is never + # overwritten. + def self.reopen(user, wizard_id, submission_id) wizard = CustomWizard::Wizard.create(wizard_id, user) - return if !wizard || !wizard.save_submissions || !wizard.can_access? + return if !wizard&.save_submissions || !wizard.can_access? submissions = list(wizard).submissions + drafts = submissions.reject(&:submitted?) + return if drafts.any? { |draft| draft.fields.present? } + original = submissions.find { |submission| submission.id == submission_id } return if original.blank? - submissions.reject(&:submitted?).each(&:remove) + drafts.each(&:remove) draft = new(wizard, original.fields.slice(*wizard.field_ids)) draft.save draft end - def self.find_wizard_id(user, submission_id) - PluginStoreRow - .where("plugin_name LIKE ?", "%\\_#{KEY}") - .where(key: user.id.to_s) - .find { |row| ::JSON.parse(row.value).any? { |data| data["id"] == submission_id } } - &.plugin_name - &.delete_suffix("_#{KEY}") - end - def self.cleanup_incomplete_submissions(wizard) all_submissions = list(wizard) sorted_submissions = diff --git a/spec/extensions/reviewable_queued_post_spec.rb b/spec/extensions/reviewable_queued_post_spec.rb index 15cb5df12..8a04a05bd 100644 --- a/spec/extensions/reviewable_queued_post_spec.rb +++ b/spec/extensions/reviewable_queued_post_spec.rb @@ -1,32 +1,43 @@ # frozen_string_literal: true -describe CustomWizardReviewableQueuedPost do +describe CustomWizardReviewableQueuedPost, type: :request do fab!(:user) { Fabricate(:user, trust_level: TrustLevel[2]) } fab!(:moderator) fab!(:category) let(:wizard_template) { get_wizard_fixture("wizard") } + let(:wizard_path) { "/w/super-mega-fun-wizard" } before do + SiteSetting.custom_wizard_reopen_on_revise = true category.update!(topic_posting_review_mode: :everyone) wizard_template["multiple_submissions"] = true wizard_template["actions"][0]["visible"][0]["output"] = "false" + wizard_template["actions"][0]["category"] = [ + { + type: "assignment", + output: category.id.to_s, + output_type: "text", + output_connector: "set", + }, + ] CustomWizard::Template.save(wizard_template, skip_jobs: true) + sign_in(user) end - def build_wizard - CustomWizard::Builder.new("super_mega_fun_wizard", user).build + def submit_step(step_id, fields = {}) + put "#{wizard_path}/steps/#{step_id}.json", params: { fields: fields } + expect(response.status).to eq(200) + end + + def complete_wizard(title: "Topic Title", body: "topic body") + submit_step("step_1", step_1_field_1: title, step_1_field_2: body) + submit_step("step_2") + submit_step("step_3") end - def complete_wizard - wizard = build_wizard - wizard.create_updater( - wizard.steps.first.id, - step_1_field_1: "Topic Title", - step_1_field_2: "topic body", - ).update - wizard.create_updater(wizard.steps.second.id, {}).update - wizard.create_updater(wizard.steps.last.id, step_3_field_3: category.id).update + def queued_topic + ReviewableQueuedPost.find_by(target_created_by: user, status: :pending) end def revise(reviewable) @@ -38,7 +49,11 @@ def revise(reviewable) ) end - def field_value(wizard, field_id) + def wizard + CustomWizard::Builder.new("super_mega_fun_wizard", user).build + end + + def field_value(field_id) wizard.steps.flat_map(&:fields).find { |field| field.id == field_id }.value end @@ -46,9 +61,15 @@ def last_message Topic.private_messages_for_user(user).order(:created_at).last&.first_post&.raw end + def expect_core_message + expect(last_message).to include("Please describe the outcome.") + expect(last_message).to include("topic body") + expect(last_message).not_to include("#{wizard_path}") + end + it "reopens the wizard with the submitted answers and links to it" do complete_wizard - reviewable = ReviewableQueuedPost.find_by(target_created_by: user) + reviewable = queued_topic expect(reviewable.payload["wizard_id"]).to eq("super_mega_fun_wizard") result = revise(reviewable) @@ -56,27 +77,17 @@ def last_message expect(result.success?).to eq(true) expect(reviewable.reload.status).to eq("rejected") expect(last_message).to include("Please describe the outcome.") - expect(last_message).to include("#{Discourse.base_url}/w/super-mega-fun-wizard") - - wizard = build_wizard - expect(wizard.start).to eq(wizard.steps.first.id) - expect(field_value(wizard, "step_1_field_1")).to eq("Topic Title") - expect(field_value(wizard, "step_1_field_2")).to eq("topic body") + expect(last_message).to include("#{Discourse.base_url}#{wizard_path}") + expect(wizard.start).to eq("step_1") + expect(field_value("step_1_field_1")).to eq("Topic Title") + expect(field_value("step_1_field_2")).to eq("topic body") end it "queues the corrected topic again when the reopened wizard is submitted" do complete_wizard - revise(ReviewableQueuedPost.find_by(target_created_by: user)) - - # The client sends every field of a step, prefilled ones included. - wizard = build_wizard - wizard.create_updater( - wizard.steps.first.id, - step_1_field_1: field_value(wizard, "step_1_field_1"), - step_1_field_2: "corrected body", - ).update - wizard.create_updater(wizard.steps.second.id, {}).update - wizard.create_updater(wizard.steps.last.id, step_3_field_3: category.id).update + revise(queued_topic) + + complete_wizard(body: "corrected body") pending = ReviewableQueuedPost.where(target_created_by: user, status: :pending) expect(pending.count).to eq(1) @@ -85,15 +96,23 @@ def last_message expect(pending.first.category_id).to eq(category.id) end - it "finds the wizard of a topic queued without its id" do + it "sends the core message when the setting is disabled" do + SiteSetting.custom_wizard_reopen_on_revise = false complete_wizard - reviewable = ReviewableQueuedPost.find_by(target_created_by: user) + + revise(queued_topic) + + expect_core_message + end + + it "sends the core message when the topic was queued without the wizard id" do + complete_wizard + reviewable = queued_topic reviewable.update!(payload: reviewable.payload.except("wizard_id")) revise(reviewable) - expect(last_message).to include("/w/super-mega-fun-wizard") - expect(field_value(build_wizard, "step_1_field_1")).to eq("Topic Title") + expect_core_message end it "sends the core message when the wizard can't be submitted again" do @@ -101,22 +120,28 @@ def last_message CustomWizard::Template.save(wizard_template, skip_jobs: true) complete_wizard - revise(ReviewableQueuedPost.find_by(target_created_by: user)) + revise(queued_topic) - expect(last_message).to include("Please describe the outcome.") - expect(last_message).to include("topic body") - expect(last_message).not_to include("/w/super-mega-fun-wizard") + expect_core_message + end + + it "keeps a draft with answers and sends the core message" do + complete_wizard + reviewable = queued_topic + submit_step("step_1", step_1_field_1: "Another Title", step_1_field_2: "work in progress") + + revise(reviewable) + + expect_core_message + expect(field_value("step_1_field_2")).to eq("work in progress") end it "leaves other queued topics to core" do - manager = - NewPostManager.new( - user, - title: "A regular topic title", - raw: "regular body " * 5, - category: category.id, - ) - reviewable = manager.perform.reviewable + reviewable = + NewPostManager + .new(user, title: "A regular topic title", raw: "regular body " * 5, category: category.id) + .perform + .reviewable revise(reviewable) From 70f1792218cac13ba3f775758e65cd12d451ebd1 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 24 Nov 2026 10:17:21 +0800 Subject: [PATCH 3/4] DEV: Test revising queued wizard topics through the review queue - Perform revise, approve and reject through the review endpoint, with and without DSA reporting. - Fall back to the core message when reopening the wizard fails or the topic was posted as another user. - Check upload, category, tag and composer fields are prefilled and resubmitted. - Add a system spec for the whole flow, from the review queue to the corrected topic being queued again. Co-Authored-By: Claude Opus 5.5 --- .../extensions/reviewable_queued_post_spec.rb | 222 +++++++++++++++++- spec/system/revise_queued_topic_spec.rb | 115 +++++++++ 2 files changed, 335 insertions(+), 2 deletions(-) create mode 100644 spec/system/revise_queued_topic_spec.rb diff --git a/spec/extensions/reviewable_queued_post_spec.rb b/spec/extensions/reviewable_queued_post_spec.rb index 8a04a05bd..38e22a99c 100644 --- a/spec/extensions/reviewable_queued_post_spec.rb +++ b/spec/extensions/reviewable_queued_post_spec.rb @@ -26,7 +26,8 @@ end def submit_step(step_id, fields = {}) - put "#{wizard_path}/steps/#{step_id}.json", params: { fields: fields } + # Same as the client, which sends the step fields as JSON. + put "#{wizard_path}/steps/#{step_id}.json", params: { fields: fields }, as: :json expect(response.status).to eq(200) end @@ -41,7 +42,7 @@ def queued_topic end def revise(reviewable) - reviewable.perform( + reviewable.reload.perform( moderator, :revise_and_reject_post, revise_reason: "Needs more detail", @@ -148,4 +149,221 @@ def expect_core_message expect(last_message).to include("regular body") expect(last_message).not_to include("/w/") end + + context "when a moderator uses the review queue" do + def perform_in_queue(reviewable, action, params = {}) + sign_in(moderator) + put "/review/#{reviewable.id}/perform/#{action}.json", + params: { version: reviewable.reload.version }.merge(params) + expect(response.status).to eq(200) + sign_in(user) + end + + let(:revise_params) do + { revise_reason: "Other", revise_custom_reason: "Wrong date", revise_feedback: "" } + end + + it "reopens the wizard when revising" do + complete_wizard + reviewable = queued_topic + + perform_in_queue(reviewable, :revise_and_reject_post, revise_params) + + expect(reviewable.reload.status).to eq("rejected") + expect(last_message).to include("Wrong date") + expect(last_message).to include("#{Discourse.base_url}#{wizard_path}") + expect(field_value("step_1_field_2")).to eq("topic body") + end + + it "reopens the wizard when revising with DSA reporting enabled" do + enable_dsa_reporting( + community_rules: [{ ground: "Be accurate", explanation: "Dates must be correct." }], + ) + complete_wizard + reviewable = queued_topic + + perform_in_queue( + reviewable, + :revise_and_reject_post, + revise_params.merge( + dsa_classification: { + community_rule: DsaStatementOfRecord.community_rules.first.fetch("id"), + category: "STATEMENT_CATEGORY_OTHER_VIOLATION_TC", + }, + ), + ) + + expect(reviewable.reload.status).to eq("rejected") + expect(last_message).to include("#{Discourse.base_url}#{wizard_path}") + end + + it "approves wizard topics" do + complete_wizard + reviewable = queued_topic + + perform_in_queue(reviewable, :approve_post) + + expect(reviewable.reload.status).to eq("approved") + topic = Topic.find_by(title: "Topic Title", category_id: category.id) + expect(topic.first_post.raw).to eq("topic body") + expect(topic.custom_fields["wizard_submission_id"]).to be_present + end + + it "rejects wizard topics without reopening the wizard" do + complete_wizard + reviewable = queued_topic + + perform_in_queue(reviewable, :reject_post) + + expect(reviewable.reload.status).to eq("rejected") + expect(field_value("step_1_field_2")).to be_blank + end + end + + it "sends the core message when reopening the wizard fails" do + complete_wizard + CustomWizard::Submission.stubs(:reopen).raises(StandardError, "reopen failed") + + result = revise(queued_topic) + + expect(result.success?).to eq(true) + expect(queued_topic).to be_nil + expect_core_message + end + + context "when the topic is posted as another user" do + fab!(:poster) { Fabricate(:user, trust_level: TrustLevel[2]) } + + before do + wizard_template["actions"][0]["poster"] = [ + { + type: "assignment", + output: poster.username, + output_type: "text", + output_connector: "set", + }, + ] + CustomWizard::Template.save(wizard_template, skip_jobs: true) + end + + it "sends the core message to the poster and doesn't reopen anyone's wizard" do + complete_wizard + reviewable = ReviewableQueuedPost.find_by(target_created_by: poster, status: :pending) + + revise(reviewable) + + message = Topic.private_messages_for_user(poster).order(:created_at).last.first_post.raw + expect(message).to include("topic body") + expect(message).not_to include(wizard_path) + expect(PluginStore.get("super_mega_fun_wizard_submissions", poster.id)).to be_blank + expect(field_value("step_1_field_2")).to be_blank + end + end + + context "with upload, category, tag and composer fields" do + fab!(:tag) + fab!(:upload) { Fabricate(:upload, user: user, original_filename: "image.png") } + + let(:wizard_path) { "/w/revise-wizard" } + + def mapped(field_id) + [ + { + type: "assignment", + output: field_id, + output_type: "wizard_field", + output_connector: "set", + }, + ] + end + + def wizard + CustomWizard::Builder.new("revise_wizard", user).build + end + + def complete_wizard(body: "topic body") + submit_step( + "step_1", + title: "Topic Title", + body: body, + attachment: upload.as_json(only: %i[id url original_filename extension]), + category: [category.id], + tags: [tag.name], + ) + end + + before do + enable_subscription("standard") + CustomWizard::Template.save( + { + id: "revise_wizard", + name: "Revise wizard", + save_submissions: true, + multiple_submissions: true, + steps: [ + { + id: "step_1", + fields: [ + { id: "title", type: "text", label: "Title" }, + { id: "body", type: "composer", label: "Body" }, + { id: "attachment", type: "upload", label: "Attachment", file_types: ".png" }, + { id: "category", type: "category", label: "Category", limit: 1, property: "id" }, + { id: "tags", type: "tag", label: "Tags" }, + ], + }, + ], + actions: [ + { + id: "action_1", + type: "create_topic", + run_after: "step_1", + skip_redirect: true, + title: mapped("title"), + post: "body", + category: mapped("category"), + tags: mapped("tags"), + }, + ], + }.as_json, + skip_jobs: true, + ) + end + + it "prefills the reopened wizard with the submitted values" do + complete_wizard + reviewable = queued_topic + expect(reviewable.payload["tags"]).to eq([tag.name]) + submitted = PluginStore.get("revise_wizard_submissions", user.id).sole + + revise(reviewable) + + get "#{wizard_path}.json" + expect(response.status).to eq(200) + values = + response.parsed_body["steps"] + .flat_map { |step| step["fields"] } + .to_h { |field| [field["id"], field["value"]] } + + %w[title body attachment category tags].each do |field_id| + expect(values[field_id]).to eq(submitted[field_id]) + end + expect(values["attachment"]["id"]).to eq(upload.id) + expect(values["category"]).to eq([category.id]) + expect(values["tags"]).to eq([tag.name]) + end + + it "queues the corrected topic with the same upload, category and tags" do + complete_wizard + revise(queued_topic) + + complete_wizard(body: "corrected body") + + reviewable = queued_topic + expect(reviewable.payload["raw"]).to eq("corrected body") + expect(reviewable.category_id).to eq(category.id) + expect(reviewable.payload["tags"]).to eq([tag.name]) + submission = PluginStore.get("revise_wizard_submissions", user.id).last + expect(submission["attachment"]["id"]).to eq(upload.id) + end + end end diff --git a/spec/system/revise_queued_topic_spec.rb b/spec/system/revise_queued_topic_spec.rb new file mode 100644 index 000000000..2c4e91992 --- /dev/null +++ b/spec/system/revise_queued_topic_spec.rb @@ -0,0 +1,115 @@ +# frozen_string_literal: true + +describe "Revising a queued wizard topic" do + fab!(:user) { Fabricate(:user, trust_level: TrustLevel[2]) } + fab!(:moderator) + fab!(:category) { Fabricate(:category, name: "Information documents") } + fab!(:tag) { Fabricate(:tag, name: "public-notice") } + fab!(:upload) { Fabricate(:upload, user: user, original_filename: "notice.png") } + + let(:review_page) { PageObjects::Pages::Review.new } + let(:revise_modal) { PageObjects::Modals::ReviseAndRejectPost.new } + + def mapped(field_id) + [{ type: "assignment", output: field_id, output_type: "wizard_field", output_connector: "set" }] + end + + before do + enable_subscription("standard") + SiteSetting.custom_wizard_reopen_on_revise = true + category.update!(topic_posting_review_mode: :everyone) + + CustomWizard::Template.save( + { + id: "revise_wizard", + name: "Revise wizard", + save_submissions: true, + multiple_submissions: true, + steps: [ + { + id: "step_1", + fields: [ + { id: "title", type: "text", label: "Title" }, + { id: "body", type: "composer", label: "Body" }, + { id: "attachment", type: "upload", label: "Attachment", file_types: ".png" }, + { id: "category", type: "category", label: "Category", limit: 1, property: "id" }, + { id: "tags", type: "tag", label: "Tags" }, + ], + }, + ], + actions: [ + { + id: "action_1", + type: "create_topic", + run_after: "step_1", + skip_redirect: true, + title: mapped("title"), + post: "body", + category: mapped("category"), + tags: mapped("tags"), + }, + ], + }.as_json, + skip_jobs: true, + ) + end + + # Submits the wizard as the steps controller does, which queues the topic. + def submit_wizard + wizard = CustomWizard::Builder.new("revise_wizard", user).build + wizard.create_updater( + "step_1", + title: "Notice about the road closure", + body: "The road is closed on the wrong date.", + attachment: upload.as_json(only: %i[id url original_filename extension]), + category: [category.id], + tags: [tag.name], + ).update + CustomWizard::Builder.new("revise_wizard", user).build.cleanup_on_complete! + end + + it "lets the author fix the topic in the prefilled wizard and queues it again" do + submit_wizard + reviewable = ReviewableQueuedPost.find_by(target_created_by: user) + + sign_in(moderator) + review_page.visit_reviewable(reviewable) + expect(page).to have_content("Notice about the road closure") + review_page.select_bundled_action(reviewable, "revise_and_reject_post") + expect(revise_modal).to be_open + PageObjects::Components::SelectKit.new( + ".revise-and-reject-reviewable__reason", + ).select_row_by_value("other_reason") + revise_modal.fill_reason("Wrong date") + revise_modal.fill_feedback("Please correct the date.") + revise_modal.click_primary_button + expect(review_page).to have_reviewable_with_rejected_status(reviewable) + expect(reviewable.reload).to be_rejected + + sign_in(user) + message = Topic.private_messages_for_user(user).order(:created_at).last + expect(message.first_post.raw).to include("Wrong date") + visit(message.relative_url) + find(".cooked a[href$='/w/revise-wizard']").click + + expect(page).to have_field(with: "Notice about the road closure") + expect(find(".wizard-field.body .d-editor-input").value).to eq( + "The road is closed on the wrong date.", + ) + expect(page).to have_css(".wizard-field.attachment", text: "notice.png") + expect(page).to have_css(".wizard-field.category", text: category.name) + expect(page).to have_css(".wizard-field.tags", text: tag.name) + + find(".wizard-field.body .d-editor-input").fill_in(with: "The road is closed on Monday.") + find(".wizard-btn.done").click + + expect(page).to have_no_css(".wizard-btn.done") + reviewable = ReviewableQueuedPost.find_by(target_created_by: user, status: :pending) + expect(reviewable.payload["raw"]).to eq("The road is closed on Monday.") + expect(reviewable.payload["title"]).to eq("Notice about the road closure") + expect(reviewable.category_id).to eq(category.id) + expect(reviewable.payload["tags"]).to eq([tag.name]) + submission = PluginStore.get("revise_wizard_submissions", user.id).last + expect(submission["attachment"]["id"]).to eq(upload.id) + end +end From 137686b92a97d4127158b213867479b6e3979366 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 24 Nov 2026 10:28:09 +0800 Subject: [PATCH 4/4] DEV: Expect the problem confirmed status after revising Co-Authored-By: Claude Opus 5.5 --- spec/system/revise_queued_topic_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/revise_queued_topic_spec.rb b/spec/system/revise_queued_topic_spec.rb index 2c4e91992..9ab446803 100644 --- a/spec/system/revise_queued_topic_spec.rb +++ b/spec/system/revise_queued_topic_spec.rb @@ -83,7 +83,7 @@ def submit_wizard revise_modal.fill_reason("Wrong date") revise_modal.fill_feedback("Please correct the date.") revise_modal.click_primary_button - expect(review_page).to have_reviewable_with_rejected_status(reviewable) + expect(review_page).to have_reviewable_with_problem_confirmed_status(reviewable) expect(reviewable.reload).to be_rejected sign_in(user)