]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Add feedback :-)
[friendica.git] / src / Model / Contact.php
index 12cba0c080411b5053d3fe505a7c6d5ddcbe2ec1..7f72b38d61aa14484fab852a846b0f61ac5bd647 100644 (file)
@@ -453,6 +453,11 @@ class Contact
         */
        public static function isLocal($url)
        {
+               if (!parse_url($url, PHP_URL_SCHEME)) {
+                       $addr_parts = explode('@', $url);
+                       return (count($addr_parts) == 2) && ($addr_parts[1] == DI::baseUrl()->getHostname());
+               }
+
                return Strings::compareLink(self::getBasepath($url, true), DI::baseUrl());
        }
 
@@ -671,7 +676,7 @@ class Contact
        public static function updateSelfFromUserID($uid, $update_avatar = false)
        {
                $fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'avatar', 'prvkey', 'pubkey',
-                       'xmpp', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
+                       'xmpp', 'matrix', 'contact-type', 'forum', 'prv', 'avatar-date', 'url', 'nurl', 'unsearchable',
                        'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco', 'network'];
                $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
                if (!DBA::isResult($self)) {
@@ -685,7 +690,7 @@ class Contact
                }
 
                $fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region',
-                       'country-name', 'pub_keywords', 'xmpp', 'net-publish'];
+                       'country-name', 'pub_keywords', 'xmpp', 'matrix', 'net-publish'];
                $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
                if (!DBA::isResult($profile)) {
                        return false;
@@ -697,7 +702,7 @@ class Contact
                        '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'], 'network' => Protocol::DFRN];
+                       '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'];
@@ -1731,7 +1736,7 @@ class Contact
        {
                $condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)",
                        Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0];
-               $contact = self::selectFirst(['id', 'updated'], $condition);
+               $contact = self::selectFirst(['id', 'updated'], $condition, ['order' => ['uid' => true]]);
                return self::getAvatarUrlForId($contact['id'] ?? 0, $size, $contact['updated'] ?? '');
        }
 
@@ -1808,7 +1813,7 @@ class Contact
 
                // 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);
+                       $pcid = self::getIdForURL($contact['url'], 0, 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);
@@ -1924,11 +1929,6 @@ class Contact
         */
        private static function updateContact(int $id, int $uid, string $old_url, string $new_url, array $fields)
        {
-               if (Strings::normaliseLink($new_url) != Strings::normaliseLink($old_url)) {
-                       Logger::notice('New URL differs from old URL', ['old' => $old_url, 'new' => $new_url]);
-                       return;
-               }
-
                if (!DBA::update('contact', $fields, ['id' => $id])) {
                        Logger::info('Couldn\'t update contact.', ['id' => $id, 'fields' => $fields]);
                        return;
@@ -2059,11 +2059,11 @@ class Contact
                 */
 
                // These fields aren't updated by this routine:
-               // 'xmpp', 'sensitive'
+               // 'sensitive'
 
                $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'];
+                       'network', 'alias', 'baseurl', 'gsid', 'forum', 'prv', 'contact-type', 'pubkey', 'last-item', 'xmpp', 'matrix'];
                $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
                if (!DBA::isResult($contact)) {
                        return false;
@@ -2101,6 +2101,12 @@ class Contact
 
                $updated = DateTimeFormat::utcNow();
 
+               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]);
+                       return false;
+               }
+
                // We must not try to update relay contacts via probe. They are no real contacts.
                // We check after the probing to be able to correct falsely detected contact types.
                if (($contact['contact-type'] == self::TYPE_RELAY) &&
@@ -2117,7 +2123,7 @@ class Contact
                }
 
                if (Strings::normaliseLink($ret['url']) != Strings::normaliseLink($contact['url'])) {
-                       $cid = self::getIdForURL($ret['url']);
+                       $cid = self::getIdForURL($ret['url'], 0, false);
                        if (!empty($cid) && ($cid != $id)) {
                                Logger::notice('URL of contact changed.', ['id' => $id, 'new_id' => $cid, 'old' => $contact['url'], 'new' => $ret['url']]);
                                return self::updateFromProbeArray($cid, $ret);
@@ -2306,16 +2312,15 @@ class Contact
         *
         * Takes a $uid and a url/handle and adds a new contact
         *
-        * @param array  $user        The user the contact should be created for
+        * @param int    $uid         The user id the contact should be created for
         * @param string $url         The profile URL of the contact
-        * @param bool   $interactive
         * @param string $network
         * @return array
         * @throws HTTPException\InternalServerErrorException
         * @throws HTTPException\NotFoundException
         * @throws \ImagickException
         */
-       public static function createFromProbe(array $user, $url, $interactive = false, $network = '')
+       public static function createFromProbeForUser(int $uid, $url, $network = '')
        {
                $result = ['cid' => -1, 'success' => false, 'message' => ''];
 
@@ -2351,7 +2356,7 @@ class Contact
                        $ret = $arr['contact'];
                } else {
                        $probed = true;                 
-                       $ret = Probe::uri($url, $network, $user['uid']);
+                       $ret = Probe::uri($url, $network, $uid);
                }
 
                if (($network != '') && ($ret['network'] != $network)) {
@@ -2363,10 +2368,10 @@ class Contact
                // the poll url is more reliable than the profile url, as we may have
                // indirect links or webfinger links
 
-               $condition = ['uid' => $user['uid'], 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
+               $condition = ['uid' => $uid, 'poll' => [$ret['poll'], Strings::normaliseLink($ret['poll'])], 'network' => $ret['network'], 'pending' => false];
                $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
                if (!DBA::isResult($contact)) {
-                       $condition = ['uid' => $user['uid'], 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
+                       $condition = ['uid' => $uid, 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
                        $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
                }
 
@@ -2431,7 +2436,7 @@ class Contact
 
                        // create contact record
                        self::insert([
-                               'uid'     => $user['uid'],
+                               'uid'     => $uid,
                                'created' => DateTimeFormat::utcNow(),
                                'url'     => $ret['url'],
                                'nurl'    => Strings::normaliseLink($ret['url']),
@@ -2459,7 +2464,7 @@ class Contact
                        ]);
                }
 
-               $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $user['uid']]);
+               $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]);
                if (!DBA::isResult($contact)) {
                        $result['message'] .= DI::l10n()->t('Unable to retrieve contact information.') . EOL;
                        return $result;
@@ -2468,7 +2473,7 @@ class Contact
                $contact_id = $contact['id'];
                $result['cid'] = $contact_id;
 
-               Group::addMember(User::getDefaultGroup($user['uid'], $contact["network"]), $contact_id);
+               Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id);
 
                // Update the avatar
                self::updateAvatar($contact_id, $ret['photo']);
@@ -2484,7 +2489,7 @@ class Contact
                        Worker::add(PRIORITY_HIGH, 'UpdateContact', $contact_id);
                }
 
-               $owner = User::getOwnerDataById($user['uid']);
+               $owner = User::getOwnerDataById($uid);
 
                if (DBA::isResult($owner)) {
                        if (in_array($protocol, [Protocol::OSTATUS, Protocol::DFRN])) {
@@ -2513,7 +2518,7 @@ class Contact
                                        return false;
                                }
 
-                               $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $user['uid'], $activity_id);
+                               $ret = ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $uid, $activity_id);
                                Logger::log('Follow returns: ' . $ret);
                        }
                }
@@ -2574,14 +2579,9 @@ class Contact
         */
        public static function follow(int $cid, int $uid)
        {
-               $user = User::getById($uid);
-               if (empty($user)) {
-                       return false;
-               }
-
                $contact = self::getById($cid, ['url']);
 
-               $result = self::createFromProbe($user, $contact['url'], false);
+               $result = self::createFromProbeForUser($uid, $contact['url']);
 
                return $result['cid'];
        }
@@ -2739,7 +2739,7 @@ class Contact
                                }
                        } elseif (DBA::isResult($user) && in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
                                if (($user['page-flags'] == User::PAGE_FLAGS_FREELOVE) && ($network != Protocol::DIASPORA)) {
-                                       self::createFromProbe($importer, $url, false, $network);
+                                       self::createFromProbeForUser($importer['uid'], $url, $network);
                                }
 
                                $condition = ['uid' => $importer['uid'], 'url' => $url, 'pending' => true];
@@ -2757,12 +2757,14 @@ class Contact
                return null;
        }
 
-       public static function removeFollower($importer, $contact)
+       public static function removeFollower(array $contact)
        {
-               if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING)) {
+               if (in_array($contact['rel'] ?? [], [self::FRIEND, self::SHARING])) {
                        DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]);
-               } else {
+               } elseif (!empty($contact['id'])) {
                        self::remove($contact['id']);
+               } else {
+                       DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact]);
                }
        }