From e5c1b90668adde6b6fd9633912ca926ddac47a2c Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 May 2021 19:26:46 +0000 Subject: [PATCH] API: Reworked request parameter handling --- .../Api/Mastodon/Accounts/Followers.php | 22 ++++--- .../Api/Mastodon/Accounts/Following.php | 22 ++++--- src/Module/Api/Mastodon/Accounts/Search.php | 30 +++++----- src/Module/Api/Mastodon/Accounts/Statuses.php | 45 +++++++------- src/Module/Api/Mastodon/Apps.php | 46 +++++++-------- src/Module/Api/Mastodon/Blocks.php | 21 ++++--- src/Module/Api/Mastodon/Bookmarks.php | 31 +++++----- src/Module/Api/Mastodon/Directory.php | 18 +++--- src/Module/Api/Mastodon/Favourited.php | 24 ++++---- src/Module/Api/Mastodon/FollowRequests.php | 20 ++++--- src/Module/Api/Mastodon/Lists/Accounts.php | 26 ++++---- src/Module/Api/Mastodon/Mutes.php | 21 ++++--- src/Module/Api/Mastodon/Notifications.php | 59 ++++++++----------- src/Module/Api/Mastodon/Timelines/Home.php | 36 ++++++----- .../Api/Mastodon/Timelines/ListTimeline.php | 31 +++++----- .../Api/Mastodon/Timelines/PublicTimeline.php | 46 +++++++-------- 16 files changed, 233 insertions(+), 265 deletions(-) diff --git a/src/Module/Api/Mastodon/Accounts/Followers.php b/src/Module/Api/Mastodon/Accounts/Followers.php index 4f560776c..67f57cb29 100644 --- a/src/Module/Api/Mastodon/Accounts/Followers.php +++ b/src/Module/Api/Mastodon/Accounts/Followers.php @@ -49,24 +49,22 @@ class Followers extends BaseApi DI::mstdnError()->RecordNotFound(); } - // Return results older than this id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than this id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Maximum number of results to return. Defaults to 20. - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + $request = self::getRequest([ + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'limit' => 20, // Maximum number of results to return. Defaults to 20. + ]); - - $params = ['order' => ['cid' => true], 'limit' => $limit]; + $params = ['order' => ['cid' => true], 'limit' => $request['limit']]; $condition = ['relation-cid' => $id, 'follows' => true]; - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`cid` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`cid` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]); } if (!empty($min_id)) { diff --git a/src/Module/Api/Mastodon/Accounts/Following.php b/src/Module/Api/Mastodon/Accounts/Following.php index 7edf4e987..582f84d7f 100644 --- a/src/Module/Api/Mastodon/Accounts/Following.php +++ b/src/Module/Api/Mastodon/Accounts/Following.php @@ -49,24 +49,22 @@ class Following extends BaseApi DI::mstdnError()->RecordNotFound(); } - // Return results older than this id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than this id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Maximum number of results to return. Defaults to 20. - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + $request = self::getRequest([ + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'limit' => 20, // Maximum number of results to return. Defaults to 20. + ]); - - $params = ['order' => ['relation-cid' => true], 'limit' => $limit]; + $params = ['order' => ['relation-cid' => true], 'limit' => $request['limit']]; $condition = ['cid' => $id, 'follows' => true]; - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['since_id']]); } if (!empty($min_id)) { diff --git a/src/Module/Api/Mastodon/Accounts/Search.php b/src/Module/Api/Mastodon/Accounts/Search.php index 30031a3d1..cb565ef73 100644 --- a/src/Module/Api/Mastodon/Accounts/Search.php +++ b/src/Module/Api/Mastodon/Accounts/Search.php @@ -43,34 +43,32 @@ class Search extends BaseApi self::login(self::SCOPE_READ); $uid = self::getCurrentUserID(); - // What to search for - $q = $_REQUEST['q'] ?? ''; - // Maximum number of results. Defaults to 40. - $limit = (int)($_REQUEST['limit'] ?? 40); - // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address. - $resolve = ($_REQUEST['resolve'] ?? '') == 'true'; - // Only who the user is following. Defaults to false. - $following = ($_REQUEST['following'] ?? '') == 'true'; + $request = self::getRequest([ + 'q' => '', // What to search for + 'limit' => 40, // Maximum number of results. Defaults to 40. + 'resolve' => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address. + 'following' => false, // Only who the user is following. Defaults to false. + ]); $accounts = []; - if (!$following) { - if ((strrpos($q, '@') > 0) && $resolve) { - $results = CoreSearch::getContactsFromProbe($q); + if (!$request['following']) { + if ((strrpos($request['q'], '@') > 0) && $request['resolve']) { + $results = CoreSearch::getContactsFromProbe($request['q']); } if (empty($results)) { if (DI::config()->get('system', 'poco_local_search')) { - $results = CoreSearch::getContactsFromLocalDirectory($q, CoreSearch::TYPE_ALL, 0, $limit); + $results = CoreSearch::getContactsFromLocalDirectory($request['q'], CoreSearch::TYPE_ALL, 0, $request['limit']); } elseif (!empty(DI::config()->get('system', 'directory'))) { - $results = CoreSearch::getContactsFromGlobalDirectory($q, CoreSearch::TYPE_ALL, 1); + $results = CoreSearch::getContactsFromGlobalDirectory($request['q'], CoreSearch::TYPE_ALL, 1); } } if (!empty($results)) { $counter = 0; foreach ($results->getResults() as $result) { - if (++$counter > $limit) { + if (++$counter > $request['limit']) { continue; } if ($result instanceof ContactResult) { @@ -81,14 +79,14 @@ class Search extends BaseApi } } } else { - $contacts = Contact::searchByName($q, '', $uid); + $contacts = Contact::searchByName($request['q'], '', $uid); $counter = 0; foreach ($contacts as $contact) { if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) { continue; } - if (++$counter > $limit) { + if (++$counter > $request['limit']) { continue; } $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid); diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php index f824abc9a..e2aeade55 100644 --- a/src/Module/Api/Mastodon/Accounts/Statuses.php +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -51,21 +51,18 @@ class Statuses extends BaseApi DI::mstdnError()->RecordNotFound(); } - // Show only statuses with media attached? Defaults to false. - $only_media = (bool)!isset($_REQUEST['only_media']) ? false : ($_REQUEST['only_media'] == 'true'); - // Return results older than this id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than this id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Return results immediately newer than this id - $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; - // Maximum number of results to return. Defaults to 20. - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + $request = self::getRequest([ + 'only_media' => false, // Show only statuses with media attached? Defaults to false. + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'min_id' => 0, // Return results immediately newer than this id + 'limit' => 20, // Maximum number of results to return. Defaults to 20. + 'pinned' => false, // Only pinned posts + 'exclude_replies' => false, // Don't show comments + 'with_muted' => false, // Unknown parameter + ]); - $pinned = (bool)!isset($_REQUEST['pinned']) ? false : ($_REQUEST['pinned'] == 'true'); - $exclude_replies = (bool)!isset($_REQUEST['exclude_replies']) ? false : ($_REQUEST['exclude_replies'] == 'true'); - - $params = ['order' => ['uri-id' => true], 'limit' => $limit]; + $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $uid = self::getCurrentUserID(); @@ -79,29 +76,29 @@ class Statuses extends BaseApi $condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ?))", GRAVITY_PARENT, GRAVITY_COMMENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE)]); - if ($only_media) { + if ($request['only_media']) { $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))", Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]); } - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); } - if (!empty($min_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); $params['order'] = ['uri-id']; } - if ($pinned) { + if ($request['pinned']) { $condition = DBA::mergeConditions($condition, ['pinned' => true]); } - if ($exclude_replies) { + if ($request['exclude_replies']) { $condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]); } @@ -113,7 +110,7 @@ class Statuses extends BaseApi } DBA::close($items); - if (!empty($min_id)) { + if (!empty($request['min_id'])) { array_reverse($statuses); } diff --git a/src/Module/Api/Mastodon/Apps.php b/src/Module/Api/Mastodon/Apps.php index c22225160..a0cb250d1 100644 --- a/src/Module/Api/Mastodon/Apps.php +++ b/src/Module/Api/Mastodon/Apps.php @@ -38,42 +38,42 @@ class Apps extends BaseApi */ public static function post(array $parameters = []) { + $request = self::getRequest([ + 'client_name' => '', + 'redirect_uris' => '', + 'scopes' => 'read', + 'website' => '', + ]); + // Workaround for AndStatus, see issue https://github.com/andstatus/andstatus/issues/538 - if (empty($_REQUEST['client_name']) || empty($_REQUEST['redirect_uris'])) { - $postdata = Network::postdata(); - if (!empty($postdata)) { - $_REQUEST = json_decode($postdata, true); - if (empty($_REQUEST)) { - DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Missing parameters')); - } + $postdata = Network::postdata(); + if (!empty($postdata)) { + $postrequest = json_decode($postdata, true); + if (!empty($postrequest) && is_array($postrequest)) { + $request = array_merge($request, $$postrequest); } } - - $name = $_REQUEST['client_name'] ?? ''; - $redirect = $_REQUEST['redirect_uris'] ?? ''; - $scopes = $_REQUEST['scopes'] ?? 'read'; - $website = $_REQUEST['website'] ?? ''; - - if (empty($name) || empty($redirect)) { + + if (empty($request['client_name']) || empty($request['redirect_uris'])) { DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Missing parameters')); } $client_id = bin2hex(random_bytes(32)); $client_secret = bin2hex(random_bytes(32)); - $fields = ['client_id' => $client_id, 'client_secret' => $client_secret, 'name' => $name, 'redirect_uri' => $redirect]; + $fields = ['client_id' => $client_id, 'client_secret' => $client_secret, 'name' => $request['client_name'], 'redirect_uri' => $request['redirect_uris']]; - if (!empty($scopes)) { - $fields['scopes'] = $scopes; + if (!empty($request['scopes'])) { + $fields['scopes'] = $request['scopes']; } - $fields['read'] = (stripos($scopes, self::SCOPE_READ) !== false); - $fields['write'] = (stripos($scopes, self::SCOPE_WRITE) !== false); - $fields['follow'] = (stripos($scopes, self::SCOPE_FOLLOW) !== false); - $fields['push'] = (stripos($scopes, self::SCOPE_PUSH) !== false); + $fields['read'] = (stripos($request['scopes'], self::SCOPE_READ) !== false); + $fields['write'] = (stripos($request['scopes'], self::SCOPE_WRITE) !== false); + $fields['follow'] = (stripos($request['scopes'], self::SCOPE_FOLLOW) !== false); + $fields['push'] = (stripos($request['scopes'], self::SCOPE_PUSH) !== false); - if (!empty($website)) { - $fields['website'] = $website; + if (!empty($request['website'])) { + $fields['website'] = $request['website']; } if (!DBA::insert('application', $fields)) { diff --git a/src/Module/Api/Mastodon/Blocks.php b/src/Module/Api/Mastodon/Blocks.php index b05cb313e..b1d802d05 100644 --- a/src/Module/Api/Mastodon/Blocks.php +++ b/src/Module/Api/Mastodon/Blocks.php @@ -49,23 +49,22 @@ class Blocks extends BaseApi DI::mstdnError()->RecordNotFound(); } - // Return results older than this id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than this id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Maximum number of results. Defaults to 40. - $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit']; + $request = self::getRequest([ + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'limit' => 40, // Maximum number of results. Defaults to 40. + ]); - $params = ['order' => ['cid' => true], 'limit' => $limit]; + $params = ['order' => ['cid' => true], 'limit' => $request['limit']]; $condition = ['cid' => $id, 'blocked' => true, 'uid' => $uid]; - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`cid` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`cid` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]); } if (!empty($min_id)) { diff --git a/src/Module/Api/Mastodon/Bookmarks.php b/src/Module/Api/Mastodon/Bookmarks.php index 88de40d69..90afbc1d3 100644 --- a/src/Module/Api/Mastodon/Bookmarks.php +++ b/src/Module/Api/Mastodon/Bookmarks.php @@ -42,29 +42,28 @@ class Bookmarks extends BaseApi self::login(self::SCOPE_READ); $uid = self::getCurrentUserID(); - // Maximum number of results to return. Defaults to 20. - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; - // Return results older than id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Return results immediately newer than id - $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; + $request = self::getRequest([ + 'limit' => 20, // Maximum number of results to return. Defaults to 20. + 'max_id' => 0, // Return results older than id + 'since_id' => 0, // Return results newer than id + 'min_id' => 0, // Return results immediately newer than id + 'with_muted' => false, // Unknown parameter + ]); - $params = ['order' => ['uri-id' => true], 'limit' => $limit]; + $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $condition = ['pinned' => true, 'uid' => $uid]; - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); } - if (!empty($min_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); $params['order'] = ['uri-id']; } @@ -77,7 +76,7 @@ class Bookmarks extends BaseApi } DBA::close($items); - if (!empty($min_id)) { + if (!empty($request['min_id'])) { array_reverse($statuses); } diff --git a/src/Module/Api/Mastodon/Directory.php b/src/Module/Api/Mastodon/Directory.php index f2849f7f8..115b02a86 100644 --- a/src/Module/Api/Mastodon/Directory.php +++ b/src/Module/Api/Mastodon/Directory.php @@ -42,14 +42,16 @@ class Directory extends BaseApi */ public static function rawContent(array $parameters = []) { - $offset = (int)!isset($_REQUEST['offset']) ? 0 : $_REQUEST['offset']; - $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit']; - $order = $_REQUEST['order'] ?? 'active'; - $local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true'); + $request = self::getRequest([ + 'offset' => 0, // How many accounts to skip before returning results. Default 0. + 'limit' => 40, // How many accounts to load. Default 40. + 'order' => 'active', // active to sort by most recently posted statuses (default) or new to sort by most recently created profiles. + 'local' => false, // Only return local accounts. + ]); - Logger::info('directory', ['offset' => $offset, 'limit' => $limit, 'order' => $order, 'local' => $local]); + Logger::info('directory', ['offset' => $request['offset'], 'limit' => $request['limit'], 'order' => $request['order'], 'local' => $request['local']]); - if ($local) { + if ($request['local']) { $table = 'owner-view'; $condition = ['net-publish' => true]; } else { @@ -57,8 +59,8 @@ class Directory extends BaseApi $condition = ['uid' => 0, 'hidden' => false, 'network' => Protocol::FEDERATED]; } - $params = ['limit' => [$offset, $limit], - 'order' => [($order == 'active') ? 'last-item' : 'created' => true]]; + $params = ['limit' => [$request['offset'], $request['limit']], + 'order' => [($request['order'] == 'active') ? 'last-item' : 'created' => true]]; $accounts = []; $contacts = DBA::select($table, ['id', 'uid'], $condition, $params); diff --git a/src/Module/Api/Mastodon/Favourited.php b/src/Module/Api/Mastodon/Favourited.php index 1c0cf0a34..b5bd78690 100644 --- a/src/Module/Api/Mastodon/Favourited.php +++ b/src/Module/Api/Mastodon/Favourited.php @@ -43,23 +43,23 @@ class Favourited extends BaseApi self::login(self::SCOPE_READ); $uid = self::getCurrentUserID(); - // Maximum number of results to return. Defaults to 20. - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; - // Return results immediately newer than id - $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; - // Return results older than id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; + $request = self::getRequest([ + 'limit' => 20, // Maximum number of results to return. Defaults to 20. + 'min_id' => 0, // Return results immediately newer than id + 'max_id' => 0, // Return results older than id + 'with_muted' => false, // Unknown parameter + ]); - $params = ['order' => ['thr-parent-id' => true], 'limit' => $limit]; + $params = ['order' => ['thr-parent-id' => true], 'limit' => $request['limit']]; $condition = ['gravity' => GRAVITY_ACTIVITY, 'origin' => true, 'verb' => Activity::LIKE, 'uid' => $uid]; - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`thr-parent-id` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`thr-parent-id` < ?", $request['max_id']]); } - if (!empty($min_id)) { - $condition = DBA::mergeConditions($condition, ["`thr-parent-id` > ?", $min_id]); + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`thr-parent-id` > ?", $request['min_id']]); $params['order'] = ['thr-parent-id']; } @@ -72,7 +72,7 @@ class Favourited extends BaseApi } DBA::close($items); - if (!empty($min_id)) { + if (!empty($request['min_id'])) { array_reverse($statuses); } diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php index 3c66b4e6c..5b8e9f092 100644 --- a/src/Module/Api/Mastodon/FollowRequests.php +++ b/src/Module/Api/Mastodon/FollowRequests.php @@ -79,25 +79,27 @@ class FollowRequests extends BaseApi * @param array $parameters * @throws HTTPException\InternalServerErrorException * @throws \ImagickException - * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows + * @see https://docs.joinmastodon.org/methods/accounts/follow_requests/ */ public static function rawContent(array $parameters = []) { self::login(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $min_id = $_GET['min_id'] ?? null; - $max_id = $_GET['max_id'] ?? null; - $limit = intval($_GET['limit'] ?? 40); + $request = self::getRequest([ + 'min_id' => 0, + 'max_id' => 0, + 'limit' => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header. + ]); $baseUrl = DI::baseUrl(); $introductions = DI::intro()->selectByBoundaries( ['`uid` = ? AND NOT `ignore`', $uid], ['order' => ['id' => 'DESC']], - $min_id, - $max_id, - $limit + $request['min_id'], + $request['max_id'], + $request['limit'] ); $return = []; @@ -113,11 +115,11 @@ class FollowRequests extends BaseApi $base_query = []; if (isset($_GET['limit'])) { - $base_query['limit'] = $limit; + $base_query['limit'] = $request['limit']; } $links = []; - if ($introductions->getTotalCount() > $limit) { + if ($introductions->getTotalCount() > $request['limit']) { $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"'; } diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index 1ca2c8359..16b1d9d0f 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -61,30 +61,26 @@ class Accounts extends BaseApi DI::mstdnError()->RecordNotFound(); } - // Return results older than this id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than this id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Maximum number of results. Defaults to 40. Max 40. - // Set to 0 in order to get all accounts without pagination. - $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit']; - + $request = self::getRequest([ + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'limit' => 40, // Maximum number of results. Defaults to 40. Max 40. Set to 0 in order to get all accounts without pagination. + ]); $params = ['order' => ['contact-id' => true]]; - if ($limit != 0) { - $params['limit'] = $limit; - + if ($request['limit'] != 0) { + $params['limit'] = min($request['limit'], 40); } $condition = ['gid' => $id]; - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $request['since_id']]); } if (!empty($min_id)) { diff --git a/src/Module/Api/Mastodon/Mutes.php b/src/Module/Api/Mastodon/Mutes.php index 9e53da504..f3aca86fa 100644 --- a/src/Module/Api/Mastodon/Mutes.php +++ b/src/Module/Api/Mastodon/Mutes.php @@ -49,23 +49,22 @@ class Mutes extends BaseApi DI::mstdnError()->RecordNotFound(); } - // Return results older than this id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than this id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Maximum number of results. Defaults to 40. - $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit']; + $request = self::getRequest([ + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'limit' => 40, // Maximum number of results. Defaults to 40. + ]); - $params = ['order' => ['cid' => true], 'limit' => $limit]; + $params = ['order' => ['cid' => true], 'limit' => $request['limit']]; $condition = ['cid' => $id, 'ignored' => true, 'uid' => $uid]; - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`cid` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`cid` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]); } if (!empty($min_id)) { diff --git a/src/Module/Api/Mastodon/Notifications.php b/src/Module/Api/Mastodon/Notifications.php index 3dfcaa668..6704e958f 100644 --- a/src/Module/Api/Mastodon/Notifications.php +++ b/src/Module/Api/Mastodon/Notifications.php @@ -50,64 +50,51 @@ 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]); + $request = self::getRequest([ + 'max_id' => 0, // Return results older than this ID + 'since_id' => 0, // Return results newer than this ID + 'min_id' => 0, // Return results immediately newer than this ID + 'limit' => 20, // Maximum number of results to return (default 20) + 'exclude_types' => [], // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request) + 'account_id' => 0, // Return only notifications received from this account + 'with_muted' => false, // Unknown parameter + 'count' => 0, // Unknown parameter + ]); - // Return results older than this ID - $max_id = $request['max_id']; - - // Return results newer than this ID - $since_id = $request['since_id']; - - // Return results immediately newer than this ID - $min_id = $request['min_id']; - - // Maximum number of results to return (default 20) - $limit = $request['limit']; - - // Array of types to exclude (follow, favourite, reblog, mention, poll, follow_request) - $exclude_types = $request['exclude_types']; - - // Return only notifications received from this account - $account_id = $request['account_id']; - - // Unknown parameter - $with_muted = $request['with_muted']; - - $params = ['order' => ['id' => true], 'limit' => $limit]; + $params = ['order' => ['id' => true], 'limit' => $request['limit']]; $condition = ['uid' => $uid, 'seen' => false, 'type' => []]; - if (!empty($account_id)) { - $contact = Contact::getById($account_id, ['url']); + if (!empty($request['account_id'])) { + $contact = Contact::getById($request['account_id'], ['url']); if (!empty($contact['url'])) { $condition['url'] = $contact['url']; } } - if (!in_array('follow_request', $exclude_types)) { + if (!in_array('follow_request', $request['exclude_types'])) { $condition['type'] = array_merge($condition['type'], [Notification\Type::INTRO]); } - if (!in_array('mention', $exclude_types)) { + if (!in_array('mention', $request['exclude_types'])) { $condition['type'] = array_merge($condition['type'], [Notification\Type::WALL, Notification\Type::COMMENT, Notification\Type::MAIL, Notification\Type::TAG_SELF, Notification\Type::POKE]); } - if (!in_array('status', $exclude_types)) { + if (!in_array('status', $request['exclude_types'])) { $condition['type'] = array_merge($condition['type'], [Notification\Type::SHARE]); } - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]); } - if (!empty($min_id)) { - $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]); + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]); $params['order'] = ['id']; } @@ -119,7 +106,7 @@ class Notifications extends BaseApi $notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']); } - if (!empty($min_id)) { + if (!empty($request['min_id'])) { array_reverse($notifications); } diff --git a/src/Module/Api/Mastodon/Timelines/Home.php b/src/Module/Api/Mastodon/Timelines/Home.php index 9c8cb7a0b..07a5bc7e6 100644 --- a/src/Module/Api/Mastodon/Timelines/Home.php +++ b/src/Module/Api/Mastodon/Timelines/Home.php @@ -42,35 +42,33 @@ class Home extends BaseApi self::login(self::SCOPE_READ); $uid = self::getCurrentUserID(); - // Return results older than id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Return results immediately newer than id - $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; - // Maximum number of results to return. Defaults to 20. - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; - // Return only local statuses? Defaults to false. - $local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true'); + $request = self::getRequest([ + 'max_id' => 0, // Return results older than id + 'since_id' => 0, // Return results newer than id + 'min_id' => 0, // Return results immediately newer than id + 'limit' => 20, // Maximum number of results to return. Defaults to 20. + 'local' => false, // Return only local statuses? Defaults to false. + 'with_muted' => false, // Unknown parameter + ]); - $params = ['order' => ['uri-id' => true], 'limit' => $limit]; + $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'uid' => $uid]; - if ($local) { + if ($request['local']) { $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); } - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); } - if (!empty($min_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); $params['order'] = ['uri-id']; } @@ -83,7 +81,7 @@ class Home extends BaseApi } DBA::close($items); - if (!empty($min_id)) { + if (!empty($request['min_id'])) { array_reverse($statuses); } diff --git a/src/Module/Api/Mastodon/Timelines/ListTimeline.php b/src/Module/Api/Mastodon/Timelines/ListTimeline.php index f046cab78..f50c2d2aa 100644 --- a/src/Module/Api/Mastodon/Timelines/ListTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/ListTimeline.php @@ -46,30 +46,29 @@ class ListTimeline extends BaseApi DI::mstdnError()->UnprocessableEntity(); } - // Return results older than id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Return results immediately newer than id - $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; - // Maximum number of results to return. Defaults to 20. - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + $request = self::getRequest([ + 'max_id' => 0, // Return results older than id + 'since_id' => 0, // Return results newer than id + 'min_id' => 0, // Return results immediately newer than id + 'limit' => 20, // Maximum number of results to return. Defaults to 20. + 'with_muted' => false, // Unknown parameter + ]); - $params = ['order' => ['uri-id' => true], 'limit' => $limit]; + $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", $uid, GRAVITY_PARENT, GRAVITY_COMMENT, $parameters['id']]; - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); } - if (!empty($min_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); $params['order'] = ['uri-id']; } @@ -82,7 +81,7 @@ class ListTimeline extends BaseApi } DBA::close($items); - if (!empty($min_id)) { + if (!empty($request['min_id'])) { array_reverse($statuses); } diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 487b45e34..fc5227778 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -41,49 +41,45 @@ class PublicTimeline extends BaseApi */ public static function rawContent(array $parameters = []) { - // Show only local statuses? Defaults to false. - $local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true'); - // Show only remote statuses? Defaults to false. - $remote = (bool)!isset($_REQUEST['remote']) ? false : ($_REQUEST['remote'] == 'true'); - // Show only statuses with media attached? Defaults to false. - $only_media = (bool)!isset($_REQUEST['only_media']) ? false : ($_REQUEST['only_media'] == 'true'); - // Return results older than this id - $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; - // Return results newer than this id - $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; - // Return results immediately newer than this id - $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; - // Maximum number of results to return. Defaults to 20. - $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + $request = self::getRequest([ + 'local' => false, // Show only local statuses? Defaults to false. + 'remote' => false, // Show only remote statuses? Defaults to false. + 'only_media' => false, // Show only statuses with media attached? Defaults to false. + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'min_id' => 0, // Return results immediately newer than this id + 'limit' => 20, // Maximum number of results to return. Defaults to 20. + 'with_muted' => false, // Unknown parameter + ]); - $params = ['order' => ['uri-id' => true], 'limit' => $limit]; + $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']]; $condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => Item::PUBLIC, 'uid' => 0, 'network' => Protocol::FEDERATED]; - if ($local) { + if ($request['local']) { $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); } - if ($remote) { + if ($request['remote']) { $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); } - if ($only_media) { + if ($request['only_media']) { $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))", Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]); } - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); } - if (!empty($since_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); } - if (!empty($min_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); $params['order'] = ['uri-id']; } @@ -95,7 +91,7 @@ class PublicTimeline extends BaseApi } DBA::close($items); - if (!empty($min_id)) { + if (!empty($request['min_id'])) { array_reverse($statuses); }