Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 0350b46

Browse files
authored
FIX: Check if user has existing solution despite trust_level=any for first accepted solution validator used in automation (#377)
There exists a bug in the following trigger (see screenshot) where if the user has an existing solution already, they will still pass validation for "first accepted solution" due to the trust level being "any".
1 parent cee0ffc commit 0350b46

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

lib/discourse_solved/first_accepted_post_solution_validator.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ class FirstAcceptedPostSolutionValidator
55
def self.check(post, trust_level:)
66
return false if post.archetype != Archetype.default
77
return false if !post&.user&.human?
8-
return true if trust_level == "any"
98

10-
return false if TrustLevel.compare(post&.user&.trust_level, trust_level.to_i)
11-
12-
if !UserAction.where(user_id: post&.user_id, action_type: UserAction::SOLVED).exists?
13-
return true
9+
if trust_level != "any" && TrustLevel.compare(post&.user&.trust_level, trust_level.to_i)
10+
return false
1411
end
1512

16-
false
13+
!DiscourseSolved::SolvedTopic
14+
.joins(:answer_post)
15+
.where("posts.user_id = ?", post.user_id)
16+
.exists?
1717
end
1818
end
1919
end

spec/lib/first_accepted_post_solution_validator_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "rails_helper"
4-
53
describe DiscourseSolved::FirstAcceptedPostSolutionValidator do
64
fab!(:user_tl1) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) }
75

@@ -52,6 +50,13 @@
5250
post_1 = create_post(user: user_tl1)
5351
expect(described_class.check(post_1, trust_level: "any")).to eq(true)
5452
end
53+
54+
it "invalidates if post user already has an accepted post" do
55+
accepted_post = create_post(user: user_tl1)
56+
DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user)
57+
post_1 = create_post(user: user_tl1)
58+
expect(described_class.check(post_1, trust_level: "any")).to eq(false)
59+
end
5560
end
5661

5762
context "when user is system" do

0 commit comments

Comments
 (0)