diff --git a/app/Http/Controllers/LinkController.php b/app/Http/Controllers/LinkController.php index 9da6d7842..6c784dfd0 100644 --- a/app/Http/Controllers/LinkController.php +++ b/app/Http/Controllers/LinkController.php @@ -36,7 +36,7 @@ public function index(ListLinksRequest $request, #[CurrentUser] User $user, Link return Inertia::render('links/index', [ 'url' => $url, 'link' => $link, - 'stats' => $link === null ? ['commits' => 0, 'people' => 0] : $commits->stats($link), + 'stats' => $link === null ? ['commits' => 0, 'people' => 0] : $commits->stats($link, $user), 'commits' => $link === null ? null : Inertia::scroll(fn (): CursorPaginator => ShownStreams::page($commits->builder($link, $user)->cursorPaginate(20))), ]); } diff --git a/app/Queries/LinkCommitsQuery.php b/app/Queries/LinkCommitsQuery.php index a493adca4..4291fbc91 100644 --- a/app/Queries/LinkCommitsQuery.php +++ b/app/Queries/LinkCommitsQuery.php @@ -124,9 +124,9 @@ public function totalStarCounts(array $linkIds): array /** * @return array{commits: int, people: int} */ - public function stats(LinkPreview $link): array + public function stats(LinkPreview $link, User $user): array { - $commits = Commit::query()->where('link_preview_id', $link->id)->approved(); + $commits = Commit::query()->where('link_preview_id', $link->id)->visibleTo($user); return [ 'commits' => $commits->count(), diff --git a/tests/Feature/Controllers/LinkControllerTest.php b/tests/Feature/Controllers/LinkControllerTest.php index bd220f410..604bd6525 100644 --- a/tests/Feature/Controllers/LinkControllerTest.php +++ b/tests/Feature/Controllers/LinkControllerTest.php @@ -154,14 +154,17 @@ $blocked = User::factory()->create(); $viewer->blocking()->attach($blocked); $link = LinkPreview::factory()->create(); - $visible = Commit::factory()->create(['link_preview_id' => $link->id]); - Commit::factory()->for($blocked)->create(['link_preview_id' => $link->id]); + $visibleAuthor = User::factory()->create(); + $visible = Commit::factory()->for($visibleAuthor)->create(['link_preview_id' => $link->id]); + $alsoVisible = Commit::factory()->for($visibleAuthor)->create(['link_preview_id' => $link->id]); + Commit::factory()->count(2)->for($blocked)->create(['link_preview_id' => $link->id]); Commit::factory()->pending()->create(['link_preview_id' => $link->id]); $own = Commit::factory()->pending()->for($viewer)->create(['link_preview_id' => $link->id]); $this->actingAs($viewer)->get(route('link.index', ['url' => $link->url]))->assertOk()->assertInertia(fn ($page) => $page - ->has('commits.data', 2) - ->where('commits.data', fn ($commits): bool => collect($commits)->pluck('id')->sort()->values()->all() === collect([$visible->id, $own->id])->sort()->values()->all())); + ->where('stats', ['commits' => 3, 'people' => 2]) + ->has('commits.data', 3) + ->where('commits.data', fn ($commits): bool => collect($commits)->pluck('id')->sort()->values()->all() === collect([$visible->id, $alsoVisible->id, $own->id])->sort()->values()->all())); }); it('finds the link of a bare domain like its card does', function (): void {