]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #13124 from ne20002/develop
[friendica.git] / src / Model / Contact.php
index a16d55976c6d6786a111f588e18a95c290320bda..648193b09cc2749e3c6b762ab63c2732f334ffad 100644 (file)
@@ -23,7 +23,7 @@ namespace Friendica\Model;
 
 use Friendica\Contact\Avatar;
 use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException;
-use Friendica\Content\Conversation;
+use Friendica\Content\Conversation As ConversationContent;
 use Friendica\Content\Pager;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Hook;
@@ -239,19 +239,17 @@ class Contact
         * @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?
+        * @return boolean was the update successful?
         * @throws \Exception
         * @todo Let's get rid of boolean type of $old_fields
         */
-       public static function update(array $fields, array $condition, $old_fields = [])
+       public static function update(array $fields, array $condition, $old_fields = []): bool
        {
-               $fields = DI::dbaDefinition()->truncateFieldsForTable('contact', $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;
+               $fields = DI::dbaDefinition()->truncateFieldsForTable('contact', $fields);
+               return DBA::update('contact', $fields, $condition, $old_fields);
        }
 
        /**
@@ -572,7 +570,7 @@ class Contact
        {
                if (!parse_url($url, PHP_URL_SCHEME)) {
                        $addr_parts = explode('@', $url);
-                       return (count($addr_parts) == 2) && ($addr_parts[1] == DI::baseUrl()->getHostname());
+                       return (count($addr_parts) == 2) && ($addr_parts[1] == DI::baseUrl()->getHost());
                }
 
                return Strings::compareLink(self::getBasepath($url, true), DI::baseUrl());
@@ -1610,7 +1608,7 @@ class Contact
                                }
                        }
 
-                       $o .= DI::conversation()->create($items, Conversation::MODE_CONTACTS, $update, false, 'pinned_commented', DI::userSession()->getLocalUserId());
+                       $o .= DI::conversation()->create($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_commented', DI::userSession()->getLocalUserId());
                } else {
                        $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
                        $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
@@ -1625,7 +1623,7 @@ class Contact
                                }
                        }
 
-                       $o .= DI::conversation()->create($items, Conversation::MODE_CONTACT_POSTS, $update);
+                       $o .= DI::conversation()->create($items, ConversationContent::MODE_CONTACT_POSTS, $update);
                }
 
                if (!$update) {
@@ -1688,7 +1686,7 @@ class Contact
         * Unblocks a contact
         *
         * @param int $cid Contact id to unblock
-        * @return bool Whether it was successfull
+        * @return bool Whether it was successful
         */
        public static function unblock(int $cid): bool
        {
@@ -1735,7 +1733,7 @@ class Contact
         *
         * @param array  $contact   contact array
         * @param string $size      Size of the avatar picture
-        * @param bool   $no_update Don't perfom an update if no cached avatar was found
+        * @param bool   $no_update Don't perform an update if no cached avatar was found
         * @return string photo path
         */
        private static function getAvatarPath(array $contact, string $size, bool $no_update = false): string
@@ -1769,7 +1767,7 @@ class Contact
         * Return the photo path for a given contact array
         *
         * @param array  $contact   Contact array
-        * @param bool   $no_update Don't perfom an update if no cached avatar was found
+        * @param bool   $no_update Don't perform an update if no cached avatar was found
         * @return string photo path
         */
        public static function getPhoto(array $contact, bool $no_update = false): string
@@ -1781,7 +1779,7 @@ class Contact
         * Return the photo path (thumb size) for a given contact array
         *
         * @param array  $contact   Contact array
-        * @param bool   $no_update Don't perfom an update if no cached avatar was found
+        * @param bool   $no_update Don't perform an update if no cached avatar was found
         * @return string photo path
         */
        public static function getThumb(array $contact, bool $no_update = false): string
@@ -1793,7 +1791,7 @@ class Contact
         * Return the photo path (micro size) for a given contact array
         *
         * @param array  $contact   Contact array
-        * @param bool   $no_update Don't perfom an update if no cached avatar was found
+        * @param bool   $no_update Don't perform an update if no cached avatar was found
         * @return string photo path
         */
        public static function getMicro(array $contact, bool $no_update = false): string
@@ -1805,7 +1803,7 @@ class Contact
         * Check the given contact array for avatar cache fields
         *
         * @param array $contact
-        * @param bool  $no_update Don't perfom an update if no cached avatar was found
+        * @param bool  $no_update Don't perform an update if no cached avatar was found
         * @return array contact array with avatar cache fields
         */
        private static function checkAvatarCacheByArray(array $contact, bool $no_update = false): array
@@ -2206,20 +2204,38 @@ class Contact
                        return;
                }
 
+               if (!Network::isValidHttpUrl($avatar)) {
+                       Logger::warning('Invalid avatar', ['cid' => $cid, 'avatar' => $avatar]);
+                       $avatar = '';
+               }
+
                $uid = $contact['uid'];
 
                // Only update the cached photo links of public contacts when they already are cached
                if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
                        if (($contact['avatar'] != $avatar) || empty($contact['blurhash'])) {
                                $update_fields = ['avatar' => $avatar];
-                               $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
-
-                               $img_str = $fetchResult->getBody();
-                               if (!empty($img_str)) {
-                                       $image = new Image($img_str, Images::getMimeTypeByData($img_str));
-                                       if ($image->isValid()) {
-                                               $update_fields['blurhash'] = $image->getBlurHash();
+                               if (!Network::isLocalLink($avatar)) {
+                                       try {
+                                               $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
+
+                                               $img_str = $fetchResult->getBody();
+                                               if (!empty($img_str)) {
+                                                       $image = new Image($img_str, Images::getMimeTypeByData($img_str));
+                                                       if ($image->isValid()) {
+                                                               $update_fields['blurhash'] = $image->getBlurHash();
+                                                       } else {
+                                                               return;
+                                                       }
+                                               }
+                                       } catch (\Exception $exception) {
+                                               Logger::notice('Error fetching avatar', ['avatar' => $avatar, 'exception' => $exception]);
+                                               return;
                                        }
+                               } elseif (!empty($contact['blurhash'])) {
+                                       $update_fields['blurhash'] = null;
+                               } else {
+                                       return;
                                }
 
                                self::update($update_fields, ['id' => $cid]);
@@ -2252,7 +2268,9 @@ class Contact
                }
 
                if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || $cache_avatar) {
-                       Avatar::deleteCache($contact);
+                       if (Avatar::deleteCache($contact)) {
+                               $force = true;
+                       }
 
                        if ($default_avatar && Proxy::isLocalImage($avatar)) {
                                $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
@@ -2391,6 +2409,11 @@ class Contact
                $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url)];
 
                $condition['network'] = [Protocol::DFRN, Protocol::DIASPORA, Protocol::ACTIVITYPUB];
+
+               if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT) && Protocol::supportsProbe($contact['network'])) {
+                       $condition['network'][] = $contact['network'];
+               }
+
                self::update($fields, $condition);
 
                // We mustn't set the update fields for OStatus contacts since they are updated in OnePoll
@@ -2659,6 +2682,8 @@ class Contact
                        return true;
                }
 
+                $has_local_data = self::hasLocalData($id, $contact);
+
                $uid = $contact['uid'];
                unset($contact['uid']);
 
@@ -2679,9 +2704,7 @@ class Contact
 
                $updated = DateTimeFormat::utcNow();
 
-               $has_local_data = self::hasLocalData($id, $contact);
-
-               if (!Probe::isProbable($ret['network'])) {
+               if (!Probe::isProbable($ret['network']) && !Probe::isProbable($contact['network'])) {
                        // Periodical checks are only done on federated contacts
                        $failed_next_update  = null;
                        $success_next_update = null;
@@ -2968,7 +2991,7 @@ class Contact
                }
 
                // check if we already have a contact
-               $condition = ['uid' => $uid, 'nurl' => Strings::normaliseLink($ret['url'])];
+               $condition = ['uid' => $uid, 'nurl' => Strings::normaliseLink($ret['url']), 'deleted' => false];
                $contact = DBA::selectFirst('contact', ['id', 'rel', 'url', 'pending', 'hub-verify'], $condition);
 
                $protocol = self::getProtocol($ret['url'], $ret['network']);
@@ -3291,7 +3314,7 @@ class Contact
                if ($contact['rel'] == self::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
                        self::remove($contact['id']);
                } else {
-                       self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]);
+                       self::update(['rel' => self::FOLLOWER, 'pending' => false], ['id' => $contact['id']]);
                }
 
                Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $contact['uid']);
@@ -3486,16 +3509,17 @@ class Contact
        /**
         * Search contact table by nick or name
         *
-        * @param string $search Name or nick
-        * @param string $mode   Search mode (e.g. "community")
-        * @param int    $uid    User ID
-        * @param int    $limit  Maximum amount of returned values
-        * @param int    $offset Limit offset
+        * @param string $search       Name or nick
+        * @param string $mode         Search mode (e.g. "community")
+        * @param bool   $show_blocked Show users from blocked servers. Default is false
+        * @param int    $uid          User ID
+        * @param int    $limit        Maximum amount of returned values
+        * @param int    $offset       Limit offset
         *
         * @return array with search results
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function searchByName(string $search, string $mode = '', int $uid = 0, int $limit = 0, int $offset = 0): array
+       public static function searchByName(string $search, string $mode = '', bool $show_blocked = false, int $uid = 0, int $limit = 0, int $offset = 0): array
        {
                if (empty($search)) {
                        return [];
@@ -3511,7 +3535,18 @@ class Contact
                        $networks[] = Protocol::OSTATUS;
                }
 
-               $condition = ['network' => $networks, 'failed' => false, 'deleted' => false, 'uid' => $uid];
+               $condition = [
+                       'network'        => $networks,
+                       'server-failed'  => false,
+                       'failed'         => false,
+                       'deleted'        => false,
+                       'unsearchable'   => false,
+                       'uid'            => $uid
+               ];
+
+               if (!$show_blocked) {
+                       $condition['server-blocked'] = true;
+               }
 
                if ($uid == 0) {
                        $condition['blocked'] = false;
@@ -3535,10 +3570,9 @@ class Contact
                }
 
                $condition = DBA::mergeConditions($condition,
-                       ["(NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` WHERE `publish` OR `net-publish`))
-                       AND (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", $search, $search, $search]);
+                       ["(`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", $search, $search, $search]);
 
-               return self::selectToArray([], $condition, $params);
+               return DBA::selectToArray('account-user-view', [], $condition, $params);
        }
 
        /**