Skip to content

Commit 37003f9

Browse files
authored
FIX: Exclude deleted topics and posts as solution in user summary (#358)
Follow up to #352 The user summary solutions count is more than it should be because of deleted topics and posts.
1 parent e2a0941 commit 37003f9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/discourse_solved/user_summary_extension.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ module DiscourseSolved::UserSummaryExtension
55

66
def solved_count
77
DiscourseSolved::SolvedTopic
8-
.joins("JOIN posts ON posts.id = discourse_solved_solved_topics.answer_post_id")
9-
.where(posts: { user_id: @user.id })
8+
.joins(answer_post: :user, topic: {})
9+
.where(posts: { user_id: @user.id, deleted_at: nil })
10+
.where(topics: { archetype: Archetype.default, deleted_at: nil })
1011
.count
1112
end
1213
end

spec/models/user_summary_spec.rb

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# frozen_string_literal: true
22

33
describe UserSummary do
4+
fab!(:admin)
5+
46
describe "solved_count" do
57
it "indicates the number of times a user's post is a topic's solution" do
68
topic = Fabricate(:topic)
79
Fabricate(:post, topic:)
810
user = Fabricate(:user)
9-
admin = Fabricate(:admin)
1011
post = Fabricate(:post, topic:, user:)
1112

1213
user_summary = UserSummary.new(user, Guardian.new)
@@ -20,5 +21,33 @@
2021
expect(user_summary.solved_count).to eq(1)
2122
expect(admin_summary.solved_count).to eq(0)
2223
end
24+
25+
it "excludes deleted topics" do
26+
topic = Fabricate(:topic)
27+
Fabricate(:post, topic:)
28+
user = Fabricate(:user)
29+
post = Fabricate(:post, topic:, user:)
30+
31+
user_summary = UserSummary.new(user, Guardian.new)
32+
DiscourseSolved.accept_answer!(post, admin)
33+
34+
topic.update!(deleted_at: Time.zone.now)
35+
36+
expect(user_summary.solved_count).to eq(0)
37+
end
38+
39+
it "excludes deleted posts" do
40+
topic = Fabricate(:topic)
41+
Fabricate(:post, topic:)
42+
user = Fabricate(:user)
43+
post = Fabricate(:post, topic:, user:)
44+
45+
user_summary = UserSummary.new(user, Guardian.new)
46+
DiscourseSolved.accept_answer!(post, admin)
47+
48+
post.update!(deleted_at: Time.zone.now)
49+
50+
expect(user_summary.solved_count).to eq(0)
51+
end
2352
end
2453
end

0 commit comments

Comments
 (0)