X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FContact.php;h=8f9cb4d051c76832e52a4c9641fcf4a09f766ceb;hb=2e6ab0e31293a8e154561001924ce8d888b32c88;hp=eabe378a46253b553529e4123155300350194511;hpb=4c64c6ed9810903a808838b562528ba8aee0e3d7;p=friendica.git diff --git a/src/Model/Contact.php b/src/Model/Contact.php index eabe378a46..8f9cb4d051 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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; @@ -136,6 +140,18 @@ class Contact return $contact; } + /** + * @param array $fields Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * @return array + * @throws \Exception + */ + public static function selectAccountToArray(array $fields = [], array $condition = [], array $params = []): array + { + return DBA::selectToArray('account-user-view', $fields, $condition, $params); + } + /** * @param array $fields Array of selected fields, empty for all * @param array $condition Array of fields for condition @@ -1383,7 +1399,17 @@ class Contact } if ($data['network'] == Protocol::DIASPORA) { - FContact::updateFromProbeArray($data); + try { + DI::dsprContact()->updateFromProbeArray($data); + } catch (\InvalidArgumentException $e) { + Logger::error($e->getMessage(), ['url' => $url, 'data' => $data]); + } + } elseif (!empty($data['networks'][Protocol::DIASPORA])) { + try { + DI::dsprContact()->updateFromProbeArray($data['networks'][Protocol::DIASPORA]); + } catch (\InvalidArgumentException $e) { + Logger::error($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]); + } } self::updateFromProbeArray($contact_id, $data); @@ -2045,9 +2071,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 @@ -2077,7 +2104,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) : ''); } /** @@ -2102,9 +2137,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 @@ -2135,7 +2171,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) : ''); } /** @@ -2153,7 +2197,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; @@ -2163,8 +2207,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; @@ -2235,7 +2290,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 { @@ -2453,13 +2508,23 @@ 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 (\InvalidArgumentException $e) { + Logger::error($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 (\InvalidArgumentException $e) { + Logger::error($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]); + } } - return self::updateFromProbeArray($id, $ret); + return self::updateFromProbeArray($id, $data); } /** @@ -2641,7 +2706,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']; @@ -3154,11 +3219,14 @@ class Contact return; } + Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $contact['uid']); + 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']]); + } } /** @@ -3177,6 +3245,8 @@ class Contact } else { self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]); } + + Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $contact['uid']); } /**