From b1a2de5cb5b3f30b15a0b60e12cd70232d8f7620 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 24 May 2022 07:02:42 +0000 Subject: [PATCH 1/6] Resubscribe to relay servers --- src/Protocol/ActivityPub/Delivery.php | 60 +++++++++++++++++++++++++-- src/Protocol/Relay.php | 11 +++++ src/Worker/Cron.php | 4 ++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/Protocol/ActivityPub/Delivery.php b/src/Protocol/ActivityPub/Delivery.php index 2a2a63fee1..6f9272be08 100644 --- a/src/Protocol/ActivityPub/Delivery.php +++ b/src/Protocol/ActivityPub/Delivery.php @@ -22,6 +22,7 @@ namespace Friendica\Protocol\ActivityPub; use Friendica\Core\Logger; +use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\GServer; @@ -32,6 +33,12 @@ use Friendica\Worker\Delivery as WorkerDelivery; class Delivery { + /** + * Deliver posts to the given inbox + * + * @param string $inbox + * @return array with the elements "success" and "uri_ids" of the failed posts + */ public static function deliver(string $inbox): array { $uri_ids = []; @@ -50,7 +57,7 @@ class Delivery } } - if ($serverfail || !$result['success']) { + if ($serverfail || (!$result['success'] && !$result['drop'])) { $uri_ids[] = $post['uri-id']; } } @@ -59,6 +66,17 @@ class Delivery return ['success' => empty($uri_ids), 'uri_ids' => $uri_ids]; } + /** + * Deliver the given post to the given inbox + * + * @param string $cmd + * @param integer $item_id + * @param string $inbox + * @param integer $uid + * @param array $receivers + * @param integer $uri_id + * @return array + */ public static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id): array { if (empty($item_id) && !empty($uri_id) && !empty($uid)) { @@ -74,6 +92,7 @@ class Delivery $success = true; $serverfail = false; + $drop = false; if ($cmd == WorkerDelivery::MAIL) { $data = ActivityPub\Transmitter::createActivityFromMail($item_id); @@ -99,20 +118,37 @@ class Delivery $success = $response->isSuccess(); $serverfail = $response->isTimeout(); if (!$success) { + // 5xx errors are problems on the server. We don't need to continue delivery then. if (!$serverfail && ($response->getReturnCode() >= 500) && ($response->getReturnCode() <= 599)) { $serverfail = true; } + // A 404 means that the inbox doesn't exist. We can stop the delivery here. + if (!$serverfail && ($response->getReturnCode() == 404)) { + $serverfail = true; + } + $xrd_timeout = DI::config()->get('system', 'xrd_timeout'); if (!$serverfail && $xrd_timeout && ($runtime > $xrd_timeout)) { $serverfail = true; } + $curl_timeout = DI::config()->get('system', 'curl_timeout'); if (!$serverfail && $curl_timeout && ($runtime > $curl_timeout)) { $serverfail = true; } - 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]); + // Resubscribe to relay server upon client error + if (!$serverfail && ($response->getReturnCode() >= 400) && ($response->getReturnCode() <= 499)) { + $actor = self:: fetchActorForRelayInbox($inbox); + if (!empty($actor)) { + $drop = !ActivityPub\Transmitter::sendRelayFollow($actor); + Logger::notice('Resubscribed to relay', ['url' => $actor, 'success' => !$drop]); + } + + } + + 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]); } if ($uri_id) { if ($success) { @@ -132,9 +168,27 @@ class Delivery Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB); } - return ['success' => $success, 'serverfailure' => $serverfail]; + return ['success' => $success, 'serverfailure' => $serverfail, 'drop' => $drop]; } + /** + * Fetch the actor of the given inbox of an relay server + * + * @param string $inbox + * @return string + */ + private static function fetchActorForRelayInbox(string $inbox): string + { + return DBA::selectFirst('apcontact', ['url'], ['sharedinbox' => $inbox, 'type' => 'Application']) ?: ''; + } + + /** + * mark or unmark the given receivers for archival upon succoess + * + * @param array $receivers + * @param boolean $success + * @return void + */ private static function setSuccess(array $receivers, bool $success) { $gsid = null; diff --git a/src/Protocol/Relay.php b/src/Protocol/Relay.php index f50b73b7d7..8679bbec5b 100644 --- a/src/Protocol/Relay.php +++ b/src/Protocol/Relay.php @@ -341,4 +341,15 @@ class Relay // It should never happen that we arrive here return []; } + + /** + * Resubscribe to all relay servers + */ + public static function reSubscribe() + { + foreach (self::getList() as $server) { + $success = ActivityPub\Transmitter::sendRelayFollow($server['url']); + Logger::debug('Resubscribed', ['profile' => $server['url'], 'success' => $success]); + } + } } diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php index 01c54fed18..30efc61dba 100644 --- a/src/Worker/Cron.php +++ b/src/Worker/Cron.php @@ -27,6 +27,7 @@ use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Tag; +use Friendica\Protocol\Relay; class Cron { @@ -125,6 +126,9 @@ class Cron } DI::config()->set('system', 'last_cron_daily', time()); + + // Resubscribe to relay servers + Relay::reSubscribe(); } Logger::notice('end'); From e68ff3b3bb93ca5d5e5b0ccf6022e1ff9aa19d48 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 24 May 2022 08:02:55 +0000 Subject: [PATCH 2/6] Return the actor, not the array --- src/Protocol/ActivityPub/Delivery.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Protocol/ActivityPub/Delivery.php b/src/Protocol/ActivityPub/Delivery.php index 6f9272be08..c959d8c615 100644 --- a/src/Protocol/ActivityPub/Delivery.php +++ b/src/Protocol/ActivityPub/Delivery.php @@ -179,7 +179,8 @@ class Delivery */ private static function fetchActorForRelayInbox(string $inbox): string { - return DBA::selectFirst('apcontact', ['url'], ['sharedinbox' => $inbox, 'type' => 'Application']) ?: ''; + $apcontact = DBA::selectFirst('apcontact', ['url'], ['sharedinbox' => $inbox, 'type' => 'Application']); + return $apcontact['url'] ?? ''; } /** From 8969e83134ef487b1fcfe434a844b42e5f861864 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 24 May 2022 08:06:48 +0000 Subject: [PATCH 3/6] Use a better query to fetch the relay actor --- src/Protocol/ActivityPub/Delivery.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Protocol/ActivityPub/Delivery.php b/src/Protocol/ActivityPub/Delivery.php index c959d8c615..966f0e5e16 100644 --- a/src/Protocol/ActivityPub/Delivery.php +++ b/src/Protocol/ActivityPub/Delivery.php @@ -179,7 +179,8 @@ class Delivery */ private static function fetchActorForRelayInbox(string $inbox): string { - $apcontact = DBA::selectFirst('apcontact', ['url'], ['sharedinbox' => $inbox, 'type' => 'Application']); + $apcontact = DBA::selectFirst('apcontact', ['url'], ["`sharedinbox` = ? AND `type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` = ?)", + $inbox, 'Application', 0, Contact::FRIEND]); return $apcontact['url'] ?? ''; } From 034d838248f69c60e8b588e1b1266d78f4254182 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 24 May 2022 12:27:35 +0000 Subject: [PATCH 4/6] Ignore delivery problems with drops --- src/Protocol/ActivityPub/Delivery.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Protocol/ActivityPub/Delivery.php b/src/Protocol/ActivityPub/Delivery.php index 966f0e5e16..ebf8ffb2b6 100644 --- a/src/Protocol/ActivityPub/Delivery.php +++ b/src/Protocol/ActivityPub/Delivery.php @@ -144,6 +144,11 @@ class Delivery if (!empty($actor)) { $drop = !ActivityPub\Transmitter::sendRelayFollow($actor); Logger::notice('Resubscribed to relay', ['url' => $actor, 'success' => !$drop]); + } elseif ($cmd = WorkerDelivery::DELETION) { + // Remote systems not always accept our deletion requests, so we drop them if rejected. + // Situation is: In Friendica we allow the thread owner to delete foreign comments to their thread. + // Most AP systems don't allow this, so they will reject the deletion request. + $drop = true; } } From bee6ffe968858a9c9b3b67c4225d11a2942ae61a Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 24 May 2022 12:32:04 +0000 Subject: [PATCH 5/6] Support dropping delivery for single message transfer as well --- src/Worker/APDelivery.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Worker/APDelivery.php b/src/Worker/APDelivery.php index 4703b8ca16..203c409761 100644 --- a/src/Worker/APDelivery.php +++ b/src/Worker/APDelivery.php @@ -66,14 +66,16 @@ class APDelivery if (empty($uri_id)) { $result = ActivityPub\Delivery::deliver($inbox); $success = $result['success']; + $drop = false; $uri_ids = $result['uri_ids']; } else { $result = ActivityPub\Delivery::deliverToInbox($cmd, $item_id, $inbox, $uid, $receivers, $uri_id); $success = $result['success']; + $drop = $result['drop']; $uri_ids = [$uri_id]; } - if (!$success && !Worker::defer() && !empty($uri_ids)) { + if (!$drop && !$success && !Worker::defer() && !empty($uri_ids)) { foreach ($uri_ids as $uri_id) { Post\Delivery::remove($uri_id, $inbox); Post\DeliveryData::incrementQueueFailed($uri_id); From 74b102b94806bbf5b04b7f2a87acbfeeb68d7525 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 24 May 2022 17:28:35 +0000 Subject: [PATCH 6/6] Increase the delivery queue counter on drop --- src/Protocol/ActivityPub/Delivery.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Protocol/ActivityPub/Delivery.php b/src/Protocol/ActivityPub/Delivery.php index ebf8ffb2b6..6e97b0b927 100644 --- a/src/Protocol/ActivityPub/Delivery.php +++ b/src/Protocol/ActivityPub/Delivery.php @@ -167,9 +167,9 @@ class Delivery self::setSuccess($receivers, $success); - Logger::debug('Delivered', ['uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox, 'success' => $success]); + Logger::debug('Delivered', ['uri-id' => $uri_id, 'uid' => $uid, 'item_id' => $item_id, 'cmd' => $cmd, 'inbox' => $inbox, 'success' => $success, 'serverfailure' => $serverfail, 'drop' => $drop]); - if ($success && in_array($cmd, [WorkerDelivery::POST])) { + if (($success || $drop) && in_array($cmd, [WorkerDelivery::POST])) { Post\DeliveryData::incrementQueueDone($uri_id, Post\DeliveryData::ACTIVITYPUB); }