From 0350b46bdafc2cb55ab7ff01dead6bdd2cea1f09 Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Mon, 30 Jun 2025 15:40:16 +0800 Subject: [PATCH] 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". --- .../first_accepted_post_solution_validator.rb | 12 ++++++------ .../first_accepted_post_solution_validator_spec.rb | 9 +++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/discourse_solved/first_accepted_post_solution_validator.rb b/lib/discourse_solved/first_accepted_post_solution_validator.rb index 9c1deae..62c8fb6 100644 --- a/lib/discourse_solved/first_accepted_post_solution_validator.rb +++ b/lib/discourse_solved/first_accepted_post_solution_validator.rb @@ -5,15 +5,15 @@ module DiscourseSolved def self.check(post, trust_level:) return false if post.archetype != Archetype.default 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 !UserAction.where(user_id: post&.user_id, action_type: UserAction::SOLVED).exists? - return true + if trust_level != "any" && TrustLevel.compare(post&.user&.trust_level, trust_level.to_i) + return false end - false + !DiscourseSolved::SolvedTopic + .joins(:answer_post) + .where("posts.user_id = ?", post.user_id) + .exists? end end end diff --git a/spec/lib/first_accepted_post_solution_validator_spec.rb b/spec/lib/first_accepted_post_solution_validator_spec.rb index 75139a9..db80a18 100644 --- a/spec/lib/first_accepted_post_solution_validator_spec.rb +++ b/spec/lib/first_accepted_post_solution_validator_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "rails_helper" - describe DiscourseSolved::FirstAcceptedPostSolutionValidator do 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) expect(described_class.check(post_1, trust_level: "any")).to eq(true) 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 context "when user is system" do