From 5a9db1b3bc68438083c467b28e1a6a56001dc2e6 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 17 Aug 2021 22:53:52 +0000 Subject: [PATCH 1/4] The push api payload content is improved --- src/Worker/PushSubscription.php | 72 ++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/src/Worker/PushSubscription.php b/src/Worker/PushSubscription.php index f740410e03..60f559bc97 100644 --- a/src/Worker/PushSubscription.php +++ b/src/Worker/PushSubscription.php @@ -21,12 +21,16 @@ 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\Model\Notification; +use Friendica\Model\Post; use Friendica\Model\Subscription as ModelSubscription; -use Friendica\Util\DateTimeFormat; +use Friendica\Model\User; use Minishlink\WebPush\WebPush; use Minishlink\WebPush\Subscription; @@ -48,32 +52,57 @@ class PushSubscription 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']); } - $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(), @@ -84,10 +113,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(); From 4ef4fd28d465811321ace64bb5ff3354ea782619 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 17 Aug 2021 22:58:00 +0000 Subject: [PATCH 2/4] Updated messages.po --- view/lang/C/messages.po | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/view/lang/C/messages.po b/view/lang/C/messages.po index c4b37aee96..f7fb3aa405 100644 --- a/view/lang/C/messages.po +++ b/view/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 2021.09-dev\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-08-16 06:14+0000\n" +"POT-Creation-Date: 2021-08-17 22:57+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -10541,6 +10541,14 @@ msgstr "" msgid "(no subject)" msgstr "" +#: src/Worker/PushSubscription.php:100 +msgid "Notification from Friendica" +msgstr "" + +#: src/Worker/PushSubscription.php:101 +msgid "Empty Post" +msgstr "" + #: view/theme/duepuntozero/config.php:52 msgid "default" msgstr "" From 6f6fe7fa25f7c56729440ebf05f1cfb2c635876a Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 18 Aug 2021 10:27:45 +0000 Subject: [PATCH 3/4] Clarify the upcoming functionality --- src/Worker/PushSubscription.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Worker/PushSubscription.php b/src/Worker/PushSubscription.php index 60f559bc97..dcdbe2e1bf 100644 --- a/src/Worker/PushSubscription.php +++ b/src/Worker/PushSubscription.php @@ -82,6 +82,9 @@ class PushSubscription } } + // @todo Add a good title here, see the functionality in enotify.php + $title = ''; + $push = Subscription::create([ 'contentEncoding' => 'aesgcm', 'endpoint' => $subscription['endpoint'], @@ -97,7 +100,7 @@ class PushSubscription 'notification_id' => $nid, 'notification_type' => $type, 'icon' => $actor['thumb'] ?? '', - 'title' => $l10n->t('Notification from Friendica'), // @todo Replace it with a speaking title + 'title' => $title ?: $l10n->t('Notification from Friendica'), 'body' => $body ?: $l10n->t('Empty Post'), ]; From 9bc6b086188d04dbdbfe5e857164a06e5d202524 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 18 Aug 2021 18:54:03 +0200 Subject: [PATCH 4/4] Update src/Worker/PushSubscription.php Co-authored-by: Hypolite Petovan --- src/Worker/PushSubscription.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Worker/PushSubscription.php b/src/Worker/PushSubscription.php index dcdbe2e1bf..25c4047241 100644 --- a/src/Worker/PushSubscription.php +++ b/src/Worker/PushSubscription.php @@ -82,7 +82,7 @@ class PushSubscription } } - // @todo Add a good title here, see the functionality in enotify.php + // @todo Add a meaningful title here, see the functionality in enotify.php $title = ''; $push = Subscription::create([