]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #10787 from fabrixxm/issue/10767
[friendica.git] / src / Model / Contact.php
index 609a70718ed2fccd3b6b5737190379d31ba68bce..1375a8b3ef234b170b5d998f760b13d0876bef56 100644 (file)
@@ -627,9 +627,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::getAvatarUrlForId($user['uid']),
+                       'thumb'       => User::getAvatarUrlForId($user['uid'], Proxy::SIZE_THUMB),
+                       'micro'       => User::getAvatarUrlForId($user['uid'], Proxy::SIZE_MICRO),
                        'blocked'     => 0,
                        'pending'     => 0,
                        'url'         => DI::baseUrl() . '/profile/' . $user['nickname'],
@@ -742,7 +742,7 @@ class Contact
                        $fields['micro'] = self::getDefaultAvatar($fields, Proxy::SIZE_MICRO);
                }
 
-               $fields['avatar'] = DI::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
+               $fields['avatar'] = User::getAvatarUrlForId($uid);
                $fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
                $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
                $fields['unsearchable'] = !$profile['net-publish'];
@@ -768,8 +768,11 @@ class Contact
                        DBA::update('contact', $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::getAvatarUrlForId($uid),
+                               'thumb' => User::getAvatarUrlForId($uid, Proxy::SIZE_THUMB)
+                       ];
+
                        DBA::update('profile', $fields, ['uid' => $uid]);
                }
 
@@ -842,6 +845,12 @@ class Contact
                        if ($dissolve) {
                                ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $user['uid']);
                        }
+               } else {
+                       $hook_data = [
+                               'contact' => $contact,
+                               'dissolve' => $dissolve,
+                       ];
+                       Hook::callAll('unfollow', $hook_data);
                }
        }
 
@@ -1411,11 +1420,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) {
@@ -1929,11 +1938,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;
@@ -2106,6 +2110,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) &&
@@ -2311,16 +2321,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' => ''];
 
@@ -2355,8 +2364,8 @@ class Contact
                        $probed = false;
                        $ret = $arr['contact'];
                } else {
-                       $probed = true;                 
-                       $ret = Probe::uri($url, $network, $user['uid']);
+                       $probed = true;
+                       $ret = Probe::uri($url, $network, $uid);
                }
 
                if (($network != '') && ($ret['network'] != $network)) {
@@ -2368,10 +2377,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);
                }
 
@@ -2436,7 +2445,7 @@ class Contact
 
                        // create contact record
                        self::insert([
-                               'uid'     => $user['uid'],
+                               'uid'     => $uid,
                                'created' => DateTimeFormat::utcNow(),
                                'url'     => $ret['url'],
                                'nurl'    => Strings::normaliseLink($ret['url']),
@@ -2464,7 +2473,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;
@@ -2473,7 +2482,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']);
@@ -2489,7 +2498,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])) {
@@ -2518,7 +2527,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);
                        }
                }
@@ -2579,14 +2588,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'];
        }
@@ -2744,7 +2748,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];
@@ -2762,12 +2766,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, 'callstack' => System::callstack()]);
                }
        }