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,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;
}
}
}