]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/PushSubscription.php
New parameter to create a share block for display reasons
[friendica.git] / src / Worker / PushSubscription.php
index f3c3a4046faa687771d7c2efd1646ef7dfe257c1..17b47f1cbd8388dc512e1846e6868e518890e1b4 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 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\Factory\Api\Mastodon\Notification as NotificationFactory;
 use Friendica\Model\Contact;
+use Friendica\Model\Post;
 use Friendica\Model\Subscription as ModelSubscription;
-use Friendica\Util\DateTimeFormat;
+use Friendica\Model\User;
+use Friendica\Network\HTTPException\NotFoundException;
 use Minishlink\WebPush\WebPush;
 use Minishlink\WebPush\Subscription;
 
 class PushSubscription
 {
+       /**
+        * Creates push subscription by subscription and notification ids
+        *
+        * @param int $sid Subscription id
+        * @param int $nid Notification id
+        * @return void
+        */
        public static function execute(int $sid, int $nid)
        {
+               Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]);
+
                $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
-               $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
+               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']);
 
-               if (!empty($notification['uri-id'])) {
-                       $notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]);
+               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(),
                        ],
                ];
 
-               $webPush = new WebPush($auth);
+               $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();
 
                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()]);
                }
        }
 }