From 4c81a7ab91c1ffafaf1cabb6393ceb8f6f11af87 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 28 Mar 2023 05:33:14 +0000 Subject: [PATCH 1/4] Don't search for contacts on blocked or failed systems --- database.sql | 10 +++++++--- src/Model/Contact.php | 11 +++++++++-- static/dbstructure.config.php | 2 +- static/dbview.config.php | 4 ++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/database.sql b/database.sql index eefd389840..42a6c1f2e6 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2023.03-rc (Giant Rhubarb) --- DB_UPDATE_VERSION 1517 +-- DB_UPDATE_VERSION 1518 -- ------------------------------------------ @@ -2849,7 +2849,9 @@ CREATE VIEW `account-view` AS SELECT `apcontact`.`statuses_count` AS `ap-statuses_count`, `gserver`.`site_name` AS `site_name`, `gserver`.`platform` AS `platform`, - `gserver`.`version` AS `version` + `gserver`.`version` AS `version`, + `gserver`.`blocked` AS `server-blocked`, + `gserver`.`failed` AS `server-failed` FROM `contact` LEFT JOIN `item-uri` ON `item-uri`.`id` = `contact`.`uri-id` LEFT JOIN `apcontact` ON `apcontact`.`uri-id` = `contact`.`uri-id` @@ -2953,7 +2955,9 @@ CREATE VIEW `account-user-view` AS SELECT `apcontact`.`statuses_count` AS `ap-statuses_count`, `gserver`.`site_name` AS `site_name`, `gserver`.`platform` AS `platform`, - `gserver`.`version` AS `version` + `gserver`.`version` AS `version`, + `gserver`.`blocked` AS `server-blocked`, + `gserver`.`failed` AS `server-failed` FROM `contact` AS `ucontact` INNER JOIN `contact` ON `contact`.`uri-id` = `ucontact`.`uri-id` AND `contact`.`uid` = 0 LEFT JOIN `item-uri` ON `item-uri`.`id` = `ucontact`.`uri-id` diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 8792273eef..54689fb624 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -3529,7 +3529,14 @@ class Contact $networks[] = Protocol::OSTATUS; } - $condition = ['network' => $networks, 'failed' => false, 'deleted' => false, 'uid' => $uid]; + $condition = [ + 'network' => $networks, + 'server-failed' => false, + 'server-blocked' => false, + 'failed' => false, + 'deleted' => false, + 'uid' => $uid + ]; if ($uid == 0) { $condition['blocked'] = false; @@ -3556,7 +3563,7 @@ class Contact ["(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]); - return self::selectToArray([], $condition, $params); + return DBA::selectToArray('account-user-view', [], $condition, $params); } /** diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 9acfbfcf98..ffb66d9701 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1517); + define('DB_UPDATE_VERSION', 1518); } return [ diff --git a/static/dbview.config.php b/static/dbview.config.php index 0f2201953b..76278c9a24 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -1009,6 +1009,8 @@ "site_name" => ["gserver", "site_name"], "platform" => ["gserver", "platform"], "version" => ["gserver", "version"], + "server-blocked" => ["gserver", "blocked"], + "server-failed" => ["gserver", "failed"], ], "query" => "FROM `contact` LEFT JOIN `item-uri` ON `item-uri`.`id` = `contact`.`uri-id` @@ -1111,6 +1113,8 @@ "site_name" => ["gserver", "site_name"], "platform" => ["gserver", "platform"], "version" => ["gserver", "version"], + "server-blocked" => ["gserver", "blocked"], + "server-failed" => ["gserver", "failed"], ], "query" => "FROM `contact` AS `ucontact` INNER JOIN `contact` ON `contact`.`uri-id` = `ucontact`.`uri-id` AND `contact`.`uid` = 0 From 74222532dcece0f53aa4fa8ad38a22b7cad58c4c Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 28 Mar 2023 06:34:09 +0000 Subject: [PATCH 2/4] Simply use "unsearchable" (and ensure it is updated for local users) --- src/Model/Contact.php | 4 ++-- update.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 54689fb624..d93c5f8367 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -3535,6 +3535,7 @@ class Contact 'server-blocked' => false, 'failed' => false, 'deleted' => false, + 'unsearchable' => false, 'uid' => $uid ]; @@ -3560,8 +3561,7 @@ 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 DBA::selectToArray('account-user-view', [], $condition, $params); } diff --git a/update.php b/update.php index 8dcd1b71dd..3cd0a45a7b 100644 --- a/update.php +++ b/update.php @@ -1315,3 +1315,14 @@ function update_1516() return Update::SUCCESS; } + +function update_1518() +{ + $users = DBA::select('user', ['uid']); + while ($user = DBA::fetch($users)) { + Contact::updateSelfFromUserID($user['uid']); + } + DBA::close($users); + + return Update::SUCCESS; +} From e6ce165bb54b81fde632b86e3baf385466c87bd9 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 28 Mar 2023 10:06:41 +0000 Subject: [PATCH 3/4] Use the correct ids for the search results --- src/Core/Search.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Core/Search.php b/src/Core/Search.php index 3c0ace3db5..b641f2897f 100644 --- a/src/Core/Search.php +++ b/src/Core/Search.php @@ -79,7 +79,7 @@ class Search $user_data['url'] ?? '', $user_data['photo'] ?? '', $user_data['network'] ?? '', - $contactDetails['id'] ?? 0, + $contactDetails['cid'] ?? 0, $user_data['id'] ?? 0, $user_data['tags'] ?? '' ); @@ -146,7 +146,7 @@ class Search $profile['photo'] ?? '', Protocol::DFRN, $contactDetails['cid'] ?? 0, - 0, + $contactDetails['zid'] ?? 0, $profile['tags'] ?? '' ); @@ -179,12 +179,12 @@ class Search $result = new ContactResult( $contact['name'], $contact['addr'], - $contact['addr'], + $contact['addr'] ?: $contact['url'], $contact['url'], $contact['photo'], $contact['network'], - $contact['cid'] ?? 0, - $contact['zid'] ?? 0, + 0, + $contact['pid'], $contact['keywords'] ); From f0743e4e128dfcb5d6305f4ed09e969573ba6b15 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 29 Mar 2023 18:39:21 +0000 Subject: [PATCH 4/4] Allow the search for contacts on blocked servers via web --- src/Core/Search.php | 4 ++-- src/Model/Contact.php | 18 +++++++++++------- src/Module/Api/Mastodon/Accounts/Search.php | 2 +- src/Module/Api/Mastodon/Search.php | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Core/Search.php b/src/Core/Search.php index b641f2897f..7f05a3a947 100644 --- a/src/Core/Search.php +++ b/src/Core/Search.php @@ -171,7 +171,7 @@ class Search { Logger::info('Searching', ['search' => $search, 'type' => $type, 'start' => $start, 'itempage' => $itemPage]); - $contacts = Contact::searchByName($search, $type == self::TYPE_FORUM ? 'community' : ''); + $contacts = Contact::searchByName($search, $type == self::TYPE_FORUM ? 'community' : '', true); $resultList = new ResultList($start, $itemPage, count($contacts)); @@ -226,7 +226,7 @@ class Search // check if searching in the local global contact table is enabled if (DI::config()->get('system', 'poco_local_search')) { - $return = Contact::searchByName($search, $mode); + $return = Contact::searchByName($search, $mode, true); } else { $p = $page > 1 ? 'p=' . $page : ''; $curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), HttpClientAccept::JSON); diff --git a/src/Model/Contact.php b/src/Model/Contact.php index d93c5f8367..cf1b528da8 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -3504,16 +3504,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 []; @@ -3532,13 +3533,16 @@ class Contact $condition = [ 'network' => $networks, 'server-failed' => false, - 'server-blocked' => false, 'failed' => false, 'deleted' => false, 'unsearchable' => false, 'uid' => $uid ]; + if (!$show_blocked) { + $condition['server-blocked'] = true; + } + if ($uid == 0) { $condition['blocked'] = false; } else { diff --git a/src/Module/Api/Mastodon/Accounts/Search.php b/src/Module/Api/Mastodon/Accounts/Search.php index 5a03cba196..5276161640 100644 --- a/src/Module/Api/Mastodon/Accounts/Search.php +++ b/src/Module/Api/Mastodon/Accounts/Search.php @@ -60,7 +60,7 @@ class Search extends BaseApi } if (empty($accounts)) { - $contacts = Contact::searchByName($request['q'], '', $request['following'] ? $uid : 0, $request['limit'], $request['offset']); + $contacts = Contact::searchByName($request['q'], '', false, $request['following'] ? $uid : 0, $request['limit'], $request['offset']); foreach ($contacts as $contact) { $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid); } diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 69e2627682..956e3d73b8 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -115,7 +115,7 @@ class Search extends BaseApi } $accounts = []; - foreach (Contact::searchByName($q, '', $following ? $uid : 0, $limit, $offset) as $contact) { + foreach (Contact::searchByName($q, '', $following ? $uid : 0, false, $limit, $offset) as $contact) { $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid); }