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;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\GServer;
29 use Friendica\Model\Post;
30 use Friendica\Protocol\ActivityPub;
31 use Friendica\Util\HTTPSignature;
32 use Friendica\Worker\Delivery as WorkerDelivery;
37 * Deliver posts to the given inbox
39 * @param string $inbox
40 * @return array with the elements "success" and "uri_ids" of the failed posts
42 public static function deliver(string $inbox): array
45 $posts = Post\Delivery::selectForInbox($inbox);
48 foreach ($posts as $post) {
50 $result = self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], $post['receivers'], $post['uri-id']);
52 if ($result['serverfailure']) {
53 // In a timeout situation we assume that every delivery to that inbox will time out.
54 // So we set the flag and try all deliveries at a later time.
55 Logger::info('Inbox delivery has a server failure', ['inbox' => $inbox]);
60 if ($serverfail || (!$result['success'] && !$result['drop'])) {
61 $uri_ids[] = $post['uri-id'];
65 Logger::debug('Inbox delivery done', ['inbox' => $inbox, 'posts' => count($posts), 'failed' => count($uri_ids), 'serverfailure' => $serverfail]);
66 return ['success' => empty($uri_ids), 'uri_ids' => $uri_ids];
70 * Deliver the given post to the given inbox
73 * @param integer $item_id
74 * @param string $inbox
76 * @param array $receivers
77 * @param integer $uri_id
80 public static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id): array
82 if (empty($item_id) && !empty($uri_id) && !empty($uid)) {
83 $item = Post::selectFirst(['id', 'parent', 'origin'], ['uri-id' => $uri_id, 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
84 if (empty($item['id'])) {
85 Logger::notice('Item not found, removing delivery', ['uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]);
86 Post\Delivery::remove($uri_id, $inbox);
89 $item_id = $item['id'];
97 if ($cmd == WorkerDelivery::MAIL) {
98 $data = ActivityPub\Transmitter::createActivityFromMail($item_id);
100 $success = HTTPSignature::transmit($data, $inbox, $uid);
102 } elseif ($cmd == WorkerDelivery::SUGGESTION) {
103 $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
104 } elseif ($cmd == WorkerDelivery::RELOCATION) {
105 // @todo Implementation pending
106 } elseif ($cmd == WorkerDelivery::POKE) {
107 // Implementation not planned
108 } elseif ($cmd == WorkerDelivery::REMOVAL) {
109 $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
110 } elseif ($cmd == WorkerDelivery::PROFILEUPDATE) {
111 $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
113 $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
115 $timestamp = microtime(true);
116 $response = HTTPSignature::post($data, $inbox, $uid);
117 $runtime = microtime(true) - $timestamp;
118 $success = $response->isSuccess();
119 $serverfail = $response->isTimeout();
121 // 5xx errors are problems on the server. We don't need to continue delivery then.
122 if (!$serverfail && ($response->getReturnCode() >= 500) && ($response->getReturnCode() <= 599)) {
126 // A 404 means that the inbox doesn't exist. We can stop the delivery here.
127 if (!$serverfail && ($response->getReturnCode() == 404)) {
131 $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
132 if (!$serverfail && $xrd_timeout && ($runtime > $xrd_timeout)) {
136 $curl_timeout = DI::config()->get('system', 'curl_timeout');
137 if (!$serverfail && $curl_timeout && ($runtime > $curl_timeout)) {
141 // Resubscribe to relay server upon client error
142 if (!$serverfail && ($response->getReturnCode() >= 400) && ($response->getReturnCode() <= 499)) {
143 $actor = self:: fetchActorForRelayInbox($inbox);
144 if (!empty($actor)) {
145 $drop = !ActivityPub\Transmitter::sendRelayFollow($actor);
146 Logger::notice('Resubscribed to relay', ['url' => $actor, 'success' => !$drop]);
151 Logger::info('Delivery failed', ['retcode' => $response->getReturnCode(), 'serverfailure' => $serverfail, 'drop' => $drop, 'runtime' => round($runtime, 3), 'uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox]);
155 Post\Delivery::remove($uri_id, $inbox);
157 Post\Delivery::incrementFailed($uri_id, $inbox);
163 self::setSuccess($receivers, $success);
165 Logger::debug('Delivered', ['uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox, 'success' => $success]);
167 if ($success && in_array($cmd, [WorkerDelivery::POST])) {
168 Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB);
171 return ['success' => $success, 'serverfailure' => $serverfail, 'drop' => $drop];
175 * Fetch the actor of the given inbox of an relay server
177 * @param string $inbox
180 private static function fetchActorForRelayInbox(string $inbox): string
182 return DBA::selectFirst('apcontact', ['url'], ['sharedinbox' => $inbox, 'type' => 'Application']) ?: '';
186 * mark or unmark the given receivers for archival upon succoess
188 * @param array $receivers
189 * @param boolean $success
192 private static function setSuccess(array $receivers, bool $success)
196 foreach ($receivers as $receiver) {
197 $contact = Contact::getById($receiver);
198 if (empty($contact)) {
202 $gsid = $gsid ?: $contact['gsid'];
205 Contact::unmarkForArchival($contact);
207 Contact::markForArchival($contact);
212 GServer::setProtocol($gsid, Post\DeliveryData::ACTIVITYPUB);