]> git.mxchange.org Git - friendica.git/blob - src/Worker/PushSubscription.php
Fix usage
[friendica.git] / src / Worker / PushSubscription.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
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;
32
33 class PushSubscription
34 {
35         public static function execute(int $sid, int $nid)
36         {
37                 Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]);
38
39                 $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
40                 if (empty($subscription)) {
41                         Logger::info('Subscription not found', ['subscription' => $sid]);
42                         return;
43                 }
44
45                 $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
46                 if (empty($notification)) {
47                         Logger::info('Notification not found', ['notification' => $nid]);
48                         return;
49                 }
50
51                 if (!empty($notification['uri-id'])) {
52                         $notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]);
53                 }
54
55                 if (!empty($notification['actor-id'])) {
56                         $actor = Contact::getById($notification['actor-id']);
57                 }
58
59                 $push = [
60                         'subscription' => Subscription::create([
61                                 'endpoint'  => $subscription['endpoint'],
62                                 'publicKey' => $subscription['pubkey'],
63                                 'authToken' => $subscription['secret'],
64                         ]),
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'] ?? '',
70                                 'image'     => '',
71                                 'badge'     => DI::baseUrl()->get() . '/images/friendica-192.png',
72                                 'tag'       => $notification['parent-uri-id'] ?? '',
73                                 'timestamp' => DateTimeFormat::utc($notification['created'], DateTimeFormat::JSON),
74                         ]),
75                 ];
76
77                 $auth = [
78                         'VAPID' => [
79                                 'subject'    => DI::baseUrl()->getHostname(),
80                                 'publicKey'  => ModelSubscription::getPublicVapidKey(),
81                                 'privateKey' => ModelSubscription::getPrivateVapidKey(),
82                         ],
83                 ];
84
85                 $webPush = new WebPush($auth, [], DI::config()->get('system', 'xrd_timeout'));
86
87                 $report = $webPush->sendOneNotification(
88                         $push['subscription'],
89                         $push['payload']
90                 );
91
92                 $endpoint = $report->getRequest()->getUri()->__toString();
93
94                 if ($report->isSuccess()) {
95                         Logger::info('Message sent successfully for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint]);
96                 } else {
97                         Logger::info('Message failed to sent for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint, 'reason' => $report->getReason()]);
98                 }
99         }
100 }