Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/Http/Controllers/PublicUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ public function __invoke($userName)
$data = [
'items_created' => $user->items()->visibleForCurrentUser()->count(),
'comments_created' => $user->comments()
->public()
->whereHas('item', fn ($q) => $q->visibleForCurrentUser())
->count(),
'votes_created' => $user->votes()
->whereHasMorph('model', [Item::class, Comment::class], function ($query, $type) {
if ($type === Item::class) {
$query->visibleForCurrentUser();
} elseif ($type === Comment::class) {
$query->whereHas('item', fn ($q) => $q->visibleForCurrentUser());
$query->public()
->whereHas('item', fn ($q) => $q->visibleForCurrentUser());
}
})
->count(),
Expand Down Expand Up @@ -57,6 +59,7 @@ private function getRecentActivities(User $user): Collection
});

$comments = $user->comments()
->public()
->whereHas('item', fn ($query) => $query->visibleForCurrentUser())
->with(['item' => fn ($q) => $q->with('project')])
->latest()
Expand All @@ -79,7 +82,8 @@ private function getRecentActivities(User $user): Collection
if ($type === Item::class) {
$query->visibleForCurrentUser();
} elseif ($type === Comment::class) {
$query->whereHas('item', fn ($q) => $q->visibleForCurrentUser());
$query->public()
->whereHas('item', fn ($q) => $q->visibleForCurrentUser());
}
})
->with(['model' => function ($query) {
Expand Down
43 changes: 43 additions & 0 deletions tests/Feature/Controllers/PublicUserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@
->assertDontSee('Private');
});

test('hides private comments on public items', function () {
$viewer = User::factory()->create();
$author = User::factory()->create();
$item = Item::factory()->create(['private' => false]);

Comment::factory()->create([
'user_id' => $author->id,
'item_id' => $item->id,
'content' => 'Private profile note',
'private' => true,
]);

actingAs($viewer)
->get(route('public-user', $author->username))
->assertDontSee('Private profile note');
});

test('hides comments on items in private projects', function () {
$viewer = User::factory()->create();
$author = User::factory()->create();
Expand Down Expand Up @@ -131,6 +148,27 @@
->assertDontSee('Hidden Comment');
});

test('hides votes on private comments', function () {
$viewer = User::factory()->create();
$voter = User::factory()->create();
$item = Item::factory()->create(['title' => 'Public Item', 'private' => false]);
$comment = Comment::factory()->create([
'item_id' => $item->id,
'content' => 'Private voted note',
'private' => true,
]);

Vote::factory()->create([
'user_id' => $voter->id,
'model_type' => Comment::class,
'model_id' => $comment->id,
]);

actingAs($viewer)
->get(route('public-user', $voter->username))
->assertDontSee('Private voted note');
});

test('counts only visible items', function () {
$viewer = User::factory()->create();
$author = User::factory()->create();
Expand All @@ -151,6 +189,11 @@
$privateItem = Item::factory()->create(['private' => true]);

Comment::factory()->count(2)->create(['user_id' => $author->id, 'item_id' => $publicItem->id]);
Comment::factory()->create([
'user_id' => $author->id,
'item_id' => $publicItem->id,
'private' => true,
]);
Comment::factory()->create(['user_id' => $author->id, 'item_id' => $privateItem->id]);

$response = actingAs($viewer)->get(route('public-user', $author->username));
Expand Down
Loading