]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #9364 from annando/issue-9363
[friendica.git] / src / Model / Contact.php
index b6b0fec5d93739fc4379f9c0c6a9a10501599480..e997bffdef95a5dee31c178b12cf57ab09a9ae28 100644 (file)
 
 namespace Friendica\Model;
 
-use DOMDocument;
-use DOMXPath;
 use Friendica\App\BaseURL;
 use Friendica\Content\Pager;
+use Friendica\Content\Text\HTML;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
+use Friendica\Core\Renderer;
 use Friendica\Core\Session;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
@@ -53,6 +53,10 @@ use Friendica\Util\Strings;
  */
 class Contact
 {
+       const DEFAULT_AVATAR_PHOTO = '/images/person-300.jpg';
+       const DEFAULT_AVATAR_THUMB = '/images/person-80.jpg';
+       const DEFAULT_AVATAR_MICRO = '/images/person-48.jpg';
+
        /**
         * @deprecated since version 2019.03
         * @see User::PAGE_FLAGS_NORMAL
@@ -124,6 +128,7 @@ class Contact
         * Relationship types
         * @{
         */
+       const NOTHING  = 0;
        const FOLLOWER = 1;
        const SHARING  = 2;
        const FRIEND   = 3;
@@ -220,7 +225,7 @@ class Contact
                // Add internal fields
                $removal = [];
                if (!empty($fields)) {
-                       foreach (['id', 'updated', 'network'] as $internal) {
+                       foreach (['id', 'avatar', 'created', 'updated', 'last-update', 'success_update', 'failure_update', 'network'] as $internal) {
                                if (!in_array($internal, $fields)) {
                                        $fields[] = $internal;
                                        $removal[] = $internal;
@@ -250,9 +255,9 @@ class Contact
                }
 
                // Update the contact in the background if needed
-               if ((($contact['updated'] < DateTimeFormat::utc('now -7 days')) || empty($contact['avatar'])) &&
-                       in_array($contact['network'], Protocol::FEDERATED)) {
-                       Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id'], ($uid == 0 ? 'force' : ''));
+               $updated = max($contact['success_update'], $contact['created'], $contact['updated'], $contact['last-update'], $contact['failure_update']);
+               if (($updated < DateTimeFormat::utc('now -7 days')) && in_array($contact['network'], Protocol::FEDERATED)) {
+                       Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']);
                }
 
                // Remove the internal fields
@@ -306,7 +311,7 @@ class Contact
         */
        public static function isFollower($cid, $uid)
        {
-               if (self::isBlockedByUser($cid, $uid)) {
+               if (Contact\User::isBlocked($cid, $uid)) {
                        return false;
                }
 
@@ -331,7 +336,7 @@ class Contact
         */
        public static function isFollowerByURL($url, $uid)
        {
-               $cid = self::getIdForURL($url, $uid, false);
+               $cid = self::getIdForURL($url, $uid);
 
                if (empty($cid)) {
                        return false;
@@ -352,7 +357,7 @@ class Contact
         */
        public static function isSharing($cid, $uid)
        {
-               if (self::isBlockedByUser($cid, $uid)) {
+               if (Contact\User::isBlocked($cid, $uid)) {
                        return false;
                }
 
@@ -377,7 +382,7 @@ class Contact
         */
        public static function isSharingByURL($url, $uid)
        {
-               $cid = self::getIdForURL($url, $uid, false);
+               $cid = self::getIdForURL($url, $uid);
 
                if (empty($cid)) {
                        return false;
@@ -410,7 +415,7 @@ class Contact
                }
 
                // Update the existing contact
-               self::updateFromProbe($contact['id'], '', true);
+               self::updateFromProbe($contact['id']);
 
                // And fetch the result
                $contact = DBA::selectFirst('contact', ['baseurl'], ['id' => $contact['id']]);
@@ -472,7 +477,7 @@ class Contact
                if (!DBA::isResult($self)) {
                        return false;
                }
-               return self::getIdForURL($self['url'], 0, false);
+               return self::getIdForURL($self['url']);
        }
 
        /**
@@ -509,7 +514,7 @@ class Contact
                        $ucid = $contact['id'];
                } else {
                        $pcid = $contact['id'];
-                       $ucid = Contact::getIdForURL($contact['url'], $uid, false);
+                       $ucid = Contact::getIdForURL($contact['url'], $uid);
                }
 
                return ['public' => $pcid, 'user' => $ucid];
@@ -537,214 +542,6 @@ class Contact
                }
        }
 
-       /**
-        * Block contact id for user id
-        *
-        * @param int     $cid     Either public contact id or user's contact id
-        * @param int     $uid     User ID
-        * @param boolean $blocked Is the contact blocked or unblocked?
-        * @throws \Exception
-        */
-       public static function setBlockedForUser($cid, $uid, $blocked)
-       {
-               $cdata = self::getPublicAndUserContacID($cid, $uid);
-               if (empty($cdata)) {
-                       return;
-               }
-
-               if ($cdata['user'] != 0) {
-                       DBA::update('contact', ['blocked' => $blocked], ['id' => $cdata['user'], 'pending' => false]);
-               }
-
-               DBA::update('user-contact', ['blocked' => $blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
-       }
-
-       /**
-        * Returns "block" state for contact id and user id
-        *
-        * @param int $cid Either public contact id or user's contact id
-        * @param int $uid User ID
-        *
-        * @return boolean is the contact id blocked for the given user?
-        * @throws \Exception
-        */
-       public static function isBlockedByUser($cid, $uid)
-       {
-               $cdata = self::getPublicAndUserContacID($cid, $uid);
-               if (empty($cdata)) {
-                       return;
-               }
-
-               $public_blocked = false;
-
-               if (!empty($cdata['public'])) {
-                       $public_contact = DBA::selectFirst('user-contact', ['blocked'], ['cid' => $cdata['public'], 'uid' => $uid]);
-                       if (DBA::isResult($public_contact)) {
-                               $public_blocked = $public_contact['blocked'];
-                       }
-               }
-
-               $user_blocked = $public_blocked;
-
-               if (!empty($cdata['user'])) {
-                       $user_contact = DBA::selectFirst('contact', ['blocked'], ['id' => $cdata['user'], 'pending' => false]);
-                       if (DBA::isResult($user_contact)) {
-                               $user_blocked = $user_contact['blocked'];
-                       }
-               }
-
-               if ($user_blocked != $public_blocked) {
-                       DBA::update('user-contact', ['blocked' => $user_blocked], ['cid' => $cdata['public'], 'uid' => $uid], true);
-               }
-
-               return $user_blocked;
-       }
-
-       /**
-        * Ignore contact id for user id
-        *
-        * @param int     $cid     Either public contact id or user's contact id
-        * @param int     $uid     User ID
-        * @param boolean $ignored Is the contact ignored or unignored?
-        * @throws \Exception
-        */
-       public static function setIgnoredForUser($cid, $uid, $ignored)
-       {
-               $cdata = self::getPublicAndUserContacID($cid, $uid);
-               if (empty($cdata)) {
-                       return;
-               }
-
-               if ($cdata['user'] != 0) {
-                       DBA::update('contact', ['readonly' => $ignored], ['id' => $cdata['user'], 'pending' => false]);
-               }
-
-               DBA::update('user-contact', ['ignored' => $ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
-       }
-
-       /**
-        * Returns "ignore" state for contact id and user id
-        *
-        * @param int $cid Either public contact id or user's contact id
-        * @param int $uid User ID
-        *
-        * @return boolean is the contact id ignored for the given user?
-        * @throws \Exception
-        */
-       public static function isIgnoredByUser($cid, $uid)
-       {
-               $cdata = self::getPublicAndUserContacID($cid, $uid);
-               if (empty($cdata)) {
-                       return;
-               }
-
-               $public_ignored = false;
-
-               if (!empty($cdata['public'])) {
-                       $public_contact = DBA::selectFirst('user-contact', ['ignored'], ['cid' => $cdata['public'], 'uid' => $uid]);
-                       if (DBA::isResult($public_contact)) {
-                               $public_ignored = $public_contact['ignored'];
-                       }
-               }
-
-               $user_ignored = $public_ignored;
-
-               if (!empty($cdata['user'])) {
-                       $user_contact = DBA::selectFirst('contact', ['readonly'], ['id' => $cdata['user'], 'pending' => false]);
-                       if (DBA::isResult($user_contact)) {
-                               $user_ignored = $user_contact['readonly'];
-                       }
-               }
-
-               if ($user_ignored != $public_ignored) {
-                       DBA::update('user-contact', ['ignored' => $user_ignored], ['cid' => $cdata['public'], 'uid' => $uid], true);
-               }
-
-               return $user_ignored;
-       }
-
-       /**
-        * Set "collapsed" for contact id and user id
-        *
-        * @param int     $cid       Either public contact id or user's contact id
-        * @param int     $uid       User ID
-        * @param boolean $collapsed are the contact's posts collapsed or uncollapsed?
-        * @throws \Exception
-        */
-       public static function setCollapsedForUser($cid, $uid, $collapsed)
-       {
-               $cdata = self::getPublicAndUserContacID($cid, $uid);
-               if (empty($cdata)) {
-                       return;
-               }
-
-               DBA::update('user-contact', ['collapsed' => $collapsed], ['cid' => $cdata['public'], 'uid' => $uid], true);
-       }
-
-       /**
-        * Returns "collapsed" state for contact id and user id
-        *
-        * @param int $cid Either public contact id or user's contact id
-        * @param int $uid User ID
-        *
-        * @return boolean is the contact id blocked for the given user?
-        * @throws HTTPException\InternalServerErrorException
-        * @throws \ImagickException
-        */
-       public static function isCollapsedByUser($cid, $uid)
-       {
-               $cdata = self::getPublicAndUserContacID($cid, $uid);
-               if (empty($cdata)) {
-                       return;
-               }
-
-               $collapsed = false;
-
-               if (!empty($cdata['public'])) {
-                       $public_contact = DBA::selectFirst('user-contact', ['collapsed'], ['cid' => $cdata['public'], 'uid' => $uid]);
-                       if (DBA::isResult($public_contact)) {
-                               $collapsed = $public_contact['collapsed'];
-                       }
-               }
-
-               return $collapsed;
-       }
-
-       /**
-        * Returns a list of contacts belonging in a group
-        *
-        * @param int $gid
-        * @return array
-        * @throws \Exception
-        */
-       public static function getByGroupId($gid)
-       {
-               $return = [];
-
-               if (intval($gid)) {
-                       $stmt = DBA::p('SELECT `group_member`.`contact-id`, `contact`.*
-                               FROM `contact`
-                               INNER JOIN `group_member`
-                                       ON `contact`.`id` = `group_member`.`contact-id`
-                               WHERE `gid` = ?
-                               AND `contact`.`uid` = ?
-                               AND NOT `contact`.`self`
-                               AND NOT `contact`.`deleted`
-                               AND NOT `contact`.`blocked`
-                               AND NOT `contact`.`pending`
-                               ORDER BY `contact`.`name` ASC',
-                               $gid,
-                               local_user()
-                       );
-
-                       if (DBA::isResult($stmt)) {
-                               $return = DBA::toArray($stmt);
-                       }
-               }
-
-               return $return;
-       }
-
        /**
         * Creates the self-contact for the provided user id
         *
@@ -759,7 +556,7 @@ class Contact
                        return true;
                }
 
-               $user = DBA::selectFirst('user', ['uid', 'username', 'nickname'], ['uid' => $uid]);
+               $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'pubkey', 'prvkey'], ['uid' => $uid]);
                if (!DBA::isResult($user)) {
                        return false;
                }
@@ -770,6 +567,8 @@ class Contact
                        'self'        => 1,
                        'name'        => $user['username'],
                        'nick'        => $user['nickname'],
+                       'pubkey'      => $user['pubkey'],
+                       'prvkey'      => $user['prvkey'],
                        'photo'       => DI::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
                        'thumb'       => DI::baseUrl() . '/photo/avatar/'  . $user['uid'] . '.jpg',
                        'micro'       => DI::baseUrl() . '/photo/micro/'   . $user['uid'] . '.jpg',
@@ -801,7 +600,7 @@ class Contact
         */
        public static function updateSelfFromUserID($uid, $update_avatar = false)
        {
-               $fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar',
+               $fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey',
                        'xmpp', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
                        'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco'];
                $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
@@ -809,7 +608,7 @@ class Contact
                        return;
                }
 
-               $fields = ['nickname', 'page-flags', 'account-type'];
+               $fields = ['nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
                $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
                if (!DBA::isResult($user)) {
                        return;
@@ -827,8 +626,8 @@ class Contact
                $fields = ['name' => $profile['name'], 'nick' => $user['nickname'],
                        'avatar-date' => $self['avatar-date'], 'location' => Profile::formatLocation($profile),
                        'about' => $profile['about'], 'keywords' => $profile['pub_keywords'],
-                       'contact-type' => $user['account-type'],
-                       'xmpp' => $profile['xmpp']];
+                       'contact-type' => $user['account-type'], 'prvkey' => $user['prvkey'],
+                       'pubkey' => $user['pubkey'], 'xmpp' => $profile['xmpp']];
 
                $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
                if (DBA::isResult($avatar)) {
@@ -853,9 +652,9 @@ class Contact
                        $fields['micro'] = $prefix . '6' . $suffix;
                } else {
                        // We hadn't found a photo entry, so we use the default avatar
-                       $fields['photo'] = DI::baseUrl() . '/images/person-300.jpg';
-                       $fields['thumb'] = DI::baseUrl() . '/images/person-80.jpg';
-                       $fields['micro'] = DI::baseUrl() . '/images/person-48.jpg';
+                       $fields['photo'] = DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO;
+                       $fields['thumb'] = DI::baseUrl() . self::DEFAULT_AVATAR_THUMB;
+                       $fields['micro'] = DI::baseUrl() . self::DEFAULT_AVATAR_MICRO;
                }
 
                $fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
@@ -1202,113 +1001,6 @@ class Contact
                return $menucondensed;
        }
 
-       /**
-        * Returns ungrouped contact count or list for user
-        *
-        * Returns either the total number of ungrouped contacts for the given user
-        * id or a paginated list of ungrouped contacts.
-        *
-        * @param int $uid uid
-        * @return array
-        * @throws \Exception
-        */
-       public static function getUngroupedList($uid)
-       {
-               return q("SELECT *
-                          FROM `contact`
-                          WHERE `uid` = %d
-                          AND NOT `self`
-                          AND NOT `deleted`
-                          AND NOT `blocked`
-                          AND NOT `pending`
-                          AND `id` NOT IN (
-                               SELECT DISTINCT(`contact-id`)
-                               FROM `group_member`
-                               INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
-                               WHERE `group`.`uid` = %d
-                          )", intval($uid), intval($uid));
-       }
-
-       /**
-        * Have a look at all contact tables for a given profile url.
-        * This function works as a replacement for probing the contact.
-        *
-        * @param string  $url Contact URL
-        * @param integer $cid Contact ID
-        *
-        * @return array Contact array in the "probe" structure
-       */
-       private static function getProbeDataFromDatabase($url, $cid = null)
-       {
-               // The link could be provided as http although we stored it as https
-               $ssl_url = str_replace('http://', 'https://', $url);
-
-               $fields = ['id', 'uid', 'url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
-                       'photo', 'keywords', 'location', 'about', 'network',
-                       'priority', 'batch', 'request', 'confirm', 'poco'];
-
-               if (!empty($cid)) {
-                       $data = DBA::selectFirst('contact', $fields, ['id' => $cid]);
-                       if (DBA::isResult($data)) {
-                               return $data;
-                       }
-               }
-
-               $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]);
-
-               if (!DBA::isResult($data)) {
-                       $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
-                       $data = DBA::selectFirst('contact', $fields, $condition);
-               }
-
-               if (DBA::isResult($data)) {
-                       // For security reasons we don't fetch key data from our users
-                       $data["pubkey"] = '';
-                       return $data;
-               }
-
-               $fields = ['url', 'addr', 'alias', 'notify', 'name', 'nick',
-                       'photo', 'keywords', 'location', 'about', 'network'];
-               $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
-               $data = DBA::selectFirst('contact', $fields, $condition);
-
-               if (DBA::isResult($data)) {
-                       $data["pubkey"] = '';
-                       $data["poll"] = '';
-                       $data["priority"] = 0;
-                       $data["batch"] = '';
-                       $data["request"] = '';
-                       $data["confirm"] = '';
-                       $data["poco"] = '';
-                       return $data;
-               }
-
-               $data = ActivityPub::probeProfile($url, false);
-               if (!empty($data)) {
-                       return $data;
-               }
-
-               $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
-                       'photo', 'network', 'priority', 'batch', 'request', 'confirm'];
-               $data = DBA::selectFirst('fcontact', $fields, ['url' => $url]);
-
-               if (!DBA::isResult($data)) {
-                       $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
-                       $data = DBA::selectFirst('contact', $fields, $condition);
-               }
-
-               if (DBA::isResult($data)) {
-                       $data["pubkey"] = '';
-                       $data["keywords"] = '';
-                       $data["location"] = '';
-                       $data["about"] = '';
-                       $data["poco"] = '';
-                       return $data;
-               }
-
-               return [];
-       }
-
        /**
         * Fetch the contact id for a given URL and user
         *
@@ -1329,117 +1021,102 @@ class Contact
         *
         * @param string  $url       Contact URL
         * @param integer $uid       The user id for the contact (0 = public contact)
-        * @param boolean $update    true = always update, false = never update, null = update when not found or outdated
-        * @param array   $default   Default value for creating the contact when every else fails
-        * @param boolean $in_loop   Internally used variable to prevent an endless loop
+        * @param boolean $update    true = always update, false = never update, null = update when not found
+        * @param array   $default   Default value for creating the contact when everything else fails
         *
         * @return integer Contact ID
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function getIdForURL($url, $uid = 0, $update = null, $default = [], $in_loop = false)
+       public static function getIdForURL($url, $uid = 0, $update = null, $default = [])
        {
-               Logger::info('Get contact data', ['url' => $url, 'user' => $uid]);
-
                $contact_id = 0;
 
                if ($url == '') {
+                       Logger::notice('Empty url, quitting', ['url' => $url, 'user' => $uid, 'default' => $default]);
                        return 0;
                }
 
-               $contact = self::getByURL($url, false, ['id', 'avatar', 'updated', 'network'], $uid);
+               $contact = self::getByURL($url, false, ['id', 'network'], $uid);
 
                if (!empty($contact)) {
                        $contact_id = $contact["id"];
 
-                       if (empty($default) && in_array($contact['network'], [Protocol::MAIL, Protocol::PHANTOM]) && ($uid == 0)) {
-                               // Update public mail accounts via their user's accounts
-                               $fields = ['network', 'addr', 'name', 'nick', 'avatar', 'photo', 'thumb', 'micro'];
-                               $mailcontact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `network` = ? AND `uid` != 0", $url, Protocol::MAIL]);
-                               if (!DBA::isResult($mailcontact)) {
-                                       $mailcontact = DBA::selectFirst('contact', $fields, ["`nurl` = ? AND `network` = ? AND `uid` != 0", $url, Protocol::MAIL]);
-                               }
-
-                               if (DBA::isResult($mailcontact)) {
-                                       DBA::update('contact', $mailcontact, ['id' => $contact_id]);
-                               }
-                       }
-
                        if (empty($update)) {
+                               Logger::debug('Contact found', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
                                return $contact_id;
                        }
                } elseif ($uid != 0) {
-                       // Non-existing user-specific contact, exiting
+                       Logger::debug('Contact does not exist for the user', ['url' => $url, 'uid' => $uid, 'update' => $update]);
+                       return 0;
+               } elseif (empty($default) && !is_null($update) && !$update) {
+                       Logger::info('Contact not found, update not desired', ['url' => $url, 'uid' => $uid, 'update' => $update]);
                        return 0;
                }
 
-               if (!$update && empty($default)) {
-                       // When we don't want to update, we look if we know this contact in any way
-                       $data = self::getProbeDataFromDatabase($url, $contact_id);
-                       $background_update = true;
-               } elseif (!$update && !empty($default['network'])) {
-                       // If there are default values, take these
-                       $data = $default;
-                       $background_update = false;
-               } else {
-                       $data = [];
-                       $background_update = false;
-               }
+               $data = [];
 
-               if ((empty($data) && is_null($update)) || $update) {
+               if (empty($default['network']) || $update) {
                        $data = Probe::uri($url, "", $uid);
-               }
 
-               // Take the default values when probing failed
-               if (!empty($default) && !in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
-                       $data = array_merge($data, $default);
+                       // Take the default values when probing failed
+                       if (!empty($default) && !in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
+                               $data = array_merge($data, $default);
+                       }
+               } elseif (!empty($default['network'])) {
+                       $data = $default;
                }
 
-               if (empty($data) || ($data['network'] == Protocol::PHANTOM)) {
-                       Logger::info('No valid network found', ['url' => $url, 'data' => $data, 'callstack' => System::callstack(20)]);
-                       return 0;
-               }
+               if (($uid == 0) && (empty($data['network']) || ($data['network'] == Protocol::PHANTOM))) {
+                       // Fetch data for the public contact via the first found personal contact
+                       /// @todo Check if this case can happen at all (possibly with mail accounts?)
+                       $fields = ['name', 'nick', 'url', 'addr', 'alias', 'avatar', 'contact-type',
+                               'keywords', 'location', 'about', 'unsearchable', 'batch', 'notify', 'poll',
+                               'request', 'confirm', 'poco', 'subscribe', 'network', 'baseurl', 'gsid'];
+
+                       $personal_contact = DBA::selectFirst('contact', $fields, ["`addr` = ? AND `uid` != 0", $url]);
+                       if (!DBA::isResult($personal_contact)) {
+                               $personal_contact = DBA::selectFirst('contact', $fields, ["`nurl` = ? AND `uid` != 0", Strings::normaliseLink($url)]);
+                       }
 
-               if (!empty($data['baseurl'])) {
-                       $data['baseurl'] = GServer::cleanURL($data['baseurl']);
+                       if (DBA::isResult($personal_contact)) {
+                               Logger::info('Take contact data from personal contact', ['url' => $url, 'update' => $update, 'contact' => $personal_contact, 'callstack' => System::callstack(20)]);
+                               $data = $personal_contact;
+                               $data['photo'] = $personal_contact['avatar'];
+                               $data['account-type'] = $personal_contact['contact-type'];
+                               $data['hide'] = $personal_contact['unsearchable'];
+                               unset($data['avatar']);
+                               unset($data['contact-type']);
+                               unset($data['unsearchable']);
+                       }
                }
 
-               if (!empty($data['baseurl']) && empty($data['gsid'])) {
-                       $data['gsid'] = GServer::getID($data['baseurl']);
+               if (empty($data['network']) || ($data['network'] == Protocol::PHANTOM)) {
+                       Logger::notice('No valid network found', ['url' => $url, 'uid' => $uid, 'default' => $default, 'update' => $update, 'callstack' => System::callstack(20)]);
+                       return 0;
                }
 
-               if (!$contact_id && !empty($data['alias']) && ($data['alias'] != $data['url']) && !$in_loop) {
-                       $contact_id = self::getIdForURL($data["alias"], $uid, false, $default, true);
+               if (!$contact_id) {
+                       $urls = [Strings::normaliseLink($url), Strings::normaliseLink($data['url'])];
+                       if (!empty($data['alias'])) {
+                               $urls[] = Strings::normaliseLink($data['alias']);
+                       }
+                       $contact = self::selectFirst(['id'], ['nurl' => $urls, 'uid' => $uid]);
+                       if (!empty($contact['id'])) {
+                               $contact_id = $contact['id'];
+                               Logger::info('Fetched id by url', ['cid' => $contact_id, 'uid' => $uid, 'url' => $url, 'probed_url' => $data['url'], 'alias' => $data['alias'], 'addr' => $data['addr']]);
+                       }
                }
 
                if (!$contact_id) {
+                       // We only insert the basic data. The rest will be done in "updateFromProbeArray"
                        $fields = [
                                'uid'       => $uid,
-                               'created'   => DateTimeFormat::utcNow(),
                                'url'       => $data['url'],
                                'nurl'      => Strings::normaliseLink($data['url']),
-                               'addr'      => $data['addr'] ?? '',
-                               'alias'     => $data['alias'] ?? '',
-                               'notify'    => $data['notify'] ?? '',
-                               'poll'      => $data['poll'] ?? '',
-                               'name'      => $data['name'] ?? '',
-                               'nick'      => $data['nick'] ?? '',
-                               'keywords'  => $data['keywords'] ?? '',
-                               'location'  => $data['location'] ?? '',
-                               'about'     => $data['about'] ?? '',
                                'network'   => $data['network'],
-                               'pubkey'    => $data['pubkey'] ?? '',
+                               'created'   => DateTimeFormat::utcNow(),
                                'rel'       => self::SHARING,
-                               'priority'  => $data['priority'] ?? 0,
-                               'batch'     => $data['batch'] ?? '',
-                               'request'   => $data['request'] ?? '',
-                               'confirm'   => $data['confirm'] ?? '',
-                               'poco'      => $data['poco'] ?? '',
-                               'baseurl'   => $data['baseurl'] ?? '',
-                               'gsid'      => $data['gsid'] ?? null,
-                               'name-date' => DateTimeFormat::utcNow(),
-                               'uri-date'  => DateTimeFormat::utcNow(),
-                               'avatar-date' => DateTimeFormat::utcNow(),
                                'writable'  => 1,
                                'blocked'   => 0,
                                'readonly'  => 0,
@@ -1448,71 +1125,29 @@ class Contact
                        $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
 
                        // Before inserting we do check if the entry does exist now.
+                       DBA::lock('contact');
                        $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
-                       if (!DBA::isResult($contact)) {
-                               Logger::info('Create new contact', $fields);
-
-                               self::insert($fields);
-
-                               // We intentionally aren't using lastInsertId here. There is a chance for duplicates.
-                               $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
-                               if (!DBA::isResult($contact)) {
-                                       Logger::info('Contact creation failed', $fields);
-                                       // Shouldn't happen
-                                       return 0;
-                               }
+                       if (DBA::isResult($contact)) {
+                               $contact_id = $contact['id'];
+                               Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
                        } else {
-                               Logger::info('Contact had been created before', ['id' => $contact["id"], 'url' => $url, 'contact' => $fields]);
+                               DBA::insert('contact', $fields);
+                               $contact_id = DBA::lastInsertId();
+                               if ($contact_id) {
+                                       Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
+                               }
                        }
-
-                       $contact_id = $contact["id"];
-               }
-
-               if (!empty($data['photo']) && ($data['network'] != Protocol::FEED)) {
-                       self::updateAvatar($contact_id, $data['photo']);
-               }
-
-               if (in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
-                       if ($background_update) {
-                               // Update in the background when we fetched the data solely from the database
-                               Worker::add(PRIORITY_MEDIUM, "UpdateContact", $contact_id, ($uid == 0 ? 'force' : ''));
-                       } else {
-                               // Else do a direct update
-                               self::updateFromProbe($contact_id, '', false);
+                       DBA::unlock();
+                       if (!$contact_id) {
+                               Logger::info('Contact was not inserted', ['url' => $url, 'uid' => $uid]);
+                               return 0;
                        }
                } else {
-                       $fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'baseurl', 'gsid'];
-                       $contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]);
-
-                       // This condition should always be true
-                       if (!DBA::isResult($contact)) {
-                               return $contact_id;
-                       }
-
-                       $updated = [
-                               'url' => $data['url'],
-                               'nurl' => Strings::normaliseLink($data['url']),
-                               'updated' => DateTimeFormat::utcNow(),
-                               'failed' => false
-                       ];
-
-                       $fields = ['addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'baseurl', 'gsid'];
-
-                       foreach ($fields as $field) {
-                               $updated[$field] = ($data[$field] ?? '') ?: $contact[$field];
-                       }
-
-                       if (($updated['addr'] != $contact['addr']) || (!empty($data['alias']) && ($data['alias'] != $contact['alias']))) {
-                               $updated['uri-date'] = DateTimeFormat::utcNow();
-                       }
-
-                       if (($data['name'] != $contact['name']) || ($data['nick'] != $contact['nick'])) {
-                               $updated['name-date'] = DateTimeFormat::utcNow();
-                       }
-
-                       DBA::update('contact', $updated, ['id' => $contact_id], $contact);
+                       Logger::info('Contact will be updated', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
                }
 
+               self::updateFromProbeArray($contact_id, $data);
+
                return $contact_id;
        }
 
@@ -1640,7 +1275,7 @@ class Contact
                }
 
                if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
-                       $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))";
+                       $sql = "`item`.`uid` IN (0, ?)";
                } else {
                        $sql = "`item`.`uid` = ?";
                }
@@ -1648,13 +1283,18 @@ class Contact
                $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
 
                if ($thread_mode) {
-                       $condition = ["`$contact_field` = ? AND `gravity` = ? AND " . $sql,
-                               $cid, GRAVITY_PARENT, local_user()];
+                       $condition = ["(`$contact_field` = ? OR (`causer-id` = ? AND `post-type` = ?)) AND `gravity` = ? AND " . $sql,
+                               $cid, $cid, Item::PT_ANNOUNCEMENT, GRAVITY_PARENT, local_user()];
                } else {
                        $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
                                $cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
                }
 
+               $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
+               if (!empty($last_received)) {
+                       $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
+               }
+
                if (DI::mode()->isMobile()) {
                        $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
                                DI::config()->get('system', 'itemspage_network_mobile'));
@@ -1665,25 +1305,45 @@ class Contact
 
                $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
 
-               $params = ['order' => ['received' => true],
+               $params = ['order' => ['received' => true], 'group_by' => ['uri-id'],
                        'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
 
-               if ($thread_mode) {
-                       $r = Item::selectThreadForUser(local_user(), ['uri'], $condition, $params);
+               if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
+                       $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
+                       $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
+               } else {
+                       $o = '';
+               }
 
-                       $items = Item::inArray($r);
+               if ($thread_mode) {             
+                       $r = Item::selectForUser(local_user(), ['uri', 'gravity', 'parent-uri'], $condition, $params);
+                       $items = [];
+                       while ($item = DBA::fetch($r)) {
+                               if ($item['gravity'] != GRAVITY_PARENT) {
+                                       $item['uri'] = $item['parent-uri'];
+                               }
+                               unset($item['parent-uri']);
+                               unset($item['gravity']);
+                               
+                               $items[] = $item;
+                       }
+                       DBA::close($r);
 
-                       $o = conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
+                       $o .= conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
                } else {
                        $r = Item::selectForUser(local_user(), [], $condition, $params);
 
                        $items = Item::inArray($r);
 
-                       $o = conversation($a, $items, 'contact-posts', false);
+                       $o .= conversation($a, $items, 'contact-posts', false);
                }
 
                if (!$update) {
-                       $o .= $pager->renderMinimal(count($items));
+                       if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
+                               $o .= HTML::scrollLoader();
+                       } else {
+                               $o .= $pager->renderMinimal(count($items));
+                       }
                }
 
                return $o;
@@ -1831,7 +1491,7 @@ class Contact
         */
        public static function getPhoto(array $contact, string $avatar = '')
        {
-               return self::getAvatarPath($contact, 'photo', DI::baseUrl() . '/images/person-300.jpg', Proxy::SIZE_SMALL, $avatar);
+               return self::getAvatarPath($contact, 'photo', DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO, Proxy::SIZE_SMALL, $avatar);
        }
 
        /**
@@ -1843,7 +1503,7 @@ class Contact
         */
        public static function getThumb(array $contact, string $avatar = '')
        {
-               return self::getAvatarPath($contact, 'thumb', DI::baseUrl() . '/images/person-80.jpg', Proxy::SIZE_THUMB, $avatar);
+               return self::getAvatarPath($contact, 'thumb', DI::baseUrl() . self::DEFAULT_AVATAR_THUMB, Proxy::SIZE_THUMB, $avatar);
        }
 
        /**
@@ -1855,7 +1515,7 @@ class Contact
         */
        public static function getMicro(array $contact, string $avatar = '')
        {
-               return self::getAvatarPath($contact, 'micro', DI::baseUrl() . '/images/person-48.jpg', Proxy::SIZE_MICRO, $avatar);
+               return self::getAvatarPath($contact, 'micro', DI::baseUrl() . self::DEFAULT_AVATAR_MICRO, Proxy::SIZE_MICRO, $avatar);
        }
 
        /**
@@ -1894,13 +1554,13 @@ class Contact
 
                /// add the default avatars if the fields aren't filled
                if (isset($contact['photo']) && empty($contact['photo'])) {
-                       $contact['photo'] = DI::baseUrl() . '/images/person-300.jpg';
+                       $contact['photo'] = DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO;
                }
                if (isset($contact['thumb']) && empty($contact['thumb'])) {
-                       $contact['thumb'] = DI::baseUrl() . '/images/person-80.jpg';
+                       $contact['thumb'] = DI::baseUrl() . self::DEFAULT_AVATAR_THUMB;
                }
                if (isset($contact['micro']) && empty($contact['micro'])) {
-                       $contact['micro'] = DI::baseUrl() . '/images/person-48.jpg';
+                       $contact['micro'] = DI::baseUrl() . self::DEFAULT_AVATAR_MICRO;
                }
 
                return $contact;
@@ -1909,18 +1569,19 @@ class Contact
        /**
         * Updates the avatar links in a contact only if needed
         *
-        * @param int    $cid    Contact id
-        * @param string $avatar Link to avatar picture
-        * @param bool   $force  force picture update
+        * @param int    $cid          Contact id
+        * @param string $avatar       Link to avatar picture
+        * @param bool   $force        force picture update
+        * @param bool   $create_cache Enforces the creation of cached avatar fields
         *
         * @return void
         * @throws HTTPException\InternalServerErrorException
         * @throws HTTPException\NotFoundException
         * @throws \ImagickException
         */
-       public static function updateAvatar(int $cid, string $avatar, bool $force = false)
+       public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
        {
-               $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid, 'self' => false]);
+               $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl', 'url', 'network'], ['id' => $cid, 'self' => false]);
                if (!DBA::isResult($contact)) {
                        return;
                }
@@ -1928,7 +1589,7 @@ class Contact
                $uid = $contact['uid'];
 
                // Only update the cached photo links of public contacts when they already are cached
-               if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro'])) {
+               if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
                        if ($contact['avatar'] != $avatar) {
                                DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
                                Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
@@ -1936,35 +1597,93 @@ class Contact
                        return;
                }
 
-               $data = [
-                       $contact['photo'] ?? '',
-                       $contact['thumb'] ?? '',
-                       $contact['micro'] ?? '',
-               ];
+               // User contacts use are updated through the public contacts
+               if (($uid != 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
+                       $pcid = self::getIdForURL($contact['url'], false);
+                       if (!empty($pcid)) {
+                               Logger::debug('Update the private contact via the public contact', ['id' => $cid, 'uid' => $uid, 'public' => $pcid]);
+                               self::updateAvatar($pcid, $avatar, $force, true);
+                               return;
+                       }
+               }
+               
+               // Replace cached avatar pictures from the default avatar with the default avatars in different sizes
+               if (strpos($avatar, self::DEFAULT_AVATAR_PHOTO)) {
+                       $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
+                               'photo' => DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO,
+                               'thumb' => DI::baseUrl() . self::DEFAULT_AVATAR_THUMB,
+                               'micro' => DI::baseUrl() . self::DEFAULT_AVATAR_MICRO];
+                       Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
+               }
 
-               $update = ($contact['avatar'] != $avatar) || $force;
+               // Use the data from the self account
+               if (empty($fields)) {
+                       $local_uid = User::getIdForURL($contact['url']);
+                       if (!empty($local_uid)) {
+                               $fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]);
+                               Logger::debug('Use owner data', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
+                       }
+               }
 
-               if (!$update) {
-                       foreach ($data as $image_uri) {
-                               $image_rid = Photo::ridFromURI($image_uri);
-                               if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
-                                       Logger::info('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
-                                       $update = true;
+               if (empty($fields)) {
+                       $update = ($contact['avatar'] != $avatar) || $force;
+
+                       if (!$update) {
+                               $data = [
+                                       $contact['photo'] ?? '',
+                                       $contact['thumb'] ?? '',
+                                       $contact['micro'] ?? '',
+                               ];
+               
+                               foreach ($data as $image_uri) {
+                                       $image_rid = Photo::ridFromURI($image_uri);
+                                       if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
+                                               Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
+                                               $update = true;
+                                       }
                                }
                        }
+
+                       if ($update) {
+                               $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
+                               if ($photos) {
+                                       $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
+                                       $update = !empty($fields);
+                                       Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
+                               } else {
+                                       $update = false;
+                               }
+                       }
+               } else {
+                       $update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
                }
 
-               if ($update) {
-                       $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
-                       if ($photos) {
-                               $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
-                               DBA::update('contact', $fields, ['id' => $cid]);
-                       } elseif (empty($contact['avatar'])) {
-                               // Ensure that the avatar field is set
-                               DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);                          
-                               Logger::info('Failed profile import', ['id' => $cid, 'force' => $force, 'avatar' => $avatar, 'contact' => $contact]);
+               if (!$update) {
+                       return;
+               }
+
+               $cids = [];
+               $uids = [];
+               if (($uid == 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
+                       // Collect all user contacts of the given public contact
+                       $personal_contacts = DBA::select('contact', ['id', 'uid'],
+                               ["`nurl` = ? AND `id` != ? AND NOT `self`", $contact['nurl'], $cid]);
+                       while ($personal_contact = DBA::fetch($personal_contacts)) {
+                               $cids[] = $personal_contact['id'];
+                               $uids[] = $personal_contact['uid'];
+                       }
+                       DBA::close($personal_contacts);
+
+                       if (!empty($cids)) {
+                               // Delete possibly existing cached user contact avatars
+                               Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'album' => Photo::CONTACT_PHOTOS]);
                        }
                }
+
+               $cids[] = $cid;
+               $uids[] = $uid;
+               Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
+               DBA::update('contact', $fields, ['id' => $cids]);
        }
 
        /**
@@ -2071,12 +1790,29 @@ class Contact
        /**
         * @param integer $id      contact id
         * @param string  $network Optional network we are probing for
-        * @param boolean $force   Optional forcing of network probing (otherwise we use the cached data)
         * @return boolean
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function updateFromProbe(int $id, string $network = '', bool $force = false)
+       public static function updateFromProbe(int $id, string $network = '')
+       {
+               $contact = DBA::selectFirst('contact', ['uid', 'url'], ['id' => $id]);
+               if (!DBA::isResult($contact)) {
+                       return false;
+               }
+
+               $ret = Probe::uri($contact['url'], $network, $contact['uid']);
+               return self::updateFromProbeArray($id, $ret);
+       }
+
+       /**
+        * @param integer $id      contact id
+        * @param array   $ret     Probed data
+        * @return boolean
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       private static function updateFromProbeArray(int $id, array $ret)
        {
                /*
                  Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
@@ -2086,9 +1822,9 @@ class Contact
                // These fields aren't updated by this routine:
                // 'xmpp', 'sensitive'
 
-               $fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe',
+               $fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'subscribe', 'manually-approve',
                        'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
-                       'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey'];
+                       'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item'];
                $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
                if (!DBA::isResult($contact)) {
                        return false;
@@ -2103,8 +1839,6 @@ class Contact
                $contact['photo'] = $contact['avatar'];
                unset($contact['avatar']);
 
-               $ret = Probe::uri($contact['url'], $network, $uid, !$force);
-
                $updated = DateTimeFormat::utcNow();
 
                // We must not try to update relay contacts via probe. They are no real contacts.
@@ -2118,16 +1852,12 @@ class Contact
 
                // If Probe::uri fails the network code will be different ("feed" or "unkn")
                if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) {
-                       if ($force && ($uid == 0)) {
+                       if ($uid == 0) {
                                self::updateContact($id, $uid, $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
                        }
                        return false;
                }
 
-               if (ContactRelation::isDiscoverable($ret['url'])) {
-                       Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
-               }
-
                if (isset($ret['hide']) && is_bool($ret['hide'])) {
                        $ret['unsearchable'] = $ret['hide'];
                }
@@ -2136,16 +1866,18 @@ class Contact
                        $ret['forum'] = false;
                        $ret['prv'] = false;
                        $ret['contact-type'] = $ret['account-type'];
-                       if ($ret['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
-                               $apcontact = APContact::getByURL($ret['url'], false);
-                               if (isset($apcontact['manually-approve'])) {
-                                       $ret['forum'] = (bool)!$apcontact['manually-approve'];
-                                       $ret['prv'] = (bool)!$ret['forum'];
-                               }
+                       if (($ret['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY) && isset($ret['manually-approve'])) {
+                               $ret['forum'] = (bool)!$ret['manually-approve'];
+                               $ret['prv'] = (bool)!$ret['forum'];
                        }
                }
 
-               $new_pubkey = $ret['pubkey'];
+               $new_pubkey = $ret['pubkey'] ?? '';
+
+               if ($uid == 0) {
+                       $ret['last-item'] = Probe::getLastUpdate($ret);
+                       Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
+               }
 
                $update = false;
 
@@ -2161,18 +1893,29 @@ class Contact
                        }
                }
 
+               if (!empty($ret['last-item']) && ($contact['last-item'] < $ret['last-item'])) {
+                       $update = true;
+               } else {
+                       unset($ret['last-item']);
+               }
+
                if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) {
-                       self::updateAvatar($id, $ret['photo'], $update || $force);
+                       self::updateAvatar($id, $ret['photo'], $update);
                }
 
                if (!$update) {
-                       if ($force) {
-                               self::updateContact($id, $uid, $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
-                       }
+                       self::updateContact($id, $uid, $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
 
+                       if (Contact\Relation::isDiscoverable($ret['url'])) {
+                               Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
+                       }
+       
                        // Update the public contact
                        if ($uid != 0) {
-                               self::updateFromProbeByURL($ret['url']);
+                               $contact = self::getByURL($ret['url'], false, ['id']);
+                               if (!empty($contact['id'])) {
+                                       self::updateFromProbeArray($contact['id'], $ret);
+                               }
                        }
 
                        return true;
@@ -2186,7 +1929,7 @@ class Contact
                        $ret['pubkey'] = $new_pubkey;
                }
 
-               if (($ret['addr'] != $contact['addr']) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) {
+               if ((!empty($ret['addr']) && ($ret['addr'] != $contact['addr'])) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) {
                        $ret['uri-date'] = DateTimeFormat::utcNow();
                }
 
@@ -2194,7 +1937,7 @@ class Contact
                        $ret['name-date'] = $updated;
                }
 
-               if ($force && ($uid == 0)) {
+               if ($uid == 0) {
                        $ret['last-update'] = $updated;
                        $ret['success_update'] = $updated;
                        $ret['failed'] = false;
@@ -2204,10 +1947,20 @@ class Contact
 
                self::updateContact($id, $uid, $ret['url'], $ret);
 
+               if (Contact\Relation::isDiscoverable($ret['url'])) {
+                       Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
+               }
+
                return true;
        }
 
-       public static function updateFromProbeByURL($url, $force = false)
+       /**
+        * @param integer $url contact url
+        * @return integer Contact id
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function updateFromProbeByURL($url)
        {
                $id = self::getIdForURL($url);
 
@@ -2215,7 +1968,7 @@ class Contact
                        return $id;
                }
 
-               self::updateFromProbe($id, '', $force);
+               self::updateFromProbe($id);
 
                return $id;
        }
@@ -2311,7 +2064,7 @@ class Contact
                if (!empty($arr['contact']['name'])) {
                        $ret = $arr['contact'];
                } else {
-                       $ret = Probe::uri($url, $network, $user['uid'], false);
+                       $ret = Probe::uri($url, $network, $user['uid']);
                }
 
                if (($network != '') && ($ret['network'] != $network)) {
@@ -2356,7 +2109,7 @@ class Contact
                }
 
                // do we have enough information?
-               if (empty($ret['name']) || empty($ret['poll']) || (empty($ret['url']) && empty($ret['addr']))) {
+               if (empty($protocol) || ($protocol == Protocol::PHANTOM) || (empty($ret['url']) && empty($ret['addr']))) {
                        $result['message'] .= DI::l10n()->t('The profile address specified does not provide adequate information.') . EOL;
                        if (empty($ret['poll'])) {
                                $result['message'] .= DI::l10n()->t('No compatible communication protocols or feeds were discovered.') . EOL;
@@ -2390,11 +2143,8 @@ class Contact
                $hidden = (($protocol === Protocol::MAIL) ? 1 : 0);
 
                $pending = false;
-               if ($protocol == Protocol::ACTIVITYPUB) {
-                       $apcontact = APContact::getByURL($ret['url'], false);
-                       if (isset($apcontact['manually-approve'])) {
-                               $pending = (bool)$apcontact['manually-approve'];
-                       }
+               if (($protocol == Protocol::ACTIVITYPUB) && isset($ret['manually-approve'])) {
+                       $pending = (bool)$ret['manually-approve'];
                }
 
                if (in_array($protocol, [Protocol::MAIL, Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
@@ -2588,7 +2338,7 @@ class Contact
 
                        // Contact is blocked at user-level
                        if (!empty($contact['id']) && !empty($importer['id']) &&
-                               self::isBlockedByUser($contact['id'], $importer['id'])) {
+                               Contact\User::isBlocked($contact['id'], $importer['id'])) {
                                return false;
                        }
 
@@ -2602,7 +2352,7 @@ class Contact
                        }
 
                        // Ensure to always have the correct network type, independent from the connection request method
-                       self::updateFromProbe($contact['id'], '', true);
+                       self::updateFromProbe($contact['id']);
 
                        return true;
                } else {
@@ -2631,7 +2381,7 @@ class Contact
                        $contact_id = DBA::lastInsertId();
 
                        // Ensure to always have the correct network type, independent from the connection request method
-                       self::updateFromProbe($contact_id, '', true);
+                       self::updateFromProbe($contact_id);
 
                        self::updateAvatar($contact_id, $photo, true);
 
@@ -2789,15 +2539,15 @@ class Contact
                        return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
                }
 
-               $data = self::getProbeDataFromDatabase($contact_url);
-               if (empty($data)) {
+               $contact = self::getByURL($contact_url, false);
+               if (empty($contact)) {
                        return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
                }
 
                // Prevents endless loop in case only a non-public contact exists for the contact URL
-               unset($data['uid']);
+               unset($contact['uid']);
 
-               return self::magicLinkByContact($data, $url ?: $contact_url);
+               return self::magicLinkByContact($contact, $url ?: $contact_url);
        }
 
        /**
@@ -2831,7 +2581,7 @@ class Contact
        {
                $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
 
-               if (!Session::isAuthenticated() || ($contact['network'] != Protocol::DFRN)) {
+               if (!Session::isAuthenticated()) {
                        return $destination;
                }
 
@@ -2840,6 +2590,14 @@ class Contact
                        return $url;
                }
 
+               if (DI::pConfig()->get(local_user(), 'system', 'stay_local') && ($url == '')) {
+                       return 'contact/' . $contact['id'] . '/conversations';
+               }
+
+               if ($contact['network'] != Protocol::DFRN) {
+                       return $destination;
+               }
+
                if (!empty($contact['uid'])) {
                        return self::magicLink($contact['url'], $url);
                }
@@ -2857,18 +2615,6 @@ class Contact
                return $redirect;
        }
 
-       /**
-        * Remove a contact from all groups
-        *
-        * @param integer $contact_id
-        *
-        * @return boolean Success
-        */
-       public static function removeFromGroups($contact_id)
-       {
-               return DBA::delete('group_member', ['contact-id' => $contact_id]);
-       }
-
        /**
         * Is the contact a forum?
         *
@@ -2952,110 +2698,6 @@ class Contact
                return $contacts;
        }
 
-       /**
-        * @param int $uid   user
-        * @param int $start optional, default 0
-        * @param int $limit optional, default 80
-        * @return array
-        */
-       static public function getSuggestions(int $uid, int $start = 0, int $limit = 80)
-       {
-               $cid = self::getPublicIdByUserId($uid);
-               $totallimit = $start + $limit;
-               $contacts = [];
-
-               Logger::info('Collecting suggestions', ['uid' => $uid, 'cid' => $cid, 'start' => $start, 'limit' => $limit]);
-
-               $diaspora = DI::config()->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::ACTIVITYPUB;
-               $ostatus = !DI::config()->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::ACTIVITYPUB;
-
-               // The query returns contacts where contacts interacted with whom the given user follows.
-               // Contacts who already are in the user's contact table are ignored.
-               $results = DBA::select('contact', [],
-                       ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
-                               (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ?)
-                                       AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
-                                               (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))))
-                       AND NOT `hidden` AND `network` IN (?, ?, ?, ?)",
-                       $cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
-                       Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
-                       ['order' => ['last-item' => true], 'limit' => $totallimit]
-               );
-
-               while ($contact = DBA::fetch($results)) {
-                       $contacts[$contact['id']] = $contact;
-               }
-               DBA::close($results);
-
-               Logger::info('Contacts of contacts who are followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
-
-               if (count($contacts) >= $totallimit) {
-                       return array_slice($contacts, $start, $limit);
-               }
-
-               // The query returns contacts where contacts interacted with whom also interacted with the given user.
-               // Contacts who already are in the user's contact table are ignored.
-               $results = DBA::select('contact', [],
-                       ["`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` IN
-                               (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)
-                                       AND NOT `cid` IN (SELECT `id` FROM `contact` WHERE `uid` = ? AND `nurl` IN
-                                               (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))))
-                       AND NOT `hidden` AND `network` IN (?, ?, ?, ?)",
-                       $cid, 0, $uid, Contact::FRIEND, Contact::SHARING,
-                       Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
-                       ['order' => ['last-item' => true], 'limit' => $totallimit]
-               );
-
-               while ($contact = DBA::fetch($results)) {
-                       $contacts[$contact['id']] = $contact;
-               }
-               DBA::close($results);
-
-               Logger::info('Contacts of contacts who are following the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
-
-               if (count($contacts) >= $totallimit) {
-                       return array_slice($contacts, $start, $limit);
-               }
-
-               // The query returns contacts that follow the given user but aren't followed by that user.
-               $results = DBA::select('contact', [],
-                       ["`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` = ?)
-                       AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
-                       $uid, Contact::FOLLOWER, 0, 
-                       Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
-                       ['order' => ['last-item' => true], 'limit' => $totallimit]
-               );
-
-               while ($contact = DBA::fetch($results)) {
-                       $contacts[$contact['id']] = $contact;
-               }
-               DBA::close($results);
-
-               Logger::info('Followers that are not followed by the given user', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
-
-               if (count($contacts) >= $totallimit) {
-                       return array_slice($contacts, $start, $limit);
-               }
-
-               // The query returns any contact that isn't followed by that user.
-               $results = DBA::select('contact', [],
-                       ["NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))
-                       AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
-                       $uid, Contact::FRIEND, Contact::SHARING, 0, 
-                       Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus],
-                       ['order' => ['last-item' => true], 'limit' => $totallimit]
-               );
-
-               while ($contact = DBA::fetch($results)) {
-                       $contacts[$contact['id']] = $contact;
-               }
-               DBA::close($results);
-
-               Logger::info('Any contact', ['uid' => $uid, 'cid' => $cid, 'count' => count($contacts)]);
-
-               return array_slice($contacts, $start, $limit);
-       }
-
        /**
         * Add public contacts from an array
         *
@@ -3066,198 +2708,24 @@ class Contact
        {
                $added = 0;
                $updated = 0;
+               $unchanged = 0;
                $count = 0;
 
                foreach ($urls as $url) {
-                       $contact = Contact::getByURL($url, false, ['id']); 
+                       $contact = Contact::getByURL($url, false, ['id', 'updated']);
                        if (empty($contact['id'])) {
                                Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);
                                ++$added;
-                       } else {
+                       } elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) {
                                Worker::add(PRIORITY_LOW, 'UpdateContact', $contact['id']);
                                ++$updated;
-                       }
-                       ++$count;
-               }
-
-               return ['count' => $count, 'added' => $added, 'updated' => $updated];
-       }
-
-       /**
-        * Set the last date that the contact had posted something
-        *
-        * This functionality is currently unused
-        *
-        * @param string $data  probing result
-        * @param bool   $force force updating
-        */
-       private static function setLastUpdate(array $data, bool $force = false)
-       {
-               $contact = self::getByURL($data['url'], false, []);
-               if (empty($contact)) {
-                       return;
-               }
-               if (!$force && !GServer::updateNeeded($contact['created'], $contact['updated'], $contact['last_failure'], $contact['last_contact'])) {
-                       Logger::info("Don't update profile", ['url' => $data['url'], 'updated' => $contact['updated']]);
-                       return;
-               }
-
-               if (self::updateFromNoScrape($data)) {
-                       return;
-               }
-
-               if (!empty($data['outbox'])) {
-                       self::updateFromOutbox($data['outbox'], $data);
-               } elseif (!empty($data['poll']) && ($data['network'] == Protocol::ACTIVITYPUB)) {
-                       self::updateFromOutbox($data['poll'], $data);
-               } elseif (!empty($data['poll'])) {
-                       self::updateFromFeed($data);
-               }
-       }
-
-       /**
-        * Update a global contact via the "noscrape" endpoint
-        *
-        * @param string $data Probing result
-        *
-        * @return bool 'true' if update was successful or the server was unreachable
-        */
-       private static function updateFromNoScrape(array $data)
-       {
-               // Check the 'noscrape' endpoint when it is a Friendica server
-               $gserver = DBA::selectFirst('gserver', ['noscrape'], ["`nurl` = ? AND `noscrape` != ''",
-               Strings::normaliseLink($data['baseurl'])]);
-               if (!DBA::isResult($gserver)) {
-                       return false;
-               }
-
-               $curlResult = DI::httpRequest()->get($gserver['noscrape'] . '/' . $data['nick']);
-
-               if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
-                       $noscrape = json_decode($curlResult->getBody(), true);
-                       if (!empty($noscrape) && !empty($noscrape['updated'])) {
-                               $noscrape['updated'] = DateTimeFormat::utc($noscrape['updated'], DateTimeFormat::MYSQL);
-                               $fields = ['failed' => false, 'last_contact' => DateTimeFormat::utcNow(), 'updated' => $noscrape['updated']];
-                               DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($data['url'])]);
-                               return true;
-                       }
-               } elseif ($curlResult->isTimeout()) {
-                       // On a timeout return the existing value, but mark the contact as failure
-                       $fields = ['failed' => true, 'last_failure' => DateTimeFormat::utcNow()];
-                       DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($data['url'])]);
-                       return true;
-               }
-               return false;
-       }
-
-       /**
-        * Update a global contact via an ActivityPub Outbox
-        *
-        * @param string $feed
-        * @param array  $data Probing result
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       private static function updateFromOutbox(string $feed, array $data)
-       {
-               $outbox = ActivityPub::fetchContent($feed);
-               if (empty($outbox)) {
-                       return;
-               }
-
-               if (!empty($outbox['orderedItems'])) {
-                       $items = $outbox['orderedItems'];
-               } elseif (!empty($outbox['first']['orderedItems'])) {
-                       $items = $outbox['first']['orderedItems'];
-               } elseif (!empty($outbox['first']['href']) && ($outbox['first']['href'] != $feed)) {
-                       self::updateFromOutbox($outbox['first']['href'], $data);
-                       return;
-               } elseif (!empty($outbox['first'])) {
-                       if (is_string($outbox['first']) && ($outbox['first'] != $feed)) {
-                               self::updateFromOutbox($outbox['first'], $data);
-                       } else {
-                               Logger::warning('Unexpected data', ['outbox' => $outbox]);
-                       }
-                       return;
-               } else {
-                       $items = [];
-               }
-
-               $last_updated = '';
-               foreach ($items as $activity) {
-                       if (!empty($activity['published'])) {
-                               $published =  DateTimeFormat::utc($activity['published']);
-                       } elseif (!empty($activity['object']['published'])) {
-                               $published =  DateTimeFormat::utc($activity['object']['published']);
                        } else {
-                               continue;
+                               ++$unchanged;
                        }
-
-                       if ($last_updated < $published) {
-                               $last_updated = $published;
-                       }
-               }
-
-               if (empty($last_updated)) {
-                       return;
-               }
-
-               $fields = ['failed' => false, 'last_contact' => DateTimeFormat::utcNow(), 'updated' => $last_updated];
-               DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($data['url'])]);
-       }
-
-       /**
-        * Update a global contact via an XML feed
-        *
-        * @param string $data Probing result
-        */
-       private static function updateFromFeed(array $data)
-       {
-               // Search for the newest entry in the feed
-               $curlResult = DI::httpRequest()->get($data['poll']);
-               if (!$curlResult->isSuccess()) {
-                       $fields = ['failed' => true, 'last_failure' => DateTimeFormat::utcNow()];
-                       DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($data['url'])]);
-
-                       Logger::info("Profile wasn't reachable (no feed)", ['url' => $data['url']]);
-                       return;
-               }
-
-               $doc = new DOMDocument();
-               @$doc->loadXML($curlResult->getBody());
-
-               $xpath = new DOMXPath($doc);
-               $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
-
-               $entries = $xpath->query('/atom:feed/atom:entry');
-
-               $last_updated = '';
-
-               foreach ($entries as $entry) {
-                       $published_item = $xpath->query('atom:published/text()', $entry)->item(0);
-                       $updated_item   = $xpath->query('atom:updated/text()'  , $entry)->item(0);
-                       $published      = !empty($published_item->nodeValue) ? DateTimeFormat::utc($published_item->nodeValue) : null;
-                       $updated        = !empty($updated_item->nodeValue) ? DateTimeFormat::utc($updated_item->nodeValue) : null;
-
-                       if (empty($published) || empty($updated)) {
-                               Logger::notice('Invalid entry for XPath.', ['entry' => $entry, 'url' => $data['url']]);
-                               continue;
-                       }
-
-                       if ($last_updated < $published) {
-                               $last_updated = $published;
-                       }
-
-                       if ($last_updated < $updated) {
-                               $last_updated = $updated;
-                       }
-               }
-
-               if (empty($last_updated)) {
-                       return;
+                       ++$count;
                }
 
-               $fields = ['failed' => false, 'last_contact' => DateTimeFormat::utcNow(), 'updated' => $last_updated];
-               DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($data['url'])]);
+               return ['count' => $count, 'added' => $added, 'updated' => $updated, 'unchanged' => $unchanged];
        }
 
        /**