This commit is contained in:
2026-03-20 21:17:26 +01:00
parent 1a62fcb81d
commit 29c3ff8572
229 changed files with 13147 additions and 2577 deletions

View File

@@ -2,7 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Services\Posts\NotificationDigestService;
use App\Services\NotificationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
@@ -14,48 +14,24 @@ use App\Http\Controllers\Controller;
*/
class NotificationController extends Controller
{
public function __construct(private NotificationDigestService $digest) {}
public function __construct(private NotificationService $notifications) {}
public function index(Request $request): JsonResponse
{
$user = $request->user();
$page = max(1, (int) $request->query('page', 1));
$notifications = $user->notifications()
->latest()
->limit(200) // aggregate from last 200 raw notifs
->get();
$digested = $this->digest->aggregate($notifications);
// Simple manual pagination on the digested array
$perPage = 20;
$total = count($digested);
$sliced = array_slice($digested, ($page - 1) * $perPage, $perPage);
$unread = $user->unreadNotifications()->count();
return response()->json([
'data' => array_values($sliced),
'unread_count' => $unread,
'meta' => [
'total' => $total,
'current_page' => $page,
'last_page' => (int) ceil($total / $perPage) ?: 1,
'per_page' => $perPage,
],
]);
return response()->json(
$this->notifications->listForUser($request->user(), (int) $request->query('page', 1), 20)
);
}
public function readAll(Request $request): JsonResponse
{
$request->user()->unreadNotifications()->update(['read_at' => now()]);
$this->notifications->markAllRead($request->user());
return response()->json(['message' => 'All notifications marked as read.']);
}
public function markRead(Request $request, string $id): JsonResponse
{
$notif = $request->user()->notifications()->findOrFail($id);
$notif->markAsRead();
$this->notifications->markRead($request->user(), $id);
return response()->json(['message' => 'Notification marked as read.']);
}
}