Loheide-eu-mitarbeiterplatt.../app/Http/Controllers/NotificationController.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

52 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\Str;
/**
* Glocke im Portal-Header: Benachrichtigungen anzeigen und als gelesen markieren.
*/
class NotificationController extends Controller
{
public function index(Request $request): JsonResponse
{
$notifications = $request->user()->notifications()->limit(20)->get();
return response()->json([
'notifications' => $notifications->map(fn (DatabaseNotification $n) => [
'id' => $n->id,
'title' => (string) ($n->data['title'] ?? ''),
'body' => (string) ($n->data['body'] ?? ''),
'icon' => (string) ($n->data['icon'] ?? 'bell'),
'has_url' => filled($n->data['url'] ?? null),
'read' => $n->read_at !== null,
'created_at' => $n->created_at?->toIso8601String(),
]),
'unread' => $request->user()->unreadNotifications()->count(),
]);
}
public function open(Request $request, string $id): RedirectResponse
{
$notification = $request->user()->notifications()->whereKey($id)->firstOrFail();
$notification->markAsRead();
$url = (string) ($notification->data['url'] ?? '');
// Nur Adressen innerhalb des Portals ansteuern.
$local = preg_match('#^/(?![/\\\\])#', $url) || Str::startsWith($url, url('/').'/');
return redirect()->to($local ? $url : '/');
}
public function markAllRead(Request $request): JsonResponse
{
$request->user()->unreadNotifications()->update(['read_at' => now()]);
return response()->json(['unread' => 0]);
}
}