]> git.mxchange.org Git - friendica.git/commitdiff
Resubscribe to relay servers
authorMichael <heluecht@pirati.ca>
Tue, 24 May 2022 07:02:42 +0000 (07:02 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 24 May 2022 07:02:42 +0000 (07:02 +0000)
src/Protocol/ActivityPub/Delivery.php
src/Protocol/Relay.php
src/Worker/Cron.php

index 2a2a63fee148f5188f4f3bcb503d2ec45f0dbc66..6f9272be0842a00775181caee2e345010fbc5600 100644 (file)
@@ -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;
index f50b73b7d707de5c6a20bd3d7023d7582770700c..8679bbec5bf1ee50f87fc8d49c1a5457d863ad9d 100644 (file)
@@ -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]);
+               }       
+       }
 }
index 01c54fed1845576124498faf43d02f712bb52eb9..30efc61dba8144f1ec6daade4a3dd96c7d771e05 100644 (file)
@@ -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');