The push api payload content is improved

This commit is contained in:
Michael 2021-08-17 22:53:52 +00:00
parent e205bd450e
commit 5a9db1b3bc

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,57 @@ 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([
'endpoint' => $subscription['endpoint'], if (!empty($notification['target-uri-id'])) {
'publicKey' => $subscription['pubkey'], $post = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]]);
'authToken' => $subscription['secret'], if (!empty($post['body'])) {
]), $body = BBCode::toPlaintext($post['body'], false);
// @todo Check if we are supposed to transmit a payload at all $body = Plaintext::shorten($body, 160, $notification['uid']);
'payload' => json_encode([ }
'title' => 'Friendica', }
'body' => $notify['msg'] ?? '',
'icon' => $actor['thumb'] ?? '', $push = Subscription::create([
'image' => '', 'contentEncoding' => 'aesgcm',
'badge' => DI::baseUrl()->get() . '/images/friendica-192.png', 'endpoint' => $subscription['endpoint'],
'tag' => $notification['parent-uri-id'] ?? '', 'keys' => [
'timestamp' => DateTimeFormat::utc($notification['created'], DateTimeFormat::JSON), '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 = [ $auth = [
'VAPID' => [ 'VAPID' => [
'subject' => DI::baseUrl()->getHostname(), '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( $report = $webPush->sendOneNotification($push, json_encode($payload), ['urgency' => 'normal']);
$push['subscription'],
$push['payload']
);
$endpoint = $report->getRequest()->getUri()->__toString(); $endpoint = $report->getRequest()->getUri()->__toString();