Repository navigation
Expand file tree
/
Copy pathNotificationController.php
More file actions
50 lines (42 loc) · 1.9 KB
/
Copy pathNotificationController.php
File metadata and controls
50 lines (42 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api;
use App\Actions\UpdateNotificationsReadAt;
use App\Http\Requests\ListNotificationsRequest;
use App\Http\Resources\NotificationResource;
use App\Models\Notification;
use App\Models\User;
use App\Queries\CommitBuilder;
use App\Queries\NotificationGroupsQuery;
use Carbon\CarbonImmutable;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
final readonly class NotificationController
{
/**
* The notifications grouped and filtered by ?type= like the web does it. As on the web, listing them marks every
* notification as read, whatever the filter, and read_at says when, so the ones read since then are the new ones.
* A commit comes with its author and the commit it replies to, when the user may see it.
*/
public function index(ListNotificationsRequest $request, #[CurrentUser] User $user, NotificationGroupsQuery $query, UpdateNotificationsReadAt $action): AnonymousResourceCollection
{
$readAt = $action->handle($user, CarbonImmutable::now());
$type = $request->type($user);
$notifications = $query->paginate($user, $type, $request->through());
/** @var array<int, Notification> $items */
$items = $notifications->items();
new Collection($items)->loadMissing([
'commit.author',
'timeout',
'commit.parent' => fn (BelongsTo $query): BelongsTo => $query
->where(fn (CommitBuilder $query): CommitBuilder => $query->visibleTo($user))
->with('author'),
]);
return NotificationResource::collection($notifications)->additional([
'filters' => ['type' => $type],
'read_at' => $readAt->toIso8601ZuluString(),
]);
}
}