A platform for community discussion. Free, open, simple.
FEATURE: prevent moving solved posts out of topic (#37289)
David Battersby03c0a8e0b5a1
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/components/modal/move-solution-confirmation.gjs b/plugins/discourse-solved/assets/javascripts/discourse/components/modal/move-solution-confirmation.gjsnew file mode 100644index 00000000000..80776c942c3--- /dev/null+++ b/plugins/discourse-solved/assets/javascripts/discourse/components/modal/move-solution-confirmation.gjs@@ -0,0 +1,70 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { Input } from "@ember/component";+import { on } from "@ember/modifier";+import { action } from "@ember/object";+import DButton from "discourse/components/d-button";+import DModal from "discourse/components/d-modal";+import { i18n } from "discourse-i18n";++export default class MoveSolutionConfirmationModal extends Component {+ @tracked dontShowAgain = false;++ get confirmLabel() {+ return this.args.model.count > 1+ ? "solved.move_post_confirm.other"+ : "solved.move_post_confirm.one";+ }++ @action+ toggleDontShowAgain(event) {+ this.dontShowAgain = event.target.checked;+ }++ @action+ confirm() {+ this.args.closeModal({+ confirmed: true,+ dontShowAgain: this.dontShowAgain,+ });+ }++ @action+ cancel() {+ this.args.closeModal({ confirmed: false });+ }++ <template>+ <DModal+ @closeModal={{this.cancel}}+ @title={{i18n "solved.confirm_move_solution_title"}}+ class="move-solution-confirmation-modal"+ >+ <:body>+ <p>{{i18n "solved.confirm_move_solution" [email protected]}}</p>+ <div class="control-group">+ <label class="checkbox-label move-solution-dont-show-again">+ <Input+ @type="checkbox"+ @checked={{this.dontShowAgain}}+ {{on "change" this.toggleDontShowAgain}}+ />+ {{i18n "solved.dont_show_again"}}+ </label>+ </div>+ </:body>+ <:footer>+ <DButton+ @action={{this.confirm}}+ @label={{this.confirmLabel}}+ class="btn-primary"+ />+ <DButton+ @action={{this.cancel}}+ @label="solved.move_post_cancel"+ class="btn-transparent"+ />+ </:footer>+ </DModal>+ </template>+}diff --git a/plugins/discourse-solved/assets/javascripts/discourse/pre-initializers/solved-move-confirmation.js b/plugins/discourse-solved/assets/javascripts/discourse/pre-initializers/solved-move-confirmation.jsnew file mode 100644index 00000000000..d4c59578a03--- /dev/null+++ b/plugins/discourse-solved/assets/javascripts/discourse/pre-initializers/solved-move-confirmation.js@@ -0,0 +1,58 @@+import MoveToTopic from "discourse/components/modal/move-to-topic";+import { withPluginApi } from "discourse/lib/plugin-api";+import MoveSolutionConfirmationModal from "../components/modal/move-solution-confirmation";++const STORAGE_KEY = "discourse-solved-hide-move-confirmation";++export default {+ name: "solved-move-confirmation",++ before: "inject-discourse-objects",++ initialize() {+ withPluginApi((api) => {+ api.modifyClass("service:modal", {+ pluginId: "discourse-solved",++ _solvedOriginalShow: null,++ async show(modal, opts) {+ if (!this._solvedOriginalShow) {+ this._solvedOriginalShow = this._super.bind(this);+ }++ const originalShow = this._solvedOriginalShow;++ if (modal === MoveToTopic && opts?.model?.selectedPosts) {+ const selectedPosts = opts.model.selectedPosts;+ const hasSolvedPost = selectedPosts?.some((p) => p.accepted_answer);++ if (hasSolvedPost) {+ const hideConfirmation =+ localStorage.getItem(STORAGE_KEY) === "true";++ if (!hideConfirmation) {+ const result = await originalShow(+ MoveSolutionConfirmationModal,+ {+ model: { count: selectedPosts.length },+ }+ );++ if (!result?.confirmed) {+ return;+ }++ if (result.dontShowAgain) {+ localStorage.setItem(STORAGE_KEY, "true");+ }+ }+ }+ }++ return originalShow(modal, opts);+ },+ });+ });+ },+};diff --git a/plugins/discourse-solved/config/locales/client.en.yml b/plugins/discourse-solved/config/locales/client.en.ymlindex edde522b98b..c2b965dc734 100644--- a/plugins/discourse-solved/config/locales/client.en.yml+++ b/plugins/discourse-solved/config/locales/client.en.yml@@ -35,6 +35,15 @@ en:no_solved_topics_title_others: "%{username} has not solved any topics yet"no_solved_topics_body: "When you provide a helpful reply to a topic, your reply might be selected as the solution by the topic owner or staff."marked_solved_by: "Marked as solved by %{user}"+ confirm_move_solution_title: "Move solution post"+ confirm_move_solution:+ one: "The post you selected is marked as the solution for this topic. If you move it, the topic will be updated to unsolved."+ other: "One of the posts you selected is marked as the solution for this topic. If you move them, the topic will be updated to unsolved."+ move_post_confirm:+ one: "Move post"+ other: "Move posts"+ move_post_cancel: "Cancel"+ dont_show_again: "Got it, don't show me this again"no_answer:title: Has your question been answered?diff --git a/plugins/discourse-solved/lib/discourse_solved/topics_controller_extension.rb b/plugins/discourse-solved/lib/discourse_solved/topics_controller_extension.rbnew file mode 100644index 00000000000..8640fdc7225--- /dev/null+++ b/plugins/discourse-solved/lib/discourse_solved/topics_controller_extension.rb@@ -0,0 +1,24 @@+# frozen_string_literal: true++module DiscourseSolved+ module TopicsControllerExtension+ def move_posts+ post_ids = params[:post_ids]+ topic_id = params[:topic_id]++ if post_ids.present? && topic_id.present?+ topic = Topic.with_deleted.find_by(id: topic_id)++ if topic&.solved.present?+ solution_post_id = topic.solved.answer_post_id+ if post_ids.map(&:to_i).include?(solution_post_id)+ solution_post = Post.find_by(id: solution_post_id)+ DiscourseSolved.unaccept_answer!(solution_post) if solution_post+ end+ end+ end++ super+ end+ end+enddiff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rbindex 211f6f0e952..c5f726878d8 100644--- a/plugins/discourse-solved/plugin.rb+++ b/plugins/discourse-solved/plugin.rb@@ -180,6 +180,7 @@ after_initialize do::Category.prepend(DiscourseSolved::CategoryExtension)::PostSerializer.prepend(DiscourseSolved::PostSerializerExtension)::UserSummary.prepend(DiscourseSolved::UserSummaryExtension)+ ::TopicsController.prepend(DiscourseSolved::TopicsControllerExtension)::Topic.attr_accessor(:accepted_answer_user_id)::TopicPostersSummary.alias_method(:old_user_ids, :user_ids)::TopicPostersSummary.prepend(DiscourseSolved::TopicPostersSummaryExtension)diff --git a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb b/plugins/discourse-solved/spec/requests/topics_controller_spec.rbindex 430b00f43b5..4f8fdbfcb4d 100644--- a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb+++ b/plugins/discourse-solved/spec/requests/topics_controller_spec.rb@@ -122,4 +122,67 @@ RSpec.describe TopicsController doexpect(response.body).not_to include(schema_json(1))endend++ describe "#move_posts" do+ fab!(:admin)+ fab!(:category)+ fab!(:topic) { Fabricate(:topic, category: category) }+ fab!(:op) { Fabricate(:post, topic: topic) }+ fab!(:reply) { Fabricate(:post, topic: topic) }+ fab!(:another_reply) { Fabricate(:post, topic: topic) }++ before do+ SiteSetting.allow_solved_on_all_topics = true+ sign_in(admin)+ end++ it "allows moving posts from a topic without a solution" do+ post "/t/#{topic.id}/move-posts.json",+ params: {+ post_ids: [reply.id],+ title: "This is a new topic for the moved post",+ }++ expect(response.status).to eq(200)+ end++ it "unsolves the topic when moving a post that is marked as the solution" do+ Fabricate(:solved_topic, topic: topic, answer_post: reply)++ post "/t/#{topic.id}/move-posts.json",+ params: {+ post_ids: [reply.id],+ title: "This is a new topic for the moved post",+ }++ expect(response.status).to eq(200)+ expect(topic.reload.solved).to be_nil+ end++ it "unsolves the topic when moving the solution post to an existing topic" do+ Fabricate(:solved_topic, topic: topic, answer_post: reply)+ destination_topic = Fabricate(:topic)++ post "/t/#{topic.id}/move-posts.json",+ params: {+ post_ids: [reply.id],+ destination_topic_id: destination_topic.id,+ }++ expect(response.status).to eq(200)+ expect(topic.reload.solved).to be_nil+ end++ it "allows moving non-solution posts from a topic with a solution" do+ Fabricate(:solved_topic, topic: topic, answer_post: reply)++ post "/t/#{topic.id}/move-posts.json",+ params: {+ post_ids: [another_reply.id],+ title: "This is a new topic for the moved post",+ }++ expect(response.status).to eq(200)+ end+ endenddiff --git a/plugins/discourse-solved/test/javascripts/acceptance/solved-move-post-test.js b/plugins/discourse-solved/test/javascripts/acceptance/solved-move-post-test.jsnew file mode 100644index 00000000000..7b80809694f--- /dev/null+++ b/plugins/discourse-solved/test/javascripts/acceptance/solved-move-post-test.js@@ -0,0 +1,132 @@+import { click, visit } from "@ember/test-helpers";+import { test } from "qunit";+import { acceptance } from "discourse/tests/helpers/qunit-helpers";+import { i18n } from "discourse-i18n";+import { postStreamWithAcceptedAnswerExcerpt } from "../helpers/discourse-solved-helpers";++acceptance("Discourse Solved | Move Solution Post", function (needs) {+ needs.user({ admin: true });++ needs.settings({+ solved_enabled: true,+ allow_solved_on_all_topics: true,+ });++ needs.pretender((server, helper) => {+ server.get("/t/12.json", () =>+ helper.response(postStreamWithAcceptedAnswerExcerpt(null))+ );+ });++ needs.hooks.beforeEach(() => {+ localStorage.removeItem("discourse-solved-hide-move-confirmation");+ });++ needs.hooks.afterEach(() => {+ localStorage.removeItem("discourse-solved-hide-move-confirmation");+ });++ test("shows confirmation modal when moving a solved post", async function (assert) {+ await visit("/t/without-excerpt/12");++ await click(".topic-admin-menu-trigger");+ await click(".topic-admin-menu-content .topic-admin-multi-select button");+ await click("#post_2 .select-post");+ await click(".selected-posts .move-to-topic");++ assert+ .dom(".move-solution-confirmation-modal")+ .exists("Confirmation modal is shown");++ assert+ .dom(".move-solution-confirmation-modal .d-modal__title")+ .hasText(+ i18n("solved.confirm_move_solution_title"),+ "Modal has correct title"+ );++ assert+ .dom(".move-solution-confirmation-modal .d-modal__footer .btn-primary")+ .exists("Confirm button exists");++ assert+ .dom(+ ".move-solution-confirmation-modal .d-modal__footer .btn-transparent"+ )+ .exists("Cancel button exists");+ });++ test("canceling the confirmation modal does not open move-to-topic modal", async function (assert) {+ await visit("/t/without-excerpt/12");++ await click(".topic-admin-menu-trigger");+ await click(".topic-admin-menu-content .topic-admin-multi-select button");+ await click("#post_2 .select-post");+ await click(".selected-posts .move-to-topic");++ assert+ .dom(".move-solution-confirmation-modal")+ .exists("Confirmation modal is shown");++ await click(+ ".move-solution-confirmation-modal .d-modal__footer .btn-transparent"+ );++ assert+ .dom(".move-solution-confirmation-modal")+ .doesNotExist("Confirmation modal is closed");++ assert+ .dom("#choosing-topic")+ .doesNotExist("Move-to-topic modal is not shown");+ });++ test("does not show confirmation modal for non-solved posts", async function (assert) {+ await visit("/t/without-excerpt/12");++ await click(".topic-admin-menu-trigger");+ await click(".topic-admin-menu-content .topic-admin-multi-select button");+ await click("#post_1 .select-post");+ await click(".selected-posts .move-to-topic");++ assert+ .dom(".move-solution-confirmation-modal")+ .doesNotExist("Confirmation modal is not shown");++ assert+ .dom("#choosing-topic")+ .exists("Move-to-topic modal is shown directly");+ });++ test("respects 'don't show again' preference", async function (assert) {+ await visit("/t/without-excerpt/12");++ await click(".topic-admin-menu-trigger");+ await click(".topic-admin-menu-content .topic-admin-multi-select button");+ await click("#post_2 .select-post");+ await click(".selected-posts .move-to-topic");++ assert+ .dom(".move-solution-confirmation-modal")+ .exists("Confirmation modal is shown");++ await click(".move-solution-dont-show-again input");+ await click(+ ".move-solution-confirmation-modal .d-modal__footer .btn-primary"+ );++ assert.dom("#choosing-topic").exists("Move-to-topic modal is shown");++ await click("#choosing-topic .modal-close");++ await click(".selected-posts .move-to-topic");++ assert+ .dom(".move-solution-confirmation-modal")+ .doesNotExist("Confirmation modal is not shown second time");++ assert+ .dom("#choosing-topic")+ .exists("Move-to-topic modal is shown directly");+ });+});