]> git.mxchange.org Git - friendica.git/blob - src/Worker/PushSubscription.php
f3c3a4046faa687771d7c2efd1646ef7dfe257c1
[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                 $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
38                 $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
39
40                 if (!empty($notification['uri-id'])) {
41                         $notify = DBA::selectFirst('notify', ['msg'], ['uri-id' => $notification['target-uri-id']]);
42                 }
43
44                 if (!empty($notification['actor-id'])) {
45                         $actor = Contact::getById($notification['actor-id']);
46                 }
47
48                 $push = [
49                         'subscription' => Subscription::create([
50                                 'endpoint'  => $subscription['endpoint'],
51                                 'publicKey' => $subscription['pubkey'],
52                                 'authToken' => $subscription['secret'],
53                         ]),
54                         // @todo Check if we are supposed to transmit a payload at all
55                         'payload' => json_encode([
56                                 'title'     => 'Friendica',
57                                 'body'      => $notify['msg'] ?? '',
58                                 'icon'      => $actor['thumb'] ?? '',
59                                 'image'     => '',
60                                 'badge'     => DI::baseUrl()->get() . '/images/friendica-192.png',
61                                 'tag'       => $notification['parent-uri-id'] ?? '',
62                                 'timestamp' => DateTimeFormat::utc($notification['created'], DateTimeFormat::JSON),
63                         ]),
64                 ];
65
66                 $auth = [
67                         'VAPID' => [
68                                 'subject'    => DI::baseUrl()->getHostname(),
69                                 'publicKey'  => ModelSubscription::getPublicVapidKey(),
70                                 'privateKey' => ModelSubscription::getPrivateVapidKey(),
71                         ],
72                 ];
73
74                 $webPush = new WebPush($auth);
75
76                 $report = $webPush->sendOneNotification(
77                         $push['subscription'],
78                         $push['payload']
79                 );
80
81                 $endpoint = $report->getRequest()->getUri()->__toString();
82
83                 if ($report->isSuccess()) {
84                         Logger::info('Message sent successfully for subscription', ['endpoint' => $endpoint]);
85                 } else {
86                         Logger::info('Message failed to sent for subscription', ['endpoint' => $endpoint, 'reason' => $report->getReason()]);
87                 }
88         }
89 }