Skip to content

A platform for community discussion. Free, open, simple.

FIX: merge topics with solved answers (#38532)

David Battersbyd73aaabaa028
diff --git a/plugins/discourse-solved/lib/discourse_solved/post_mover_extension.rb b/plugins/discourse-solved/lib/discourse_solved/post_mover_extension.rb
new file mode 100644
index 00000000000..d74f945107e
--- /dev/null
+++ b/plugins/discourse-solved/lib/discourse_solved/post_mover_extension.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module DiscourseSolved
+ module PostMoverExtension
+ private
+
+ # original_topic, post_ids, and user are attr_readers on PostMover
+ def move_posts_to(topic)
+ solved_topic = DiscourseSolved::SolvedTopic.find_by(topic_id: original_topic.id)
+
+ if solved_topic && post_ids.include?(solved_topic.answer_post_id)
+ accepts_answers = Guardian.new(user).allow_accepted_answers?(topic)
+ has_no_solution = !DiscourseSolved::SolvedTopic.exists?(topic_id: topic.id)
+
+ if accepts_answers && has_no_solution
+ solved_topic.update!(topic_id: topic.id)
+ else
+ DiscourseSolved::UnacceptAnswer.call(
+ params: {
+ post_id: solved_topic.answer_post_id,
+ },
+ guardian: Discourse.system_user.guardian,
+ )
+ end
+ end
+
+ super
+ end
+ end
+end
diff --git a/plugins/discourse-solved/lib/discourse_solved/topics_controller_extension.rb b/plugins/discourse-solved/lib/discourse_solved/topics_controller_extension.rb
deleted file mode 100644
index 56a52aeb825..00000000000
--- a/plugins/discourse-solved/lib/discourse_solved/topics_controller_extension.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-# 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)
- if solution_post
- DiscourseSolved::UnacceptAnswer.call(
- params: {
- post_id: solution_post.id,
- },
- guardian: Discourse.system_user.guardian,
- )
- end
- end
- end
- end
-
- super
- end
- end
-end
diff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rb
index c41474a241d..c0badbc8feb 100644
--- a/plugins/discourse-solved/plugin.rb
+++ b/plugins/discourse-solved/plugin.rb
@@ -50,8 +50,9 @@ after_initialize do
::Topic.prepend(DiscourseSolved::TopicExtension)
::Category.prepend(DiscourseSolved::CategoryExtension)
::PostSerializer.prepend(DiscourseSolved::PostSerializerExtension)
+ ::PostMover.prepend(DiscourseSolved::PostMoverExtension)
::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/lib/post_mover_extension_spec.rb b/plugins/discourse-solved/spec/lib/post_mover_extension_spec.rb
new file mode 100644
index 00000000000..aa697e09ebb
--- /dev/null
+++ b/plugins/discourse-solved/spec/lib/post_mover_extension_spec.rb
@@ -0,0 +1,71 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseSolved::PostMoverExtension do
+ fab!(:admin)
+ fab!(:category)
+ fab!(:topic) { Fabricate(:topic_with_op, category: category) }
+ fab!(:reply) { Fabricate(:post, topic: topic) }
+ fab!(:another_reply) { Fabricate(:post, topic: topic) }
+ fab!(:destination_topic) { Fabricate(:topic_with_op, category: category) }
+
+ let!(:post_ids) { topic.posts.pluck(:id) }
+ let!(:solved) { Fabricate(:solved_topic, topic: topic, answer_post: reply) }
+
+ before { SiteSetting.allow_solved_on_all_topics = true }
+
+ context "when moving the solution post" do
+ it "transfers the solution to an existing topic" do
+ topic.move_posts(admin, post_ids, destination_topic_id: destination_topic.id)
+
+ expect(solved.reload.topic_id).to eq(destination_topic.id)
+ end
+
+ it "transfers the solution to a new topic" do
+ topic.move_posts(admin, [reply.id], title: "This is a new topic for the moved post")
+
+ expect(topic.reload.solved).to be_nil
+ expect(solved.reload.topic_id).not_to eq(topic.id)
+ end
+
+ it "does not transfer the solution when destination topic does not allow solved" do
+ SiteSetting.allow_solved_on_all_topics = false
+
+ topic.move_posts(admin, post_ids, destination_topic_id: destination_topic.id)
+
+ expect(topic.reload.solved).to be_nil
+ expect(destination_topic.reload.solved).to be_nil
+ end
+
+ it "does not transfer the solution when the destination topic already has a solution" do
+ Fabricate(:solved_topic, topic: destination_topic, answer_post: destination_topic.first_post)
+
+ topic.move_posts(admin, post_ids, destination_topic_id: destination_topic.id)
+
+ expect(topic.reload.solved).to be_nil
+ expect(destination_topic.reload.solved.answer_post_id).not_to eq(reply.id)
+ end
+ end
+
+ context "when not moving the solution post" do
+ it "keeps the solution on the original topic" do
+ topic.move_posts(admin, [post_ids.first], destination_topic_id: destination_topic.id)
+
+ expect(solved.reload.topic_id).to eq(topic.id)
+ end
+
+ it "keeps the solution when moving to a new topic" do
+ topic.move_posts(admin, [another_reply.id], title: "This is a new topic for the moved post")
+
+ expect(solved.reload.topic_id).to eq(topic.id)
+ end
+
+ it "does not affect the destination topic's solution" do
+ Fabricate(:solved_topic, topic: destination_topic, answer_post: destination_topic.first_post)
+
+ topic.move_posts(admin, [another_reply.id], destination_topic_id: destination_topic.id)
+
+ expect(solved.reload.topic_id).to eq(topic.id)
+ expect(destination_topic.reload.solved.answer_post_id).to eq(destination_topic.first_post.id)
+ end
+ end
+end
diff --git a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb b/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
index 4f8fdbfcb4d..430b00f43b5 100644
--- a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
+++ b/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
@@ -122,67 +122,4 @@ RSpec.describe TopicsController do
expect(response.body).not_to include(schema_json(1))
end
end
-
- 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
- end
end