]> git.mxchange.org Git - friendica.git/blob - src/Worker/APDelivery.php
Display the runtime
[friendica.git] / src / Worker / APDelivery.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\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Model\Contact;
27 use Friendica\Model\GServer;
28 use Friendica\Model\Post;
29 use Friendica\Protocol\ActivityPub;
30 use Friendica\Util\HTTPSignature;
31
32 class APDelivery
33 {
34         /**
35          * Delivers ActivityPub messages
36          *
37          * @param string  $cmd       One of the Worker\Delivery constant values
38          * @param integer $item_id   0 if no item is involved (like Delivery::REMOVAL and Delivery::PROFILEUPDATE)
39          * @param string  $inbox     The URL of the recipient profile
40          * @param integer $uid       The ID of the user who triggered this delivery
41          * @param array   $receivers The contact IDs related to the inbox URL for contact archival housekeeping
42          * @param int     $uri_id    URI-ID of item to be transmitted
43          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
44          * @throws \ImagickException
45          */
46         public static function execute(string $cmd, int $item_id, string $inbox, int $uid, array $receivers = [], int $uri_id = 0)
47         {
48                 if (ActivityPub\Transmitter::archivedInbox($inbox)) {
49                         Logger::info('Inbox is archived', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid]);
50                         if (empty($uri_id) && !empty($item_id)) {
51                                 $item = Post::selectFirst(['uri-id'], ['id' => $item_id]);
52                                 $uri_id = $item['uri-id'] ?? 0;
53                         }
54                         if (empty($uri_id)) {
55                                 $posts   = Post\Delivery::selectForInbox($inbox);
56                                 $uri_ids = array_column($posts, 'uri-id');
57                         } else {
58                                 $uri_ids = [$uri_id];
59                         }
60
61                         foreach ($uri_ids as $uri_id) {
62                                 Post\Delivery::remove($uri_id, $inbox);
63                                 Post\DeliveryData::incrementQueueFailed($uri_id);
64                         }
65                         return;
66                 }
67
68                 Logger::info('Invoked', ['cmd' => $cmd, 'inbox' => $inbox, 'id' => $item_id, 'uri-id' => $uri_id, 'uid' => $uid]);
69
70                 if (empty($uri_id)) {
71                         $result  = self::deliver($inbox);
72                         $success = $result['success'];
73                         $uri_ids = $result['uri_ids'];
74                 } else {
75                         $result  = self::deliverToInbox($cmd, $item_id, $inbox, $uid, $receivers, $uri_id);
76                         $success = $result['success'];
77                         $uri_ids = [$uri_id];
78                 }
79
80                 if (!$success && !Worker::defer() && !empty($uri_ids)) {
81                         foreach ($uri_ids as $uri_id) {
82                                 Post\Delivery::remove($uri_id, $inbox);
83                                 Post\DeliveryData::incrementQueueFailed($uri_id);
84                         }
85                 }
86         }
87
88         private static function deliver(string $inbox):array
89         {
90                 $uri_ids = [];
91                 $posts   = Post\Delivery::selectForInbox($inbox);
92                 $timeout = false;
93
94                 foreach ($posts as $post) {
95                         if (!$timeout) {
96                                 $result = self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], $post['receivers'], $post['uri-id']);
97
98                                 if ($result['timeout']) {
99                                         // In a timeout situation we assume that every delivery to that inbox will time out.
100                                         // So we set the flag and try all deliveries at a later time.
101                                         Logger::debug('Inbox delivery has a time out', ['inbox' => $inbox]);
102                                         $timeout = true;
103                                 }
104                         }
105
106                         if ($timeout || !$result['success']) {
107                                 $uri_ids[] = $post['uri-id'];
108                         }
109                 }
110
111                 Logger::debug('Inbox delivery done', ['inbox' => $inbox, 'posts' => count($posts), 'failed' => count($uri_ids)]);
112                 return ['success' => empty($uri_ids), 'uri_ids' => $uri_ids];
113         }
114
115         private static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id): array
116         {
117                 if (empty($item_id) && !empty($uri_id) && !empty($uid)) {
118                         $item = Post::selectFirst(['id', 'parent', 'origin'], ['uri-id' => $uri_id, 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
119                         if (empty($item['id'])) {
120                                 Logger::debug('Item not found, removing delivery', ['uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]);
121                                 Post\Delivery::remove($uri_id, $inbox);
122                                 return true;
123                         } else {
124                                 $item_id = $item['id'];
125                         }
126                 }
127
128                 $success = true;
129                 $timeout = false;
130
131                 if ($cmd == Delivery::MAIL) {
132                         $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
133                         if (!empty($data)) {
134                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
135                         }
136                 } elseif ($cmd == Delivery::SUGGESTION) {
137                         $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
138                 } elseif ($cmd == Delivery::RELOCATION) {
139                         // @todo Implementation pending
140                 } elseif ($cmd == Delivery::POKE) {
141                         // Implementation not planned
142                 } elseif ($cmd == Delivery::REMOVAL) {
143                         $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
144                 } elseif ($cmd == Delivery::PROFILEUPDATE) {
145                         $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
146                 } else {
147                         $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
148                         if (!empty($data)) {
149                                 $timestamp = microtime(true);
150                                 $response = HTTPSignature::post($data, $inbox, $uid);
151                                 $runtime  = microtime(true) - $timestamp;
152                                 $success  = $response->isSuccess();
153                                 $timeout  = $response->isTimeout();
154                                 if (!$success) {
155                                         Logger::debug('Delivery failed', ['retcode' => $response->getReturnCode(), 'timeout' => $timeout, 'runtime' => round($runtime, 3), 'uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox]);
156                                 }
157                                 if ($uri_id) {
158                                         if ($success) {
159                                                 Post\Delivery::remove($uri_id, $inbox);
160                                         } else {
161                                                 Post\Delivery::incrementFailed($uri_id, $inbox);
162                                         }
163                                 }
164                         }
165                 }
166
167                 self::setSuccess($receivers, $success);
168
169                 Logger::info('Delivered', ['uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox, 'success' => $success]);
170
171                 if ($success && in_array($cmd, [Delivery::POST])) {
172                         Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
173                 }
174
175                 return ['success' => $success, 'timeout' => $timeout];
176         }
177
178         private static function setSuccess(array $receivers, bool $success)
179         {
180                 $gsid = null;
181
182                 foreach ($receivers as $receiver) {
183                         $contact = Contact::getById($receiver);
184                         if (empty($contact)) {
185                                 continue;
186                         }
187
188                         $gsid = $gsid ?: $contact['gsid'];
189
190                         if ($success) {
191                                 Contact::unmarkForArchival($contact);
192                         } else {
193                                 Contact::markForArchival($contact);
194                         }
195                 }
196
197                 if (!empty($gsid)) {
198                         GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);
199                 }
200         }
201 }