]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Fix overly strict return value for terminateFriendship methods
[friendica.git] / src / Model / Contact.php
index 3a214d00bed5da67e75b93182404f05317b7fbb1..cbe370a4aa8250d35a899d3b1de59d2e32105adf 100644 (file)
@@ -133,6 +133,7 @@ class Contact
        const FOLLOWER = 1;
        const SHARING  = 2;
        const FRIEND   = 3;
+       const SELF     = 4;
        /**
         * @}
         */
@@ -175,7 +176,7 @@ class Contact
         * @param array $fields         field array
         * @param int   $duplicate_mode Do an update on a duplicate entry
         *
-        * @return boolean was the insert successful?
+        * @return int  id of the created contact
         * @throws \Exception
         */
        public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT)
@@ -190,15 +191,40 @@ class Contact
                        $fields['created'] = DateTimeFormat::utcNow();
                }
 
-               $ret = DBA::insert('contact', $fields, $duplicate_mode);
-               $contact = DBA::selectFirst('contact', ['nurl', 'uid'], ['id' => DBA::lastInsertId()]);
+               DBA::insert('contact', $fields, $duplicate_mode);
+               $contact = DBA::selectFirst('contact', [], ['id' => DBA::lastInsertId()]);
                if (!DBA::isResult($contact)) {
                        // Shouldn't happen
-                       return $ret;
+                       Logger::warning('Created contact could not be found', ['fields' => $fields]);
+                       return 0;
                }
 
+               Contact\User::insertForContactArray($contact);
+
                // Search for duplicated contacts and get rid of them
-               self::removeDuplicates($contact['nurl'], $contact['uid']);
+               if (!$contact['self']) {
+                       self::removeDuplicates($contact['nurl'], $contact['uid']);
+               }
+
+               return $contact['id'];
+       }
+
+       /**
+        * Updates rows in the contact table
+        *
+        * @param array         $fields     contains the fields that are updated
+        * @param array         $condition  condition array with the key values
+        * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate, false = don't update identical fields)
+        *
+        * @return boolean was the update successfull?
+        * @throws \Exception
+        */
+       public static function update(array $fields, array $condition, $old_fields = [])
+       {
+               $ret = DBA::update('contact', $fields, $condition, $old_fields);
+
+               // Apply changes to the "user-contact" table on dedicated fields
+               Contact\User::updateByContactUpdate($fields, $condition);
 
                return $ret;
        }
@@ -627,9 +653,9 @@ class Contact
                        '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',
+                       'photo'       => User::getAvatarUrl($user),
+                       'thumb'       => User::getAvatarUrl($user, Proxy::SIZE_THUMB),
+                       'micro'       => User::getAvatarUrl($user, Proxy::SIZE_MICRO),
                        'blocked'     => 0,
                        'pending'     => 0,
                        'url'         => DI::baseUrl() . '/profile/' . $user['nickname'],
@@ -650,7 +676,7 @@ class Contact
 
                // Only create the entry if it doesn't exist yet
                if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
-                       $return = DBA::insert('contact', $contact);
+                       $return = (bool)self::insert($contact);
                }
 
                // Create the public contact
@@ -659,7 +685,7 @@ class Contact
                        $contact['uid']    = 0;
                        $contact['prvkey'] = null;
 
-                       DBA::insert('contact', $contact, Database::INSERT_IGNORE);
+                       self::insert($contact, Database::INSERT_IGNORE);
                }
 
                return $return;
@@ -683,7 +709,7 @@ class Contact
                        return false;
                }
 
-               $fields = ['nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
+               $fields = ['uid', 'nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
                $user = DBA::selectFirst('user', $fields, ['uid' => $uid, 'account_expired' => false]);
                if (!DBA::isResult($user)) {
                        return false;
@@ -742,7 +768,7 @@ class Contact
                        $fields['micro'] = self::getDefaultAvatar($fields, Proxy::SIZE_MICRO);
                }
 
-               $fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
+               $fields['avatar'] = User::getAvatarUrl($user);
                $fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
                $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
                $fields['unsearchable'] = !$profile['net-publish'];
@@ -760,16 +786,19 @@ class Contact
                                $fields['name-date'] = DateTimeFormat::utcNow();
                        }
                        $fields['updated'] = DateTimeFormat::utcNow();
-                       DBA::update('contact', $fields, ['id' => $self['id']]);
+                       self::update($fields, ['id' => $self['id']]);
 
                        // Update the public contact as well
                        $fields['prvkey'] = null;
                        $fields['self']   = false;
-                       DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]);
+                       self::update($fields, ['uid' => 0, 'nurl' => $self['nurl']]);
 
                        // Update the profile
-                       $fields = ['photo' => DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix,
-                               'thumb' => DI::baseUrl() . '/photo/avatar/' . $uid .'.' . $file_suffix];
+                       $fields = [
+                               'photo' => User::getAvatarUrl($user),
+                               'thumb' => User::getAvatarUrl($user, Proxy::SIZE_THUMB)
+                       ];
+
                        DBA::update('profile', $fields, ['uid' => $uid]);
                }
 
@@ -780,7 +809,6 @@ class Contact
         * Marks a contact for removal
         *
         * @param int $id contact id
-        * @return null
         * @throws HTTPException\InternalServerErrorException
         */
        public static function remove($id)
@@ -792,59 +820,69 @@ class Contact
                }
 
                // Archive the contact
-               DBA::update('contact', ['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
+               self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
 
                // Delete it in the background
                Worker::add(PRIORITY_MEDIUM, 'RemoveContact', $id);
        }
 
        /**
-        * Sends an unfriend message. Does not remove the contact
+        * Sends an unfriend message. Removes the contact for two-way unfriending or sharing only protocols (feed an mail)
+        *
+        * @param array   $user    User unfriending
+        * @param array   $contact Contact (uid != 0) unfriended
+        * @param boolean $two_way Revoke eventual inbound follow as well
+        * @return bool|null true if successful, false if not, null if no remote action was performed
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function terminateFriendship(array $user, array $contact): ?bool
+       {
+               $result = Protocol::terminateFriendship($user, $contact);
+
+               if ($contact['rel'] == Contact::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
+                       self::remove($contact['id']);
+               } else {
+                       self::update(['rel' => Contact::FOLLOWER], ['id' => $contact['id']]);
+               }
+
+               return $result;
+       }
+
+       /**
+        * Revoke follow privileges of the remote user contact
         *
-        * @param array   $user     User unfriending
         * @param array   $contact  Contact unfriended
-        * @param boolean $dissolve Remove the contact on the remote side
-        * @return void
+        * @return bool|null Whether the remote operation is successful or null if no remote operation was performed
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function terminateFriendship(array $user, array $contact, $dissolve = false)
+       public static function revokeFollow(array $contact): bool
        {
                if (empty($contact['network'])) {
-                       return;
+                       throw new \InvalidArgumentException('Empty network in contact array');
                }
 
-               $protocol = $contact['network'];
-               if (($protocol == Protocol::DFRN) && !empty($contact['protocol'])) {
-                       $protocol = $contact['protocol'];
+               if (empty($contact['uid'])) {
+                       throw new \InvalidArgumentException('Unexpected public contact record');
                }
 
-               if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
-                       // create an unfollow slap
-                       $item = [];
-                       $item['verb'] = Activity::O_UNFOLLOW;
-                       $item['gravity'] = GRAVITY_ACTIVITY;
-                       $item['follow'] = $contact["url"];
-                       $item['body'] = '';
-                       $item['title'] = '';
-                       $item['guid'] = '';
-                       $item['uri-id'] = 0;
-                       $slap = OStatus::salmon($item, $user);
+               $result = Protocol::revokeFollow($contact);
 
-                       if (!empty($contact['notify'])) {
-                               Salmon::slapper($user, $contact['notify'], $slap);
-                       }
-               } elseif ($protocol == Protocol::DIASPORA) {
-                       Diaspora::sendUnshare($user, $contact);
-               } elseif ($protocol == Protocol::ACTIVITYPUB) {
-                       ActivityPub\Transmitter::sendContactUndo($contact['url'], $contact['id'], $user['uid']);
-
-                       if ($dissolve) {
-                               ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
+               // A null value here means the remote network doesn't support explicit follow revocation, we can still
+               // break the locally recorded relationship
+               if ($result !== false) {
+                       if ($contact['rel'] == self::FRIEND) {
+                               self::update(['rel' => self::SHARING], ['id' => $contact['id']]);
+                       } else {
+                               self::remove($contact['id']);
                        }
                }
+
+               return $result;
        }
 
+
        /**
         * Marks a contact for archival after a communication issue delay
         *
@@ -878,8 +916,8 @@ class Contact
                }
 
                if ($contact['term-date'] <= DBA::NULL_DATETIME) {
-                       DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
-                       DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]);
+                       self::update(['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
+                       self::update(['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', Strings::normaliseLink($contact['url']), DBA::NULL_DATETIME]);
                } else {
                        /* @todo
                         * We really should send a notification to the owner after 2-3 weeks
@@ -896,8 +934,8 @@ class Contact
                                 * delete, though if the owner tries to unarchive them we'll start
                                 * the whole process over again.
                                 */
-                               DBA::update('contact', ['archive' => true], ['id' => $contact['id']]);
-                               DBA::update('contact', ['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
+                               self::update(['archive' => true], ['id' => $contact['id']]);
+                               self::update(['archive' => true], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
                        }
                }
        }
@@ -918,7 +956,7 @@ class Contact
                        $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
                        $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY];
                        if (!DBA::exists('contact', array_merge($condition, $fields))) {
-                               DBA::update('contact', $fields, $condition);
+                               self::update($fields, $condition);
                        }
                }
 
@@ -942,8 +980,8 @@ class Contact
 
                // It's a miracle. Our dead contact has inexplicably come back to life.
                $fields = ['failed' => false, 'term-date' => DBA::NULL_DATETIME, 'archive' => false];
-               DBA::update('contact', $fields, ['id' => $contact['id']]);
-               DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
+               self::update($fields, ['id' => $contact['id']]);
+               self::update($fields, ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]);
        }
 
        /**
@@ -960,7 +998,6 @@ class Contact
                $pm_url = '';
                $status_link = '';
                $photos_link = '';
-               $contact_drop_link = '';
                $poke_link = '';
 
                if ($uid == 0) {
@@ -1012,13 +1049,9 @@ class Contact
 
                $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations';
 
-               if (!$contact['self']) {
-                       $contact_drop_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/drop?confirm=1';
-               }
-
                $follow_link = '';
                $unfollow_link = '';
-               if (!$contact['self'] && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
+               if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) {
                        if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
                                $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1';
                        } elseif(!$contact['pending']) {
@@ -1026,10 +1059,6 @@ class Contact
                        }
                }
 
-               if (!empty($follow_link) || !empty($unfollow_link)) {
-                       $contact_drop_link = '';
-               }
-
                /**
                 * Menu array:
                 * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
@@ -1049,7 +1078,6 @@ class Contact
                                'photos'  => [DI::l10n()->t('View Photos')   , $photos_link      , true],
                                'network' => [DI::l10n()->t('Network Posts') , $posts_link       , false],
                                'edit'    => [DI::l10n()->t('View Contact')  , $contact_url      , false],
-                               'drop'    => [DI::l10n()->t('Drop Contact')  , $contact_drop_link, 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],
@@ -1209,8 +1237,7 @@ class Contact
                                        $contact_id = $contact['id'];
                                        Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
                                } else {
-                                       DBA::insert('contact', $fields);
-                                       $contact_id = DBA::lastInsertId();
+                                       $contact_id = self::insert($fields);
                                        if ($contact_id) {
                                                Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
                                        }
@@ -1337,12 +1364,13 @@ class Contact
         * @param bool   $thread_mode
         * @param int    $update      Update mode
         * @param int    $parent      Item parent ID for the update mode
+        * @param bool   $only_media  Only display media content
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0)
+       public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0, bool $only_media = false)
        {
-               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent);
+               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media);
        }
 
        /**
@@ -1351,14 +1379,13 @@ class Contact
         * @param int  $cid         Contact ID
         * @param bool $thread_mode
         * @param int  $update      Update mode
-        * @param int  $parent     Item parent ID for the update mode
+        * @param int  $parent      Item parent ID for the update mode
+        * @param bool $only_media  Only display media content
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0)
+       public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0, bool $only_media = false)
        {
-               $a = DI::app();
-
                $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
                if (!DBA::isResult($contact)) {
                        return '';
@@ -1389,6 +1416,11 @@ class Contact
                        }
                }
 
+               if ($only_media) {
+                       $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))",
+                               Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
+               }
+
                if (DI::mode()->isMobile()) {
                        $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
                                DI::config()->get('system', 'itemspage_network_mobile'));
@@ -1411,11 +1443,11 @@ class Contact
                if ($thread_mode) {
                        $items = Post::toArray(Post::selectForUser(local_user(), ['uri-id', 'gravity', 'parent-uri-id', 'thr-parent-id', 'author-id'], $condition, $params));
 
-                       $o .= conversation($a, $items, 'contacts', $update, false, 'commented', local_user());
+                       $o .= DI::conversation()->create($items, 'contacts', $update, false, 'commented', local_user());
                } else {
                        $items = Post::toArray(Post::selectForUser(local_user(), Item::DISPLAY_FIELDLIST, $condition, $params));
 
-                       $o .= conversation($a, $items, 'contact-posts', $update);
+                       $o .= DI::conversation()->create($items, 'contact-posts', $update);
                }
 
                if (!$update) {
@@ -1492,7 +1524,7 @@ class Contact
         */
        public static function block($cid, $reason = null)
        {
-               $return = DBA::update('contact', ['blocked' => true, 'block_reason' => $reason], ['id' => $cid]);
+               $return = self::update(['blocked' => true, 'block_reason' => $reason], ['id' => $cid]);
 
                return $return;
        }
@@ -1506,7 +1538,7 @@ class Contact
         */
        public static function unblock($cid)
        {
-               $return = DBA::update('contact', ['blocked' => false, 'block_reason' => null], ['id' => $cid]);
+               $return = self::update(['blocked' => false, 'block_reason' => null], ['id' => $cid]);
 
                return $return;
        }
@@ -1690,19 +1722,22 @@ class Contact
         * Get avatar link for given contact id
         *
         * @param integer $cid     contact id
-        * @param string  $size    One of the ProxyUtils::SIZE_* constants
+        * @param string  $size    One of the Proxy::SIZE_* constants
         * @param string  $updated Contact update date
         * @return string avatar link
         */
-       public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = ''):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
-               if (empty($updated)) {
-                       $contact = self::getById($cid, ['updated']);
-                       $updated = $contact['updated'] ?? '';
+               if (empty($updated) || empty($guid)) {
+                       $account = DBA::selectFirst('account-user-view', ['updated', 'guid'], ['id' => $cid]);
+                       $updated = $account['updated'] ?? '';
+                       $guid = $account['guid'] ?? '';
                }
 
+               $guid = urlencode($guid);
+
                $url = DI::baseUrl() . '/photo/contact/';
                switch ($size) {
                        case Proxy::SIZE_MICRO:
@@ -1721,7 +1756,7 @@ class Contact
                                $url .= Proxy::PIXEL_LARGE . '/';
                                break;
                }
-               return $url . $cid . ($updated ? '?ts=' . strtotime($updated) : '');
+               return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : '');
        }
 
        /**
@@ -1729,7 +1764,7 @@ class Contact
         *
         * @param string  $url  contact url
         * @param integer $uid  user id
-        * @param string  $size One of the ProxyUtils::SIZE_* constants
+        * @param string  $size One of the Proxy::SIZE_* constants
         * @return string avatar link
         */
        public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''):string
@@ -1744,19 +1779,22 @@ class Contact
         * Get header link for given contact id
         *
         * @param integer $cid     contact id
-        * @param string  $size    One of the ProxyUtils::SIZE_* constants
+        * @param string  $size    One of the Proxy::SIZE_* constants
         * @param string  $updated Contact update date
         * @return string header link
         */
-       public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = ''):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
-               if (empty($updated)) {
-                       $contact = self::getById($cid, ['updated']);
-                       $updated = $contact['updated'] ?? '';
+               if (empty($updated) || empty($guid)) {
+                       $account = DBA::selectFirst('account-user-view', ['updated', 'guid'], ['id' => $cid]);
+                       $updated = $account['updated'] ?? '';
+                       $guid = $account['guid'] ?? '';
                }
 
+               $guid = urlencode($guid);
+
                $url = DI::baseUrl() . '/photo/header/';
                switch ($size) {
                        case Proxy::SIZE_MICRO:
@@ -1776,7 +1814,7 @@ class Contact
                                break;
                }
 
-               return $url . $cid . ($updated ? '?ts=' . strtotime($updated) : '');
+               return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : '');
        }
 
        /**
@@ -1805,7 +1843,7 @@ class Contact
                // Only update the cached photo links of public contacts when they already are cached
                if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
                        if ($contact['avatar'] != $avatar) {
-                               DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
+                               self::update(['avatar' => $avatar], ['id' => $cid]);
                                Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
                        }
                        return;
@@ -1902,7 +1940,7 @@ class Contact
                $cids[] = $cid;
                $uids[] = $uid;
                Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
-               DBA::update('contact', $fields, ['id' => $cids]);
+               self::update($fields, ['id' => $cids]);
        }
 
        public static function deleteContactByUrl(string $url)
@@ -1929,7 +1967,7 @@ class Contact
         */
        private static function updateContact(int $id, int $uid, string $old_url, string $new_url, array $fields)
        {
-               if (!DBA::update('contact', $fields, ['id' => $id])) {
+               if (!self::update($fields, ['id' => $id])) {
                        Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
                        return;
                }
@@ -1962,7 +2000,7 @@ class Contact
                $condition = ['self' => false, 'nurl' => Strings::normaliseLink($old_url)];
 
                $condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB];
-               DBA::update('contact', $fields, $condition);
+               self::update($fields, $condition);
 
                // We mustn't set the update fields for OStatus contacts since they are updated in OnePoll
                $condition['network'] = Protocol::OSTATUS;
@@ -1978,7 +2016,7 @@ class Contact
                        return;
                }
 
-               DBA::update('contact', $fields, $condition);
+               self::update($fields, $condition);
        }
 
        /**
@@ -1991,7 +2029,7 @@ class Contact
         */
        public static function removeDuplicates(string $nurl, int $uid)
        {
-               $condition = ['nurl' => $nurl, 'uid' => $uid, 'deleted' => false, 'network' => Protocol::FEDERATED];
+               $condition = ['nurl' => $nurl, 'uid' => $uid, 'self' => false, 'deleted' => false, 'network' => Protocol::FEDERATED];
                $count = DBA::count('contact', $condition);
                if ($count <= 1) {
                        return false;
@@ -2152,7 +2190,7 @@ class Contact
                }
 
                $update = false;
-               $guid = $ret['guid'] ?? '';
+               $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url'], parse_url($ret['url'], PHP_URL_HOST));
 
                // make sure to not overwrite existing values with blank entries except some technical fields
                $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl'];
@@ -2180,6 +2218,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]);
 
@@ -2198,12 +2238,7 @@ class Contact
                        return true;
                }
 
-               if (empty($guid)) {
-                       $ret['uri-id'] = ItemURI::getIdByURI($ret['url']);
-               } else {
-                       $ret['uri-id'] = ItemURI::insert(['uri' => $ret['url'], 'guid' => $guid]);
-               }
-
+               $ret['uri-id']  = $uriid;
                $ret['nurl']    = Strings::normaliseLink($ret['url']);
                $ret['updated'] = $updated;
                $ret['failed']  = false;
@@ -2252,7 +2287,7 @@ class Contact
                        }
                }
                if (!empty($fields)) {
-                       DBA::update('contact', $fields, ['id' => $id, 'self' => false]);
+                       self::update($fields, ['id' => $id, 'self' => false]);
                        Logger::info('Updating local contact', ['id' => $id]);
                }
        }
@@ -2355,7 +2390,7 @@ class Contact
                        $probed = false;
                        $ret = $arr['contact'];
                } else {
-                       $probed = true;                 
+                       $probed = true;
                        $ret = Probe::uri($url, $network, $uid);
                }
 
@@ -2430,7 +2465,7 @@ class Contact
                        $new_relation = (($contact['rel'] == self::FOLLOWER) ? self::FRIEND : self::SHARING);
 
                        $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
-                       DBA::update('contact', $fields, ['id' => $contact['id']]);
+                       self::update($fields, ['id' => $contact['id']]);
                } else {
                        $new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
 
@@ -2563,7 +2598,7 @@ class Contact
                        $fields = ['url' => $contact['url'], 'request' => $contact['request'],
                                        'notify' => $contact['notify'], 'poll' => $contact['poll'],
                                        'confirm' => $contact['confirm'], 'poco' => $contact['poco']];
-                       DBA::update('contact', $fields, ['id' => $contact['id']]);
+                       self::update($fields, ['id' => $contact['id']]);
                }
 
                return $contact;
@@ -2666,14 +2701,14 @@ class Contact
 
                        if (($contact['rel'] == self::SHARING)
                                || ($sharing && $contact['rel'] == self::FOLLOWER)) {
-                               DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
+                               self::update(['rel' => self::FRIEND, 'writable' => true, 'pending' => false],
                                                ['id' => $contact['id'], 'uid' => $importer['uid']]);
                        }
 
                        // Ensure to always have the correct network type, independent from the connection request method
                        self::updateFromProbe($contact['id']);
 
-                       Post\UserNotification::insertNotication($contact['id'], Verb::getID(Activity::FOLLOW), $importer['uid']);
+                       Post\UserNotification::insertNotification($contact['id'], Activity::FOLLOW, $importer['uid']);
 
                        return true;
                } else {
@@ -2684,7 +2719,7 @@ class Contact
                        }
 
                        // create contact record
-                       DBA::insert('contact', [
+                       $contact_id = self::insert([
                                'uid'      => $importer['uid'],
                                'created'  => DateTimeFormat::utcNow(),
                                'url'      => $url,
@@ -2699,14 +2734,12 @@ class Contact
                                'writable' => 1,
                        ]);
 
-                       $contact_id = DBA::lastInsertId();
-
                        // Ensure to always have the correct network type, independent from the connection request method
                        self::updateFromProbe($contact_id);
 
                        self::updateAvatar($contact_id, $photo, true);
 
-                       Post\UserNotification::insertNotication($contact_id, Verb::getID(Activity::FOLLOW), $importer['uid']);
+                       Post\UserNotification::insertNotification($contact_id, Activity::FOLLOW, $importer['uid']);
 
                        $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);
 
@@ -2748,7 +2781,7 @@ class Contact
                                        $fields['rel'] = self::FRIEND;
                                }
 
-                               DBA::update('contact', $fields, $condition);
+                               self::update($fields, $condition);
 
                                return true;
                        }
@@ -2771,7 +2804,7 @@ class Contact
        public static function removeSharer($importer, $contact)
        {
                if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::FOLLOWER)) {
-                       DBA::update('contact', ['rel' => self::FOLLOWER], ['id' => $contact['id']]);
+                       self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]);
                } else {
                        self::remove($contact['id']);
                }