3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Subscription as ModelSubscription;
29 use Friendica\Util\DateTimeFormat;
30 use Minishlink\WebPush\WebPush;
31 use Minishlink\WebPush\Subscription;
33 class PushSubscription
35 public static function execute(int $sid, int $nid)
37 Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]);
39 $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
40 if (empty($subscription)) {
41 Logger::info('Subscription not found', ['subscription' => $sid]);
45 $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
46 if (empty($notification)) {
47 Logger::info('Notification not found', ['notification' => $nid]);
51 if (!empty($notification['uri-id'])) {
52 $notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]);
55 if (!empty($notification['actor-id'])) {
56 $actor = Contact::getById($notification['actor-id']);
60 'subscription' => Subscription::create([
61 'endpoint' => $subscription['endpoint'],
62 'publicKey' => $subscription['pubkey'],
63 'authToken' => $subscription['secret'],
65 // @todo Check if we are supposed to transmit a payload at all
66 'payload' => json_encode([
67 'title' => 'Friendica',
68 'body' => $notify['msg'] ?? '',
69 'icon' => $actor['thumb'] ?? '',
71 'badge' => DI::baseUrl()->get() . '/images/friendica-192.png',
72 'tag' => $notification['parent-uri-id'] ?? '',
73 'timestamp' => DateTimeFormat::utc($notification['created'], DateTimeFormat::JSON),
79 'subject' => DI::baseUrl()->getHostname(),
80 'publicKey' => ModelSubscription::getPublicVapidKey(),
81 'privateKey' => ModelSubscription::getPrivateVapidKey(),
85 $webPush = new WebPush($auth, [], DI::config()->get('system', 'xrd_timeout'));
87 $report = $webPush->sendOneNotification(
88 $push['subscription'],
92 $endpoint = $report->getRequest()->getUri()->__toString();
94 if ($report->isSuccess()) {
95 Logger::info('Message sent successfully for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint]);
97 Logger::info('Message failed to sent for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint, 'reason' => $report->getReason()]);