]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Fix performance issues due to relay contact requests
[friendica.git] / src / Protocol / Diaspora.php
index d578ba4545264085a265dd6ac3f574d721547707..2ff271ee0da0a2a40cf6b7fd29dfb99d967fd3ad 100644 (file)
@@ -27,6 +27,7 @@ use Friendica\Model\Conversation;
 use Friendica\Model\GContact;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
+use Friendica\Model\ItemDeliveryData;
 use Friendica\Model\Mail;
 use Friendica\Model\Profile;
 use Friendica\Model\User;
@@ -46,6 +47,36 @@ use SimpleXMLElement;
  */
 class Diaspora
 {
+       /**
+        * Mark the relay contact of the given contact for archival
+        * This is called whenever there is a communication issue with the server.
+        * It avoids sending stuff to servers who don't exist anymore.
+        * The relay contact is a technical contact entry that exists once per server.
+        *
+        * @param array $contact of the relay contact
+        */
+       public static function markRelayForArchival($contact)
+       {
+               if (!empty($contact['contact-type']) && ($contact['contact-type'] == Contact::TYPE_RELAY)) {
+                       // This is already the relay contact, we don't need to fetch it
+                       $relay_contact = $contact;
+               } elseif (empty($contact['baseurl'])) {
+                       if (!empty($contact['batch'])) {
+                               $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => Contact::TYPE_RELAY];
+                               $relay_contact = DBA::selectFirst('contact', [], $condition);
+                       } else {
+                               return;
+                       }
+               } else {
+                       $relay_contact = self::getRelayContact($contact['baseurl'], []);
+               }
+
+               if (!empty($relay_contact)) {
+                       Logger::info('Relay contact will be marked for archival', ['id' => $relay_contact['id'], 'url' => $relay_contact['url']]);
+                       Contact::markForArchival($relay_contact);
+               }
+       }
+
        /**
         * @brief Return a list of relay servers
         *
@@ -140,13 +171,12 @@ class Diaspora
         * @brief Return a contact for a given server address or creates a dummy entry
         *
         * @param string $server_url The url of the server
+        * @param array $fields Fieldlist
         * @return array with the contact
         * @throws \Exception
         */
-       private static function getRelayContact($server_url)
+       private static function getRelayContact($server_url, $fields = ['batch', 'id', 'name', 'network', 'protocol', 'archive', 'blocked'])
        {
-               $fields = ['batch', 'id', 'name', 'network', 'protocol', 'archive', 'blocked'];
-
                // Fetch the relay contact
                $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
                        'contact-type' => Contact::TYPE_RELAY];
@@ -200,7 +230,7 @@ class Diaspora
                        DBA::update('contact', $fields, $condition, $old);
                } else {
                        Logger::info('Create relay contact', ['fields' => $fields]);
-                       DBA::insert('contact', $fields);
+                       Contact::insert($fields);
                }
        }
 
@@ -2208,7 +2238,9 @@ class Diaspora
                        }
 
                        Logger::info('Deliver participation', ['item' => $comment['id'], 'contact' => $contact_id]);
-                       Worker::add(PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['id'], $contact_id);
+                       if (Worker::add(PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['id'], $contact_id)) {
+                               ItemDeliveryData::incrementQueueCount($comment['id'], 1);
+                       }
                }
                DBA::close($comments);
 
@@ -3181,8 +3213,6 @@ class Diaspora
 
                $logid = Strings::getRandomHex(4);
 
-               $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
-
                // We always try to use the data from the fcontact table.
                // This is important for transmitting data to Friendica servers.
                if (!empty($contact['addr'])) {
@@ -3192,6 +3222,10 @@ class Diaspora
                        }
                }
 
+               if (empty($dest_url)) {
+                       $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
+               }
+
                if (!$dest_url) {
                        Logger::log("no url for contact: ".$contact["id"]." batch mode =".$public_batch);
                        return 0;