Skip to content

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

FIX: Delete other posts in a topic shouldn't unanswer the answer post (#36800)

small-lovely-cat2a1e96d326ff
diff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rb
index 4afa70724a2..f7ca97cf7b9 100644
--- a/plugins/discourse-solved/plugin.rb
+++ b/plugins/discourse-solved/plugin.rb
@@ -133,6 +133,7 @@ after_initialize do
topic ||= Topic.unscoped.find_by(id: post.topic_id)
return if topic.nil?
return if topic.solved.nil?
+ return if topic.solved.answer_post_id != post.id
DistributedMutex.synchronize("discourse_solved_toggle_answer_#{topic.id}") do
solved = topic.solved
diff --git a/plugins/discourse-solved/spec/integration/solved_spec.rb b/plugins/discourse-solved/spec/integration/solved_spec.rb
index cde46c129bc..4ae69db0d91 100644
--- a/plugins/discourse-solved/spec/integration/solved_spec.rb
+++ b/plugins/discourse-solved/spec/integration/solved_spec.rb
@@ -357,6 +357,29 @@ RSpec.describe "Managing Posts solved status" do
expect(topic.solved).to be(nil)
end
+ it "does not remove the solution when an unrelated post is deleted" do
+ reply1 = Fabricate(:post, post_number: 2, topic: topic)
+ reply2 = Fabricate(:post, post_number: 3, topic: topic)
+ reply3 = Fabricate(:post, post_number: 4, topic: topic)
+
+ post "/solution/accept.json", params: { id: reply2.id }
+ expect(response.status).to eq(200)
+
+ expect(topic.solved.answer_post_id).to eq(reply2.id)
+
+ PostDestroyer.new(Discourse.system_user, reply1, context: "spec").destroy
+ topic.reload
+
+ expect(topic.solved).to be_present
+ expect(topic.solved.answer_post_id).to eq(reply2.id)
+
+ PostDestroyer.new(Discourse.system_user, reply3, context: "spec").destroy
+ topic.reload
+
+ expect(topic.solved).to be_present
+ expect(topic.solved.answer_post_id).to eq(reply2.id)
+ end
+
it "does not allow you to accept a whisper" do
whisper = Fabricate(:post, topic: topic, post_type: Post.types[:whisper])
sign_in(Fabricate(:admin))