]> git.mxchange.org Git - friendica.git/blob - src/Worker/PushSubscription.php
ab323105b7a3a541836a68f1565289dd3b3ca983
[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 Minishlink\WebPush\WebPush;
27 use Minishlink\WebPush\Subscription;
28
29 Class PushSubscription
30 {
31         public static function execute(int $sid)
32         {
33                 $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
34
35                 $notification = [
36                         'subscription' => Subscription::create([
37                                 'endpoint'  => $subscription['endpoint'],
38                                 'publicKey' => $subscription['pubkey'],
39                                 'authToken' => $subscription['secret'],
40                         ]),
41                         'payload' => null,
42                 ];
43
44                 $webPush = new WebPush();
45
46                 $report = $webPush->sendOneNotification(
47                         $notification['subscription'],
48                         $notification['payload']
49                 );
50
51                 $endpoint = $report->getRequest()->getUri()->__toString();
52
53                 if ($report->isSuccess()) {
54                         Logger::info('Message sent successfully for subscription', ['endpoint' => $endpoint]);
55                 } else {
56                         Logger::info('Message failed to sent for subscription', ['endpoint' => $endpoint, 'reason' => $report->getReason()]);
57                 }
58         }
59 }