]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Fix capitalization of table definition key in DBStructure
[friendica.git] / src / Protocol / Diaspora.php
index 50300d136c25e65704c5016551dcd3cb6e1fec00..7704d3f07b4e7aae14ce30626fbd0a13277a8526 100644 (file)
@@ -27,10 +27,12 @@ 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;
 use Friendica\Network\Probe;
+use Friendica\Protocol\ActivityNamespace;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Map;
@@ -46,6 +48,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(array $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 +172,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(string $server_url, array $fields = ['batch', 'id', 'url', '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 +231,7 @@ class Diaspora
                        DBA::update('contact', $fields, $condition, $old);
                } else {
                        Logger::info('Create relay contact', ['fields' => $fields]);
-                       DBA::insert('contact', $fields);
+                       Contact::insert($fields);
                }
        }
 
@@ -219,11 +250,11 @@ class Diaspora
         */
        public static function participantsForThread($thread, array $contacts)
        {
-               $r = DBA::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, `contact`.`protocol`,
+               $r = DBA::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`url`, `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'])) {
@@ -383,8 +414,8 @@ class Diaspora
        /**
         * @brief: Decodes incoming Diaspora message in the new format
         *
-        * @param array   $importer Array of the importer user
         * @param string  $raw      raw post message
+        * @param string  $privKey   The private key of the importer
         * @param boolean $no_exit  Don't do an http exit on error
         *
         * @return array
@@ -394,7 +425,7 @@ class Diaspora
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function decodeRaw(array $importer, $raw, $no_exit = false)
+       public static function decodeRaw(string $raw, string $privKey = '', bool $no_exit = false)
        {
                $data = json_decode($raw);
 
@@ -404,7 +435,7 @@ class Diaspora
                        $ciphertext = base64_decode($data->encrypted_magic_envelope);
 
                        $outer_key_bundle = '';
-                       @openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $importer['prvkey']);
+                       @openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
                        $j_outer_key_bundle = json_decode($outer_key_bundle);
 
                        if (!is_object($j_outer_key_bundle)) {
@@ -435,7 +466,7 @@ class Diaspora
                        }
                }
 
-               $base = $basedom->children(NAMESPACE_SALMON_ME);
+               $base = $basedom->children(ActivityNamespace::SALMON_ME);
 
                // Not sure if this cleaning is needed
                $data = str_replace([" ", "\t", "\r", "\n"], ["", "", "", ""], $base->data);
@@ -489,8 +520,8 @@ class Diaspora
        /**
         * @brief: Decodes incoming Diaspora message in the deprecated format
         *
-        * @param array  $importer Array of the importer user
         * @param string $xml      urldecoded Diaspora salmon
+        * @param string $privKey  The private key of the importer
         *
         * @return array
         * 'message' -> decoded Diaspora XML message
@@ -499,7 +530,7 @@ class Diaspora
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function decode(array $importer, $xml)
+       public static function decode(string $xml, string $privKey = '')
        {
                $public = false;
                $basedom = XML::parseString($xml);
@@ -518,7 +549,7 @@ class Diaspora
                        $author_link = str_replace('acct:', '', $children->header->author_id);
                } else {
                        // This happens with posts from a relais
-                       if (!$importer) {
+                       if (empty($privKey)) {
                                Logger::log("This is no private post in the old format", Logger::DEBUG);
                                return false;
                        }
@@ -529,7 +560,7 @@ class Diaspora
                        $ciphertext = base64_decode($encrypted_header->ciphertext);
 
                        $outer_key_bundle = '';
-                       openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $importer['prvkey']);
+                       openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
 
                        $j_outer_key_bundle = json_decode($outer_key_bundle);
 
@@ -547,7 +578,7 @@ class Diaspora
                        $author_link = str_replace('acct:', '', $idom->author_id);
                }
 
-               $dom = $basedom->children(NAMESPACE_SALMON_ME);
+               $dom = $basedom->children(ActivityNamespace::SALMON_ME);
 
                // figure out where in the DOM tree our data is hiding
 
@@ -942,31 +973,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);
 
@@ -975,12 +1016,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);
                        }
                }
 
@@ -1117,6 +1153,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
         *
@@ -1425,23 +1475,21 @@ class Diaspora
         */
        public static function fetchByURL($url, $uid = 0)
        {
-               if (!preg_match("=([http|https].*)/(.*)/(.*)=ism", $url, $matches)) {
+               // 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;
                }
 
-               // Check for Diaspora (and Friendica) typical path components
-               if (!in_array($matches[2], ['posts', 'display'])) {
-                       return false;
-               }
+               $guid = urldecode($matches[2]);
 
-               $item = Item::selectFirst(['id'], ['guid' => $matches[3], 'uid' => $uid]);
+               $item = Item::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
                if (DBA::isResult($item)) {
                        return $item['id'];
                }
 
-               self::storeByGuid($matches[3], $matches[1], $uid);
+               self::storeByGuid($guid, $matches[1], $uid);
 
-               $item = Item::selectFirst(['id'], ['guid' => $matches[3], 'uid' => $uid]);
+               $item = Item::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]);
                if (DBA::isResult($item)) {
                        return $item['id'];
                } else {
@@ -1561,7 +1609,7 @@ class Diaspora
                }
 
                if (self::isRedmatrix($contact["url"])) {
-                       return $contact["url"] . "/?f=&mid=" . $guid;
+                       return $contact["url"] . "/?mid=" . $guid;
                }
 
                if ($parent_guid != '') {
@@ -1798,7 +1846,7 @@ class Diaspora
                $datarray["guid"] = $guid;
                $datarray["uri"] = self::getUriFromGuid($author, $guid);
 
-               $datarray["verb"] = ACTIVITY_POST;
+               $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_COMMENT;
 
                if ($thr_uri != "") {
@@ -1807,7 +1855,7 @@ class Diaspora
                        $datarray["parent-uri"] = $parent_item["uri"];
                }
 
-               $datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
+               $datarray["object-type"] = Activity\ObjectType::COMMENT;
 
                $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
                $datarray["source"] = $xml;
@@ -2015,9 +2063,9 @@ class Diaspora
                // "positive" = "false" would be a Dislike - wich isn't currently supported by Diaspora
                // We would accept this anyhow.
                if ($positive == "true") {
-                       $verb = ACTIVITY_LIKE;
+                       $verb = Activity::LIKE;
                } else {
-                       $verb = ACTIVITY_DISLIKE;
+                       $verb = Activity::DISLIKE;
                }
 
                $datarray = [];
@@ -2038,7 +2086,7 @@ class Diaspora
                $datarray["gravity"] = GRAVITY_ACTIVITY;
                $datarray["parent-uri"] = $parent_item["uri"];
 
-               $datarray["object-type"] = ACTIVITY_OBJ_NOTE;
+               $datarray["object-type"] = Activity\ObjectType::NOTE;
 
                $datarray["body"] = $verb;
 
@@ -2191,7 +2239,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);
 
@@ -2426,101 +2476,30 @@ class Diaspora
                        return false;
                }
 
-               $batch = (($ret["batch"]) ? $ret["batch"] : implode("/", array_slice(explode("/", $ret["url"]), 0, 3))."/receive/public");
-
-               q(
-                       "INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`nurl`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
-                       VALUES (%d, '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d)",
-                       intval($importer["uid"]),
-                       DBA::escape($ret["network"]),
-                       DBA::escape($ret["addr"]),
-                       DateTimeFormat::utcNow(),
-                       DBA::escape($ret["url"]),
-                       DBA::escape(Strings::normaliseLink($ret["url"])),
-                       DBA::escape($batch),
-                       DBA::escape($ret["name"]),
-                       DBA::escape($ret["nick"]),
-                       DBA::escape($ret["photo"]),
-                       DBA::escape($ret["pubkey"]),
-                       DBA::escape($ret["notify"]),
-                       DBA::escape($ret["poll"]),
-                       1,
-                       2
-               );
-
-               // find the contact record we just created
-
-               $contact_record = self::contactByHandle($importer["uid"], $author);
-
-               if (!$contact_record) {
-                       Logger::log("unable to locate newly created contact record.");
-                       return;
-               }
-
-               Logger::log("Author ".$author." was added as contact number ".$contact_record["id"].".", Logger::DEBUG);
-
-               Group::addMember(User::getDefaultGroup($importer['uid'], $ret["network"]), $contact_record['id']);
-
-               Contact::updateAvatar($ret["photo"], $importer['uid'], $contact_record["id"], true);
-
-               if (in_array($importer["page-flags"], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
-                       Logger::log("Sending intra message for author ".$author.".", Logger::DEBUG);
-
-                       $hash = Strings::getRandomHex().(string)time();   // Generate a confirm_key
-
-                       q(
-                               "INSERT INTO `intro` (`uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
-                               VALUES (%d, %d, %d, %d, '%s', '%s', '%s')",
-                               intval($importer["uid"]),
-                               intval($contact_record["id"]),
-                               0,
-                               0,
-                               DBA::escape(L10n::t("Sharing notification from Diaspora network")),
-                               DBA::escape($hash),
-                               DBA::escape(DateTimeFormat::utcNow())
-                       );
+               $cid = Contact::getIdForURL($ret['url'], $importer['uid']);
+               if (!empty($cid)) {
+                       $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
                } else {
-                       // automatic friend approval
-
-                       Logger::log("Does an automatic friend approval for author ".$author.".", Logger::DEBUG);
+                       $contact = [];
+               }
 
-                       Contact::updateAvatar($contact_record["photo"], $importer["uid"], $contact_record["id"]);
+               $item = ['author-id' => Contact::getIdForURL($ret['url']),
+                       'author-link' => $ret['url']];
 
-                       /*
-                        * technically they are sharing with us (Contact::SHARING),
-                        * but if our page-type is Profile::PAGE_COMMUNITY or Profile::PAGE_SOAPBOX
-                        * we are going to change the relationship and make them a follower.
-                        */
-                       if (($importer["page-flags"] == User::PAGE_FLAGS_FREELOVE) && $sharing && $following) {
-                               $new_relation = Contact::FRIEND;
-                       } elseif (($importer["page-flags"] == User::PAGE_FLAGS_FREELOVE) && $sharing) {
-                               $new_relation = Contact::SHARING;
-                       } else {
-                               $new_relation = Contact::FOLLOWER;
+               $result = Contact::addRelationship($importer, $contact, $item, false);
+               if ($result === true) {
+                       $contact_record = self::contactByHandle($importer['uid'], $author);
+                       if (!$contact_record) {
+                               Logger::info('unable to locate newly created contact record.');
+                               return;
                        }
 
-                       q(
-                               "UPDATE `contact` SET `rel` = %d,
-                               `name-date` = '%s',
-                               `uri-date` = '%s',
-                               `blocked` = 0,
-                               `pending` = 0,
-                               `writable` = 1
-                               WHERE `id` = %d
-                               ",
-                               intval($new_relation),
-                               DBA::escape(DateTimeFormat::utcNow()),
-                               DBA::escape(DateTimeFormat::utcNow()),
-                               intval($contact_record["id"])
-                       );
-
-                       $user = DBA::selectFirst('user', [], ['uid' => $importer["uid"]]);
+                       $user = DBA::selectFirst('user', [], ['uid' => $importer['uid']]);
                        if (DBA::isResult($user)) {
-                               Logger::log("Sending share message (Relation: ".$new_relation.") to author ".$author." - Contact: ".$contact_record["id"]." - User: ".$importer["uid"], Logger::DEBUG);
                                self::sendShare($user, $contact_record);
 
                                // Send the profile data, maybe it weren't transmitted before
-                               self::sendProfile($importer["uid"], [$contact_record]);
+                               self::sendProfile($importer['uid'], [$contact_record]);
                        }
                }
 
@@ -2544,7 +2523,7 @@ class Diaspora
                }
 
                // Do we already have this item?
-               $fields = ['body', 'tag', 'app', 'created', 'object-type', 'uri', 'guid',
+               $fields = ['body', 'title', 'attach', 'tag', 'app', 'created', 'object-type', 'uri', 'guid',
                        'author-name', 'author-link', 'author-avatar'];
                $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => false];
                $item = Item::selectFirst($fields, $condition);
@@ -2588,7 +2567,7 @@ class Diaspora
                        }
 
                        if ($stored) {
-                               $fields = ['body', 'tag', 'app', 'created', 'object-type', 'uri', 'guid',
+                               $fields = ['body', 'title', 'attach', 'tag', 'app', 'created', 'object-type', 'uri', 'guid',
                                        'author-name', 'author-link', 'author-avatar'];
                                $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => false];
                                $item = Item::selectFirst($fields, $condition);
@@ -2635,9 +2614,9 @@ class Diaspora
                $datarray['uri'] = self::getUriFromGuid($author, $datarray['guid']);
                $datarray['parent-uri'] = $parent['uri'];
 
-               $datarray['verb'] = $datarray['body'] = ACTIVITY2_ANNOUNCE;
+               $datarray['verb'] = $datarray['body'] = Activity::ANNOUNCE;
                $datarray['gravity'] = GRAVITY_ACTIVITY;
-               $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
+               $datarray['object-type'] = Activity\ObjectType::NOTE;
 
                $datarray['protocol'] = $item['protocol'];
 
@@ -2708,7 +2687,7 @@ class Diaspora
                $datarray["guid"] = $guid;
                $datarray["uri"] = $datarray["parent-uri"] = self::getUriFromGuid($author, $guid);
 
-               $datarray["verb"] = ACTIVITY_POST;
+               $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_PARENT;
 
                $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
@@ -2722,9 +2701,15 @@ class Diaspora
                        $original_item["created"],
                        $orig_url
                );
+
+               if (!empty($original_item['title'])) {
+                       $prefix .= '[h3]' . $original_item['title'] . "[/h3]\n";
+               }
+
                $datarray["body"] = $prefix.$original_item["body"]."[/share]";
 
                $datarray["tag"] = $original_item["tag"];
+               $datarray["attach"] = $original_item["attach"];
                $datarray["app"]  = $original_item["app"];
 
                $datarray["plink"] = self::plink($author, $guid);
@@ -2913,9 +2898,9 @@ class Diaspora
                                        XML::unescape($photo->remote_photo_name)."[/img]\n".$body;
                        }
 
-                       $datarray["object-type"] = ACTIVITY_OBJ_IMAGE;
+                       $datarray["object-type"] = Activity\ObjectType::IMAGE;
                } else {
-                       $datarray["object-type"] = ACTIVITY_OBJ_NOTE;
+                       $datarray["object-type"] = Activity\ObjectType::NOTE;
 
                        // Add OEmbed and other information to the body
                        if (!self::isRedmatrix($contact["url"])) {
@@ -2945,7 +2930,7 @@ class Diaspora
                $datarray["guid"] = $guid;
                $datarray["uri"] = $datarray["parent-uri"] = self::getUriFromGuid($author, $guid);
 
-               $datarray["verb"] = ACTIVITY_POST;
+               $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_PARENT;
 
                $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
@@ -3164,8 +3149,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'])) {
@@ -3175,6 +3158,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;
@@ -3404,72 +3391,40 @@ class Diaspora
        {
                $body = trim($body);
 
-               // Skip if it isn't a pure repeated messages
-               // Does it start with a share?
-               if ((strpos($body, "[share") > 0) && $complete) {
-                       return false;
-               }
-
-               // Does it end with a share?
-               if (strlen($body) > (strrpos($body, "[/share]") + 8)) {
+               $reshared = Item::getShareArray(['body' => $body]);
+               if (empty($reshared)) {
                        return false;
                }
 
-               $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
-               // Skip if there is no shared message in there
-               if ($body == $attributes) {
+               // Skip if it isn't a pure repeated messages
+               // Does it start with a share?
+               if (!empty($reshared['comment']) && $complete) {
                        return false;
                }
 
-               // If we don't do the complete check we quit here
-
-               $guid = "";
-               preg_match("/guid='(.*?)'/ism", $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $guid = $matches[1];
-               }
-
-               preg_match('/guid="(.*?)"/ism', $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $guid = $matches[1];
-               }
-
-               if (($guid != "") && $complete) {
-                       $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
+               if (!empty($reshared['guid']) && $complete) {
+                       $condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
                        $item = Item::selectFirst(['contact-id'], $condition);
                        if (DBA::isResult($item)) {
-                               $ret= [];
+                               $ret = [];
                                $ret["root_handle"] = self::handleFromContact($item["contact-id"]);
-                               $ret["root_guid"] = $guid;
+                               $ret["root_guid"] = $reshared['guid'];
                                return $ret;
                        } elseif ($complete) {
                                // We are resharing something that isn't a DFRN or Diaspora post.
                                // So we have to return "false" on "$complete" to not trigger a reshare.
                                return false;
                        }
-               } elseif (($guid == "") && $complete) {
+               } elseif (empty($reshared['guid']) && $complete) {
                        return false;
                }
 
-               $ret["root_guid"] = $guid;
+               $ret = [];
 
-               $profile = "";
-               preg_match("/profile='(.*?)'/ism", $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $profile = $matches[1];
-               }
-
-               preg_match('/profile="(.*?)"/ism', $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $profile = $matches[1];
-               }
-
-               $ret= [];
-
-               if ($profile != "") {
-                       if (Contact::getIdForURL($profile)) {
-                               $author = Contact::getDetailsByURL($profile);
-                               $ret["root_handle"] = $author['addr'];
+               if (!empty($reshared['profile']) && ($cid = Contact::getIdForURL($reshared['profile']))) {
+                       $contact = DBA::selectFirst('contact', ['addr'], ['id' => $cid]);
+                       if (!empty($contact['addr'])) {
+                               $ret['root_handle'] = $contact['addr'];
                        }
                }
 
@@ -3584,8 +3539,7 @@ class Diaspora
                $myaddr = self::myHandle($owner);
 
                $public = ($item["private"] ? "false" : "true");
-
-               $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
+               $created = DateTimeFormat::utc($item['received'], DateTimeFormat::ATOM);
                $edited = DateTimeFormat::utc($item["edited"] ?? $item["created"], DateTimeFormat::ATOM);
 
                // Detect a share element and do a reshare
@@ -3603,6 +3557,14 @@ class Diaspora
                        $title = $item["title"];
                        $body = $item["body"];
 
+                       // Fetch the title from an attached link - if there is one
+                       if (empty($item["title"]) && PConfig::get($owner['uid'], 'system', 'attach_link_title')) {
+                               $page_data = BBCode::getAttachmentData($item['body']);
+                               if (!empty($page_data['type']) && !empty($page_data['title']) && ($page_data['type'] == 'link')) {
+                                       $title = $page_data['title'];
+                               }
+                       }
+
                        if ($item['author-link'] != $item['owner-link']) {
                                require_once 'mod/share.php';
                                $body = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
@@ -3614,7 +3576,7 @@ class Diaspora
 
                        // Adding the title
                        if (strlen($title)) {
-                               $body = "## ".html_entity_decode($title)."\n\n".$body;
+                               $body = "### ".html_entity_decode($title)."\n\n".$body;
                        }
 
                        if ($item["attach"]) {
@@ -3729,9 +3691,9 @@ class Diaspora
 
                $target_type = ($parent["uri"] === $parent["parent-uri"] ? "Post" : "Comment");
                $positive = null;
-               if ($item['verb'] === ACTIVITY_LIKE) {
+               if ($item['verb'] === Activity::LIKE) {
                        $positive = "true";
-               } elseif ($item['verb'] === ACTIVITY_DISLIKE) {
+               } elseif ($item['verb'] === Activity::DISLIKE) {
                        $positive = "false";
                }
 
@@ -3760,13 +3722,13 @@ class Diaspora
                }
 
                switch ($item['verb']) {
-                       case ACTIVITY_ATTEND:
+                       case Activity::ATTEND:
                                $attend_answer = 'accepted';
                                break;
-                       case ACTIVITY_ATTENDNO:
+                       case Activity::ATTENDNO:
                                $attend_answer = 'declined';
                                break;
-                       case ACTIVITY_ATTENDMAYBE:
+                       case Activity::ATTENDMAYBE:
                                $attend_answer = 'tentative';
                                break;
                        default:
@@ -3799,7 +3761,7 @@ class Diaspora
                        return $result;
                }
 
-               $toplevel_item = Item::selectFirst(['guid', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]);
+               $toplevel_item = Item::selectFirst(['guid', 'author-id', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]);
                if (!DBA::isResult($toplevel_item)) {
                        Logger::error('Missing parent conversation item', ['parent' => $item["parent"]]);
                        return false;
@@ -3807,12 +3769,18 @@ class Diaspora
 
                $thread_parent_item = $toplevel_item;
                if ($item['thr-parent'] != $item['parent-uri']) {
-                       $thread_parent_item = Item::selectFirst(['guid', 'author-link'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
+                       $thread_parent_item = Item::selectFirst(['guid', 'author-id', 'author-link'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
                }
 
                $body = $item["body"];
 
-               if ((empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions'))
+               // The replied to autor mention is prepended for clarity if:
+               // - Item replied isn't yours
+               // - Item is public or explicit mentions are disabled
+               // - Implicit mentions are enabled
+               if (
+                       $item['author-id'] != $thread_parent_item['author-id']
+                       && (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions'))
                        && !Config::get('system', 'disable_implicit_mentions')
                ) {
                        $body = self::prependParentAuthorMention($body, $thread_parent_item['author-link']);
@@ -3856,13 +3824,13 @@ class Diaspora
         */
        public static function sendFollowup(array $item, array $owner, array $contact, $public_batch = false)
        {
-               if (in_array($item['verb'], [ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE])) {
+               if (in_array($item['verb'], [Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE])) {
                        $message = self::constructAttend($item, $owner);
                        $type = "event_participation";
-               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               } elseif (in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        $message = self::constructLike($item, $owner);
                        $type = "like";
-               } elseif (!in_array($item["verb"], [ACTIVITY_FOLLOW, ACTIVITY_TAG])) {
+               } elseif (!in_array($item["verb"], [Activity::FOLLOW, Activity::TAG])) {
                        $message = self::constructComment($item, $owner);
                        $type = "comment";
                }
@@ -3891,7 +3859,7 @@ class Diaspora
                        $message = ["author" => $item['signer'],
                                        "target_guid" => $signed_parts[0],
                                        "target_type" => $signed_parts[1]];
-               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               } elseif (in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        $message = ["author" => $signed_parts[4],
                                        "guid" => $signed_parts[1],
                                        "parent_guid" => $signed_parts[3],
@@ -3936,7 +3904,7 @@ class Diaspora
        {
                if ($item["deleted"]) {
                        return self::sendRetraction($item, $owner, $contact, $public_batch, true);
-               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               } elseif (in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        $type = "like";
                } else {
                        $type = "comment";
@@ -3997,7 +3965,7 @@ class Diaspora
 
                if ($item['id'] == $item['parent']) {
                        $target_type = "Post";
-               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               } elseif (in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        $target_type = "Like";
                } else {
                        $target_type = "Comment";
@@ -4263,7 +4231,7 @@ class Diaspora
                        return false;
                }
 
-               if (!in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               if (!in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        return false;
                }