Add guest push notifications and queue alerts
This commit is contained in:
82
app/Services/Push/WebPushDispatcher.php
Normal file
82
app/Services/Push/WebPushDispatcher.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Push;
|
||||
|
||||
use App\Models\PushSubscription;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Minishlink\WebPush\MessageSentReport;
|
||||
use Minishlink\WebPush\Subscription as WebPushSubscription;
|
||||
use Minishlink\WebPush\WebPush;
|
||||
|
||||
class WebPushDispatcher
|
||||
{
|
||||
private ?WebPush $client = null;
|
||||
|
||||
public function send(PushSubscription $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 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('Web push transport error', [
|
||||
'event_id' => $subscription->event_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('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;
|
||||
}
|
||||
}
|
||||
153
app/Services/PushSubscriptionService.php
Normal file
153
app/Services/PushSubscriptionService.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\PushSubscription;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class PushSubscriptionService
|
||||
{
|
||||
public function register(Event $event, string $guestIdentifier, string $deviceId, array $payload): PushSubscription
|
||||
{
|
||||
$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.');
|
||||
}
|
||||
|
||||
$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' => $event->tenant_id,
|
||||
'event_id' => $event->getKey(),
|
||||
'guest_identifier' => $this->sanitizeIdentifier($guestIdentifier),
|
||||
'device_id' => $this->sanitizeIdentifier($deviceId) ?? 'anonymous',
|
||||
'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,
|
||||
];
|
||||
|
||||
/** @var PushSubscription $subscription */
|
||||
$subscription = PushSubscription::query()
|
||||
->where('endpoint_hash', $endpointHash)
|
||||
->first();
|
||||
|
||||
if ($subscription) {
|
||||
$subscription->fill($data);
|
||||
$subscription->status = 'active';
|
||||
$subscription->endpoint = $endpoint;
|
||||
$subscription->save();
|
||||
|
||||
return $subscription;
|
||||
}
|
||||
|
||||
return PushSubscription::create(array_merge($data, [
|
||||
'endpoint' => $endpoint,
|
||||
'endpoint_hash' => $endpointHash,
|
||||
]));
|
||||
}
|
||||
|
||||
public function revoke(Event $event, string $endpoint): bool
|
||||
{
|
||||
$hash = hash('sha256', (string) $endpoint);
|
||||
|
||||
$subscription = PushSubscription::query()
|
||||
->where('event_id', $event->getKey())
|
||||
->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(PushSubscription $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(PushSubscription $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)) {
|
||||
// Push API reports milliseconds
|
||||
$seconds = (int) round(((float) $value) / 1000);
|
||||
|
||||
return CarbonImmutable::createFromTimestampUTC($seconds);
|
||||
}
|
||||
|
||||
try {
|
||||
return CarbonImmutable::parse((string) $value);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user