]> 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 768b4c7b07e8789ad36ef7745ab2487a42ffa9f0..2ff271ee0da0a2a40cf6b7fd29dfb99d967fd3ad 100644 (file)
@@ -27,8 +27,9 @@ 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\Queue;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Util\Crypto;
@@ -37,6 +38,7 @@ use Friendica\Util\Map;
 use Friendica\Util\Network;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
+use Friendica\Worker\Delivery;
 use SimpleXMLElement;
 
 /**
@@ -45,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
         *
@@ -139,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', 'archive', 'blocked'];
-
                // Fetch the relay contact
                $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
                        'contact-type' => Contact::TYPE_RELAY];
@@ -179,22 +210,28 @@ class Diaspora
        public static function setRelayContact($server_url, array $network_fields = [])
        {
                $fields = ['created' => DateTimeFormat::utcNow(),
-                       'name' => 'relay', 'nick' => 'relay',
-                       'url' => $server_url, 'network' => Protocol::DIASPORA,
+                       'name' => 'relay', 'nick' => 'relay', 'url' => $server_url,
+                       'nurl' => Strings::normaliseLink($server_url),
+                       'network' => Protocol::DIASPORA, 'uid' => 0,
                        'batch' => $server_url . '/receive/public',
                        'rel' => Contact::FOLLOWER, 'blocked' => false,
-                       'pending' => false, 'writable' => true];
+                       'pending' => false, 'writable' => true,
+                       'baseurl' => $server_url, 'contact-type' => Contact::TYPE_RELAY];
 
                $fields = array_merge($fields, $network_fields);
 
-               $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
-                       'contact-type' => Contact::TYPE_RELAY];
-
-               if (DBA::exists('contact', $condition)) {
+               $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url)];
+               $old = DBA::selectFirst('contact', [], $condition);
+               if (DBA::isResult($old)) {
                        unset($fields['created']);
-               }
+                       $condition = ['id' => $old['id']];
 
-               DBA::update('contact', $fields, $condition, true);
+                       Logger::info('Update relay contact', ['fields' => $fields, 'condition' => $condition]);
+                       DBA::update('contact', $fields, $condition, $old);
+               } else {
+                       Logger::info('Create relay contact', ['fields' => $fields]);
+                       Contact::insert($fields);
+               }
        }
 
        /**
@@ -212,11 +249,11 @@ class Diaspora
         */
        public static function participantsForThread($thread, array $contacts)
        {
-               $r = DBA::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`,
+               $r = DBA::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, `contact`.`protocol`,
                                `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation`
                                INNER JOIN `contact` ON `contact`.`id` = `participation`.`cid`
                                INNER JOIN `fcontact` ON `fcontact`.`id` = `participation`.`fid`
-                               WHERE `participation`.`iid` = ?", $thread);
+                               WHERE `participation`.`iid` = ? AND NOT `contact`.`archive`", $thread);
 
                while ($contact = DBA::fetch($r)) {
                        if (!empty($contact['fnetwork'])) {
@@ -224,6 +261,10 @@ class Diaspora
                        }
                        unset($contact['fnetwork']);
 
+                       if (empty($contact['protocol'])) {
+                               $contact['protocol'] = $contact['network'];
+                       }
+
                        if (empty($contact['batch']) && !empty($contact['fbatch'])) {
                                $contact['batch'] = $contact['fbatch'];
                        }
@@ -401,7 +442,7 @@ class Diaspora
                                if ($no_exit) {
                                        return false;
                                } else {
-                                       System::httpExit(400);
+                                       throw new \Friendica\Network\HTTPException\BadRequestException();
                                }
                        }
 
@@ -420,7 +461,7 @@ class Diaspora
                        if ($no_exit) {
                                return false;
                        } else {
-                               System::httpExit(400);
+                               throw new \Friendica\Network\HTTPException\BadRequestException();
                        }
                }
 
@@ -446,7 +487,7 @@ class Diaspora
                        if ($no_exit) {
                                return false;
                        } else {
-                               System::httpExit(400);
+                               throw new \Friendica\Network\HTTPException\BadRequestException();
                        }
                }
 
@@ -456,7 +497,7 @@ class Diaspora
                        if ($no_exit) {
                                return false;
                        } else {
-                               System::httpExit(400);
+                               throw new \Friendica\Network\HTTPException\BadRequestException();
                        }
                }
 
@@ -466,7 +507,7 @@ class Diaspora
                        if ($no_exit) {
                                return false;
                        } else {
-                               System::httpExit(400);
+                               throw new \Friendica\Network\HTTPException\BadRequestException();
                        }
                }
 
@@ -551,7 +592,7 @@ class Diaspora
 
                if (!$base) {
                        Logger::log('unable to locate salmon data in xml');
-                       System::httpExit(400);
+                       throw new \Friendica\Network\HTTPException\BadRequestException();
                }
 
 
@@ -589,7 +630,7 @@ class Diaspora
 
                if (!$author_link) {
                        Logger::log('Could not retrieve author URI.');
-                       System::httpExit(400);
+                       throw new \Friendica\Network\HTTPException\BadRequestException();
                }
                // Once we have the author URI, go to the web and try to find their public key
                // (first this will look it up locally if it is in the fcontact cache)
@@ -600,14 +641,14 @@ class Diaspora
 
                if (!$key) {
                        Logger::log('Could not retrieve author key.');
-                       System::httpExit(400);
+                       throw new \Friendica\Network\HTTPException\BadRequestException();
                }
 
                $verify = Crypto::rsaVerify($signed_data, $signature, $key);
 
                if (!$verify) {
                        Logger::log('Message did not verify. Discarding.');
-                       System::httpExit(400);
+                       throw new \Friendica\Network\HTTPException\BadRequestException();
                }
 
                Logger::log('Message verified.');
@@ -931,31 +972,41 @@ class Diaspora
         * @brief Fetches data for a given handle
         *
         * @param string $handle The handle
+        * @param boolean $update true = always update, false = never update, null = update when not found or outdated
         *
         * @return array the queried data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function personByHandle($handle)
+       public static function personByHandle($handle, $update = null)
        {
-               $update = false;
-
                $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
+               if (!DBA::isResult($person)) {
+                       $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
+                       $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]);
+               }
+
                if (DBA::isResult($person)) {
                        Logger::debug("In cache " . print_r($person, true));
 
-                       // update record occasionally so it doesn't get stale
-                       $d = strtotime($person["updated"]." +00:00");
-                       if ($d < strtotime("now - 14 days")) {
-                               $update = true;
-                       }
+                       if (is_null($update)) {
+                               // update record occasionally so it doesn't get stale
+                               $d = strtotime($person["updated"]." +00:00");
+                               if ($d < strtotime("now - 14 days")) {
+                                       $update = true;
+                               }
 
-                       if ($person["guid"] == "") {
-                               $update = true;
+                               if ($person["guid"] == "") {
+                                       $update = true;
+                               }
                        }
+               } elseif (is_null($update)) {
+                       $update = !DBA::isResult($person);
+               } else {
+                       $person = [];
                }
 
-               if (!DBA::isResult($person) || $update) {
+               if ($update) {
                        Logger::log("create or refresh", Logger::DEBUG);
                        $r = Probe::uri($handle, Protocol::DIASPORA);
 
@@ -964,12 +1015,7 @@ class Diaspora
                        if ($r && ($r["network"] === Protocol::DIASPORA)) {
                                self::updateFContact($r);
 
-                               // Fetch the updated or added contact
-                               $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
-                               if (!DBA::isResult($person)) {
-                                       $person = $r;
-                                       $person['id'] = 0;
-                               }
+                               $person = self::personByHandle($handle, false);
                        }
                }
 
@@ -1106,6 +1152,20 @@ class Diaspora
                return $contact;
        }
 
+       /**
+        * Checks if the given contact url does support ActivityPub
+        *
+        * @param string  $url    profile url
+        * @param boolean $update true = always update, false = never update, null = update when not found or outdated
+        * @return boolean
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function isSupportedByContactUrl($url, $update = null)
+       {
+               return !empty(self::personByHandle($url, $update));
+       }
+
        /**
         * @brief Check if posting is allowed for this contact
         *
@@ -1403,6 +1463,39 @@ class Diaspora
                return $msg;
        }
 
+       /**
+        * @brief Fetches an item with a given URL
+        *
+        * @param string $url the message url
+        *
+        * @return int the message id of the stored message or false
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function fetchByURL($url, $uid = 0)
+       {
+               // Check for Diaspora (and Friendica) typical paths
+               if (!preg_match("=(https?://.+)/(?:posts|display)/([a-zA-Z0-9-_@.:%]+[a-zA-Z0-9])=i", $url, $matches)) {
+                       return false;
+               }
+
+               $guid = urldecode($matches[2]);
+
+               $item = Item::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
+               if (DBA::isResult($item)) {
+                       return $item['id'];
+               }
+
+               self::storeByGuid($guid, $matches[1], $uid);
+
+               $item = Item::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
+               if (DBA::isResult($item)) {
+                       return $item['id'];
+               } else {
+                       return false;
+               }
+       }
+
        /**
         * @brief Fetches the item record of a given guid
         *
@@ -1842,14 +1935,7 @@ class Diaspora
 
                $person = self::personByHandle($msg_author);
 
-               DBA::lock('mail');
-
-               if (DBA::exists('mail', ['guid' => $msg_guid, 'uid' => $importer["uid"]])) {
-                       Logger::log("duplicate message already delivered.", Logger::DEBUG);
-                       return false;
-               }
-
-               DBA::insert('mail', [
+               return Mail::insert([
                        'uid'        => $importer['uid'],
                        'guid'       => $msg_guid,
                        'convid'     => $conversation['id'],
@@ -1859,36 +1945,10 @@ class Diaspora
                        'contact-id' => $contact['id'],
                        'title'      => $subject,
                        'body'       => $body,
-                       'seen'       => 0,
-                       'reply'      => 0,
                        'uri'        => $message_uri,
                        'parent-uri' => $author . ':' . $guid,
                        'created'    => $msg_created_at
                ]);
-
-               $message_id = DBA::lastInsertId();
-
-               DBA::unlock();
-
-               DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]);
-
-               notification([
-                       "type" => NOTIFY_MAIL,
-                       "notify_flags" => $importer["notify-flags"],
-                       "language" => $importer["language"],
-                       "to_name" => $importer["username"],
-                       "to_email" => $importer["email"],
-                       "uid" => $importer["uid"],
-                       "item" => ["id" => $message_id, "title" => $subject, "subject" => $subject, "body" => $body],
-                       "parent" => $conversation["id"],
-                       "source_name" => $person["name"],
-                       "source_link" => $person["url"],
-                       "source_photo" => $person["photo"],
-                       "verb" => ACTIVITY_POST,
-                       "otype" => "mail"
-               ]);
-
-               return true;
        }
 
        /**
@@ -2106,14 +2166,7 @@ class Diaspora
 
                $body = self::replacePeopleGuid($body, $person["url"]);
 
-               DBA::lock('mail');
-
-               if (DBA::exists('mail', ['guid' => $guid, 'uid' => $importer["uid"]])) {
-                       Logger::log("duplicate message already delivered.", Logger::DEBUG);
-                       return false;
-               }
-
-               DBA::insert('mail', [
+               return Mail::insert([
                        'uid'        => $importer['uid'],
                        'guid'       => $guid,
                        'convid'     => $conversation['id'],
@@ -2123,36 +2176,11 @@ class Diaspora
                        'contact-id' => $contact['id'],
                        'title'      => $conversation['subject'],
                        'body'       => $body,
-                       'seen'       => 0,
                        'reply'      => 1,
                        'uri'        => $message_uri,
                        'parent-uri' => $author.":".$conversation['guid'],
                        'created'    => $created_at
                ]);
-
-               $message_id = DBA::lastInsertId();
-
-               DBA::unlock();
-
-               DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]);
-
-               notification([
-                       "type" => NOTIFY_MAIL,
-                       "notify_flags" => $importer["notify-flags"],
-                       "language" => $importer["language"],
-                       "to_name" => $importer["username"],
-                       "to_email" => $importer["email"],
-                       "uid" => $importer["uid"],
-                       "item" => ["id" => $message_id, "title" => $conversation["subject"], "subject" => $conversation["subject"], "body" => $body],
-                       "parent" => $conversation["id"],
-                       "source_name" => $person["name"],
-                       "source_link" => $person["url"],
-                       "source_photo" => $person["photo"],
-                       "verb" => ACTIVITY_POST,
-                       "otype" => "mail"
-               ]);
-
-               return true;
        }
 
        /**
@@ -2208,13 +2236,11 @@ class Diaspora
                        if ($comment['id'] == $comment['parent']) {
                                continue;
                        }
-                       if ($comment['verb'] == ACTIVITY_POST) {
-                               $cmd = $comment['self'] ? 'comment-new' : 'comment-import';
-                       } else {
-                               $cmd = $comment['self'] ? 'like' : 'comment-import';
+
+                       Logger::info('Deliver participation', ['item' => $comment['id'], 'contact' => $contact_id]);
+                       if (Worker::add(PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['id'], $contact_id)) {
+                               ItemDeliveryData::incrementQueueCount($comment['id'], 1);
                        }
-                       Logger::log("Send ".$cmd." for item ".$comment['id']." to contact ".$contact_id, Logger::DEBUG);
-                       Worker::add(PRIORITY_HIGH, 'Delivery', $cmd, $comment['id'], $contact_id);
                }
                DBA::close($comments);
 
@@ -2322,8 +2348,8 @@ class Diaspora
                $fields = ['name' => $name, 'location' => $location,
                        'name-date' => DateTimeFormat::utcNow(),
                        'about' => $about, 'gender' => $gender,
-                       'addr' => $author, 'nick' => $nick,
-                       'keywords' => $keywords];
+                       'addr' => $author, 'nick' => $nick, 'keywords' => $keywords,
+                       'unsearchable' => !$searchable, 'sensitive' => $nsfw];
 
                if (!empty($birthday)) {
                        $fields['bd'] = $birthday;
@@ -2331,6 +2357,8 @@ class Diaspora
 
                DBA::update('contact', $fields, ['id' => $contact['id']]);
 
+               // @todo Update the public contact, then update the gcontact from that
+
                $gcontact = ["url" => $contact["url"], "network" => Protocol::DIASPORA, "generation" => 2,
                                        "photo" => $image_url, "name" => $name, "location" => $location,
                                        "about" => $about, "birthday" => $birthday, "gender" => $gender,
@@ -3170,15 +3198,13 @@ class Diaspora
         * @param array  $contact      Target of the communication
         * @param string $envelope     The message that is to be transmitted
         * @param bool   $public_batch Is it a public post?
-        * @param bool   $queue_run    Is the transmission called from the queue?
         * @param string $guid         message guid
         *
-        * @param bool   $no_queue
         * @return int Result of the transmission
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function transmit(array $owner, array $contact, $envelope, $public_batch, $queue_run = false, $guid = "", $no_queue = false)
+       private static function transmit(array $owner, array $contact, $envelope, $public_batch, $guid = "")
        {
                $enabled = intval(Config::get("system", "diaspora_enabled"));
                if (!$enabled) {
@@ -3187,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'])) {
@@ -3198,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;
@@ -3205,36 +3233,18 @@ class Diaspora
 
                Logger::log("transmit: ".$logid."-".$guid." ".$dest_url);
 
-               if (!$queue_run && Queue::wasDelayed($contact["id"])) {
-                       $return_code = 0;
-               } else {
-                       if (!intval(Config::get("system", "diaspora_test"))) {
-                               $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json");
+               if (!intval(Config::get("system", "diaspora_test"))) {
+                       $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json");
 
-                               $postResult = Network::post($dest_url."/", $envelope, ["Content-Type: ".$content_type]);
-                               $return_code = $postResult->getReturnCode();
-                       } else {
-                               Logger::log("test_mode");
-                               return 200;
-                       }
+                       $postResult = Network::post($dest_url."/", $envelope, ["Content-Type: ".$content_type]);
+                       $return_code = $postResult->getReturnCode();
+               } else {
+                       Logger::log("test_mode");
+                       return 200;
                }
 
                Logger::log("transmit: ".$logid."-".$guid." to ".$dest_url." returns: ".$return_code);
 
-               if (!$return_code || (($return_code == 503) && (stristr($postResult->getHeader(), "retry-after")))) {
-                       if (!$no_queue && !empty($contact['contact-type']) && ($contact['contact-type'] != Contact::TYPE_RELAY)) {
-                               Logger::info('defer message', ['log' => $logid, 'guid' => $guid, 'destination' => $dest_url]);
-                               // defer message for redelivery
-                               Worker::defer();
-                       }
-
-                       // The message could not be delivered. We mark the contact as "dead"
-                       Contact::markForArchival($contact);
-               } elseif (($return_code >= 200) && ($return_code <= 299)) {
-                       // We successfully delivered a message, the contact is alive
-                       Contact::unmarkForArchival($contact);
-               }
-
                return $return_code ? $return_code : -1;
        }
 
@@ -3282,7 +3292,7 @@ class Diaspora
 
                $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $contact['pubkey'], $public_batch);
 
-               $return_code = self::transmit($owner, $contact, $envelope, $public_batch, false, $guid);
+               $return_code = self::transmit($owner, $contact, $envelope, $public_batch, $guid);
 
                Logger::log("guid: ".$guid." result ".$return_code, Logger::DEBUG);
 
@@ -3627,6 +3637,7 @@ class Diaspora
                $public = ($item["private"] ? "false" : "true");
 
                $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
+               $edited = DateTimeFormat::utc($item["edited"] ?? $item["created"], DateTimeFormat::ATOM);
 
                // Detect a share element and do a reshare
                if (!$item['private'] && ($ret = self::isReshare($item["body"]))) {
@@ -3681,6 +3692,7 @@ class Diaspora
                        $message = ["author" => $myaddr,
                                        "guid" => $item["guid"],
                                        "created_at" => $created,
+                                       "edited_at" => $edited,
                                        "public" => $public,
                                        "text" => $body,
                                        "provider_display_name" => $item["app"],
@@ -3859,11 +3871,13 @@ class Diaspora
 
                $text = html_entity_decode(BBCode::toMarkdown($body));
                $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
+               $edited = DateTimeFormat::utc($item["edited"], DateTimeFormat::ATOM);
 
                $comment = [
                        "author"      => self::myHandle($owner),
                        "guid"        => $item["guid"],
                        "created_at"  => $created,
+                       "edited_at"   => $edited,
                        "parent_guid" => $toplevel_item["guid"],
                        "text"        => $text,
                        "author_signature" => ""
@@ -3899,7 +3913,7 @@ class Diaspora
                } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
                        $message = self::constructLike($item, $owner);
                        $type = "like";
-               } elseif (!in_array($item["verb"], [ACTIVITY_FOLLOW])) {
+               } elseif (!in_array($item["verb"], [ACTIVITY_FOLLOW, ACTIVITY_TAG])) {
                        $message = self::constructComment($item, $owner);
                        $type = "comment";
                }
@@ -4276,9 +4290,10 @@ class Diaspora
 
                $message = self::createProfileData($uid);
 
+               // @ToDo Split this into single worker jobs
                foreach ($recips as $recip) {
                        Logger::log("Send updated profile data for user ".$uid." to contact ".$recip["id"], Logger::DEBUG);
-                       self::buildAndTransmit($owner, $recip, "profile", $message, false);
+                       self::buildAndTransmit($owner, $recip, "profile", $message);
                }
        }