X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FWorker%2FPushSubscription.php;h=7b9f5acbd9adfeb82553b81e8ccf4e78be959648;hb=69d7391f90a45f166ffff6379701d9c58b56c14d;hp=f740410e03127399b45682a9a5afc7209564fe51;hpb=7158b35f58a3269e57350c2cde394921de2109cc;p=friendica.git diff --git a/src/Worker/PushSubscription.php b/src/Worker/PushSubscription.php index f740410e03..7b9f5acbd9 100644 --- a/src/Worker/PushSubscription.php +++ b/src/Worker/PushSubscription.php @@ -1,6 +1,6 @@ $nid]); - if (empty($notification)) { + try { + $Notification = DI::notification()->selectOneById($nid); + } catch (NotFoundException $e) { Logger::info('Notification not found', ['notification' => $nid]); return; } - if (!empty($notification['uri-id'])) { - $notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]); + $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']); + + if ($Notification->actorId) { + $actor = Contact::getById($Notification->actorId); } - if (!empty($notification['actor-id'])) { - $actor = Contact::getById($notification['actor-id']); + $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); + } } - $push = [ - 'subscription' => Subscription::create([ - 'endpoint' => $subscription['endpoint'], - 'publicKey' => $subscription['pubkey'], - 'authToken' => $subscription['secret'], - ]), - // @todo Check if we are supposed to transmit a payload at all - 'payload' => json_encode([ - 'title' => 'Friendica', - 'body' => $notify['msg'] ?? '', - 'icon' => $actor['thumb'] ?? '', - 'image' => '', - 'badge' => DI::baseUrl()->get() . '/images/friendica-192.png', - 'tag' => $notification['parent-uri-id'] ?? '', - 'timestamp' => DateTimeFormat::utc($notification['created'], DateTimeFormat::JSON), - ]), + $message = DI::notificationFactory()->getMessageFromNotification($Notification, DI::baseUrl(), $l10n); + $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(), @@ -84,10 +116,7 @@ class PushSubscription $webPush = new WebPush($auth, [], DI::config()->get('system', 'xrd_timeout')); - $report = $webPush->sendOneNotification( - $push['subscription'], - $push['payload'] - ); + $report = $webPush->sendOneNotification($push, json_encode($payload), ['urgency' => 'normal']); $endpoint = $report->getRequest()->getUri()->__toString();