]> git.mxchange.org Git - friendica.git/blob - src/Worker/PushSubscription.php
Merge pull request #11503 from annando/bulk-delivery
[friendica.git] / src / Worker / PushSubscription.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Content\Text\BBCode;
25 use Friendica\Content\Text\Plaintext;
26 use Friendica\Core\Logger;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Post;
31 use Friendica\Model\Subscription as ModelSubscription;
32 use Friendica\Model\User;
33 use Friendica\Navigation\Notifications;
34 use Friendica\Network\HTTPException\NotFoundException;
35 use Minishlink\WebPush\WebPush;
36 use Minishlink\WebPush\Subscription;
37
38 class PushSubscription
39 {
40         public static function execute(int $sid, int $nid)
41         {
42                 Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]);
43
44                 $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
45                 if (empty($subscription)) {
46                         Logger::info('Subscription not found', ['subscription' => $sid]);
47                         return;
48                 }
49
50                 try {
51                         $Notification = DI::notification()->selectOneById($nid);
52                 } catch (NotFoundException $e) {
53                         Logger::info('Notification not found', ['notification' => $nid]);
54                         return;
55                 }
56
57                 $application_token = DBA::selectFirst('application-token', [], ['application-id' => $subscription['application-id'], 'uid' => $subscription['uid']]);
58                 if (empty($application_token)) {
59                         Logger::info('Application token not found', ['application' => $subscription['application-id']]);
60                         return;
61                 }
62
63                 $user = User::getById($Notification->uid);
64                 if (empty($user)) {
65                         Logger::info('User not found', ['application' => $subscription['uid']]);
66                         return;
67                 }
68
69                 $l10n = DI::l10n()->withLang($user['language']);
70
71                 if ($Notification->actorId) {
72                         $actor = Contact::getById($Notification->actorId);
73                 }
74
75                 $body = '';
76
77                 if ($Notification->targetUriId) {
78                         $post = Post::selectFirst([], ['uri-id' => $Notification->targetUriId, 'uid' => [0, $Notification->uid]]);
79                         if (!empty($post['body'])) {
80                                 $body = BBCode::toPlaintext($post['body'], false);
81                                 $body = Plaintext::shorten($body, 160, $Notification->uid);
82                         }
83                 }
84
85                 $message = DI::notificationFactory()->getMessageFromNotification($Notification);
86                 $title = $message['plain'] ?: '';
87
88                 $push = Subscription::create([
89                         'contentEncoding' => 'aesgcm',
90                         'endpoint'        => $subscription['endpoint'],
91                         'keys'            => [
92                                 'p256dh' => $subscription['pubkey'],
93                                 'auth'   => $subscription['secret']
94                         ],
95                 ]);
96
97                 $payload = [
98                         'access_token'      => $application_token['access_token'],
99                         'preferred_locale'  => $user['language'],
100                         'notification_id'   => $nid,
101                         'notification_type' => \Friendica\Factory\Api\Mastodon\Notification::getType($Notification),
102                         'icon'              => $actor['thumb'] ?? '',
103                         'title'             => $title ?: $l10n->t('Notification from Friendica'),
104                         'body'              => $body ?: $l10n->t('Empty Post'),
105                 ];
106
107                 Logger::info('Payload', ['payload' => $payload]);
108
109                 $auth = [
110                         'VAPID' => [
111                                 'subject'    => DI::baseUrl()->getHostname(),
112                                 'publicKey'  => ModelSubscription::getPublicVapidKey(),
113                                 'privateKey' => ModelSubscription::getPrivateVapidKey(),
114                         ],
115                 ];
116
117                 $webPush = new WebPush($auth, [], DI::config()->get('system', 'xrd_timeout'));
118
119                 $report = $webPush->sendOneNotification($push, json_encode($payload), ['urgency' => 'normal']);
120
121                 $endpoint = $report->getRequest()->getUri()->__toString();
122
123                 if ($report->isSuccess()) {
124                         Logger::info('Message sent successfully for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint]);
125                 } else {
126                         Logger::info('Message failed to sent for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint, 'reason' => $report->getReason()]);
127                 }
128         }
129 }