]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #12485 from annando/errors-warnings
[friendica.git] / src / Model / Contact.php
index 34ce8e684e7272c91c482922c5315a8d656ad689..392f7776154b863f1f1bfe54998907450fd40526 100644 (file)
@@ -34,11 +34,15 @@ use Friendica\Core\Worker;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
+use Friendica\Network\HTTPClient\Client\HttpClientAccept;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Network\HTTPException;
 use Friendica\Network\Probe;
+use Friendica\Object\Image;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\HTTPSignature;
 use Friendica\Util\Images;
 use Friendica\Util\Network;
 use Friendica\Util\Proxy;
@@ -1126,39 +1130,25 @@ class Contact
         * Returns the data array for the photo menu of a given contact
         *
         * @param array $contact contact
-        * @param int   $uid     optional, default 0
+        * @param int   $uid     Visitor user id
         * @return array
         * @throws HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function photoMenu(array $contact, int $uid = 0): array
+       public static function photoMenu(array $contact, int $uid): array
        {
-               $pm_url = '';
-               $status_link = '';
-               $photos_link = '';
-
-               if ($uid == 0) {
-                       $uid = DI::userSession()->getLocalUserId();
+               // Anonymous visitor
+               if (!$uid) {
+                       return ['profile' => [DI::l10n()->t('View Profile'), self::magicLinkByContact($contact), true]];
                }
 
-               if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
-                       if ($uid == 0) {
-                               $profile_link = self::magicLinkByContact($contact);
-                               $menu = ['profile' => [DI::l10n()->t('View Profile'), $profile_link, true]];
-
-                               return $menu;
-                       }
-
-                       // Look for our own contact if the uid doesn't match and isn't public
-                       $contact_own = DBA::selectFirst('contact', [], ['nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid]);
-                       if (DBA::isResult($contact_own)) {
-                               return self::photoMenu($contact_own, $uid);
-                       }
-               }
+               $pm_url      = '';
+               $status_link = '';
+               $photos_link = '';
 
                $sparkle = false;
                if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
-                       $sparkle = true;
+                       $sparkle      = true;
                        $profile_link = 'contact/redir/' . $contact['id'];
                } else {
                        $profile_link = $contact['url'];
@@ -1169,8 +1159,8 @@ class Contact
                }
 
                if ($sparkle) {
-                       $status_link = $profile_link . '/status';
-                       $photos_link = $profile_link . '/photos';
+                       $status_link  = $profile_link . '/status';
+                       $photos_link  = $profile_link . '/photos';
                        $profile_link = $profile_link . '/profile';
                }
 
@@ -1179,15 +1169,14 @@ class Contact
                }
 
                $contact_url = 'contact/' . $contact['id'];
-
                $posts_link = 'contact/' . $contact['id'] . '/conversations';
 
-               $follow_link = '';
+               $follow_link   = '';
                $unfollow_link = '';
                if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) {
                        if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) {
                                $unfollow_link = 'contact/unfollow?url=' . urlencode($contact['url']) . '&auto=1';
-                       } elseif(!$contact['pending']) {
+                       } elseif (!$contact['pending']) {
                                $follow_link = 'contact/follow?url=' . urlencode($contact['url']) . '&auto=1';
                        }
                }
@@ -1198,27 +1187,27 @@ class Contact
                 */
                if (empty($contact['uid'])) {
                        $menu = [
-                               'profile' => [DI::l10n()->t('View Profile')  , $profile_link , true],
-                               'network' => [DI::l10n()->t('Network Posts') , $posts_link   , false],
-                               'edit'    => [DI::l10n()->t('View Contact')  , $contact_url  , false],
-                               'follow'  => [DI::l10n()->t('Connect/Follow'), $follow_link  , true],
-                               'unfollow'=> [DI::l10n()->t('Unfollow')      , $unfollow_link, true],
+                               'profile'  => [DI::l10n()->t('View Profile')  , $profile_link , true ],
+                               'network'  => [DI::l10n()->t('Network Posts') , $posts_link   , false],
+                               'edit'     => [DI::l10n()->t('View Contact')  , $contact_url  , false],
+                               'follow'   => [DI::l10n()->t('Connect/Follow'), $follow_link  , true ],
+                               'unfollow' => [DI::l10n()->t('Unfollow')      , $unfollow_link, true ],
                        ];
                } else {
                        $menu = [
-                               'status'  => [DI::l10n()->t('View Status')   , $status_link      , true],
-                               'profile' => [DI::l10n()->t('View Profile')  , $profile_link     , true],
-                               '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],
-                               'pm'      => [DI::l10n()->t('Send PM')       , $pm_url           , false],
-                               'follow'  => [DI::l10n()->t('Connect/Follow'), $follow_link      , true],
-                               'unfollow'=> [DI::l10n()->t('Unfollow')      , $unfollow_link    , true],
+                               'status'   => [DI::l10n()->t('View Status')   , $status_link  , true ],
+                               'profile'  => [DI::l10n()->t('View Profile')  , $profile_link , true ],
+                               '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],
+                               'pm'       => [DI::l10n()->t('Send PM')       , $pm_url       , false],
+                               'follow'   => [DI::l10n()->t('Connect/Follow'), $follow_link  , true ],
+                               'unfollow' => [DI::l10n()->t('Unfollow')      , $unfollow_link, true ],
                        ];
 
                        if (!empty($contact['pending'])) {
                                try {
-                                       $intro = DI::intro()->selectForContact($contact['id']);
+                                       $intro          = DI::intro()->selectForContact($contact['id']);
                                        $menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro->id, true];
                                } catch (IntroductionNotFoundException $exception) {
                                        DI::logger()->error('Pending contact doesn\'t have an introduction.', ['exception' => $exception]);
@@ -1395,7 +1384,21 @@ class Contact
                }
 
                if ($data['network'] == Protocol::DIASPORA) {
-                       FContact::updateFromProbeArray($data);
+                       try {
+                               DI::dsprContact()->updateFromProbeArray($data);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data]);
+                       } catch (\InvalidArgumentException $e) {
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data]);
+                       }
+               } elseif (!empty($data['networks'][Protocol::DIASPORA])) {
+                       try {
+                               DI::dsprContact()->updateFromProbeArray($data['networks'][Protocol::DIASPORA]);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]);
+                       } catch (\InvalidArgumentException $e) {
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]);
+                       }
                }
 
                self::updateFromProbeArray($contact_id, $data);
@@ -2057,9 +2060,10 @@ class Contact
         * @param integer $cid     contact id
         * @param string  $size    One of the Proxy::SIZE_* constants
         * @param string  $updated Contact update date
+        * @param bool    $static  If "true" a parameter is added to convert the avatar to a static one
         * @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 = '', bool $static = false): string
        {
                // We have to fetch the "updated" variable when it wasn't provided
                // The parameter can be provided to improve performance
@@ -2089,7 +2093,15 @@ class Contact
                                $url .= Proxy::PIXEL_LARGE . '/';
                                break;
                }
-               return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : '');
+               $query_params = [];
+               if ($updated) {
+                       $query_params['ts'] = strtotime($updated);
+               }
+               if ($static) {
+                       $query_params['static'] = true;
+               }
+
+               return $url . ($guid ?: $cid) . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
        }
 
        /**
@@ -2114,9 +2126,10 @@ class Contact
         * @param integer $cid     contact id
         * @param string  $size    One of the Proxy::SIZE_* constants
         * @param string  $updated Contact update date
+        * @param bool    $static  If "true" a parameter is added to convert the header to a static one
         * @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 = '', bool $static = false): string
        {
                // We have to fetch the "updated" variable when it wasn't provided
                // The parameter can be provided to improve performance
@@ -2147,7 +2160,15 @@ class Contact
                                break;
                }
 
-               return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : '');
+               $query_params = [];
+               if ($updated) {
+                       $query_params['ts'] = strtotime($updated);
+               }
+               if ($static) {
+                       $query_params['static'] = true;
+               }
+
+               return $url . ($guid ?: $cid) . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
        }
 
        /**
@@ -2165,7 +2186,7 @@ class Contact
         */
        public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
        {
-               $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'xmpp', 'addr', 'nurl', 'url', 'network', 'uri-id'],
+               $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'blurhash', 'xmpp', 'addr', 'nurl', 'url', 'network', 'uri-id'],
                        ['id' => $cid, 'self' => false]);
                if (!DBA::isResult($contact)) {
                        return;
@@ -2175,8 +2196,19 @@ 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) {
-                               self::update(['avatar' => $avatar], ['id' => $cid]);
+                       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();
+                                       }
+                               }
+
+                               self::update($update_fields, ['id' => $cid]);
                                Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
                        }
                        return;
@@ -2247,7 +2279,7 @@ class Contact
                                if ($update) {
                                        $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
                                        if ($photos) {
-                                               $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
+                                               $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'blurhash' => $photos[3], 'avatar-date' => DateTimeFormat::utcNow()];
                                                $update = !empty($fields);
                                                Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
                                        } else {
@@ -2465,13 +2497,27 @@ class Contact
                        return false;
                }
 
-               $ret = Probe::uri($contact['url'], $network, $contact['uid']);
+               $data = Probe::uri($contact['url'], $network, $contact['uid']);
 
-               if ($ret['network'] == Protocol::DIASPORA) {
-                       FContact::updateFromProbeArray($ret);
+               if ($data['network'] == Protocol::DIASPORA) {
+                       try {
+                               DI::dsprContact()->updateFromProbeArray($data);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
+                       } catch (\InvalidArgumentException $e) {
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
+                       }
+               } elseif (!empty($data['networks'][Protocol::DIASPORA])) {
+                       try {
+                               DI::dsprContact()->updateFromProbeArray($data['networks'][Protocol::DIASPORA]);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
+                       } catch (\InvalidArgumentException $e) {
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
+                       }
                }
 
-               return self::updateFromProbeArray($id, $ret);
+               return self::updateFromProbeArray($id, $data);
        }
 
        /**
@@ -2653,7 +2699,7 @@ class Contact
                }
 
                $update = false;
-               $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url'], parse_url($ret['url'], PHP_URL_HOST));
+               $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url']);
 
                // make sure to not overwrite existing values with blank entries except some technical fields
                $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl'];
@@ -2870,15 +2916,8 @@ class Contact
                }
 
                // check if we already have a contact
-               // the poll url is more reliable than the profile url, as we may have
-               // indirect links or webfinger links
-
-               $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' => $uid, 'nurl' => Strings::normaliseLink($ret['url']), 'network' => $ret['network'], 'pending' => false];
-                       $contact = DBA::selectFirst('contact', ['id', 'rel'], $condition);
-               }
+               $condition = ['uid' => $uid, 'nurl' => Strings::normaliseLink($ret['url'])];
+               $contact = DBA::selectFirst('contact', ['id', 'rel', 'url', 'pending', 'hub-verify'], $condition);
 
                $protocol = self::getProtocol($ret['url'], $ret['network']);
 
@@ -2934,7 +2973,13 @@ class Contact
                        // update contact
                        $new_relation = (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND]) ? self::FRIEND : self::SHARING);
 
-                       $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
+                       $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false, 'network' => $ret['network']];
+
+                       if ($contact['pending'] && !empty($contact['hub-verify'])) {
+                               ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $uid);
+                               $fields['pending'] = false;
+                       }
+
                        self::update($fields, ['id' => $contact['id']]);
                } else {
                        $new_relation = (in_array($protocol, [Protocol::MAIL]) ? self::FRIEND : self::SHARING);
@@ -3171,8 +3216,9 @@ class Contact
                self::clearFollowerFollowingEndpointCache($contact['uid']);
 
                $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
-
-               DI::notification()->deleteForUserByVerb($contact['uid'], Activity::FOLLOW, ['actor-id' => $cdata['public']]);
+               if (!empty($cdata['public'])) {
+                       DI::notification()->deleteForUserByVerb($contact['uid'], Activity::FOLLOW, ['actor-id' => $cdata['public']]);
+               }
        }
 
        /**