]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub/Delivery.php
The worker is split into several classes
[friendica.git] / src / Protocol / ActivityPub / Delivery.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\Protocol\ActivityPub;
23
24 use Friendica\Core\Logger;
25 use Friendica\DI;
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 use Friendica\Worker\Delivery as WorkerDelivery;
32
33 class Delivery
34 {
35         public static function deliver(string $inbox):array
36         {
37                 $uri_ids    = [];
38                 $posts      = Post\Delivery::selectForInbox($inbox);
39                 $serverfail = false;
40
41                 foreach ($posts as $post) {
42                         if (!$serverfail) {
43                                 $result = self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], $post['receivers'], $post['uri-id']);
44
45                                 if ($result['serverfailure']) {
46                                         // In a timeout situation we assume that every delivery to that inbox will time out.
47                                         // So we set the flag and try all deliveries at a later time.
48                                         Logger::info('Inbox delivery has a server failure', ['inbox' => $inbox]);
49                                         $serverfail = true;
50                                 }
51                         }
52
53                         if ($serverfail || !$result['success']) {
54                                 $uri_ids[] = $post['uri-id'];
55                         }
56                 }
57
58                 Logger::debug('Inbox delivery done', ['inbox' => $inbox, 'posts' => count($posts), 'failed' => count($uri_ids), 'serverfailure' => $serverfail]);
59                 return ['success' => empty($uri_ids), 'uri_ids' => $uri_ids];
60         }
61
62         public static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id): array
63         {
64                 if (empty($item_id) && !empty($uri_id) && !empty($uid)) {
65                         $item = Post::selectFirst(['id', 'parent', 'origin'], ['uri-id' => $uri_id, 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
66                         if (empty($item['id'])) {
67                                 Logger::notice('Item not found, removing delivery', ['uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]);
68                                 Post\Delivery::remove($uri_id, $inbox);
69                                 return true;
70                         } else {
71                                 $item_id = $item['id'];
72                         }
73                 }
74
75                 $success    = true;
76                 $serverfail = false;
77
78                 if ($cmd == WorkerDelivery::MAIL) {
79                         $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
80                         if (!empty($data)) {
81                                 $success = HTTPSignature::transmit($data, $inbox, $uid);
82                         }
83                 } elseif ($cmd == WorkerDelivery::SUGGESTION) {
84                         $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
85                 } elseif ($cmd == WorkerDelivery::RELOCATION) {
86                         // @todo Implementation pending
87                 } elseif ($cmd == WorkerDelivery::POKE) {
88                         // Implementation not planned
89                 } elseif ($cmd == WorkerDelivery::REMOVAL) {
90                         $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
91                 } elseif ($cmd == WorkerDelivery::PROFILEUPDATE) {
92                         $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
93                 } else {
94                         $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
95                         if (!empty($data)) {
96                                 $timestamp  = microtime(true);
97                                 $response   = HTTPSignature::post($data, $inbox, $uid);
98                                 $runtime    = microtime(true) - $timestamp;
99                                 $success    = $response->isSuccess();
100                                 $serverfail = $response->isTimeout();
101                                 if (!$success) {
102                                         if (!$serverfail && ($response->getReturnCode() >= 500) && ($response->getReturnCode() <= 599)) {
103                                                 $serverfail = true;
104                                         }
105
106                                         $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
107                                         if (!$serverfail && $xrd_timeout && ($runtime > $xrd_timeout)) {
108                                                 $serverfail = true;
109                                         }
110                                         $curl_timeout = DI::config()->get('system', 'curl_timeout');
111                                         if (!$serverfail && $curl_timeout && ($runtime > $curl_timeout)) {
112                                                 $serverfail = true;
113                                         }
114
115                                         Logger::info('Delivery failed', ['retcode' => $response->getReturnCode(), 'serverfailure' => $serverfail, 'runtime' => round($runtime, 3), 'uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox]);
116                                 }
117                                 if ($uri_id) {
118                                         if ($success) {
119                                                 Post\Delivery::remove($uri_id, $inbox);
120                                         } else {
121                                                 Post\Delivery::incrementFailed($uri_id, $inbox);
122                                         }
123                                 }
124                         }
125                 }
126
127                 self::setSuccess($receivers, $success);
128
129                 Logger::debug('Delivered', ['uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox, 'success' => $success]);
130
131                 if ($success && in_array($cmd, [WorkerDelivery::POST])) {
132                         Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
133                 }
134
135                 return ['success' => $success, 'serverfailure' => $serverfail];
136         }
137
138         private static function setSuccess(array $receivers, bool $success)
139         {
140                 $gsid = null;
141
142                 foreach ($receivers as $receiver) {
143                         $contact = Contact::getById($receiver);
144                         if (empty($contact)) {
145                                 continue;
146                         }
147
148                         $gsid = $gsid ?: $contact['gsid'];
149
150                         if ($success) {
151                                 Contact::unmarkForArchival($contact);
152                         } else {
153                                 Contact::markForArchival($contact);
154                         }
155                 }
156
157                 if (!empty($gsid)) {
158                         GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);
159                 }
160         }
161 }