]> git.mxchange.org Git - friendica.git/commitdiff
The push api payload content is improved
authorMichael <heluecht@pirati.ca>
Tue, 17 Aug 2021 22:53:52 +0000 (22:53 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 17 Aug 2021 22:53:52 +0000 (22:53 +0000)
src/Worker/PushSubscription.php

index f740410e03127399b45682a9a5afc7209564fe51..60f559bc97a297c2e3ca333fdc9be255f4c09432 100644 (file)
 
 namespace Friendica\Worker;
 
 
 namespace Friendica\Worker;
 
+use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\Plaintext;
 use Friendica\Core\Logger;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Core\Logger;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
+use Friendica\Model\Notification;
+use Friendica\Model\Post;
 use Friendica\Model\Subscription as ModelSubscription;
 use Friendica\Model\Subscription as ModelSubscription;
-use Friendica\Util\DateTimeFormat;
+use Friendica\Model\User;
 use Minishlink\WebPush\WebPush;
 use Minishlink\WebPush\Subscription;
 
 use Minishlink\WebPush\WebPush;
 use Minishlink\WebPush\Subscription;
 
@@ -48,32 +52,57 @@ class PushSubscription
                        return;
                }
 
                        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']);
+
+               $type = Notification::getType($notification);
+
                if (!empty($notification['actor-id'])) {
                        $actor = Contact::getById($notification['actor-id']);
                }
 
                if (!empty($notification['actor-id'])) {
                        $actor = Contact::getById($notification['actor-id']);
                }
 
-               $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),
-                       ]),
+               $body = '';
+
+               if (!empty($notification['target-uri-id'])) {
+                       $post = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]]);
+                       if (!empty($post['body'])) {
+                               $body = BBCode::toPlaintext($post['body'], false);
+                               $body = Plaintext::shorten($body, 160, $notification['uid']);
+                       }
+               }
+
+               $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' => $type,
+                       'icon'              => $actor['thumb'] ?? '',
+                       'title'             => $l10n->t('Notification from Friendica'), // @todo Replace it with a speaking title
+                       'body'              => $body ?: $l10n->t('Empty Post'),
                ];
 
                ];
 
+               Logger::info('Payload', ['payload' => $payload]);
+
                $auth = [
                        'VAPID' => [
                                'subject'    => DI::baseUrl()->getHostname(),
                $auth = [
                        'VAPID' => [
                                'subject'    => DI::baseUrl()->getHostname(),
@@ -84,10 +113,7 @@ class PushSubscription
 
                $webPush = new WebPush($auth, [], DI::config()->get('system', 'xrd_timeout'));
 
 
                $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();
 
 
                $endpoint = $report->getRequest()->getUri()->__toString();