diff --git a/app/Models/User.php b/app/Models/User.php index 9b24ec338..6a6f8d882 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -259,7 +259,10 @@ public function scopeMostSolutions(Builder $query, int $inLastDays = null) { return $query->withCount(['replyAble as solutions_count' => function ($query) use ($inLastDays) { $query->where('replyable_type', 'threads') - ->join('threads', 'threads.solution_reply_id', '=', 'replies.id'); + ->join('threads', function ($join) { + $join->on('threads.solution_reply_id', '=', 'replies.id') + ->on('threads.author_id', '!=', 'replies.author_id'); + }); if ($inLastDays) { $query->where('replies.created_at', '>', now()->subDays($inLastDays)); diff --git a/resources/views/forum/overview.blade.php b/resources/views/forum/overview.blade.php index ab4262aa6..130142265 100644 --- a/resources/views/forum/overview.blade.php +++ b/resources/views/forum/overview.blade.php @@ -155,7 +155,7 @@

- Solutions given in the past year. + Solutions given in the past year. Excluding solutions from thread authors.

diff --git a/tests/Integration/Models/UserTest.php b/tests/Integration/Models/UserTest.php index b787d7c24..fb39dd545 100644 --- a/tests/Integration/Models/UserTest.php +++ b/tests/Integration/Models/UserTest.php @@ -1,5 +1,6 @@ countArticles())->toBe(1); }); +it('excludes author solutions from mostSolutions count', function () { + $user = $this->login(); + $thread = Thread::factory()->create([ + 'author_id' => $user->id(), + ]); + $reply = Reply::factory()->create([ + 'author_id' => $user->id(), + ]); + + $this->dispatch(new MarkThreadSolution($thread, $reply, $user)); + expect($user->mostSolutions()->find($user->id())->solutions_count)->toBe(0); + + $otherThread = Thread::factory()->create(); + + $this->dispatch(new MarkThreadSolution($otherThread, $reply, $user)); + expect($user->mostSolutions()->find($user->id())->solutions_count)->toBe(1); +}); + // Helpers function createTwoSolutionReplies(User $user) {