From c9110e03e144c053222f33166912f45cd34ea9bc Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 17 May 2021 22:22:51 +0000 Subject: [PATCH 01/11] API: Search endpoint --- doc/API-Mastodon.md | 2 +- src/Module/Api/Mastodon/Accounts/Search.php | 8 +- src/Module/Api/Mastodon/Search.php | 194 ++++++++++++++++++++ static/routes.config.php | 2 +- 4 files changed, 200 insertions(+), 6 deletions(-) create mode 100644 src/Module/Api/Mastodon/Search.php diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index 3f21d823a..1d1f497d3 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -109,6 +109,7 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`GET /api/v1/timelines/public`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/tag/:hashtag`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/trends`](https://docs.joinmastodon.org/methods/instance/trends/) +- [`GET /api/v2/search`](https://docs.joinmastodon.org/methods/search/) ## Currently unimplemented endpoints @@ -122,7 +123,6 @@ These emdpoints are planned to be implemented - [`POST /api/v1/conversations/:id/read`](https://docs.joinmastodon.org/methods/timelines/conversations/) - [`GET /api/v1/instance/activity`](https://docs.joinmastodon.org/methods/instance#weekly-activity) - [`GET /api/v1/timelines/direct`](https://docs.joinmastodon.org/methods/timelines/) -- [`GET /api/v2/search`](https://docs.joinmastodon.org/methods/search/) ## Non supportable endpoints diff --git a/src/Module/Api/Mastodon/Accounts/Search.php b/src/Module/Api/Mastodon/Accounts/Search.php index 9f1d0aadf..f74e35713 100644 --- a/src/Module/Api/Mastodon/Accounts/Search.php +++ b/src/Module/Api/Mastodon/Accounts/Search.php @@ -44,13 +44,13 @@ class Search extends BaseApi $uid = self::getCurrentUserID(); // What to search for - $q = (int)!isset($_REQUEST['q']) ? 0 : $_REQUEST['q']; + $q = $_REQUEST['q'] ?? ''; // Maximum number of results. Defaults to 40. - $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit']; + $limit = (int)$_REQUEST['limit'] ?? 40; // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address. - $resolve = (int)!isset($_REQUEST['resolve']) ? 0 : $_REQUEST['resolve']; + $resolve = (bool)!isset($_REQUEST['resolve']) ? false : ($_REQUEST['resolve'] == 'true'); // Only who the user is following. Defaults to false. - $following = (int)!isset($_REQUEST['following']) ? 0 : $_REQUEST['following']; + $following = (bool)!isset($_REQUEST['following']) ? false : ($_REQUEST['following'] == 'true'); $accounts = []; diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php new file mode 100644 index 000000000..5bd59d89a --- /dev/null +++ b/src/Module/Api/Mastodon/Search.php @@ -0,0 +1,194 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon; + +use Friendica\Core\Protocol; +use Friendica\Core\Search as CoreSearch; +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Contact; +use Friendica\Model\Post; +use Friendica\Model\Tag; +use Friendica\Module\BaseApi; +use Friendica\Object\Search\ContactResult; + +/** + * @see https://docs.joinmastodon.org/methods/search/ + */ +class Search extends BaseApi +{ + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + self::login(self::SCOPE_READ); + $uid = self::getCurrentUserID(); + + // If provided, statuses returned will be authored only by this account + $account_id = $_REQUEST['account_id'] ?? ''; + // Return results older than this id + $max_id = (int)($_REQUEST['max_id'] ?? 0); + // Return results immediately newer than this id + $min_id = (int)($_REQUEST['min_id'] ?? 0); + // Enum(accounts, hashtags, statuses) + $type = $_REQUEST['type'] ?? ''; + // Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags. + $exclude_unreviewed = (bool)!isset($_REQUEST['exclude_unreviewed']) ? false : ($_REQUEST['exclude_unreviewed'] == 'true'); + // The search query + $q = $_REQUEST['q'] ?? ''; + // Attempt WebFinger lookup. Defaults to false. + $resolve = (bool)!isset($_REQUEST['resolve']) ? false : ($_REQUEST['resolve'] == 'true'); + // Maximum number of results to load, per type. Defaults to 20. Max 40. + $limit = (int)($_REQUEST['limit'] ?? 20); + // Offset in search results. Used for pagination. Defaults to 0. + $offset = (int)($_REQUEST['offset'] ?? 0); + // Only who the user is following. Defaults to false. + $following = (bool)!isset($_REQUEST['following']) ? false : ($_REQUEST['following'] == 'true'); + + $result = ['accounts' => [], 'statuses' => [], 'hashtags' => []]; + + if (empty($type) || ($type == 'accounts')) { + $result['accounts'] = self::searchAccounts($uid, $q, $resolve, $limit, $offset, $following); + } + if ((empty($type) || ($type == 'statuses')) && (strpos($q, '@') == false)) { + $result['statuses'] = self::searchStatuses($uid, $q, $account_id, $max_id, $min_id, $limit, $offset); + } + if ((empty($type) || ($type == 'hashtags')) && (strpos($q, '@') == false)) { + $result['hashtags'] = self::searchHashtags($q, $exclude_unreviewed, $limit, $offset); + } + + System::jsonExit($result); + } + + private static function searchAccounts(int $uid, string $q, bool $resolve, int $limit, int $offset, bool $following) + { + $accounts = []; + + if (!$following) { + if ((strrpos($q, '@') > 0) && $resolve) { + $results = CoreSearch::getContactsFromProbe($q); + } + + if (empty($results)) { + if (DI::config()->get('system', 'poco_local_search')) { + $results = CoreSearch::getContactsFromLocalDirectory($q, CoreSearch::TYPE_ALL, 0, $limit); + } elseif (!empty(DI::config()->get('system', 'directory'))) { + $results = CoreSearch::getContactsFromGlobalDirectory($q, CoreSearch::TYPE_ALL, 1); + } + } + if (!empty($results)) { + $counter = 0; + foreach ($results->getResults() as $result) { + if (++$counter > $limit) { + continue; + } + if ($result instanceof ContactResult) { + $id = Contact::getIdForURL($result->getUrl(), 0, false); + + $accounts[] = DI::mstdnAccount()->createFromContactId($id, $uid); + } + } + } + } else { + $contacts = Contact::searchByName($q, '', $uid); + + $counter = 0; + foreach ($contacts as $contact) { + if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) { + continue; + } + if (++$counter > $limit) { + continue; + } + $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid); + } + DBA::close($contacts); + } + + return $accounts; + } + + private static function searchStatuses(int $uid, string $q, string $account_id, int $max_id, int $min_id, int $limit, int $offset) + { + $params = ['order' => ['uri-id' => true], 'limit' => [$offset, $limit]]; + + if (substr($q, 0, 1) == '#') { + $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`)) + AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))", + substr($q, 1), 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0]; + $table = 'tag-search-view'; + } else { + $condition = ["`uri-id` IN (SELECT `uri-id` FROM `post-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE)) + AND (`uid` = ? OR (`uid` = ? AND NOT `global`)) AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))", + str_replace('@', ' ', $q), 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0]; + $table = 'post-user-view'; + } + + if (!empty($max_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + } + + if (!empty($since_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + } + + if (!empty($min_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + + $params['order'] = ['uri-id']; + } + + $items = DBA::select($table, ['uri-id'], $condition, $params); + + $statuses = []; + while ($item = Post::fetch($items)) { + $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid); + } + DBA::close($items); + + if (!empty($min_id)) { + array_reverse($statuses); + } + + return $statuses; + } + + private static function searchHashtags(string $q, bool $exclude_unreviewed, int $limit, int $offset) + { + $q = ltrim($q, '#'); + $params = ['order' => ['name'], 'limit' => [$offset, $limit]]; + + $condition = ["`id` IN (SELECT `tid` FROM `post-tag` WHERE `type` = ? AND `tid` = `id`) AND `name` LIKE ?", Tag::HASHTAG, $q . '%']; + + $tags = DBA::select('tag', ['name'], $condition, $params); + + $hashtags = []; + foreach ($tags as $tag) { + $hashtags[] = new \Friendica\Object\Api\Mastodon\Tag(DI::baseUrl(), $tag); + } + + return $hashtags; + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 580905af9..c91751b17 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -150,7 +150,7 @@ return [ '/trends' => [Module\Api\Mastodon\Trends::class, [R::GET ]], ], '/v2' => [ - '/search' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // @todo + '/search' => [Module\Api\Mastodon\Search::class, [R::GET ]], ], '/friendica' => [ '/profile/show' => [Module\Api\Friendica\Profile\Show::class, [R::GET ]], From 59cff3511b05f0bdc28b3e0b8a374705cb13996c Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 17 May 2021 22:31:35 +0000 Subject: [PATCH 02/11] Standards --- src/Module/Api/Mastodon/Search.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 5bd59d89a..463c0eb6d 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -178,6 +178,7 @@ class Search extends BaseApi private static function searchHashtags(string $q, bool $exclude_unreviewed, int $limit, int $offset) { $q = ltrim($q, '#'); + $params = ['order' => ['name'], 'limit' => [$offset, $limit]]; $condition = ["`id` IN (SELECT `tid` FROM `post-tag` WHERE `type` = ? AND `tid` = `id`) AND `name` LIKE ?", Tag::HASHTAG, $q . '%']; From 71a398277dd57b934fbb3c533c903b6869e71272 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 17 May 2021 22:44:42 +0000 Subject: [PATCH 03/11] Improved query --- src/Module/Api/Mastodon/Search.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 463c0eb6d..b964b35db 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -181,7 +181,7 @@ class Search extends BaseApi $params = ['order' => ['name'], 'limit' => [$offset, $limit]]; - $condition = ["`id` IN (SELECT `tid` FROM `post-tag` WHERE `type` = ? AND `tid` = `id`) AND `name` LIKE ?", Tag::HASHTAG, $q . '%']; + $condition = ["EXISTS(SELECT `tid` FROM `post-tag` WHERE `type` = ? AND `tid` = `id`) AND `name` LIKE ?", Tag::HASHTAG, $q . '%']; $tags = DBA::select('tag', ['name'], $condition, $params); From 32e7ddf21bcf539f3a2a77285b585a133b361134 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 04:54:37 +0000 Subject: [PATCH 04/11] Improved parameter assignment --- src/Module/Api/Mastodon/Accounts/Search.php | 6 +++--- src/Module/Api/Mastodon/Search.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Module/Api/Mastodon/Accounts/Search.php b/src/Module/Api/Mastodon/Accounts/Search.php index f74e35713..30031a3d1 100644 --- a/src/Module/Api/Mastodon/Accounts/Search.php +++ b/src/Module/Api/Mastodon/Accounts/Search.php @@ -46,11 +46,11 @@ class Search extends BaseApi // What to search for $q = $_REQUEST['q'] ?? ''; // Maximum number of results. Defaults to 40. - $limit = (int)$_REQUEST['limit'] ?? 40; + $limit = (int)($_REQUEST['limit'] ?? 40); // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address. - $resolve = (bool)!isset($_REQUEST['resolve']) ? false : ($_REQUEST['resolve'] == 'true'); + $resolve = ($_REQUEST['resolve'] ?? '') == 'true'; // Only who the user is following. Defaults to false. - $following = (bool)!isset($_REQUEST['following']) ? false : ($_REQUEST['following'] == 'true'); + $following = ($_REQUEST['following'] ?? '') == 'true'; $accounts = []; diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index b964b35db..2da3f341c 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -55,17 +55,17 @@ class Search extends BaseApi // Enum(accounts, hashtags, statuses) $type = $_REQUEST['type'] ?? ''; // Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags. - $exclude_unreviewed = (bool)!isset($_REQUEST['exclude_unreviewed']) ? false : ($_REQUEST['exclude_unreviewed'] == 'true'); + $exclude_unreviewed = ($_REQUEST['exclude_unreviewed'] ?? '') == 'true'; // The search query $q = $_REQUEST['q'] ?? ''; // Attempt WebFinger lookup. Defaults to false. - $resolve = (bool)!isset($_REQUEST['resolve']) ? false : ($_REQUEST['resolve'] == 'true'); + $resolve = ($_REQUEST['resolve'] ?? '') == 'true'; // Maximum number of results to load, per type. Defaults to 20. Max 40. $limit = (int)($_REQUEST['limit'] ?? 20); // Offset in search results. Used for pagination. Defaults to 0. $offset = (int)($_REQUEST['offset'] ?? 0); // Only who the user is following. Defaults to false. - $following = (bool)!isset($_REQUEST['following']) ? false : ($_REQUEST['following'] == 'true'); + $following = ($_REQUEST['following'] ?? '') == 'true'; $result = ['accounts' => [], 'statuses' => [], 'hashtags' => []]; From ce427b248e1e089eee9d39be0e90335a02d90ec0 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 06:31:22 +0000 Subject: [PATCH 05/11] Central function to process request parameters --- src/Module/Api/Mastodon/Notifications.php | 18 +++++++---- src/Module/Api/Mastodon/Search.php | 24 +++++++++------ src/Module/BaseApi.php | 37 +++++++++++++++++++++++ 3 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/Module/Api/Mastodon/Notifications.php b/src/Module/Api/Mastodon/Notifications.php index 30e1060d8..3dfcaa668 100644 --- a/src/Module/Api/Mastodon/Notifications.php +++ b/src/Module/Api/Mastodon/Notifications.php @@ -50,23 +50,29 @@ class Notifications extends BaseApi System::jsonExit(DI::mstdnNotification()->createFromNotifyId($id)); } + $request = self::getRequest(['max_id' => 0, 'since_id' => 0, 'min_id' => 0, 'limit' => 20, + 'exclude_types' => [], 'account_id' => 0, 'with_muted' => false]); + // Return results older than this ID - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; + $max_id = $request['max_id']; // Return results newer than this ID - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; + $since_id = $request['since_id']; // Return results immediately newer than this ID - $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; + $min_id = $request['min_id']; // Maximum number of results to return (default 20) - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + $limit = $request['limit']; // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request) - $exclude_types = $_REQUEST['exclude_types'] ?? []; + $exclude_types = $request['exclude_types']; // Return only notifications received from this account - $account_id = (int)!isset($_REQUEST['account_id']) ? 0 : $_REQUEST['account_id']; + $account_id = $request['account_id']; + + // Unknown parameter + $with_muted = $request['with_muted']; $params = ['order' => ['id' => true], 'limit' => $limit]; diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 2da3f341c..9d7575846 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -46,26 +46,30 @@ class Search extends BaseApi self::login(self::SCOPE_READ); $uid = self::getCurrentUserID(); + $request = self::getRequest(['account_id' => 0, 'max_id' => 0, 'min_id' => 0, 'type' => '', + 'exclude_unreviewed' => false, 'q' => '', 'resolve' => false, 'limit' => 20, + 'offset' => 0, 'following' => false]); + // If provided, statuses returned will be authored only by this account - $account_id = $_REQUEST['account_id'] ?? ''; + $account_id = $request['account_id']; // Return results older than this id - $max_id = (int)($_REQUEST['max_id'] ?? 0); + $max_id = $request['max_id']; // Return results immediately newer than this id - $min_id = (int)($_REQUEST['min_id'] ?? 0); + $min_id = $request['min_id']; // Enum(accounts, hashtags, statuses) - $type = $_REQUEST['type'] ?? ''; + $type = $request['type']; // Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags. - $exclude_unreviewed = ($_REQUEST['exclude_unreviewed'] ?? '') == 'true'; + $exclude_unreviewed = $request['exclude_unreviewed']; // The search query - $q = $_REQUEST['q'] ?? ''; + $q = $request['q']; // Attempt WebFinger lookup. Defaults to false. - $resolve = ($_REQUEST['resolve'] ?? '') == 'true'; + $resolve = $request['resolve']; // Maximum number of results to load, per type. Defaults to 20. Max 40. - $limit = (int)($_REQUEST['limit'] ?? 20); + $limit = $request['limit']; // Offset in search results. Used for pagination. Defaults to 0. - $offset = (int)($_REQUEST['offset'] ?? 0); + $offset = $request['offset']; // Only who the user is following. Defaults to false. - $following = ($_REQUEST['following'] ?? '') == 'true'; + $following = $request['following']; $result = ['accounts' => [], 'statuses' => [], 'hashtags' => []]; diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index db4531d91..c12872104 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -136,6 +136,43 @@ class BaseApi extends BaseModule System::jsonError(501, $errorobj->toArray()); } + /** + * Processes data from GET requests and sets defaults + * + * @return array request data + */ + public static function getRequest(array $defaults) { + $request = []; + + foreach ($defaults as $parameter => $defaultvalue) { + if (is_string($defaultvalue)) { + $request[$parameter] = $_REQUEST[$parameter] ?? $defaultvalue; + } elseif (is_int($defaultvalue)) { + $request[$parameter] = (int)($_REQUEST[$parameter] ?? $defaultvalue); + } elseif (is_float($defaultvalue)) { + $request[$parameter] = (float)($_REQUEST[$parameter] ?? $defaultvalue); + } elseif (is_array($defaultvalue)) { + $request[$parameter] = $_REQUEST[$parameter] ?? []; + } elseif (is_bool($defaultvalue)) { + $request[$parameter] = in_array(strtolower($_REQUEST[$parameter] ?? ''), ['true', '1']); + } else { + Logger::notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($defaultvalue)]); + } + } + + foreach ($_REQUEST ?? [] as $parameter => $value) { + if ($parameter == 'pagename') { + continue; + } + if (!in_array($parameter, array_keys($defaults))) { + Logger::notice('Unhandled request field', ['parameter' => $parameter, 'value' => $value, 'command' => DI::args()->getCommand()]); + } + } + + Logger::debug('Got request parameters', ['request' => $request, 'command' => DI::args()->getCommand()]); + return $request; + } + /** * Get post data that is transmitted as JSON * From 0a6127ea6ef2f4bcc9058cfa614303e51a13bd3d Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 07:01:23 +0000 Subject: [PATCH 06/11] Set maximum value --- src/Module/Api/Mastodon/Search.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 9d7575846..bad84b2b9 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -65,12 +65,16 @@ class Search extends BaseApi // Attempt WebFinger lookup. Defaults to false. $resolve = $request['resolve']; // Maximum number of results to load, per type. Defaults to 20. Max 40. - $limit = $request['limit']; + $limit = max($request['limit'], 40); // Offset in search results. Used for pagination. Defaults to 0. $offset = $request['offset']; // Only who the user is following. Defaults to false. $following = $request['following']; + if (empty($q)) { + DI::mstdnError()->UnprocessableEntity(); + } + $result = ['accounts' => [], 'statuses' => [], 'hashtags' => []]; if (empty($type) || ($type == 'accounts')) { From 8ae29eabe81d63ff98aefb6e9bbd0a102734820c Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 07:02:50 +0000 Subject: [PATCH 07/11] Min is max --- src/Module/Api/Mastodon/Search.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index bad84b2b9..76c8ae008 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -65,7 +65,7 @@ class Search extends BaseApi // Attempt WebFinger lookup. Defaults to false. $resolve = $request['resolve']; // Maximum number of results to load, per type. Defaults to 20. Max 40. - $limit = max($request['limit'], 40); + $limit = min($request['limit'], 40); // Offset in search results. Used for pagination. Defaults to 0. $offset = $request['offset']; // Only who the user is following. Defaults to false. From f31fdf4391e0d612d2f43217816fc9b9c72464a4 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 07:13:32 +0000 Subject: [PATCH 08/11] Spaces --- src/Module/Api/Mastodon/Search.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 76c8ae008..3eec69efc 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -47,8 +47,7 @@ class Search extends BaseApi $uid = self::getCurrentUserID(); $request = self::getRequest(['account_id' => 0, 'max_id' => 0, 'min_id' => 0, 'type' => '', - 'exclude_unreviewed' => false, 'q' => '', 'resolve' => false, 'limit' => 20, - 'offset' => 0, 'following' => false]); + 'exclude_unreviewed' => false, 'q' => '', 'resolve' => false, 'limit' => 20, 'offset' => 0, 'following' => false]); // If provided, statuses returned will be authored only by this account $account_id = $request['account_id']; From 2252949bca8bedb16ec026d7c9f44409ec63215d Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 07:18:31 +0000 Subject: [PATCH 09/11] Style stuff ... --- src/Module/Api/Mastodon/Search.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 3eec69efc..d8bd26137 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -46,8 +46,9 @@ class Search extends BaseApi self::login(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest(['account_id' => 0, 'max_id' => 0, 'min_id' => 0, 'type' => '', - 'exclude_unreviewed' => false, 'q' => '', 'resolve' => false, 'limit' => 20, 'offset' => 0, 'following' => false]); + $request = self::getRequest( + ['max_id' => 0, 'min_id' => 0, 'account_id' => 0, 'type' => '', 'exclude_unreviewed' => false, + 'resolve' => false, 'q' => '', 'limit' => 20, 'offset' => 0, 'following' => false]); // If provided, statuses returned will be authored only by this account $account_id = $request['account_id']; From 724a61390f2e567fcbb7afb597f2927b909e911c Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 07:23:18 +0000 Subject: [PATCH 10/11] Coding standards --- src/Module/Api/Mastodon/Search.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index d8bd26137..45514428f 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -46,8 +46,8 @@ class Search extends BaseApi self::login(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest( - ['max_id' => 0, 'min_id' => 0, 'account_id' => 0, 'type' => '', 'exclude_unreviewed' => false, + $request = self::getRequest([ + 'max_id' => 0, 'min_id' => 0, 'account_id' => 0, 'type' => '', 'exclude_unreviewed' => false, 'resolve' => false, 'q' => '', 'limit' => 20, 'offset' => 0, 'following' => false]); // If provided, statuses returned will be authored only by this account From f935c1cf73a785040d0c686985ac8fec5ccb407c Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 08:38:04 +0000 Subject: [PATCH 11/11] Restructured variable handling --- src/Module/Api/Mastodon/Search.php | 52 ++++++++++++------------------ 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 45514428f..592175149 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -47,44 +47,34 @@ class Search extends BaseApi $uid = self::getCurrentUserID(); $request = self::getRequest([ - 'max_id' => 0, 'min_id' => 0, 'account_id' => 0, 'type' => '', 'exclude_unreviewed' => false, - 'resolve' => false, 'q' => '', 'limit' => 20, 'offset' => 0, 'following' => false]); - - // If provided, statuses returned will be authored only by this account - $account_id = $request['account_id']; - // Return results older than this id - $max_id = $request['max_id']; - // Return results immediately newer than this id - $min_id = $request['min_id']; - // Enum(accounts, hashtags, statuses) - $type = $request['type']; - // Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags. - $exclude_unreviewed = $request['exclude_unreviewed']; - // The search query - $q = $request['q']; - // Attempt WebFinger lookup. Defaults to false. - $resolve = $request['resolve']; - // Maximum number of results to load, per type. Defaults to 20. Max 40. - $limit = min($request['limit'], 40); - // Offset in search results. Used for pagination. Defaults to 0. - $offset = $request['offset']; - // Only who the user is following. Defaults to false. - $following = $request['following']; - - if (empty($q)) { + 'account_id' => 0, // If provided, statuses returned will be authored only by this account + 'max_id' => 0, // Return results older than this id + 'min_id' => 0, // Return results immediately newer than this id + 'type' => '', // Enum(accounts, hashtags, statuses) + 'exclude_unreviewed' => false, // Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags. + 'q' => '', // The search query + 'resolve' => false, // Attempt WebFinger lookup. Defaults to false. + 'limit' => 20, // Maximum number of results to load, per type. Defaults to 20. Max 40. + 'offset' => 0, // Maximum number of results to load, per type. Defaults to 20. Max 40. + 'following' => false, // Only who the user is following. Defaults to false. + ]); + + if (empty($request['q'])) { DI::mstdnError()->UnprocessableEntity(); } + $limit = min($request['limit'], 40); + $result = ['accounts' => [], 'statuses' => [], 'hashtags' => []]; - if (empty($type) || ($type == 'accounts')) { - $result['accounts'] = self::searchAccounts($uid, $q, $resolve, $limit, $offset, $following); + if (empty($request['type']) || ($request['type'] == 'accounts')) { + $result['accounts'] = self::searchAccounts($uid, $request['q'], $request['resolve'], $limit, $request['offset'], $request['following']); } - if ((empty($type) || ($type == 'statuses')) && (strpos($q, '@') == false)) { - $result['statuses'] = self::searchStatuses($uid, $q, $account_id, $max_id, $min_id, $limit, $offset); + if ((empty($request['type']) || ($request['type'] == 'statuses')) && (strpos($request['q'], '@') == false)) { + $result['statuses'] = self::searchStatuses($uid, $request['q'], $request['account_id'], $request['max_id'], $request['min_id'], $limit, $request['offset']); } - if ((empty($type) || ($type == 'hashtags')) && (strpos($q, '@') == false)) { - $result['hashtags'] = self::searchHashtags($q, $exclude_unreviewed, $limit, $offset); + if ((empty($request['type']) || ($request['type'] == 'hashtags')) && (strpos($request['q'], '@') == false)) { + $result['hashtags'] = self::searchHashtags($request['q'], $request['exclude_unreviewed'], $limit, $request['offset']); } System::jsonExit($result);