]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #11974 from annando/issue-11969
[friendica.git] / src / Model / Contact.php
index d6b2780ef3a9ec3ade68147ce9f0ffa4983c1333..fcf177d8328ebc12647ab460a338c806c23c5302 100644 (file)
@@ -160,6 +160,7 @@ class Contact
                        $fields['created'] = DateTimeFormat::utcNow();
                }
 
+               $fields = DI::dbaDefinition()->truncateFieldsForTable('contact', $fields);
                DBA::insert('contact', $fields, $duplicate_mode);
                $contact = DBA::selectFirst('contact', [], ['id' => DBA::lastInsertId()]);
                if (!DBA::isResult($contact)) {
@@ -168,16 +169,41 @@ class Contact
                        return 0;
                }
 
-               Contact\User::insertForContactArray($contact);
-
-               // Search for duplicated contacts and get rid of them
-               if (!$contact['self']) {
-                       self::removeDuplicates($contact['nurl'], $contact['uid']);
+               $fields = DI::dbaDefinition()->truncateFieldsForTable('account-user', $contact);
+               DBA::insert('account-user', $fields, Database::INSERT_IGNORE);
+               $account_user = DBA::selectFirst('account-user', ['id'], ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id']]);
+               if (empty($account_user['id'])) {
+                       Logger::warning('Account-user entry not found', ['cid' => $contact['id'], 'uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'url' => $contact['url']]);
+               } elseif ($account_user['id'] != $contact['id']) {
+                       $duplicate = DBA::selectFirst('contact', [], ['id' => $account_user['id'], 'deleted' => false]);
+                       if (!empty($duplicate['id'])) {
+                               $ret = Contact::deleteById($contact['id']);
+                               Logger::notice('Deleted duplicated contact', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $duplicate['id'], 'uid' => $duplicate['uid'], 'uri-id' => $duplicate['uri-id'], 'url' => $duplicate['url']]);
+                               $contact = $duplicate;
+                       } else {
+                               $ret = DBA::update('account-user', ['id' => $contact['id']], ['uid' => $contact['uid'], 'uri-id' => $contact['uri-id']]);
+                               Logger::notice('Updated account-user', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $contact['id'], 'uid' => $contact['uid'], 'uri-id' => $contact['uri-id'], 'url' => $contact['url']]);
+                       }
                }
 
+               Contact\User::insertForContactArray($contact);
+
                return $contact['id'];
        }
 
+       /**
+        * Delete contact by id
+        *
+        * @param integer $id
+        * @return boolean
+        */
+       public static function deleteById(int $id): bool
+       {
+               Logger::debug('Delete contact', ['id' => $id]);
+               DBA::delete('account-user', ['id' => $id]);
+               return DBA::delete('contact', ['id' => $id]);
+       }
+
        /**
         * Updates rows in the contact table
         *
@@ -191,6 +217,7 @@ class Contact
         */
        public static function update(array $fields, array $condition, $old_fields = [])
        {
+               $fields = DI::dbaDefinition()->truncateFieldsForTable('contact', $fields);
                $ret = DBA::update('contact', $fields, $condition, $old_fields);
 
                // Apply changes to the "user-contact" table on dedicated fields
@@ -250,7 +277,7 @@ class Contact
                // Add internal fields
                $removal = [];
                if (!empty($fields)) {
-                       foreach (['id', 'avatar', 'created', 'updated', 'last-update', 'success_update', 'failure_update', 'network'] as $internal) {
+                       foreach (['id', 'next-update', 'network'] as $internal) {
                                if (!in_array($internal, $fields)) {
                                        $fields[] = $internal;
                                        $removal[] = $internal;
@@ -280,9 +307,8 @@ class Contact
                }
 
                // Update the contact in the background if needed
-               $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) && !self::isLocalById($contact['id'])) {
-                       Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']);
+               if (Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
+                       Worker::add(['priority' => PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']);
                }
 
                // Remove the internal fields
@@ -327,14 +353,15 @@ class Contact
        /**
         * Tests if the given contact is a follower
         *
-        * @param int $cid Either public contact id or user's contact id
-        * @param int $uid User ID
+        * @param int  $cid    Either public contact id or user's contact id
+        * @param int  $uid    User ID
+        * @param bool $strict If "true" then contact mustn't be set to pending or readonly
         *
         * @return boolean is the contact id a follower?
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function isFollower(int $cid, int $uid): bool
+       public static function isFollower(int $cid, int $uid, bool $strict = false): bool
        {
                if (Contact\User::isBlocked($cid, $uid)) {
                        return false;
@@ -346,20 +373,24 @@ class Contact
                }
 
                $condition = ['id' => $cdata['user'], 'rel' => [self::FOLLOWER, self::FRIEND]];
+               if ($strict) {
+                       $condition = array_merge($condition, ['pending' => false, 'readonly' => false, 'blocked' => false]);
+               }
                return DBA::exists('contact', $condition);
        }
 
        /**
         * Tests if the given contact url is a follower
         *
-        * @param string $url Contact URL
-        * @param int    $uid User ID
+        * @param string $url    Contact URL
+        * @param int    $uid    User ID
+        * @param bool   $strict If "true" then contact mustn't be set to pending or readonly
         *
         * @return boolean is the contact id a follower?
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function isFollowerByURL(string $url, uid $uid): bool
+       public static function isFollowerByURL(string $url, int $uid, bool $strict = false): bool
        {
                $cid = self::getIdForURL($url, $uid);
 
@@ -367,20 +398,21 @@ class Contact
                        return false;
                }
 
-               return self::isFollower($cid, $uid);
+               return self::isFollower($cid, $uid, $strict);
        }
 
        /**
         * Tests if the given user shares with the given contact
         *
-        * @param int $cid Either public contact id or user's contact id
-        * @param int $uid User ID
+        * @param int  $cid    Either public contact id or user's contact id
+        * @param int  $uid    User ID
+        * @param bool $strict If "true" then contact mustn't be set to pending or readonly
         *
         * @return boolean is the contact sharing with given user?
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function isSharing(int $cid, int $uid): bool
+       public static function isSharing(int $cid, int $uid, bool $strict = false): bool
        {
                if (Contact\User::isBlocked($cid, $uid)) {
                        return false;
@@ -392,20 +424,24 @@ class Contact
                }
 
                $condition = ['id' => $cdata['user'], 'rel' => [self::SHARING, self::FRIEND]];
+               if ($strict) {
+                       $condition = array_merge($condition, ['pending' => false, 'readonly' => false, 'blocked' => false]);
+               }
                return DBA::exists('contact', $condition);
        }
 
        /**
         * Tests if the given user follow the given contact url
         *
-        * @param string $url Contact URL
-        * @param int    $uid User ID
+        * @param string $url    Contact URL
+        * @param int    $uid    User ID
+        * @param bool   $strict If "true" then contact mustn't be set to pending or readonly
         *
         * @return boolean is the contact url being followed?
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function isSharingByURL(string $url, int $uid): bool
+       public static function isSharingByURL(string $url, int $uid, bool $strict = false): bool
        {
                $cid = self::getIdForURL($url, $uid);
 
@@ -413,7 +449,7 @@ class Contact
                        return false;
                }
 
-               return self::isSharing($cid, $uid);
+               return self::isSharing($cid, $uid, $strict);
        }
 
        /**
@@ -474,7 +510,6 @@ class Contact
         * Check if the given contact ID is on the same server
         *
         * @param string $url The contact link
-        *
         * @return boolean Is it the same server?
         */
        public static function isLocalById(int $cid): bool
@@ -556,7 +591,6 @@ class Contact
         *
         * @param int $cid Either public contact id or user's contact id
         * @param int $uid User ID
-        *
         * @return array with public and user's contact id
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
@@ -597,12 +631,11 @@ class Contact
         * @param int $cid A contact ID
         * @param int $uid The User ID
         * @param array $fields The selected fields for the contact
-        *
         * @return array The contact details
         *
         * @throws \Exception
         */
-       public static function getContactForUser($cid, $uid, array $fields = [])
+       public static function getContactForUser(int $cid, int $uid, array $fields = []): array
        {
                $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => $uid]);
 
@@ -620,7 +653,7 @@ class Contact
         * @return bool Operation success
         * @throws HTTPException\InternalServerErrorException
         */
-       public static function createSelfFromUserId($uid)
+       public static function createSelfFromUserId(int $uid): bool
        {
                $user = DBA::selectFirst('user', ['uid', 'username', 'nickname', 'pubkey', 'prvkey'],
                        ['uid' => $uid, 'account_expired' => false]);
@@ -677,12 +710,12 @@ class Contact
        /**
         * Updates the self-contact for the provided user id
         *
-        * @param int     $uid
-        * @param boolean $update_avatar Force the avatar update
-        * @return bool   "true" if updated
+        * @param int   $uid
+        * @param bool  $update_avatar Force the avatar update
+        * @return bool "true" if updated
         * @throws HTTPException\InternalServerErrorException
         */
-       public static function updateSelfFromUserID($uid, $update_avatar = false)
+       public static function updateSelfFromUserID(int $uid, bool $update_avatar = false): bool
        {
                $fields = ['id', 'uri-id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey', 'manually-approve',
                        'xmpp', 'matrix', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
@@ -706,23 +739,33 @@ class Contact
                }
 
                $file_suffix = 'jpg';
+               $url = DI::baseUrl() . '/profile/' . $user['nickname'];
+
+               $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'],
+                       'prvkey'       => $user['prvkey'],
+                       'pubkey'       => $user['pubkey'],
+                       'xmpp'         => $profile['xmpp'],
+                       'matrix'       => $profile['matrix'],
+                       'network'      => Protocol::DFRN,
+                       'url'          => $url,
+                       // it seems as if ported accounts can have wrong values, so we make sure that now everything is fine.
+                       'nurl'         => Strings::normaliseLink($url),
+                       'uri-id'       => ItemURI::getIdByURI($url),
+                       'addr'         => $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3),
+                       'request'      => DI::baseUrl() . '/dfrn_request/' . $user['nickname'],
+                       'notify'       => DI::baseUrl() . '/dfrn_notify/' . $user['nickname'],
+                       'poll'         => DI::baseUrl() . '/dfrn_poll/'. $user['nickname'],
+                       'confirm'      => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
+                       'poco'         => DI::baseUrl() . '/poco/' . $user['nickname'],
+               ];
 
-               $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'], 'prvkey' => $user['prvkey'],
-                       'pubkey' => $user['pubkey'], 'xmpp' => $profile['xmpp'], 'matrix' => $profile['matrix'], 'network' => Protocol::DFRN];
-
-               // it seems as if ported accounts can have wrong values, so we make sure that now everything is fine.
-               $fields['url'] = DI::baseUrl() . '/profile/' . $user['nickname'];
-               $fields['nurl'] = Strings::normaliseLink($fields['url']);
-               $fields['uri-id'] = ItemURI::getIdByURI($fields['url']);
-               $fields['addr'] = $user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
-               $fields['request'] = DI::baseUrl() . '/dfrn_request/' . $user['nickname'];
-               $fields['notify'] = DI::baseUrl() . '/dfrn_notify/' . $user['nickname'];
-               $fields['poll'] = DI::baseUrl() . '/dfrn_poll/'. $user['nickname'];
-               $fields['confirm'] = DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'];
-               $fields['poco'] = DI::baseUrl() . '/poco/' . $user['nickname'];
 
                $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
                if (DBA::isResult($avatar)) {
@@ -795,9 +838,10 @@ class Contact
         * Marks a contact for removal
         *
         * @param int $id contact id
+        * @return void
         * @throws HTTPException\InternalServerErrorException
         */
-       public static function remove($id)
+       public static function remove(int $id)
        {
                // We want just to make sure that we don't delete our "self" contact
                $contact = DBA::selectFirst('contact', ['uri-id', 'photo', 'thumb', 'micro', 'uid'], ['id' => $id, 'self' => false]);
@@ -805,6 +849,8 @@ class Contact
                        return;
                }
 
+               DBA::delete('account-user', ['id' => $id]);
+
                self::clearFollowerFollowingEndpointCache($contact['uid']);
 
                // Archive the contact
@@ -822,6 +868,7 @@ class Contact
         * Unfollow the remote contact
         *
         * @param array $contact Target user-specific contact (uid != 0) array
+        * @return void
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -851,6 +898,7 @@ class Contact
         * The local relationship is updated immediately, the eventual remote server is messaged in the background.
         *
         * @param array $contact User-specific contact array (uid != 0) to revoke the follow from
+        * @return void
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -878,6 +926,7 @@ class Contact
         * Completely severs a relationship with a contact
         *
         * @param array $contact User-specific contact (uid != 0) array
+        * @return void
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -912,7 +961,6 @@ class Contact
 
                DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'followers:' . $uid);
                DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'following:' . $uid);
-               DI::cache()->delete(NoScrape::CACHEKEY . $uid);
        }
 
        /**
@@ -925,7 +973,7 @@ class Contact
         * up or some other transient event and that there's a possibility we could recover from it.
         *
         * @param array $contact contact to mark for archival
-        * @return null
+        * @return void
         * @throws HTTPException\InternalServerErrorException
         */
        public static function markForArchival(array $contact)
@@ -978,7 +1026,7 @@ class Contact
         * @see   Contact::markForArchival()
         *
         * @param array $contact contact to be unmarked for archival
-        * @return null
+        * @return void
         * @throws \Exception
         */
        public static function unmarkForArchival(array $contact)
@@ -1025,12 +1073,11 @@ class Contact
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function photoMenu(array $contact, $uid = 0)
+       public static function photoMenu(array $contact, int $uid = 0): array
        {
                $pm_url = '';
                $status_link = '';
                $photos_link = '';
-               $poke_link = '';
 
                if ($uid == 0) {
                        $uid = local_user();
@@ -1073,10 +1120,6 @@ class Contact
                        $pm_url = DI::baseUrl() . '/message/new/' . $contact['id'];
                }
 
-               if (($contact['network'] == Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
-                       $poke_link = 'contact/' . $contact['id'] . '/poke';
-               }
-
                $contact_url = DI::baseUrl() . '/contact/' . $contact['id'];
 
                $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations';
@@ -1111,7 +1154,6 @@ class Contact
                                'network' => [DI::l10n()->t('Network Posts') , $posts_link       , false],
                                'edit'    => [DI::l10n()->t('View Contact')  , $contact_url      , false],
                                'pm'      => [DI::l10n()->t('Send PM')       , $pm_url           , false],
-                               'poke'    => [DI::l10n()->t('Poke')          , $poke_link        , false],
                                'follow'  => [DI::l10n()->t('Connect/Follow'), $follow_link      , true],
                                'unfollow'=> [DI::l10n()->t('UnFollow')      , $unfollow_link    , true],
                        ];
@@ -1168,19 +1210,23 @@ class Contact
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function getIdForURL($url, $uid = 0, $update = null, $default = [])
+       public static function getIdForURL(string $url = null, int $uid = 0, $update = null, array $default = []): int
        {
                $contact_id = 0;
 
-               if ($url == '') {
+               if (empty($url)) {
                        Logger::notice('Empty url, quitting', ['url' => $url, 'user' => $uid, 'default' => $default]);
                        return 0;
                }
 
-               $contact = self::getByURL($url, false, ['id', 'network', 'uri-id'], $uid);
+               $contact = self::getByURL($url, false, ['id', 'network', 'uri-id', 'next-update'], $uid);
 
                if (!empty($contact)) {
-                       $contact_id = $contact["id"];
+                       $contact_id = $contact['id'];
+
+                       if (Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
+                               Worker::add(['priority' => PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']);
+                       }
 
                        if (empty($update) && (!empty($contact['uri-id']) || is_bool($update))) {
                                Logger::debug('Contact found', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]);
@@ -1197,10 +1243,10 @@ class Contact
                $data = [];
 
                if (empty($default['network']) || $update) {
-                       $data = Probe::uri($url, "", $uid);
+                       $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]))) {
+                       if (!empty($default) && !in_array($data['network'], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
                                $data = array_merge($data, $default);
                        }
                } elseif (!empty($default['network'])) {
@@ -1236,6 +1282,11 @@ class Contact
                        return 0;
                }
 
+               if (!$contact_id && !empty($data['account-type']) && $data['account-type'] == User::ACCOUNT_TYPE_DELETED) {
+                       Logger::info('Contact is a tombstone. It will not be inserted', ['url' => $url, 'uid' => $uid]);
+                       return 0;
+               }
+
                if (!$contact_id) {
                        $urls = [Strings::normaliseLink($url), Strings::normaliseLink($data['url'])];
                        if (!empty($data['alias'])) {
@@ -1265,24 +1316,19 @@ class Contact
                        $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
 
                        // Before inserting we do check if the entry does exist now.
-                       if (DI::lock()->acquire(self::LOCK_INSERT, 0)) {
-                               $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
-                               if (DBA::isResult($contact)) {
-                                       $contact_id = $contact['id'];
-                                       Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
-                               } else {
-                                       $contact_id = self::insert($fields);
-                                       if ($contact_id) {
-                                               Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
-                                       }
-                               }
-                               DI::lock()->release(self::LOCK_INSERT);
+                       $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
+                       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::warning('Contact lock had not been acquired');
+                               $contact_id = self::insert($fields);
+                               if ($contact_id) {
+                                       Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
+                               }
                        }
 
                        if (!$contact_id) {
-                               Logger::info('Contact was not inserted', ['url' => $url, 'uid' => $uid]);
+                               Logger::warning('Contact was not inserted', ['url' => $url, 'uid' => $uid]);
                                return 0;
                        }
                } else {
@@ -1312,7 +1358,7 @@ class Contact
         * @return boolean Is the contact archived?
         * @throws HTTPException\InternalServerErrorException
         */
-       public static function isArchived(int $cid)
+       public static function isArchived(int $cid): bool
        {
                if ($cid == 0) {
                        return false;
@@ -1352,11 +1398,10 @@ class Contact
         * Checks if the contact is blocked
         *
         * @param int $cid contact id
-        *
         * @return boolean Is the contact blocked?
         * @throws HTTPException\InternalServerErrorException
         */
-       public static function isBlocked($cid)
+       public static function isBlocked(int $cid): bool
        {
                if ($cid == 0) {
                        return false;
@@ -1378,11 +1423,10 @@ class Contact
         * Checks if the contact is hidden
         *
         * @param int $cid contact id
-        *
         * @return boolean Is the contact hidden?
         * @throws \Exception
         */
-       public static function isHidden($cid)
+       public static function isHidden(int $cid): bool
        {
                if ($cid == 0) {
                        return false;
@@ -1406,7 +1450,7 @@ class Contact
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0, bool $only_media = false)
+       public static function getPostsFromUrl(string $contact_url, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
        {
                return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media);
        }
@@ -1422,7 +1466,7 @@ class Contact
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0, bool $only_media = false)
+       public static function getPostsFromId(int $cid, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
        {
                $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
                if (!DBA::isResult($contact)) {
@@ -1438,7 +1482,7 @@ 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` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ?)) AND " . $sql,
+                       $condition = ["((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql,
                                $cid, GRAVITY_PARENT, $cid, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()];
                } else {
                        $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
@@ -1958,7 +2002,7 @@ class Contact
         * @param string  $updated Contact update date
         * @return string avatar link
         */
-       public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''):string
+       public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string
        {
                // We have to fetch the "updated" variable when it wasn't provided
                // The parameter can be provided to improve performance
@@ -1999,7 +2043,7 @@ class Contact
         * @param string  $size One of the Proxy::SIZE_* constants
         * @return string avatar link
         */
-       public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''):string
+       public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''): string
        {
                $condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)",
                        Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0];
@@ -2015,7 +2059,7 @@ class Contact
         * @param string  $updated Contact update date
         * @return string header link
         */
-       public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''):string
+       public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string
        {
                // We have to fetch the "updated" variable when it wasn't provided
                // The parameter can be provided to improve performance
@@ -2204,25 +2248,22 @@ class Contact
        /**
         * Helper function for "updateFromProbe". Updates personal and public contact
         *
-        * @param integer $id      contact id
-        * @param integer $uid     user id
-        * @param string  $old_url The previous profile URL of the contact
-        * @param string  $new_url The profile URL of the contact
-        * @param array   $fields  The fields that are updated
+        * @param integer $id     contact id
+        * @param integer $uid    user id
+        * @param integer $uri_id Uri-Id
+        * @param string  $url    The profile URL of the contact
+        * @param array   $fields The fields that are updated
         *
         * @throws \Exception
         */
-       private static function updateContact(int $id, int $uid, string $old_url, string $new_url, array $fields)
+       private static function updateContact(int $id, int $uid, int $uri_id, string $url, array $fields)
        {
                if (!self::update($fields, ['id' => $id])) {
                        Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
                        return;
                }
 
-               // Search for duplicated contacts and get rid of them
-               if (self::removeDuplicates(Strings::normaliseLink($new_url), $uid)) {
-                       return;
-               }
+               self::setAccountUser($id, $uid, $uri_id, $url);
 
                // Archive or unarchive the contact.
                $contact = DBA::selectFirst('contact', [], ['id' => $id]);
@@ -2244,7 +2285,7 @@ class Contact
                }
 
                // Update contact data for all users
-               $condition = ['self' => false, 'nurl' => Strings::normaliseLink($old_url)];
+               $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url)];
 
                $condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB];
                self::update($fields, $condition);
@@ -2266,6 +2307,51 @@ class Contact
                self::update($fields, $condition);
        }
 
+       /**
+        * Create or update an "account-user" entry
+        *
+        * @param integer $id
+        * @param integer $uid
+        * @param integer $uri_id
+        * @param string $url
+        * @return void
+        */
+       public static function setAccountUser(int $id, int $uid, int $uri_id, string $url)
+       {
+               if (empty($uri_id)) {
+                       return;
+               }
+
+               $account_user = DBA::selectFirst('account-user', ['id', 'uid', 'uri-id'], ['id' => $id]);
+               if (!empty($account_user['uri-id']) && ($account_user['uri-id'] != $uri_id)) {
+                       if ($account_user['uid'] == $uid) {
+                               $ret = DBA::update('account-user', ['uri-id' => $uri_id], ['id' => $id]);
+                               Logger::notice('Updated account-user uri-id', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
+                       } else {
+                               // This should never happen
+                               Logger::warning('account-user exists for a different uri-id and uid', ['account_user' => $account_user, 'id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
+                       }
+               }
+
+               $account_user = DBA::selectFirst('account-user', ['id', 'uid', 'uri-id'], ['uid' => $uid, 'uri-id' => $uri_id]);
+               if (!empty($account_user['id'])) {
+                       if ($account_user['id'] == $id) {
+                               Logger::debug('account-user already exists', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
+                               return;
+                       } elseif (!DBA::exists('contact', ['id' => $account_user['id'], 'deleted' => false])) {
+                               $ret = DBA::update('account-user', ['id' => $id], ['uid' => $uid, 'uri-id' => $uri_id]);
+                               Logger::notice('Updated account-user', ['ret' => $ret, 'account-user' => $account_user, 'cid' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
+                               return;
+                       }
+                       Logger::warning('account-user exists for a different contact id', ['account_user' => $account_user, 'id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
+                       Worker::add(PRIORITY_HIGH, 'MergeContact', $account_user['id'], $id, $uid);
+               } elseif (DBA::insert('account-user', ['id' => $id, 'uri-id' => $uri_id, 'uid' => $uid], Database::INSERT_IGNORE)) {
+                       Logger::notice('account-user was added', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
+               } else {
+                       Logger::warning('account-user was not added', ['id' => $id, 'uid' => $uid, 'uri-id' => $uri_id, 'url' => $url]);
+               }
+       }
+
        /**
         * Remove duplicated contacts
         *
@@ -2290,11 +2376,6 @@ class Contact
 
                $first = $first_contact['id'];
                Logger::info('Found duplicates', ['count' => $count, 'first' => $first, 'uid' => $uid, 'nurl' => $nurl]);
-               if (($uid != 0 && ($first_contact['network'] == Protocol::DFRN))) {
-                       // Don't handle non public DFRN duplicates by now (legacy DFRN is very special because of the key handling)
-                       Logger::info('Not handling non public DFRN duplicate', ['uid' => $uid, 'nurl' => $nurl]);
-                       return false;
-               }
 
                // Find all duplicates
                $condition = ["`nurl` = ? AND `uid` = ? AND `id` != ? AND NOT `self` AND NOT `deleted`", $nurl, $uid, $first];
@@ -2312,6 +2393,8 @@ class Contact
        }
 
        /**
+        * Updates contact record by provided id and optional network
+        *
         * @param integer $id      contact id
         * @param string  $network Optional network we are probing for
         * @return boolean
@@ -2335,13 +2418,56 @@ class Contact
        }
 
        /**
+        * Checks if the given contact has got local data
+        *
+        * @param int   $id
+        * @param array $contact
+        *
+        * @return boolean
+        */
+       private static function hasLocalData(int $id, array $contact): bool
+       {
+               if (!empty($contact['uri-id']) && DBA::exists('contact', ["`uri-id` = ? AND `uid` != ?", $contact['uri-id'], 0])) {
+                       // User contacts with the same uri-id exist
+                       return true;
+               } elseif (DBA::exists('contact', ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($contact['url']), 0])) {
+                       // User contacts with the same nurl exists (compatibility mode for systems with missing uri-id values)
+                       return true;
+               }
+               if (DBA::exists('post-tag', ['cid' => $id])) {
+                       // Is tagged in a post
+                       return true;
+               }
+               if (DBA::exists('user-contact', ['cid' => $id])) {
+                       // Has got user-contact data
+                       return true;
+               }
+               if (Post::exists(['author-id' => $id])) {
+                       // Posts with this author exist
+                       return true;
+               }
+               if (Post::exists(['owner-id' => $id])) {
+                       // Posts with this owner exist
+                       return true;
+               }
+               if (Post::exists(['causer-id' => $id])) {
+                       // Posts with this causer exist
+                       return true;
+               }
+               // We don't have got this contact locally
+               return false;
+       }
+
+       /**
+        * Updates contact record by provided id and probed data
+        *
         * @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)
+       private static function updateFromProbeArray(int $id, array $ret): bool
        {
                /*
                  Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
@@ -2353,7 +2479,8 @@ class Contact
 
                $fields = ['uid', 'uri-id', 'avatar', 'header', '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', 'last-item', 'xmpp', 'matrix'];
+                       'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item', 'xmpp', 'matrix',
+                       'created', 'last-update'];
                $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
                if (!DBA::isResult($contact)) {
                        return false;
@@ -2386,14 +2513,34 @@ class Contact
                $pubkey = $contact['pubkey'];
                unset($contact['pubkey']);
 
+               $created = $contact['created'];
+               unset($contact['created']);
+
+               $last_update = $contact['last-update'];
+               unset($contact['last-update']);
+
                $contact['photo'] = $contact['avatar'];
                unset($contact['avatar']);
 
                $updated = DateTimeFormat::utcNow();
 
+               $has_local_data = self::hasLocalData($id, $contact);
+
+               if (!Probe::isProbable($ret['network'])) {
+                       // Periodical checks are only done on federated contacts
+                       $failed_next_update  = null;
+                       $success_next_update = null;
+               } elseif ($has_local_data) {
+                       $failed_next_update  = GServer::getNextUpdateDate(false, $created, $last_update, !in_array($contact['network'], Protocol::FEDERATED));
+                       $success_next_update = GServer::getNextUpdateDate(true, $created, $last_update, !in_array($contact['network'], Protocol::FEDERATED));
+               } else {
+                       $failed_next_update  = DateTimeFormat::utc('now +6 month');
+                       $success_next_update = DateTimeFormat::utc('now +1 month');
+               }
+
                if (Strings::normaliseLink($contact['url']) != Strings::normaliseLink($ret['url'])) {
                        Logger::notice('New URL differs from old URL', ['id' => $id, 'uid' => $uid, 'old' => $contact['url'], 'new' => $ret['url']]);
-                       self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
+                       self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => true, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $failed_next_update, 'failure_update' => $updated]);
                        return false;
                }
 
@@ -2401,14 +2548,14 @@ class Contact
                // We check after the probing to be able to correct falsely detected contact types.
                if (($contact['contact-type'] == self::TYPE_RELAY) &&
                        (!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))) {
-                       self::updateContact($id, $uid, $contact['url'], $contact['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
+                       self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => false, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $success_next_update, 'success_update' => $updated]);
                        Logger::info('Not updating relais', ['id' => $id, 'url' => $contact['url']]);
                        return true;
                }
 
                // If Probe::uri fails the network code will be different ("feed" or "unkn")
                if (($ret['network'] == Protocol::PHANTOM) || (($ret['network'] == Protocol::FEED) && ($ret['network'] != $contact['network']))) {
-                       self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => true, 'last-update' => $updated, 'failure_update' => $updated]);
+                       self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => true, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $failed_next_update, 'failure_update' => $updated]);
                        return false;
                }
 
@@ -2436,7 +2583,7 @@ class Contact
 
                $new_pubkey = $ret['pubkey'] ?? '';
 
-               if ($uid == 0) {
+               if ($uid == 0 && DI::config()->get('system', 'fetch_featured_posts')) {
                        if ($ret['network'] == Protocol::ACTIVITYPUB) {
                                $apcontact = APContact::getByURL($ret['url'], false);
                                if (!empty($apcontact['featured'])) {
@@ -2477,10 +2624,8 @@ class Contact
                        self::updateAvatar($id, $ret['photo'], $update);
                }
 
-               $uriid = ItemURI::insert(['uri' => $ret['url'], 'guid' => $guid]);
-
                if (!$update) {
-                       self::updateContact($id, $uid, $contact['url'], $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
+                       self::updateContact($id, $uid, $uriid, $contact['url'], ['failed' => false, 'local-data' => $has_local_data, 'last-update' => $updated, 'next-update' => $success_next_update, 'success_update' => $updated]);
 
                        if (Contact\Relation::isDiscoverable($ret['url'])) {
                                Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
@@ -2497,10 +2642,12 @@ class Contact
                        return true;
                }
 
-               $ret['uri-id']  = $uriid;
-               $ret['nurl']    = Strings::normaliseLink($ret['url']);
-               $ret['updated'] = $updated;
-               $ret['failed']  = false;
+               $ret['uri-id']      = ItemURI::insert(['uri' => $ret['url'], 'guid' => $guid]);
+               $ret['nurl']        = Strings::normaliseLink($ret['url']);
+               $ret['updated']     = $updated;
+               $ret['failed']      = false;
+               $ret['next-update'] = $success_next_update;
+               $ret['local-data']  = $has_local_data;
 
                // Only fill the pubkey if it had been empty before. We have to prevent identity theft.
                if (empty($pubkey) && !empty($new_pubkey)) {
@@ -2522,7 +2669,7 @@ class Contact
 
                unset($ret['photo']);
 
-               self::updateContact($id, $uid, $contact['url'], $ret['url'], $ret);
+               self::updateContact($id, $uid, $ret['uri-id'], $ret['url'], $ret);
 
                if (Contact\Relation::isDiscoverable($ret['url'])) {
                        Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
@@ -2552,12 +2699,14 @@ class Contact
        }
 
        /**
+        * Updates contact record by provided URL
+        *
         * @param integer $url contact url
         * @return integer Contact id
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function updateFromProbeByURL($url)
+       public static function updateFromProbeByURL(string $url): int
        {
                $id = self::getIdForURL($url);
 
@@ -2578,7 +2727,7 @@ class Contact
         * @param string $network Network of that contact
         * @return string with protocol
         */
-       public static function getProtocol($url, $network)
+       public static function getProtocol(string $url, string $network): string
        {
                if ($network != Protocol::DFRN) {
                        return $network;
@@ -2945,6 +3094,7 @@ class Contact
         * Update the local relationship when a local user loses a follower
         *
         * @param array $contact User-specific contact (uid != 0) array
+        * @return void
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -3234,12 +3384,12 @@ class Contact
                        if (empty($url) || !is_string($url)) {
                                continue;
                        }
-                       $contact = self::getByURL($url, false, ['id', 'updated']);
-                       if (empty($contact['id'])) {
+                       $contact = self::getByURL($url, false, ['id', 'network', 'next-update']);
+                       if (empty($contact['id']) && Network::isValidHttpUrl($url)) {
                                Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);
                                ++$added;
-                       } elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) {
-                               Worker::add(PRIORITY_LOW, 'UpdateContact', $contact['id']);
+                       } elseif (!empty($contact['network']) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
+                               Worker::add(['priority' => PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']);
                                ++$updated;
                        } else {
                                ++$unchanged;