Added Phase‑1 continuation work across deep links, offline moderation queue, and admin push.

resources/js/admin/mobile/lib.
  - Admin push is end‑to‑end: new backend model/migration/service/job + API endpoints, admin runtime config, push‑aware
    service worker, and a settings toggle via useAdminPushSubscription. Notifications now auto‑refresh on push.
  - New PHP/JS tests: admin push API feature test and queue/haptics unit tests
  Added admin-specific PWA icon assets and wired them into the admin manifest, service worker, and admin shell, plus a
  new “Device & permissions” card in mobile Settings with a persistent storage action and translations.
  Details: public/manifest.json, public/admin-sw.js, resources/views/admin.blade.php, new icons in public/; new hook
  resources/js/admin/mobile/hooks/useDevicePermissions.ts, helpers/tests in resources/js/admin/mobile/lib/
  devicePermissions.ts + resources/js/admin/mobile/lib/devicePermissions.test.ts, and Settings UI updates in resources/
  js/admin/mobile/SettingsPage.tsx with copy in resources/js/admin/i18n/locales/en/management.json and resources/js/
admin/i18n/locales/de/management.json.
This commit is contained in:
Codex Agent
2025-12-28 15:00:47 +01:00
parent 4ce409e918
commit b780d82d62
42 changed files with 2258 additions and 121 deletions

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Http\Controllers\Api\Tenant;
use App\Http\Controllers\Controller;
use App\Http\Requests\Tenant\AdminPushSubscriptionDeleteRequest;
use App\Http\Requests\Tenant\AdminPushSubscriptionStoreRequest;
use App\Services\TenantAdminPushSubscriptionService;
use Illuminate\Http\JsonResponse;
class AdminPushSubscriptionController extends Controller
{
public function store(
AdminPushSubscriptionStoreRequest $request,
TenantAdminPushSubscriptionService $subscriptions
): JsonResponse {
$tenant = $request->attributes->get('tenant') ?? $request->user()?->tenant;
if (! $tenant) {
return response()->json([
'error' => [
'code' => 'tenant_context_missing',
'title' => 'Tenant context missing',
'message' => 'Unable to resolve tenant for push subscriptions.',
],
], 403);
}
$payload = array_merge($request->validated(), [
'language' => $request->getPreferredLanguage() ?? $request->headers->get('Accept-Language'),
'user_agent' => (string) $request->userAgent(),
]);
$subscription = $subscriptions->register($tenant, $request->user(), $payload);
return response()->json([
'id' => $subscription->id,
'status' => $subscription->status,
], 201);
}
public function destroy(
AdminPushSubscriptionDeleteRequest $request,
TenantAdminPushSubscriptionService $subscriptions
): JsonResponse {
$tenant = $request->attributes->get('tenant') ?? $request->user()?->tenant;
if (! $tenant) {
return response()->json([
'error' => [
'code' => 'tenant_context_missing',
'title' => 'Tenant context missing',
'message' => 'Unable to resolve tenant for push subscriptions.',
],
], 403);
}
$validated = $request->validated();
$revoked = $subscriptions->revoke($tenant, $validated['endpoint'], $request->user());
return response()->json([
'status' => $revoked ? 'revoked' : 'not_found',
]);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests\Tenant;
use Illuminate\Foundation\Http\FormRequest;
class AdminPushSubscriptionDeleteRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'endpoint' => ['required', 'url', 'max:500'],
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests\Tenant;
use Illuminate\Foundation\Http\FormRequest;
class AdminPushSubscriptionStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'endpoint' => ['required', 'url', 'max:500'],
'keys.p256dh' => ['required', 'string', 'max:255'],
'keys.auth' => ['required', 'string', 'max:255'],
'expiration_time' => ['nullable'],
'content_encoding' => ['nullable', 'string', 'max:32'],
'device_id' => ['nullable', 'string', 'max:120'],
];
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace App\Jobs;
use App\Models\Event;
use App\Models\TenantAdminPushSubscription;
use App\Models\TenantNotificationLog;
use App\Services\Push\AdminWebPushDispatcher;
use App\Services\TenantAdminPushSubscriptionService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class SendTenantAdminPushNotification implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public function __construct(public int $notificationLogId)
{
$this->onQueue('notifications');
}
/**
* Execute the job.
*/
public function handle(
AdminWebPushDispatcher $dispatcher,
TenantAdminPushSubscriptionService $subscriptions
): void {
if (! config('push.enabled')) {
return;
}
$notification = TenantNotificationLog::query()->find($this->notificationLogId);
if (! $notification) {
return;
}
$targets = TenantAdminPushSubscription::query()
->where('tenant_id', $notification->tenant_id)
->where('status', 'active')
->get();
if ($targets->isEmpty()) {
return;
}
$context = $notification->context ?? [];
$eventId = Arr::get($context, 'event_id') ?? Arr::get($context, 'eventId');
$event = $eventId ? Event::query()->select(['id', 'name', 'slug'])->find($eventId) : null;
$eventName = $this->resolveEventName($event?->name);
$title = $eventName
? sprintf('%s · %s', $eventName, Str::headline(str_replace('_', ' ', $notification->type)))
: Str::headline(str_replace('_', ' ', $notification->type));
$payload = [
'title' => $title,
'body' => $notification->status === 'failed'
? ($notification->failure_reason ?: 'Benachrichtigung fehlgeschlagen')
: 'Neue Admin-Benachrichtigung',
'data' => [
'notification_id' => $notification->id,
'event_id' => $event?->id,
'url' => "/event-admin/mobile/notifications/{$notification->id}",
],
];
foreach ($targets as $target) {
try {
$report = $dispatcher->send($target, $payload);
if ($report === null) {
continue;
}
if ($report->isSuccess()) {
$subscriptions->markDelivered($target);
continue;
}
if ($report->isSubscriptionExpired()) {
$target->update(['status' => 'revoked']);
}
$subscriptions->markFailed($target, $report->getReason());
} catch (\Throwable $exception) {
Log::channel('notifications')->warning('Admin web push delivery failed', [
'subscription_id' => $target->id,
'tenant_id' => $notification->tenant_id,
'reason' => $exception->getMessage(),
]);
$subscriptions->markFailed($target, $exception->getMessage());
}
}
}
private function resolveEventName(mixed $name): ?string
{
if (is_string($name)) {
return $name;
}
if (is_array($name)) {
foreach ($name as $value) {
if (is_string($value) && $value !== '') {
return $value;
}
}
}
return null;
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class TenantAdminPushSubscription extends Model
{
/** @use HasFactory<\Database\Factories\TenantAdminPushSubscriptionFactory> */
use HasFactory;
protected $fillable = [
'tenant_id',
'user_id',
'device_id',
'endpoint',
'endpoint_hash',
'public_key',
'auth_token',
'content_encoding',
'status',
'expires_at',
'last_seen_at',
'last_notified_at',
'last_failed_at',
'failure_count',
'language',
'user_agent',
'meta',
];
protected $casts = [
'expires_at' => 'datetime',
'last_seen_at' => 'datetime',
'last_notified_at' => 'datetime',
'last_failed_at' => 'datetime',
'meta' => 'array',
];
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Services\Packages;
use App\Jobs\SendTenantAdminPushNotification;
use App\Models\Tenant;
use App\Models\TenantNotificationLog;
use Illuminate\Support\Arr;
@@ -32,6 +33,8 @@ class TenantNotificationLogger
'recipient' => $log->recipient,
]);
SendTenantAdminPushNotification::dispatch($log->id);
return $log;
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace App\Services\Push;
use App\Models\TenantAdminPushSubscription;
use Illuminate\Support\Facades\Log;
use Minishlink\WebPush\MessageSentReport;
use Minishlink\WebPush\Subscription as WebPushSubscription;
use Minishlink\WebPush\WebPush;
class AdminWebPushDispatcher
{
private ?WebPush $client = null;
public function send(TenantAdminPushSubscription $subscription, array $payload): ?MessageSentReport
{
if (! config('push.enabled')) {
return null;
}
$client = $this->client ??= $this->buildClient();
if (! $client) {
return null;
}
try {
$body = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
Log::channel('notifications')->warning('Unable to encode admin push payload', [
'reason' => $exception->getMessage(),
]);
$body = '{}';
}
try {
return $client->sendOneNotification(
WebPushSubscription::create([
'endpoint' => $subscription->endpoint,
'publicKey' => $subscription->public_key,
'authToken' => $subscription->auth_token,
'contentEncoding' => $subscription->content_encoding ?? 'aes128gcm',
]),
$body
);
} catch (\Throwable $exception) {
Log::channel('notifications')->warning('Admin web push transport error', [
'tenant_id' => $subscription->tenant_id,
'subscription_id' => $subscription->id,
'reason' => $exception->getMessage(),
]);
return null;
}
}
private function buildClient(): ?WebPush
{
$vapid = config('push.vapid', []);
if (empty($vapid['public_key']) || empty($vapid['private_key'])) {
Log::channel('notifications')->warning('Admin web push skipped because VAPID keys are missing.');
return null;
}
$client = new WebPush([
'VAPID' => [
'subject' => $vapid['subject'] ?? config('app.url'),
'publicKey' => $vapid['public_key'],
'privateKey' => $vapid['private_key'],
],
]);
$client->setDefaultOptions([
'TTL' => (int) config('push.ttl', 900),
]);
return $client;
}
}

View File

@@ -0,0 +1,153 @@
<?php
namespace App\Services;
use App\Models\Tenant;
use App\Models\TenantAdminPushSubscription;
use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
class TenantAdminPushSubscriptionService
{
public function register(Tenant $tenant, ?User $user, array $payload): TenantAdminPushSubscription
{
$endpoint = (string) ($payload['endpoint'] ?? '');
if ($endpoint === '') {
throw new \InvalidArgumentException('Push endpoint missing.');
}
$keys = Arr::get($payload, 'keys', []);
$publicKey = (string) ($keys['p256dh'] ?? '');
$authToken = (string) ($keys['auth'] ?? '');
if ($publicKey === '' || $authToken === '') {
throw new \InvalidArgumentException('Push key material missing.');
}
$deviceId = $this->sanitizeIdentifier(Arr::get($payload, 'device_id'));
$contentEncoding = (string) ($payload['content_encoding'] ?? Arr::get($payload, 'encoding', 'aes128gcm'));
$language = (string) ($payload['language'] ?? null);
$userAgent = (string) ($payload['user_agent'] ?? null);
$expiresAt = $this->normalizeExpiration(Arr::get($payload, 'expiration_time'));
$endpointHash = hash('sha256', $endpoint);
$data = [
'tenant_id' => $tenant->id,
'user_id' => $user?->id,
'device_id' => $deviceId,
'public_key' => $publicKey,
'auth_token' => $authToken,
'content_encoding' => $contentEncoding ?: 'aes128gcm',
'status' => 'active',
'expires_at' => $expiresAt,
'last_seen_at' => now(),
'language' => $language !== '' ? substr($language, 0, 12) : null,
'user_agent' => $userAgent !== '' ? substr($userAgent, 0, 255) : null,
'failure_count' => 0,
];
$subscription = TenantAdminPushSubscription::query()
->where('endpoint_hash', $endpointHash)
->first();
if ($subscription) {
$subscription->fill($data);
$subscription->status = 'active';
$subscription->endpoint = $endpoint;
$subscription->save();
return $subscription;
}
return TenantAdminPushSubscription::create(array_merge($data, [
'endpoint' => $endpoint,
'endpoint_hash' => $endpointHash,
]));
}
public function revoke(Tenant $tenant, string $endpoint, ?User $user = null): bool
{
$hash = hash('sha256', (string) $endpoint);
$subscription = TenantAdminPushSubscription::query()
->where('tenant_id', $tenant->id)
->when($user, fn ($query) => $query->where('user_id', $user->id))
->where(function ($query) use ($hash, $endpoint) {
$query->where('endpoint_hash', $hash)
->orWhere('endpoint', $endpoint);
})
->first();
if (! $subscription) {
return false;
}
$subscription->update([
'status' => 'revoked',
'last_failed_at' => now(),
]);
return true;
}
public function markFailed(TenantAdminPushSubscription $subscription, ?string $message = null): void
{
$subscription->fill([
'last_failed_at' => now(),
'failure_count' => min(65535, $subscription->failure_count + 1),
]);
if ($subscription->failure_count >= 3) {
$subscription->status = 'revoked';
}
$meta = $subscription->meta ?? [];
if ($message) {
$meta['last_error'] = substr($message, 0, 255);
}
$subscription->meta = $meta;
$subscription->save();
}
public function markDelivered(TenantAdminPushSubscription $subscription): void
{
$subscription->fill([
'last_notified_at' => now(),
'last_failed_at' => null,
'failure_count' => 0,
])->save();
}
private function sanitizeIdentifier(?string $value): ?string
{
if ($value === null) {
return null;
}
$sanitized = preg_replace('/[^A-Za-z0-9 _\-]/', '', $value) ?? '';
$sanitized = trim(mb_substr($sanitized, 0, 120));
return $sanitized === '' ? null : $sanitized;
}
private function normalizeExpiration(mixed $value): ?CarbonImmutable
{
if ($value === null || $value === '') {
return null;
}
if (is_numeric($value)) {
$seconds = (int) round(((float) $value) / 1000);
return CarbonImmutable::createFromTimestampUTC($seconds);
}
try {
return CarbonImmutable::parse((string) $value);
} catch (\Throwable) {
return null;
}
}
}