X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FContact.php;h=8f9cb4d051c76832e52a4c9641fcf4a09f766ceb;hb=2e6ab0e31293a8e154561001924ce8d888b32c88;hp=1a43d140b2c2a578f0cf76295203a00f56adf605;hpb=d8fe8b1e8b008ee0b8d87fda7467b1d058a07e75;p=friendica.git diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 1a43d140b2..8f9cb4d051 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -29,17 +29,20 @@ use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Core\Renderer; -use Friendica\Core\Session; use Friendica\Core\System; 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; @@ -97,17 +100,17 @@ class Contact * Relationship types * @{ */ - const NOTHING = 0; - const FOLLOWER = 1; - const SHARING = 2; - const FRIEND = 3; - const SELF = 4; + const NOTHING = 0; // There is no relationship between the contact and the user + const FOLLOWER = 1; // The contact is following this user (the contact is the subscriber) + const SHARING = 2; // The contact shares their content with this user (the user is the subscriber) + const FRIEND = 3; // There is a mutual relationship between the contact and the user + const SELF = 4; // This is the user theirself /** * @} */ const MIRROR_DEACTIVATED = 0; - const MIRROR_FORWARDED = 1; + const MIRROR_FORWARDED = 1; // Deprecated, now does the same like MIRROR_OWN_POST const MIRROR_OWN_POST = 2; const MIRROR_NATIVE_RESHARE = 3; @@ -137,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 @@ -261,6 +276,32 @@ class Contact return DBA::selectFirst('contact', $fields, ['uri-id' => $uri_id], ['order' => ['uid']]); } + /** + * Fetch all remote contacts for a given contact url + * + * @param string $url The URL of the contact + * @param array $fields The wanted fields + * + * @return array all remote contacts + * + * @throws \Exception + */ + public static function getVisitorByUrl(string $url, array $fields = ['id', 'uid']): array + { + $remote = []; + + $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($url), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]); + while ($contact = DBA::fetch($remote_contacts)) { + if (($contact['uid'] == 0) || Contact\User::isBlocked($contact['id'], $contact['uid'])) { + continue; + } + $remote[$contact['uid']] = $contact['id']; + } + DBA::close($remote_contacts); + + return $remote; + } + /** * Fetches a contact by a given url * @@ -704,7 +745,6 @@ class Contact 'notify' => DI::baseUrl() . '/dfrn_notify/' . $user['nickname'], 'poll' => DI::baseUrl() . '/dfrn_poll/' . $user['nickname'], 'confirm' => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'], - 'poco' => DI::baseUrl() . '/poco/' . $user['nickname'], 'name-date' => DateTimeFormat::utcNow(), 'uri-date' => DateTimeFormat::utcNow(), 'avatar-date' => DateTimeFormat::utcNow(), @@ -786,7 +826,6 @@ class Contact 'notify' => DI::baseUrl() . '/dfrn_notify/' . $user['nickname'], 'poll' => DI::baseUrl() . '/dfrn_poll/'. $user['nickname'], 'confirm' => DI::baseUrl() . '/dfrn_confirm/' . $user['nickname'], - 'poco' => DI::baseUrl() . '/poco/' . $user['nickname'], ]; @@ -877,7 +916,7 @@ class Contact self::clearFollowerFollowingEndpointCache($contact['uid']); // Archive the contact - self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]); + self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'rel' => self::NOTHING, 'deleted' => true], ['id' => $id]); if (!DBA::exists('contact', ['uri-id' => $contact['uri-id'], 'deleted' => false])) { Avatar::deleteCache($contact); @@ -1103,7 +1142,7 @@ class Contact $photos_link = ''; if ($uid == 0) { - $uid = local_user(); + $uid = DI::userSession()->getLocalUserId(); } if (empty($contact['uid']) || ($contact['uid'] != $uid)) { @@ -1124,7 +1163,7 @@ class Contact $sparkle = false; if (($contact['network'] === Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) { $sparkle = true; - $profile_link = DI::baseUrl() . '/redir/' . $contact['id']; + $profile_link = 'contact/redir/' . $contact['id']; } else { $profile_link = $contact['url']; } @@ -1135,25 +1174,25 @@ class Contact if ($sparkle) { $status_link = $profile_link . '/status'; - $photos_link = str_replace('/profile/', '/photos/', $profile_link); + $photos_link = $profile_link . '/photos'; $profile_link = $profile_link . '/profile'; } if (self::canReceivePrivateMessages($contact) && empty($contact['pending'])) { - $pm_url = DI::baseUrl() . '/message/new/' . $contact['id']; + $pm_url = 'message/new/' . $contact['id']; } - $contact_url = DI::baseUrl() . '/contact/' . $contact['id']; + $contact_url = 'contact/' . $contact['id']; - $posts_link = DI::baseUrl() . '/contact/' . $contact['id'] . '/conversations'; + $posts_link = 'contact/' . $contact['id'] . '/conversations'; $follow_link = ''; $unfollow_link = ''; if (!$contact['self'] && Protocol::supportsFollow($contact['network'])) { if ($contact['uid'] && in_array($contact['rel'], [self::SHARING, self::FRIEND])) { - $unfollow_link = 'unfollow?url=' . urlencode($contact['url']) . '&auto=1'; + $unfollow_link = 'contact/unfollow?url=' . urlencode($contact['url']) . '&auto=1'; } elseif(!$contact['pending']) { - $follow_link = 'follow?url=' . urlencode($contact['url']) . '&auto=1'; + $follow_link = 'contact/follow?url=' . urlencode($contact['url']) . '&auto=1'; } } @@ -1167,7 +1206,7 @@ class Contact '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], + 'unfollow'=> [DI::l10n()->t('Unfollow') , $unfollow_link, true], ]; } else { $menu = [ @@ -1178,7 +1217,7 @@ class Contact '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], + 'unfollow'=> [DI::l10n()->t('Unfollow') , $unfollow_link , true], ]; if (!empty($contact['pending'])) { @@ -1334,9 +1373,10 @@ class Contact 'writable' => 1, 'blocked' => 0, 'readonly' => 0, - 'pending' => 0]; + 'pending' => 0, + ]; - $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false]; + $condition = ['nurl' => Strings::normaliseLink($data['url']), 'uid' => $uid, 'deleted' => false]; // Before inserting we do check if the entry does exist now. $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]); @@ -1359,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); @@ -1506,10 +1556,10 @@ class Contact if ($thread_mode) { $condition = ["((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql, - $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()]; + $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), DI::userSession()->getLocalUserId()]; } else { $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql, - $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, local_user()]; + $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, DI::userSession()->getLocalUserId()]; } if (!empty($parent)) { @@ -1527,10 +1577,10 @@ class Contact } if (DI::mode()->isMobile()) { - $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', + $itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile')); } else { - $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', + $itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_network', DI::config()->get('system', 'itemspage_network')); } @@ -1538,7 +1588,7 @@ class Contact $params = ['order' => ['received' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]; - if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) { + if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) { $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl'); $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]); } else { @@ -1547,27 +1597,27 @@ class Contact if ($thread_mode) { $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'commented']; - $items = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params)); + $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params)); if ($pager->getStart() == 0) { - $cdata = self::getPublicAndUserContactID($cid, local_user()); + $cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId()); if (!empty($cdata['public'])) { $pinned = Post\Collection::selectToArrayForContact($cdata['public'], Post\Collection::FEATURED, $fields); $items = array_merge($items, $pinned); } } - $o .= DI::conversation()->create($items, 'contacts', $update, false, 'pinned_commented', local_user()); + $o .= DI::conversation()->create($items, 'contacts', $update, false, 'pinned_commented', DI::userSession()->getLocalUserId()); } else { $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']); - $items = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params)); + $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params)); if ($pager->getStart() == 0) { - $cdata = self::getPublicAndUserContactID($cid, local_user()); + $cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId()); if (!empty($cdata['public'])) { $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)", $cdata['public'], Post\Collection::FEATURED]; - $pinned = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params)); + $pinned = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params)); $items = array_merge($pinned, $items); } } @@ -1576,7 +1626,7 @@ class Contact } if (!$update) { - if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) { + if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) { $o .= HTML::scrollLoader(); } else { $o .= $pager->renderMinimal(count($items)); @@ -2021,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 @@ -2053,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) : ''); } /** @@ -2078,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 @@ -2111,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) : ''); } /** @@ -2129,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; @@ -2139,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; @@ -2211,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 { @@ -2429,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); } /** @@ -2617,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']; @@ -3130,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']]); + } } /** @@ -3153,6 +3245,8 @@ class Contact } else { self::update(['rel' => self::FOLLOWER], ['id' => $contact['id']]); } + + Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $contact['uid']); } /** @@ -3231,7 +3325,7 @@ class Contact */ public static function magicLink(string $contact_url, string $url = ''): string { - if (!Session::isAuthenticated()) { + if (!DI::userSession()->isAuthenticated()) { return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url; } @@ -3277,7 +3371,7 @@ class Contact { $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url']; - if (!Session::isAuthenticated()) { + if (!DI::userSession()->isAuthenticated()) { return $destination; } @@ -3286,7 +3380,7 @@ class Contact return $url; } - if (DI::pConfig()->get(local_user(), 'system', 'stay_local') && ($url == '')) { + if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'stay_local') && ($url == '')) { return 'contact/' . $contact['id'] . '/conversations'; } @@ -3298,7 +3392,7 @@ class Contact return $destination; } - $redirect = 'redir/' . $contact['id']; + $redirect = 'contact/redir/' . $contact['id']; if (($url != '') && !Strings::compareLink($contact['url'], $url)) { $redirect .= '?url=' . $url; @@ -3347,11 +3441,13 @@ class Contact * @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 * * @return array with search results * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function searchByName(string $search, string $mode = '', int $uid = 0): array + public static function searchByName(string $search, string $mode = '', int $uid = 0, int $limit = 0, int $offset = 0): array { if (empty($search)) { return []; @@ -3371,6 +3467,8 @@ class Contact if ($uid == 0) { $condition['blocked'] = false; + } else { + $condition['rel'] = [Contact::SHARING, Contact::FRIEND]; } // check if we search only communities or every contact @@ -3380,12 +3478,19 @@ class Contact $search .= '%'; + $params = []; + + if (!empty($limit) && !empty($offset)) { + $params['limit'] = [$offset, $limit]; + } elseif (!empty($limit)) { + $params['limit'] = $limit; + } + $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]); - $contacts = self::selectToArray([], $condition); - return $contacts; + return self::selectToArray([], $condition, $params); } /** @@ -3440,4 +3545,17 @@ class Contact return []; } + + /** + * Checks, if contacts with the given condition exists + * + * @param array $condition + * + * @return bool + * @throws \Exception + */ + public static function exists(array $condition): bool + { + return DBA::exists('contact', $condition); + } }