X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FWorker%2FPushSubscription.php;h=17b47f1cbd8388dc512e1846e6868e518890e1b4;hb=b9bb525fe91c176ada2323c2d628291a27594d59;hp=f740410e03127399b45682a9a5afc7209564fe51;hpb=e205bd450e5b22160600f9c9c1a67c0b8accddd9;p=friendica.git diff --git a/src/Worker/PushSubscription.php b/src/Worker/PushSubscription.php index f740410e03..17b47f1cbd 100644 --- a/src/Worker/PushSubscription.php +++ b/src/Worker/PushSubscription.php @@ -1,6 +1,6 @@ $sid, 'notification' => $nid]); @@ -42,41 +54,68 @@ class PushSubscription return; } - $notification = DBA::selectFirst('notification', [], ['id' => $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); + $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' => NotificationFactory::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(), + 'subject' => DI::baseUrl()->getHost(), 'publicKey' => ModelSubscription::getPublicVapidKey(), 'privateKey' => ModelSubscription::getPrivateVapidKey(), ], @@ -84,10 +123,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();