3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Protocol\ActivityPub;
24 use Friendica\Core\Logger;
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;
35 public static function deliver(string $inbox): array
38 $posts = Post\Delivery::selectForInbox($inbox);
41 foreach ($posts as $post) {
43 $result = self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], $post['receivers'], $post['uri-id']);
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]);
53 if ($serverfail || !$result['success']) {
54 $uri_ids[] = $post['uri-id'];
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];
62 public static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id): array
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);
71 $item_id = $item['id'];
78 if ($cmd == WorkerDelivery::MAIL) {
79 $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
81 $success = HTTPSignature::transmit($data, $inbox, $uid);
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);
94 $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
96 $timestamp = microtime(true);
97 $response = HTTPSignature::post($data, $inbox, $uid);
98 $runtime = microtime(true) - $timestamp;
99 $success = $response->isSuccess();
100 $serverfail = $response->isTimeout();
102 if (!$serverfail && ($response->getReturnCode() >= 500) && ($response->getReturnCode() <= 599)) {
106 $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
107 if (!$serverfail && $xrd_timeout && ($runtime > $xrd_timeout)) {
110 $curl_timeout = DI::config()->get('system', 'curl_timeout');
111 if (!$serverfail && $curl_timeout && ($runtime > $curl_timeout)) {
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]);
119 Post\Delivery::remove($uri_id, $inbox);
121 Post\Delivery::incrementFailed($uri_id, $inbox);
127 self::setSuccess($receivers, $success);
129 Logger::debug('Delivered', ['uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox, 'success' => $success]);
131 if ($success && in_array($cmd, [WorkerDelivery::POST])) {
132 Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
135 return ['success' => $success, 'serverfailure' => $serverfail];
138 private static function setSuccess(array $receivers, bool $success)
142 foreach ($receivers as $receiver) {
143 $contact = Contact::getById($receiver);
144 if (empty($contact)) {
148 $gsid = $gsid ?: $contact['gsid'];
151 Contact::unmarkForArchival($contact);
153 Contact::markForArchival($contact);
158 GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);