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".
This commit is contained in:
Natalie Tay 2025-06-30 15:40:16 +08:00 committed by GitHub
parent cee0ffc199
commit 0350b46bda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 8 deletions

View File

@ -5,15 +5,15 @@ module DiscourseSolved
def self.check(post, trust_level:) def self.check(post, trust_level:)
return false if post.archetype != Archetype.default return false if post.archetype != Archetype.default
return false if !post&.user&.human? return false if !post&.user&.human?
return true if trust_level == "any"
return false if TrustLevel.compare(post&.user&.trust_level, trust_level.to_i) if trust_level != "any" && TrustLevel.compare(post&.user&.trust_level, trust_level.to_i)
return false
if !UserAction.where(user_id: post&.user_id, action_type: UserAction::SOLVED).exists?
return true
end end
false !DiscourseSolved::SolvedTopic
.joins(:answer_post)
.where("posts.user_id = ?", post.user_id)
.exists?
end end
end end
end end

View File

@ -1,7 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
require "rails_helper"
describe DiscourseSolved::FirstAcceptedPostSolutionValidator do describe DiscourseSolved::FirstAcceptedPostSolutionValidator do
fab!(:user_tl1) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) } fab!(:user_tl1) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) }
@ -52,6 +50,13 @@ describe DiscourseSolved::FirstAcceptedPostSolutionValidator do
post_1 = create_post(user: user_tl1) post_1 = create_post(user: user_tl1)
expect(described_class.check(post_1, trust_level: "any")).to eq(true) expect(described_class.check(post_1, trust_level: "any")).to eq(true)
end end
it "invalidates if post user already has an accepted post" do
accepted_post = create_post(user: user_tl1)
DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user)
post_1 = create_post(user: user_tl1)
expect(described_class.check(post_1, trust_level: "any")).to eq(false)
end
end end
context "when user is system" do context "when user is system" do