Merge pull request #10613 from annando/improved-payload

The push api payload content is improved
This commit is contained in:
Hypolite Petovan 2021-08-18 13:45:18 -04:00 committed by GitHub
commit f0bb83c225
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 23 deletions

View File

@ -21,12 +21,16 @@
namespace Friendica\Worker; namespace Friendica\Worker;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\Plaintext;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; 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\WebPush;
use Minishlink\WebPush\Subscription; use Minishlink\WebPush\Subscription;
@ -48,32 +52,60 @@ class PushSubscription
return; return;
} }
if (!empty($notification['uri-id'])) { $application_token = DBA::selectFirst('application-token', [], ['application-id' => $subscription['application-id'], 'uid' => $subscription['uid']]);
$notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]); 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'])) { if (!empty($notification['actor-id'])) {
$actor = Contact::getById($notification['actor-id']); $actor = Contact::getById($notification['actor-id']);
} }
$push = [ $body = '';
'subscription' => Subscription::create([
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']);
}
}
// @todo Add a meaningful title here, see the functionality in enotify.php
$title = '';
$push = Subscription::create([
'contentEncoding' => 'aesgcm',
'endpoint' => $subscription['endpoint'], 'endpoint' => $subscription['endpoint'],
'publicKey' => $subscription['pubkey'], 'keys' => [
'authToken' => $subscription['secret'], 'p256dh' => $subscription['pubkey'],
]), 'auth' => $subscription['secret']
// @todo Check if we are supposed to transmit a payload at all ],
'payload' => json_encode([ ]);
'title' => 'Friendica',
'body' => $notify['msg'] ?? '', $payload = [
'access_token' => $application_token['access_token'],
'preferred_locale' => $user['language'],
'notification_id' => $nid,
'notification_type' => $type,
'icon' => $actor['thumb'] ?? '', 'icon' => $actor['thumb'] ?? '',
'image' => '', 'title' => $title ?: $l10n->t('Notification from Friendica'),
'badge' => DI::baseUrl()->get() . '/images/friendica-192.png', 'body' => $body ?: $l10n->t('Empty Post'),
'tag' => $notification['parent-uri-id'] ?? '',
'timestamp' => DateTimeFormat::utc($notification['created'], DateTimeFormat::JSON),
]),
]; ];
Logger::info('Payload', ['payload' => $payload]);
$auth = [ $auth = [
'VAPID' => [ 'VAPID' => [
'subject' => DI::baseUrl()->getHostname(), 'subject' => DI::baseUrl()->getHostname(),
@ -84,10 +116,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( $report = $webPush->sendOneNotification($push, json_encode($payload), ['urgency' => 'normal']);
$push['subscription'],
$push['payload']
);
$endpoint = $report->getRequest()->getUri()->__toString(); $endpoint = $report->getRequest()->getUri()->__toString();

View File

@ -10553,6 +10553,14 @@ msgstr ""
msgid "(no subject)" msgid "(no subject)"
msgstr "" 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 #: view/theme/duepuntozero/config.php:52
msgid "default" msgid "default"
msgstr "" msgstr ""