Skip to content

Commit cbd69f2

Browse files
authored
FIX: when deleting users with solved posts (#297)
it would raise an error because we delete the topic before and we need the topic record to update various meta data. This ensures we load the "deleted" version in such cases. Internal ref - t/130797
1 parent 057865a commit cbd69f2

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

plugin.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ def self.accept_answer!(post, acting_user, topic: nil)
137137

138138
def self.unaccept_answer!(post, topic: nil)
139139
topic ||= post.topic
140+
topic ||= Topic.unscoped.find_by(id: post.topic_id)
141+
142+
return if topic.nil?
140143

141144
DistributedMutex.synchronize("discourse_solved_toggle_answer_#{topic.id}") do
142145
post.custom_fields.delete(IS_ACCEPTED_ANSWER_CUSTOM_FIELD)

spec/integration/solved_spec.rb

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@
446446

447447
sign_in(user)
448448
end
449+
449450
describe "updating assignment status on solve when assignment_status_on_solve is set" do
450451
it "update all assignments to this status when a post is accepted" do
451452
assigner = Assigner.new(p1.topic, user)
@@ -483,11 +484,11 @@
483484
group.add(user_3)
484485

485486
topic_question = Fabricate(:topic, user: user_1)
486-
post_question = Fabricate(:post, topic: topic_question, user: user_1)
487487

488-
user_2_response = Fabricate(:post, topic: topic_question, user: user_2)
489-
assigner = Assigner.new(topic_question, user_2)
490-
result = assigner.assign(user_2)
488+
Fabricate(:post, topic: topic_question, user: user_1)
489+
Fabricate(:post, topic: topic_question, user: user_2)
490+
491+
result = Assigner.new(topic_question, user_2).assign(user_2)
491492
expect(result[:success]).to eq(true)
492493

493494
post_response = Fabricate(:post, topic: topic_question, user: user_3)
@@ -503,32 +504,50 @@
503504
expect(post_response.assignment.assigned_to_id).to eq(user_3.id)
504505
end
505506

506-
describe "assigned topic reminder"
507-
it "excludes solved topics when ignore_solved_topics_in_assigned_reminder is false" do
508-
other_topic = Fabricate(:topic, title: "Topic that should be there")
509-
post = Fabricate(:post, topic: other_topic, user: user)
507+
describe "assigned topic reminder" do
508+
it "excludes solved topics when ignore_solved_topics_in_assigned_reminder is false" do
509+
other_topic = Fabricate(:topic, title: "Topic that should be there")
510+
post = Fabricate(:post, topic: other_topic, user: user)
510511

511-
other_topic2 = Fabricate(:topic, title: "Topic that should be there2")
512-
post2 = Fabricate(:post, topic: other_topic2, user: user)
512+
other_topic2 = Fabricate(:topic, title: "Topic that should be there2")
513+
post2 = Fabricate(:post, topic: other_topic2, user: user)
513514

514-
Assigner.new(post.topic, user).assign(user)
515-
Assigner.new(post2.topic, user).assign(user)
515+
Assigner.new(post.topic, user).assign(user)
516+
Assigner.new(post2.topic, user).assign(user)
516517

517-
reminder = PendingAssignsReminder.new
518-
topics = reminder.send(:assigned_topics, user, order: :asc)
519-
expect(topics.to_a.length).to eq(2)
518+
reminder = PendingAssignsReminder.new
519+
topics = reminder.send(:assigned_topics, user, order: :asc)
520+
expect(topics.to_a.length).to eq(2)
520521

521-
DiscourseSolved.accept_answer!(post2, Discourse.system_user)
522-
topics = reminder.send(:assigned_topics, user, order: :asc)
523-
expect(topics.to_a.length).to eq(2)
524-
expect(topics).to include(other_topic2)
522+
DiscourseSolved.accept_answer!(post2, Discourse.system_user)
523+
topics = reminder.send(:assigned_topics, user, order: :asc)
524+
expect(topics.to_a.length).to eq(2)
525+
expect(topics).to include(other_topic2)
525526

526-
SiteSetting.ignore_solved_topics_in_assigned_reminder = true
527-
topics = reminder.send(:assigned_topics, user, order: :asc)
528-
expect(topics.to_a.length).to eq(1)
529-
expect(topics).not_to include(other_topic2)
530-
expect(topics).to include(other_topic)
527+
SiteSetting.ignore_solved_topics_in_assigned_reminder = true
528+
topics = reminder.send(:assigned_topics, user, order: :asc)
529+
expect(topics.to_a.length).to eq(1)
530+
expect(topics).not_to include(other_topic2)
531+
expect(topics).to include(other_topic)
532+
end
531533
end
532534
end
533535
end
536+
537+
describe "#unaccept_answer!" do
538+
it "works even when the topic has been deleted" do
539+
user = Fabricate(:user, trust_level: 1)
540+
topic = Fabricate(:topic, user:)
541+
reply = Fabricate(:post, topic:, user:, post_number: 2)
542+
543+
DiscourseSolved.accept_answer!(reply, user)
544+
545+
topic.trash!(Discourse.system_user)
546+
reply.reload
547+
548+
expect(reply.topic).to eq(nil)
549+
550+
expect { DiscourseSolved.unaccept_answer!(reply) }.not_to raise_error
551+
end
552+
end
534553
end

0 commit comments

Comments
 (0)