X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FWorker%2FPushSubscription.php;h=45ecb62291a08a2e7fcc6e39a2045967d40a5fe3;hb=036b565a7846916f763ce1dcbcaade0844ff1589;hp=de385afa1c16d0a497fa72b37c90499526546acb;hpb=2c1b33af87256054cc561aba1f95b3347e483f2d;p=friendica.git diff --git a/src/Worker/PushSubscription.php b/src/Worker/PushSubscription.php index de385afa1c..45ecb62291 100644 --- a/src/Worker/PushSubscription.php +++ b/src/Worker/PushSubscription.php @@ -1,6 +1,6 @@ $sid, 'notification' => $nid]); + $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]); + if (empty($subscription)) { + Logger::info('Subscription not found', ['subscription' => $sid]); + return; + } + + try { + $Notification = DI::notification()->selectOneById($nid); + } catch (NotFoundException $e) { + Logger::info('Notification not found', ['notification' => $nid]); + return; + } + + $application_token = DBA::selectFirst('application-token', [], ['application-id' => $subscription['application-id'], 'uid' => $subscription['uid']]); + if (empty($application_token)) { + Logger::info('Application token not found', ['application' => $subscription['application-id']]); + return; + } + + $user = User::getById($Notification->uid); + if (empty($user)) { + Logger::info('User not found', ['application' => $subscription['uid']]); + return; + } + + $l10n = DI::l10n()->withLang($user['language']); - $notification = [ - 'subscription' => Subscription::create([ - 'endpoint' => $subscription['endpoint'], - 'publicKey' => $subscription['pubkey'], - 'authToken' => $subscription['secret'], - ]), - 'payload' => null, + if ($Notification->actorId) { + $actor = Contact::getById($Notification->actorId); + } + + $body = ''; + + if ($Notification->targetUriId) { + $post = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]]); + if (!empty($post['body'])) { + $body = BBCode::toPlaintext($post['body'], false); + $body = Plaintext::shorten($body, 160, $Notification->uid); + } + } + + $message = DI::notificationFactory()->getMessageFromNotification($Notification); + $title = $message['plain'] ?: ''; + + $push = Subscription::create([ + 'contentEncoding' => 'aesgcm', + 'endpoint' => $subscription['endpoint'], + 'keys' => [ + 'p256dh' => $subscription['pubkey'], + 'auth' => $subscription['secret'] + ], + ]); + + $payload = [ + 'access_token' => $application_token['access_token'], + 'preferred_locale' => $user['language'], + 'notification_id' => $nid, + 'notification_type' => \Friendica\Factory\Api\Mastodon\Notification::getType($Notification), + 'icon' => $actor['thumb'] ?? '', + 'title' => $title ?: $l10n->t('Notification from Friendica'), + 'body' => $body ?: $l10n->t('Empty Post'), ]; + Logger::info('Payload', ['payload' => $payload]); + $auth = [ 'VAPID' => [ 'subject' => DI::baseUrl()->getHostname(), @@ -51,19 +114,16 @@ class PushSubscription ], ]; - $webPush = new WebPush($auth); + $webPush = new WebPush($auth, [], DI::config()->get('system', 'xrd_timeout')); - $report = $webPush->sendOneNotification( - $notification['subscription'], - $notification['payload'] - ); + $report = $webPush->sendOneNotification($push, json_encode($payload), ['urgency' => 'normal']); $endpoint = $report->getRequest()->getUri()->__toString(); if ($report->isSuccess()) { - Logger::info('Message sent successfully for subscription', ['endpoint' => $endpoint]); + Logger::info('Message sent successfully for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint]); } else { - Logger::info('Message failed to sent for subscription', ['endpoint' => $endpoint, 'reason' => $report->getReason()]); + Logger::info('Message failed to sent for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint, 'reason' => $report->getReason()]); } } }