]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/DFRN.php
New function to fetch item data especially for users
[friendica.git] / src / Protocol / DFRN.php
index bffcb2086fb328e99c767cc021abeec1c0a9554e..2487b3ca573adbee4f672432487cf6e9cc5bda93 100644 (file)
@@ -50,9 +50,9 @@ require_once "include/text.php";
 class DFRN
 {
 
-       const DFRN_TOP_LEVEL = 0;       // Top level posting
-       const DFRN_REPLY = 1;           // Regular reply that is stored locally
-       const DFRN_REPLY_RC = 2;        // Reply that will be relayed
+       const TOP_LEVEL = 0;    // Top level posting
+       const REPLY = 1;                // Regular reply that is stored locally
+       const REPLY_RC = 2;     // Reply that will be relayed
 
        /**
         * @brief Generates the atom entries for delivery.php
@@ -63,7 +63,7 @@ class DFRN
         * @param array $owner Owner record
         *
         * @return string DFRN entries
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        public static function entries($items, $owner)
        {
@@ -81,7 +81,7 @@ class DFRN
                        $root->appendChild($entry);
                }
 
-               return(trim($doc->saveXML()));
+               return trim($doc->saveXML());
        }
 
        /**
@@ -120,8 +120,6 @@ class DFRN
                        }
                }
 
-
-
                // default permissions - anonymous user
 
                $sql_extra = " AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' ";
@@ -134,6 +132,7 @@ class DFRN
                );
 
                if (! DBM::is_result($r)) {
+                       logger(sprintf('No contact found for nickname=%d', $owner_nick), LOGGER_WARNING);
                        killme();
                }
 
@@ -169,6 +168,7 @@ class DFRN
                        );
 
                        if (! DBM::is_result($r)) {
+                               logger(sprintf('No contact found for uid=%d', $owner_id), LOGGER_WARNING);
                                killme();
                        }
 
@@ -177,8 +177,10 @@ class DFRN
                        $groups = Group::getIdsByContactId($contact['id']);
 
                        if (count($groups)) {
-                               for ($x = 0; $x < count($groups); $x ++)
+                               for ($x = 0; $x < count($groups); $x ++) {
                                        $groups[$x] = '<' . intval($groups[$x]) . '>' ;
+                               }
+
                                $gs = implode('|', $groups);
                        } else {
                                $gs = '<<>>' ; // Impossible to match
@@ -219,26 +221,17 @@ class DFRN
                        //$sql_extra .= file_tag_file_query('item',$category,'category');
                }
 
-               if ($public_feed) {
-                       if (! $converse) {
-                               $sql_extra .= " AND `contact`.`self` = 1 ";
-                       }
+               if ($public_feed && ! $converse) {
+                       $sql_extra .= " AND `contact`.`self` = 1 ";
                }
 
                $check_date = DateTimeFormat::utc($last_update);
 
                $r = q(
-                       "SELECT `item`.*, `item`.`id` AS `item_id`,
-                       `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
-                       `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
-                       `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
-                       `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
+                       "SELECT `item`.`id`
                        FROM `item` USE INDEX (`uid_wall_changed`) $sql_post_table
                        STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                       AND (NOT `contact`.`blocked` OR `contact`.`pending`)
-                       LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
-                       WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
-                       AND `item`.`wall` AND `item`.`changed` > '%s'
+                       WHERE `item`.`uid` = %d AND `item`.`wall` AND `item`.`changed` > '%s'
                        $sql_extra
                        ORDER BY `item`.`parent` ".$sort.", `item`.`created` ASC LIMIT 0, 300",
                        intval($owner_id),
@@ -246,13 +239,30 @@ class DFRN
                        dbesc($sort)
                );
 
+               $ids = [];
+               foreach ($r as $item) {
+                       $ids[] = $item['id'];
+               }
+
+               if (!empty($ids)) {
+                       $condition = ['id' => $ids];
+                       $fields = ['author-id', 'uid', 'id', 'parent', 'uri', 'thr-parent',
+                               'parent-uri', 'created', 'edited', 'verb', 'object-type',
+                               'guid', 'private', 'title', 'body', 'location', 'coord', 'app',
+                               'attach', 'object', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
+                               'extid', 'target', 'tag', 'bookmark', 'deleted',
+                               'author-link', 'owner-link', 'signed_text', 'signature', 'signer'];
+                       $ret = Item::select($fields, $condition);
+                       $items = dba::inArray($ret);
+               } else {
+                       $items = [];
+               }
+
                /*
                 * Will check further below if this actually returned results.
                 * We will provide an empty feed if that is the case.
                 */
 
-               $items = $r;
-
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
 
@@ -321,33 +331,24 @@ class DFRN
        public static function itemFeed($item_id, $conversation = false)
        {
                if ($conversation) {
-                       $condition = '`item`.`parent`';
+                       $condition = ['parent' => $item_id];
                } else {
-                       $condition = '`item`.`id`';
-               }
-
-               $r = q(
-                       "SELECT `item`.*, `item`.`id` AS `item_id`,
-                       `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
-                       `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
-                       `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
-                       `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
-                       FROM `item`
-                       STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                               AND (NOT `contact`.`blocked` OR `contact`.`pending`)
-                       LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
-                       WHERE %s = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
-                       AND NOT `item`.`private`",
-                       $condition,
-                       intval($item_id)
-               );
-
-               if (!DBM::is_result($r)) {
+                       $condition = ['id' => $item_id];
+               }
+
+               $fields = ['author-id', 'uid', 'id', 'parent', 'uri', 'thr-parent',
+                       'parent-uri', 'created', 'edited', 'verb', 'object-type',
+                       'guid', 'private', 'title', 'body', 'location', 'coord', 'app',
+                       'attach', 'object', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
+                       'extid', 'target', 'tag', 'bookmark', 'deleted',
+                       'author-link', 'owner-link', 'signed_text', 'signature', 'signer'];
+               $ret = Item::select($fields, $condition);
+               $items = dba::inArray($ret);
+               if (!DBM::is_result($items)) {
                        killme();
                }
 
-               $items = $r;
-               $item = $r[0];
+               $item = $items[0];
 
                if ($item['uid'] != 0) {
                        $owner = User::getOwnerDataById($item['uid']);
@@ -397,7 +398,7 @@ class DFRN
         * @param array $owner Owner record
         *
         * @return string DFRN mail
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        public static function mail($item, $owner)
        {
@@ -423,7 +424,7 @@ class DFRN
 
                $root->appendChild($mail);
 
-               return(trim($doc->saveXML()));
+               return trim($doc->saveXML());
        }
 
        /**
@@ -433,7 +434,7 @@ class DFRN
         * @param array $owner Owner record
         *
         * @return string DFRN suggestions
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        public static function fsuggest($item, $owner)
        {
@@ -452,7 +453,7 @@ class DFRN
 
                $root->appendChild($suggest);
 
-               return(trim($doc->saveXML()));
+               return trim($doc->saveXML());
        }
 
        /**
@@ -462,7 +463,7 @@ class DFRN
         * @param int   $uid   User ID
         *
         * @return string DFRN relocations
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        public static function relocate($owner, $uid)
        {
@@ -487,7 +488,6 @@ class DFRN
                        $photos[$p['scale']] = System::baseUrl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
                }
 
-               unset($rp, $ext);
 
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
@@ -511,7 +511,7 @@ class DFRN
 
                $root->appendChild($relocate);
 
-               return(trim($doc->saveXML()));
+               return trim($doc->saveXML());
        }
 
        /**
@@ -524,7 +524,7 @@ class DFRN
         * @param bool   $public        Is it a header for public posts?
         *
         * @return object XML root object
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        private static function addHeader($doc, $owner, $authorelement, $alternatelink = "", $public = false)
        {
@@ -600,7 +600,7 @@ class DFRN
         * @param boolean $public        boolean
         *
         * @return object XML author object
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        private static function addAuthor($doc, $owner, $authorelement, $public)
        {
@@ -744,7 +744,7 @@ class DFRN
         * @param array  $item        Item elements
         *
         * @return object XML author object
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        private static function addEntryAuthor($doc, $element, $contact_url, $item)
        {
@@ -785,7 +785,7 @@ class DFRN
         * @param string $activity activity value
         *
         * @return object XML activity object
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        private static function createActivity($doc, $element, $activity)
        {
@@ -796,12 +796,15 @@ class DFRN
                        if (!$r) {
                                return false;
                        }
+
                        if ($r->type) {
                                XML::addElement($doc, $entry, "activity:object-type", $r->type);
                        }
+
                        if ($r->id) {
                                XML::addElement($doc, $entry, "id", $r->id);
                        }
+
                        if ($r->title) {
                                XML::addElement($doc, $entry, "title", $r->title);
                        }
@@ -848,7 +851,7 @@ class DFRN
         * @param array  $item Item element
         *
         * @return object XML attachment object
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
        private static function getAttachment($doc, $root, $item)
        {
@@ -888,9 +891,9 @@ class DFRN
         * @param bool   $single  If set, the entry is created as an XML document with a single "entry" element
         *
         * @return object XML entry object
-        * @todo Add type-hints
+        * @todo Find proper type-hints
         */
-       private static function entry($doc, $type, $item, $owner, $comment = false, $cid = 0, $single = false)
+       private static function entry($doc, $type, array $item, array $owner, $comment = false, $cid = 0, $single = false)
        {
                $mentioned = [];
 
@@ -934,7 +937,7 @@ class DFRN
                        $htmlbody = $body;
 
                        if ($item['title'] != "") {
-                               $htmlbody = "[b]".$item['title']."[/b]\n\n".$htmlbody;
+                               $htmlbody = "[b]" . $item['title'] . "[/b]\n\n" . $htmlbody;
                        }
 
                        $htmlbody = BBCode::convert($htmlbody, false, 7);
@@ -1061,6 +1064,7 @@ class DFRN
 
                $tags = Item::getFeedTags($item);
 
+               /// @TODO Combine this with similar below if() block?
                if (count($tags)) {
                        foreach ($tags as $t) {
                                if (($type != 'html') || ($t[0] != "@")) {
@@ -1197,6 +1201,7 @@ class DFRN
                $ret = Network::curl($url);
 
                if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+                       Contact::markForArchival($contact);
                        return -2; // timed out
                }
 
@@ -1204,24 +1209,28 @@ class DFRN
 
                $curl_stat = $a->get_curl_code();
                if (empty($curl_stat)) {
+                       Contact::markForArchival($contact);
                        return -3; // timed out
                }
 
                logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
 
                if (empty($xml)) {
+                       Contact::markForArchival($contact);
                        return 3;
                }
 
                if (strpos($xml, '<?xml') === false) {
                        logger('dfrn_deliver: no valid XML returned');
                        logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
+                       Contact::markForArchival($contact);
                        return 3;
                }
 
                $res = XML::parseString($xml);
 
-               if ((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id))) {
+               if ((intval($res->status) != 0) || !strlen($res->challenge) || !strlen($res->dfrn_id)) {
+                       Contact::markForArchival($contact);
                        return ($res->status ? $res->status : 3);
                }
 
@@ -1274,6 +1283,7 @@ class DFRN
                if ($final_dfrn_id != $orig_id) {
                        logger('dfrn_deliver: wrong dfrn_id.');
                        // did not decode properly - cannot trust this site
+                       Contact::markForArchival($contact);
                        return 3;
                }
 
@@ -1283,7 +1293,6 @@ class DFRN
                        $postvars['dissolve'] = '1';
                }
 
-
                if ((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
                        $postvars['data'] = $atom;
                        $postvars['perm'] = 'rw';
@@ -1309,6 +1318,7 @@ class DFRN
                                        break;
                                default:
                                        logger("rino: invalid requested version '$rino_remote_version'");
+                                       Contact::markForArchival($contact);
                                        return -8;
                        }
 
@@ -1346,22 +1356,26 @@ class DFRN
 
                $curl_stat = $a->get_curl_code();
                if (empty($curl_stat) || empty($xml)) {
+                       Contact::markForArchival($contact);
                        return -9; // timed out
                }
 
                if (($curl_stat == 503) && stristr($a->get_curl_headers(), 'retry-after')) {
+                       Contact::markForArchival($contact);
                        return -10;
                }
 
                if (strpos($xml, '<?xml') === false) {
                        logger('dfrn_deliver: phase 2: no valid XML returned');
                        logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
+                       Contact::markForArchival($contact);
                        return 3;
                }
 
                $res = XML::parseString($xml);
 
                if (!isset($res->status)) {
+                       Contact::markForArchival($contact);
                        return -11;
                }
 
@@ -1374,7 +1388,7 @@ class DFRN
                        logger('Delivery returned status '.$res->status.' - '.$res->message, LOGGER_DEBUG);
                }
 
-               if ($res->status == 200) {
+               if (($res->status >= 200) && ($res->status <= 299)) {
                        Contact::unmarkForArchival($contact);
                }
 
@@ -1394,26 +1408,33 @@ class DFRN
        {
                $a = get_app();
 
-               if (empty($contact['addr'])) {
-                       logger('Empty contact handle for ' . $contact['id'] . ' - ' . $contact['url'] . ' - trying to update it.');
-                       if (Contact::updateFromProbe($contact['id'])) {
-                               $new_contact = dba::selectFirst('contact', ['addr'], ['id' => $contact['id']]);
-                               $contact['addr'] = $new_contact['addr'];
-                       }
-
+               if (!$public_batch) {
                        if (empty($contact['addr'])) {
-                               logger('Unable to find contact handle for ' . $contact['id'] . ' - ' . $contact['url']);
-                               return -21;
+                               logger('Empty contact handle for ' . $contact['id'] . ' - ' . $contact['url'] . ' - trying to update it.');
+                               if (Contact::updateFromProbe($contact['id'])) {
+                                       $new_contact = dba::selectFirst('contact', ['addr'], ['id' => $contact['id']]);
+                                       $contact['addr'] = $new_contact['addr'];
+                               }
+
+                               if (empty($contact['addr'])) {
+                                       logger('Unable to find contact handle for ' . $contact['id'] . ' - ' . $contact['url']);
+                                       Contact::markForArchival($contact);
+                                       return -21;
+                               }
                        }
-               }
 
-               $fcontact = Diaspora::personByHandle($contact['addr']);
-               if (empty($fcontact)) {
-                       logger('Unable to find contact details for ' . $contact['id'] . ' - ' . $contact['addr']);
-                       return -22;
+                       $fcontact = Diaspora::personByHandle($contact['addr']);
+                       if (empty($fcontact)) {
+                               logger('Unable to find contact details for ' . $contact['id'] . ' - ' . $contact['addr']);
+                               Contact::markForArchival($contact);
+                               return -22;
+                       }
+                       $pubkey = $fcontact['pubkey'];
+               } else {
+                       $pubkey = '';
                }
 
-               $envelope = Diaspora::buildMessage($atom, $owner, $contact, $owner['uprvkey'], $fcontact['pubkey'], $public_batch);
+               $envelope = Diaspora::buildMessage($atom, $owner, $contact, $owner['uprvkey'], $pubkey, $public_batch);
 
                // Create the endpoint for public posts. This is some WIP and should later be added to the probing
                if ($public_batch && empty($contact["batch"])) {
@@ -1433,22 +1454,26 @@ class DFRN
                $curl_stat = $a->get_curl_code();
                if (empty($curl_stat) || empty($xml)) {
                        logger('Empty answer from ' . $contact['id'] . ' - ' . $dest_url);
+                       Contact::markForArchival($contact);
                        return -9; // timed out
                }
 
                if (($curl_stat == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
+                       Contact::markForArchival($contact);
                        return -10;
                }
 
                if (strpos($xml, '<?xml') === false) {
                        logger('No valid XML returned from ' . $contact['id'] . ' - ' . $dest_url);
                        logger('Returned XML: ' . $xml, LOGGER_DATA);
+                       Contact::markForArchival($contact);
                        return 3;
                }
 
                $res = XML::parseString($xml);
 
                if (empty($res->status)) {
+                       Contact::markForArchival($contact);
                        return -23;
                }
 
@@ -1456,7 +1481,7 @@ class DFRN
                        logger('Transmit to ' . $dest_url . ' returned status '.$res->status.' - '.$res->message, LOGGER_DEBUG);
                }
 
-               if ($res->status == 200) {
+               if (($res->status >= 200) && ($res->status <= 299)) {
                        Contact::unmarkForArchival($contact);
                }
 
@@ -1549,12 +1574,12 @@ class DFRN
                // Until now we aren't serving different sizes - but maybe later
                $avatarlist = [];
                /// @todo check if "avatar" or "photo" would be the best field in the specification
-               $avatars = $xpath->query($element."/atom:link[@rel='avatar']", $context);
+               $avatars = $xpath->query($element . "/atom:link[@rel='avatar']", $context);
                foreach ($avatars as $avatar) {
                        $href = "";
                        $width = 0;
                        foreach ($avatar->attributes as $attributes) {
-                               /// @TODO Rewrite these similar if () to one switch
+                               /// @TODO Rewrite these similar if() to one switch
                                if ($attributes->name == "href") {
                                        $href = $attributes->textContent;
                                }
@@ -1569,6 +1594,7 @@ class DFRN
                                $avatarlist[$width] = $href;
                        }
                }
+
                if (count($avatarlist) > 0) {
                        krsort($avatarlist);
                        $author["avatar"] = current($avatarlist);
@@ -2056,94 +2082,34 @@ class DFRN
                // Update the gcontact entry
                $relocate["server_url"] = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $relocate["url"]);
 
-               $x = q(
-                       "UPDATE `gcontact` SET
-                                       `name` = '%s',
-                                       `photo` = '%s',
-                                       `url` = '%s',
-                                       `nurl` = '%s',
-                                       `addr` = '%s',
-                                       `connect` = '%s',
-                                       `notify` = '%s',
-                                       `server_url` = '%s'
-                       WHERE `nurl` = '%s';",
-                       dbesc($relocate["name"]),
-                       dbesc($relocate["avatar"]),
-                       dbesc($relocate["url"]),
-                       dbesc(normalise_link($relocate["url"])),
-                       dbesc($relocate["addr"]),
-                       dbesc($relocate["addr"]),
-                       dbesc($relocate["notify"]),
-                       dbesc($relocate["server_url"]),
-                       dbesc(normalise_link($old["url"]))
-               );
+               $fields = ['name' => $relocate["name"], 'photo' => $relocate["avatar"],
+                       'url' => $relocate["url"], 'nurl' => normalise_link($relocate["url"]),
+                       'addr' => $relocate["addr"], 'connect' => $relocate["addr"],
+                       'notify' => $relocate["notify"], 'server_url' => $relocate["server_url"]];
+               dba::update('gcontact', $fields, ['nurl' => normalise_link($old["url"])]);
 
                // Update the contact table. We try to find every entry.
-               $x = q(
-                       "UPDATE `contact` SET
-                                       `name` = '%s',
-                                       `avatar` = '%s',
-                                       `url` = '%s',
-                                       `nurl` = '%s',
-                                       `addr` = '%s',
-                                       `request` = '%s',
-                                       `confirm` = '%s',
-                                       `notify` = '%s',
-                                       `poll` = '%s',
-                                       `site-pubkey` = '%s'
-                       WHERE (`id` = %d AND `uid` = %d) OR (`nurl` = '%s');",
-                       dbesc($relocate["name"]),
-                       dbesc($relocate["avatar"]),
-                       dbesc($relocate["url"]),
-                       dbesc(normalise_link($relocate["url"])),
-                       dbesc($relocate["addr"]),
-                       dbesc($relocate["request"]),
-                       dbesc($relocate["confirm"]),
-                       dbesc($relocate["notify"]),
-                       dbesc($relocate["poll"]),
-                       dbesc($relocate["sitepubkey"]),
-                       intval($importer["id"]),
-                       intval($importer["importer_uid"]),
-                       dbesc(normalise_link($old["url"]))
-               );
+               $fields = ['name' => $relocate["name"], 'avatar' => $relocate["avatar"],
+                       'url' => $relocate["url"], 'nurl' => normalise_link($relocate["url"]),
+                       'addr' => $relocate["addr"], 'request' => $relocate["request"],
+                       'confirm' => $relocate["confirm"], 'notify' => $relocate["notify"],
+                       'poll' => $relocate["poll"], 'site-pubkey' => $relocate["sitepubkey"]];
+               $condition = ["(`id` = ?) OR (`nurl` = ?)", $importer["id"], normalise_link($old["url"])];
+               dba::update('contact', $fields, $condition);
+
+               // @TODO No dba:update here?
+               dba::update('contact', $fields, $condition);
 
                Contact::updateAvatar($relocate["avatar"], $importer["importer_uid"], $importer["id"], true);
 
-               if ($x === false) {
-                       return false;
-               }
+               logger('Contacts are updated.');
 
                // update items
-               /// @todo This is an extreme performance killer
-               $fields = [
-                       'owner-link' => [$old["url"], $relocate["url"]],
-                       'author-link' => [$old["url"], $relocate["url"]],
-                       //'owner-avatar' => array($old["photo"], $relocate["photo"]),
-                       //'author-avatar' => array($old["photo"], $relocate["photo"]),
-               ];
-               foreach ($fields as $n => $f) {
-                       $r = q(
-                               "SELECT `id` FROM `item` WHERE `%s` = '%s' AND `uid` = %d LIMIT 1",
-                               $n,
-                               dbesc($f[0]),
-                               intval($importer["importer_uid"])
-                       );
-
-                       if (DBM::is_result($r)) {
-                               $x = q(
-                                       "UPDATE `item` SET `%s` = '%s' WHERE `%s` = '%s' AND `uid` = %d",
-                                       $n,
-                                       dbesc($f[1]),
-                                       $n,
-                                       dbesc($f[0]),
-                                       intval($importer["importer_uid"])
-                               );
+               // This is an extreme performance killer
+               Item::update(['owner-link' => $relocate["url"]], ['owner-link' => $old["url"], 'uid' => $importer["importer_uid"]]);
+               Item::update(['author-link' => $relocate["url"]], ['author-link' => $old["url"], 'uid' => $importer["importer_uid"]]);
 
-                               if ($x === false) {
-                                       return false;
-                               }
-                       }
-               }
+               logger('Items are updated.');
 
                /// @TODO
                /// merge with current record, current contents have priority
@@ -2181,10 +2147,6 @@ class DFRN
                        Item::update($fields, $condition);
 
                        $changed = true;
-
-                       if ($entrytype == DFRN_REPLY_RC) {
-                               Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $current["id"]);
-                       }
                }
                return $changed;
        }
@@ -2252,12 +2214,12 @@ class DFRN
                        }
 
                        if ($is_a_remote_action) {
-                               return DFRN_REPLY_RC;
+                               return DFRN::REPLY_RC;
                        } else {
-                               return DFRN_REPLY;
+                               return DFRN::REPLY;
                        }
                } else {
-                       return DFRN_TOP_LEVEL;
+                       return DFRN::TOP_LEVEL;
                }
        }
 
@@ -2292,6 +2254,8 @@ class DFRN
                        }
 
                        if ($Blink && link_compare($Blink, System::baseUrl() . "/profile/" . $importer["nickname"])) {
+                               $author = dba::selectFirst('contact', ['name', 'thumb', 'url'], ['id' => $item['author-id']]);
+
                                // send a notification
                                notification(
                                        [
@@ -2303,10 +2267,9 @@ class DFRN
                                        "uid"          => $importer["importer_uid"],
                                        "item"         => $item,
                                        "link"         => System::baseUrl()."/display/".urlencode(Item::getGuidById($posted_id)),
-                                       "source_name"  => stripslashes($item["author-name"]),
-                                       "source_link"  => $item["author-link"],
-                                       "source_photo" => ((link_compare($item["author-link"], $importer["url"]))
-                                               ? $importer["thumb"] : $item["author-avatar"]),
+                                       "source_name"  => $author["name"],
+                                       "source_link"  => $author["url"],
+                                       "source_photo" => $author["thumb"],
                                        "verb"         => $item["verb"],
                                        "otype"        => "person",
                                        "activity"     => $verb,
@@ -2331,7 +2294,7 @@ class DFRN
        {
                logger("Process verb ".$item["verb"]." and object-type ".$item["object-type"]." for entrytype ".$entrytype, LOGGER_DEBUG);
 
-               if (($entrytype == DFRN_TOP_LEVEL)) {
+               if (($entrytype == DFRN::TOP_LEVEL)) {
                        // The filling of the the "contact" variable is done for legcy reasons
                        // The functions below are partly used by ostatus.php as well - where we have this variable
                        $r = q("SELECT * FROM `contact` WHERE `id` = %d", intval($importer["id"]));
@@ -2373,9 +2336,9 @@ class DFRN
                                // only one like or dislike per person
                                // splitted into two queries for performance issues
                                $r = q(
-                                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1",
+                                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-id` = %d AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1",
                                        intval($item["uid"]),
-                                       dbesc($item["author-link"]),
+                                       intval($item["author-id"]),
                                        dbesc($item["verb"]),
                                        dbesc($item["parent-uri"])
                                );
@@ -2384,9 +2347,9 @@ class DFRN
                                }
 
                                $r = q(
-                                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1",
+                                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-id` = %d AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1",
                                        intval($item["uid"]),
-                                       dbesc($item["author-link"]),
+                                       intval($item["author-id"]),
                                        dbesc($item["verb"]),
                                        dbesc($item["parent-uri"])
                                );
@@ -2508,16 +2471,14 @@ class DFRN
                // Fetch the owner
                $owner = self::fetchauthor($xpath, $entry, $importer, "dfrn:owner", true);
 
-               $item["owner-name"] = $owner["name"];
                $item["owner-link"] = $owner["link"];
-               $item["owner-avatar"] = $owner["avatar"];
+               $item["owner-id"] = Contact::getIdForURL($owner["link"], 0);
 
                // fetch the author
                $author = self::fetchauthor($xpath, $entry, $importer, "atom:author", true);
 
-               $item["author-name"] = $author["name"];
                $item["author-link"] = $author["link"];
-               $item["author-avatar"] = $author["avatar"];
+               $item["author-id"] = Contact::getIdForURL($author["link"], 0);
 
                $item["title"] = $xpath->query("atom:title/text()", $entry)->item(0)->nodeValue;
 
@@ -2675,7 +2636,7 @@ class DFRN
                $entrytype = self::getEntryType($importer, $item);
 
                // Now assign the rest of the values that depend on the type of the message
-               if (in_array($entrytype, [DFRN_REPLY, DFRN_REPLY_RC])) {
+               if (in_array($entrytype, [DFRN::REPLY, DFRN::REPLY_RC])) {
                        if (!isset($item["object-type"])) {
                                $item["object-type"] = ACTIVITY_OBJ_COMMENT;
                        }
@@ -2697,10 +2658,10 @@ class DFRN
                        }
                }
 
-               if ($entrytype == DFRN_REPLY_RC) {
+               if ($entrytype == DFRN::REPLY_RC) {
                        $item["type"] = "remote-comment";
                        $item["wall"] = 1;
-               } elseif ($entrytype == DFRN_TOP_LEVEL) {
+               } elseif ($entrytype == DFRN::TOP_LEVEL) {
                        if (!isset($item["object-type"])) {
                                $item["object-type"] = ACTIVITY_OBJ_NOTE;
                        }
@@ -2744,42 +2705,25 @@ class DFRN
                        if (self::updateContent($current, $item, $importer, $entrytype)) {
                                logger("Item ".$item["uri"]." was updated.", LOGGER_DEBUG);
                        } else {
-                               logger("Item ".$item["uri"]." already existed.", LOGGER_DEBUG);
+                               logger("Item " . $item["uri"] . " already existed.", LOGGER_DEBUG);
                        }
                        return;
                }
 
-               if (in_array($entrytype, [DFRN_REPLY, DFRN_REPLY_RC])) {
+               if (in_array($entrytype, [DFRN::REPLY, DFRN::REPLY_RC])) {
                        $posted_id = Item::insert($item);
                        $parent = 0;
 
                        if ($posted_id) {
                                logger("Reply from contact ".$item["contact-id"]." was stored with id ".$posted_id, LOGGER_DEBUG);
 
-                               $item["id"] = $posted_id;
-
-                               $r = q(
-                                       "SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-                                       intval($posted_id),
-                                       intval($importer["importer_uid"])
-                               );
-                               if (DBM::is_result($r)) {
-                                       $parent = $r[0]["parent"];
-                                       $parent_uri = $r[0]["parent-uri"];
-                               }
-
-                               if ($posted_id && $parent && ($entrytype == DFRN_REPLY_RC)) {
-                                       logger("Notifying followers about comment ".$posted_id, LOGGER_DEBUG);
-                                       Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $posted_id);
+                               if ($item['uid'] == 0) {
+                                       Item::distribute($posted_id);
                                }
 
                                return true;
                        }
-               } else { // $entrytype == DFRN_TOP_LEVEL
-                       if ($importer["readonly"]) {
-                               logger('ignoring read-only contact '.$importer["id"]);
-                               return;
-                       }
+               } else { // $entrytype == DFRN::TOP_LEVEL
                        if (($importer["uid"] == 0) && ($importer["importer_uid"] != 0)) {
                                logger("Contact ".$importer["id"]." isn't known to user ".$importer["importer_uid"].". The post will be ignored.", LOGGER_DEBUG);
                                return;
@@ -2792,9 +2736,8 @@ class DFRN
                                 * but we're going to unconditionally correct it here so that the post will always be owned by our contact.
                                 */
                                logger('Correcting item owner.', LOGGER_DEBUG);
-                               $item["owner-name"]   = $importer["senderName"];
-                               $item["owner-link"]   = $importer["url"];
-                               $item["owner-avatar"] = $importer["thumb"];
+                               $item["owner-link"] = $importer["url"];
+                               $item["owner-id"] = Contact::getIdForURL($importer["url"], 0);
                        }
 
                        if (($importer["rel"] == CONTACT_IS_FOLLOWER) && (!self::tgroupCheck($importer["importer_uid"], $item))) {
@@ -2808,8 +2751,16 @@ class DFRN
 
                        $posted_id = Item::insert($item, false, $notify);
 
+                       if ($notify) {
+                               $posted_id = $notify;
+                       }
+
                        logger("Item was stored with id ".$posted_id, LOGGER_DEBUG);
 
+                       if ($item['uid'] == 0) {
+                               Item::distribute($posted_id);
+                       }
+
                        if (stristr($item["verb"], ACTIVITY_POKE)) {
                                self::doPoke($item, $importer, $posted_id);
                        }
@@ -2829,6 +2780,7 @@ class DFRN
        {
                logger("Processing deletions");
                $uri = null;
+
                foreach ($deletion->attributes as $attributes) {
                        if ($attributes->name == "ref") {
                                $uri = $attributes->textContent;
@@ -2861,23 +2813,13 @@ class DFRN
                        }
                }
 
-               $entrytype = self::getEntryType($importer, $item);
-
-               if (!$item["deleted"]) {
-                       logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG);
-               } else {
+               if ($item["deleted"]) {
                        return;
                }
 
-               Item::deleteById($item["id"]);
+               logger('deleting item '.$item['id'].' uri='.$uri, LOGGER_DEBUG);
 
-               if ($entrytype != DFRN_TOP_LEVEL) {
-                       // if this is a relayed delete, propagate it to other recipients
-                       if ($entrytype == DFRN_REPLY_RC) {
-                               logger("Notifying followers about deletion of post " . $item["id"], LOGGER_DEBUG);
-                               Worker::add(PRIORITY_HIGH, "Notifier", "drop", $item["id"]);
-                       }
-               }
+               Item::delete(['id' => $item['id']]);
        }
 
        /**