2021-08-15 16:52:46 -04:00
< ? php
/**
* @ copyright Copyright ( C ) 2010 - 2021 , the Friendica project
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*
*/
namespace Friendica\Worker ;
use Friendica\Core\Logger ;
use Friendica\Database\DBA ;
2021-08-15 17:24:23 -04:00
use Friendica\DI ;
2021-08-16 02:11:26 -04:00
use Friendica\Model\Contact ;
2021-08-15 17:24:23 -04:00
use Friendica\Model\Subscription as ModelSubscription ;
2021-08-16 02:11:26 -04:00
use Friendica\Util\DateTimeFormat ;
2021-08-15 16:52:46 -04:00
use Minishlink\WebPush\WebPush ;
use Minishlink\WebPush\Subscription ;
2021-08-15 17:03:43 -04:00
class PushSubscription
2021-08-15 16:52:46 -04:00
{
2021-08-16 02:11:26 -04:00
public static function execute ( int $sid , int $nid )
2021-08-15 16:52:46 -04:00
{
2021-08-16 11:23:34 -04:00
Logger :: info ( 'Start' , [ 'subscription' => $sid , 'notification' => $nid ]);
2021-08-15 16:52:46 -04:00
$subscription = DBA :: selectFirst ( 'subscription' , [], [ 'id' => $sid ]);
2021-08-16 11:23:34 -04:00
if ( empty ( $subscription )) {
Logger :: info ( 'Subscription not found' , [ 'subscription' => $sid ]);
return ;
}
2021-08-16 02:11:26 -04:00
$notification = DBA :: selectFirst ( 'notification' , [], [ 'id' => $nid ]);
2021-08-16 11:23:34 -04:00
if ( empty ( $notification )) {
Logger :: info ( 'Notification not found' , [ 'notification' => $nid ]);
return ;
}
2021-08-15 16:52:46 -04:00
2021-08-16 02:11:26 -04:00
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 = [
2021-08-15 16:52:46 -04:00
'subscription' => Subscription :: create ([
2021-08-15 17:01:58 -04:00
'endpoint' => $subscription [ 'endpoint' ],
2021-08-15 16:52:46 -04:00
'publicKey' => $subscription [ 'pubkey' ],
'authToken' => $subscription [ 'secret' ],
]),
2021-08-16 02:11:26 -04:00
// @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 ),
]),
2021-08-15 16:52:46 -04:00
];
2021-08-15 17:24:23 -04:00
$auth = [
'VAPID' => [
2021-08-15 17:30:27 -04:00
'subject' => DI :: baseUrl () -> getHostname (),
'publicKey' => ModelSubscription :: getPublicVapidKey (),
2021-08-15 17:24:23 -04:00
'privateKey' => ModelSubscription :: getPrivateVapidKey (),
],
];
2021-08-15 17:30:27 -04:00
2021-08-16 11:23:34 -04:00
$webPush = new WebPush ( $auth , [], DI :: config () -> get ( 'system' , 'xrd_timeout' ));
2021-08-15 16:52:46 -04:00
$report = $webPush -> sendOneNotification (
2021-08-16 02:11:26 -04:00
$push [ 'subscription' ],
$push [ 'payload' ]
2021-08-15 16:52:46 -04:00
);
2021-08-15 17:01:58 -04:00
2021-08-15 16:52:46 -04:00
$endpoint = $report -> getRequest () -> getUri () -> __toString ();
if ( $report -> isSuccess ()) {
2021-08-16 11:23:34 -04:00
Logger :: info ( 'Message sent successfully for subscription' , [ 'subscription' => $sid , 'notification' => $nid , 'endpoint' => $endpoint ]);
2021-08-15 16:52:46 -04:00
} else {
2021-08-16 11:23:34 -04:00
Logger :: info ( 'Message failed to sent for subscription' , [ 'subscription' => $sid , 'notification' => $nid , 'endpoint' => $endpoint , 'reason' => $report -> getReason ()]);
2021-08-15 16:52:46 -04:00
}
}
}