Add a payload

This commit is contained in:
Michael 2021-08-16 06:11:26 +00:00
parent 41545e8808
commit c85ce2f6b0
2 changed files with 26 additions and 6 deletions

View File

@ -155,7 +155,7 @@ class Subscription
$subscriptions = DBA::select('subscription', [], ['uid' => $notification['uid'], $type => true]); $subscriptions = DBA::select('subscription', [], ['uid' => $notification['uid'], $type => true]);
while ($subscription = DBA::fetch($subscriptions)) { while ($subscription = DBA::fetch($subscriptions)) {
Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]); Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]);
Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id']); Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $nid);
} }
DBA::close($subscriptions); DBA::close($subscriptions);
} }

View File

@ -24,23 +24,43 @@ namespace Friendica\Worker;
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\Subscription as ModelSubscription; use Friendica\Model\Subscription as ModelSubscription;
use Friendica\Util\DateTimeFormat;
use Minishlink\WebPush\WebPush; use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription; use Minishlink\WebPush\Subscription;
class PushSubscription class PushSubscription
{ {
public static function execute(int $sid) public static function execute(int $sid, int $nid)
{ {
$subscription = DBA::selectFirst('subscription', [], ['id' => $sid]); $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
$notification = DBA::selectFirst('notification', [], ['id' => $nid]);
$notification = [ if (!empty($notification['uri-id'])) {
$notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]);
}
if (!empty($notification['actor-id'])) {
$actor = Contact::getById($notification['actor-id']);
}
$push = [
'subscription' => Subscription::create([ 'subscription' => Subscription::create([
'endpoint' => $subscription['endpoint'], 'endpoint' => $subscription['endpoint'],
'publicKey' => $subscription['pubkey'], 'publicKey' => $subscription['pubkey'],
'authToken' => $subscription['secret'], 'authToken' => $subscription['secret'],
]), ]),
'payload' => null, // @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),
]),
]; ];
$auth = [ $auth = [
@ -54,8 +74,8 @@ class PushSubscription
$webPush = new WebPush($auth); $webPush = new WebPush($auth);
$report = $webPush->sendOneNotification( $report = $webPush->sendOneNotification(
$notification['subscription'], $push['subscription'],
$notification['payload'] $push['payload']
); );
$endpoint = $report->getRequest()->getUri()->__toString(); $endpoint = $report->getRequest()->getUri()->__toString();