Skip to content

Commit f445951

Browse files
authored
fix: match link statistics to commits visible to the viewer (#1416)
1 parent 6020ace commit f445951

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

‎app/Http/Controllers/LinkController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function index(ListLinksRequest $request, #[CurrentUser] User $user, Link
3636
return Inertia::render('links/index', [
3737
'url' => $url,
3838
'link' => $link,
39-
'stats' => $link === null ? ['commits' => 0, 'people' => 0] : $commits->stats($link),
39+
'stats' => $link === null ? ['commits' => 0, 'people' => 0] : $commits->stats($link, $user),
4040
'commits' => $link === null ? null : Inertia::scroll(fn (): CursorPaginator => ShownStreams::page($commits->builder($link, $user)->cursorPaginate(20))),
4141
]);
4242
}

‎app/Queries/LinkCommitsQuery.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ public function totalStarCounts(array $linkIds): array
128128
/**
129129
* @return array{commits: int, people: int}
130130
*/
131-
public function stats(LinkPreview $link): array
131+
public function stats(LinkPreview $link, User $user): array
132132
{
133-
$commits = Commit::query()->where('link_preview_id', $link->id)->approved();
133+
$commits = Commit::query()->where('link_preview_id', $link->id)->visibleTo($user);
134134

135135
return [
136136
'commits' => $commits->count(),

‎tests/Feature/Controllers/LinkControllerTest.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,17 @@
159159
$blocked = User::factory()->create();
160160
$viewer->blocking()->attach($blocked);
161161
$link = LinkPreview::factory()->create();
162-
$visible = Commit::factory()->create(['link_preview_id' => $link->id]);
163-
Commit::factory()->for($blocked)->create(['link_preview_id' => $link->id]);
162+
$visibleAuthor = User::factory()->create();
163+
$visible = Commit::factory()->for($visibleAuthor)->create(['link_preview_id' => $link->id]);
164+
$alsoVisible = Commit::factory()->for($visibleAuthor)->create(['link_preview_id' => $link->id]);
165+
Commit::factory()->count(2)->for($blocked)->create(['link_preview_id' => $link->id]);
164166
Commit::factory()->pending()->create(['link_preview_id' => $link->id]);
165167
$own = Commit::factory()->pending()->for($viewer)->create(['link_preview_id' => $link->id]);
166168

167169
$this->actingAs($viewer)->get(route('link.index', ['url' => $link->url]))->assertOk()->assertInertia(fn ($page) => $page
168-
->has('commits.data', 2)
169-
->where('commits.data', fn ($commits): bool => collect($commits)->pluck('id')->sort()->values()->all() === collect([$visible->id, $own->id])->sort()->values()->all()));
170+
->where('stats', ['commits' => 3, 'people' => 2])
171+
->has('commits.data', 3)
172+
->where('commits.data', fn ($commits): bool => collect($commits)->pluck('id')->sort()->values()->all() === collect([$visible->id, $alsoVisible->id, $own->id])->sort()->values()->all()));
170173
});
171174

172175
it('finds the link of a bare domain like its card does', function (): void {

0 commit comments

Comments
 (0)