API: Reworked request parameter handling

This commit is contained in:
Michael 2021-05-18 19:26:46 +00:00
parent dbd349b92e
commit e5c1b90668
16 changed files with 233 additions and 265 deletions

View File

@ -49,24 +49,22 @@ class Followers extends BaseApi
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->RecordNotFound();
} }
// Return results older than this id $request = self::getRequest([
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'max_id' => 0, // Return results older than this id
// Return results newer than this id 'since_id' => 0, // Return results newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'limit' => 20, // Maximum number of results to return. Defaults to 20.
// Maximum number of results to return. Defaults to 20. ]);
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
$params = ['order' => ['cid' => true], 'limit' => $request['limit']];
$params = ['order' => ['cid' => true], 'limit' => $limit];
$condition = ['relation-cid' => $id, 'follows' => true]; $condition = ['relation-cid' => $id, 'follows' => true];
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($min_id)) {

View File

@ -49,24 +49,22 @@ class Following extends BaseApi
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->RecordNotFound();
} }
// Return results older than this id $request = self::getRequest([
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'max_id' => 0, // Return results older than this id
// Return results newer than this id 'since_id' => 0, // Return results newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'limit' => 20, // Maximum number of results to return. Defaults to 20.
// Maximum number of results to return. Defaults to 20. ]);
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
$params = ['order' => ['relation-cid' => true], 'limit' => $request['limit']];
$params = ['order' => ['relation-cid' => true], 'limit' => $limit];
$condition = ['cid' => $id, 'follows' => true]; $condition = ['cid' => $id, 'follows' => true];
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($min_id)) {

View File

@ -43,34 +43,32 @@ class Search extends BaseApi
self::login(self::SCOPE_READ); self::login(self::SCOPE_READ);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
// What to search for $request = self::getRequest([
$q = $_REQUEST['q'] ?? ''; 'q' => '', // What to search for
// Maximum number of results. Defaults to 40. 'limit' => 40, // Maximum number of results. Defaults to 40.
$limit = (int)($_REQUEST['limit'] ?? 40); 'resolve' => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address.
// 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.
$resolve = ($_REQUEST['resolve'] ?? '') == 'true'; ]);
// Only who the user is following. Defaults to false.
$following = ($_REQUEST['following'] ?? '') == 'true';
$accounts = []; $accounts = [];
if (!$following) { if (!$request['following']) {
if ((strrpos($q, '@') > 0) && $resolve) { if ((strrpos($request['q'], '@') > 0) && $request['resolve']) {
$results = CoreSearch::getContactsFromProbe($q); $results = CoreSearch::getContactsFromProbe($request['q']);
} }
if (empty($results)) { if (empty($results)) {
if (DI::config()->get('system', 'poco_local_search')) { 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'))) { } 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)) { if (!empty($results)) {
$counter = 0; $counter = 0;
foreach ($results->getResults() as $result) { foreach ($results->getResults() as $result) {
if (++$counter > $limit) { if (++$counter > $request['limit']) {
continue; continue;
} }
if ($result instanceof ContactResult) { if ($result instanceof ContactResult) {
@ -81,14 +79,14 @@ class Search extends BaseApi
} }
} }
} else { } else {
$contacts = Contact::searchByName($q, '', $uid); $contacts = Contact::searchByName($request['q'], '', $uid);
$counter = 0; $counter = 0;
foreach ($contacts as $contact) { foreach ($contacts as $contact) {
if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) { if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
continue; continue;
} }
if (++$counter > $limit) { if (++$counter > $request['limit']) {
continue; continue;
} }
$accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid); $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);

View File

@ -51,21 +51,18 @@ class Statuses extends BaseApi
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->RecordNotFound();
} }
// Show only statuses with media attached? Defaults to false. $request = self::getRequest([
$only_media = (bool)!isset($_REQUEST['only_media']) ? false : ($_REQUEST['only_media'] == 'true'); 'only_media' => false, // Show only statuses with media attached? Defaults to false.
// Return results older than this id 'max_id' => 0, // Return results older than this id
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'since_id' => 0, // Return results newer than this id
// Return results newer than this id 'min_id' => 0, // Return results immediately newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'limit' => 20, // Maximum number of results to return. Defaults to 20.
// Return results immediately newer than this id 'pinned' => false, // Only pinned posts
$min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; 'exclude_replies' => false, // Don't show comments
// Maximum number of results to return. Defaults to 20. 'with_muted' => false, // Unknown parameter
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; ]);
$pinned = (bool)!isset($_REQUEST['pinned']) ? false : ($_REQUEST['pinned'] == 'true'); $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
$exclude_replies = (bool)!isset($_REQUEST['exclude_replies']) ? false : ($_REQUEST['exclude_replies'] == 'true');
$params = ['order' => ['uri-id' => true], 'limit' => $limit];
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
@ -79,29 +76,29 @@ class Statuses extends BaseApi
$condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ?))", $condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ?))",
GRAVITY_PARENT, GRAVITY_COMMENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE)]); 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 (?, ?, ?))", $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]); Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
} }
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
$params['order'] = ['uri-id']; $params['order'] = ['uri-id'];
} }
if ($pinned) { if ($request['pinned']) {
$condition = DBA::mergeConditions($condition, ['pinned' => true]); $condition = DBA::mergeConditions($condition, ['pinned' => true]);
} }
if ($exclude_replies) { if ($request['exclude_replies']) {
$condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]); $condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]);
} }
@ -113,7 +110,7 @@ class Statuses extends BaseApi
} }
DBA::close($items); DBA::close($items);
if (!empty($min_id)) { if (!empty($request['min_id'])) {
array_reverse($statuses); array_reverse($statuses);
} }

View File

@ -38,42 +38,42 @@ class Apps extends BaseApi
*/ */
public static function post(array $parameters = []) 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 // Workaround for AndStatus, see issue https://github.com/andstatus/andstatus/issues/538
if (empty($_REQUEST['client_name']) || empty($_REQUEST['redirect_uris'])) {
$postdata = Network::postdata(); $postdata = Network::postdata();
if (!empty($postdata)) { if (!empty($postdata)) {
$_REQUEST = json_decode($postdata, true); $postrequest = json_decode($postdata, true);
if (empty($_REQUEST)) { if (!empty($postrequest) && is_array($postrequest)) {
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Missing parameters')); $request = array_merge($request, $$postrequest);
}
} }
} }
$name = $_REQUEST['client_name'] ?? ''; if (empty($request['client_name']) || empty($request['redirect_uris'])) {
$redirect = $_REQUEST['redirect_uris'] ?? '';
$scopes = $_REQUEST['scopes'] ?? 'read';
$website = $_REQUEST['website'] ?? '';
if (empty($name) || empty($redirect)) {
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Missing parameters')); DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Missing parameters'));
} }
$client_id = bin2hex(random_bytes(32)); $client_id = bin2hex(random_bytes(32));
$client_secret = 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)) { if (!empty($request['scopes'])) {
$fields['scopes'] = $scopes; $fields['scopes'] = $request['scopes'];
} }
$fields['read'] = (stripos($scopes, self::SCOPE_READ) !== false); $fields['read'] = (stripos($request['scopes'], self::SCOPE_READ) !== false);
$fields['write'] = (stripos($scopes, self::SCOPE_WRITE) !== false); $fields['write'] = (stripos($request['scopes'], self::SCOPE_WRITE) !== false);
$fields['follow'] = (stripos($scopes, self::SCOPE_FOLLOW) !== false); $fields['follow'] = (stripos($request['scopes'], self::SCOPE_FOLLOW) !== false);
$fields['push'] = (stripos($scopes, self::SCOPE_PUSH) !== false); $fields['push'] = (stripos($request['scopes'], self::SCOPE_PUSH) !== false);
if (!empty($website)) { if (!empty($request['website'])) {
$fields['website'] = $website; $fields['website'] = $request['website'];
} }
if (!DBA::insert('application', $fields)) { if (!DBA::insert('application', $fields)) {

View File

@ -49,23 +49,22 @@ class Blocks extends BaseApi
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->RecordNotFound();
} }
// Return results older than this id $request = self::getRequest([
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'max_id' => 0, // Return results older than this id
// Return results newer than this id 'since_id' => 0, // Return results newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'limit' => 40, // Maximum number of results. Defaults to 40.
// Maximum number of results. Defaults to 40. ]);
$limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
$params = ['order' => ['cid' => true], 'limit' => $limit]; $params = ['order' => ['cid' => true], 'limit' => $request['limit']];
$condition = ['cid' => $id, 'blocked' => true, 'uid' => $uid]; $condition = ['cid' => $id, 'blocked' => true, 'uid' => $uid];
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($min_id)) {

View File

@ -42,29 +42,28 @@ class Bookmarks extends BaseApi
self::login(self::SCOPE_READ); self::login(self::SCOPE_READ);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
// Maximum number of results to return. Defaults to 20. $request = self::getRequest([
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; 'limit' => 20, // Maximum number of results to return. Defaults to 20.
// Return results older than id 'max_id' => 0, // Return results older than id
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'since_id' => 0, // Return results newer than id
// Return results newer than id 'min_id' => 0, // Return results immediately newer than id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'with_muted' => false, // Unknown parameter
// Return results immediately newer than id ]);
$min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id'];
$params = ['order' => ['uri-id' => true], 'limit' => $limit]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
$condition = ['pinned' => true, 'uid' => $uid]; $condition = ['pinned' => true, 'uid' => $uid];
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
$params['order'] = ['uri-id']; $params['order'] = ['uri-id'];
} }
@ -77,7 +76,7 @@ class Bookmarks extends BaseApi
} }
DBA::close($items); DBA::close($items);
if (!empty($min_id)) { if (!empty($request['min_id'])) {
array_reverse($statuses); array_reverse($statuses);
} }

View File

@ -42,14 +42,16 @@ class Directory extends BaseApi
*/ */
public static function rawContent(array $parameters = []) public static function rawContent(array $parameters = [])
{ {
$offset = (int)!isset($_REQUEST['offset']) ? 0 : $_REQUEST['offset']; $request = self::getRequest([
$limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit']; 'offset' => 0, // How many accounts to skip before returning results. Default 0.
$order = $_REQUEST['order'] ?? 'active'; 'limit' => 40, // How many accounts to load. Default 40.
$local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true'); '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'; $table = 'owner-view';
$condition = ['net-publish' => true]; $condition = ['net-publish' => true];
} else { } else {
@ -57,8 +59,8 @@ class Directory extends BaseApi
$condition = ['uid' => 0, 'hidden' => false, 'network' => Protocol::FEDERATED]; $condition = ['uid' => 0, 'hidden' => false, 'network' => Protocol::FEDERATED];
} }
$params = ['limit' => [$offset, $limit], $params = ['limit' => [$request['offset'], $request['limit']],
'order' => [($order == 'active') ? 'last-item' : 'created' => true]]; 'order' => [($request['order'] == 'active') ? 'last-item' : 'created' => true]];
$accounts = []; $accounts = [];
$contacts = DBA::select($table, ['id', 'uid'], $condition, $params); $contacts = DBA::select($table, ['id', 'uid'], $condition, $params);

View File

@ -43,23 +43,23 @@ class Favourited extends BaseApi
self::login(self::SCOPE_READ); self::login(self::SCOPE_READ);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
// Maximum number of results to return. Defaults to 20. $request = self::getRequest([
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; 'limit' => 20, // Maximum number of results to return. Defaults to 20.
// Return results immediately newer than id 'min_id' => 0, // Return results immediately newer than id
$min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; 'max_id' => 0, // Return results older than id
// Return results older than id 'with_muted' => false, // Unknown parameter
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; ]);
$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]; $condition = ['gravity' => GRAVITY_ACTIVITY, 'origin' => true, 'verb' => Activity::LIKE, 'uid' => $uid];
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`thr-parent-id` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`thr-parent-id` < ?", $request['max_id']]);
} }
if (!empty($min_id)) { if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`thr-parent-id` > ?", $min_id]); $condition = DBA::mergeConditions($condition, ["`thr-parent-id` > ?", $request['min_id']]);
$params['order'] = ['thr-parent-id']; $params['order'] = ['thr-parent-id'];
} }
@ -72,7 +72,7 @@ class Favourited extends BaseApi
} }
DBA::close($items); DBA::close($items);
if (!empty($min_id)) { if (!empty($request['min_id'])) {
array_reverse($statuses); array_reverse($statuses);
} }

View File

@ -79,25 +79,27 @@ class FollowRequests extends BaseApi
* @param array $parameters * @param array $parameters
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @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 = []) public static function rawContent(array $parameters = [])
{ {
self::login(self::SCOPE_READ); self::login(self::SCOPE_READ);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
$min_id = $_GET['min_id'] ?? null; $request = self::getRequest([
$max_id = $_GET['max_id'] ?? null; 'min_id' => 0,
$limit = intval($_GET['limit'] ?? 40); 'max_id' => 0,
'limit' => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header.
]);
$baseUrl = DI::baseUrl(); $baseUrl = DI::baseUrl();
$introductions = DI::intro()->selectByBoundaries( $introductions = DI::intro()->selectByBoundaries(
['`uid` = ? AND NOT `ignore`', $uid], ['`uid` = ? AND NOT `ignore`', $uid],
['order' => ['id' => 'DESC']], ['order' => ['id' => 'DESC']],
$min_id, $request['min_id'],
$max_id, $request['max_id'],
$limit $request['limit']
); );
$return = []; $return = [];
@ -113,11 +115,11 @@ class FollowRequests extends BaseApi
$base_query = []; $base_query = [];
if (isset($_GET['limit'])) { if (isset($_GET['limit'])) {
$base_query['limit'] = $limit; $base_query['limit'] = $request['limit'];
} }
$links = []; $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"'; $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"';
} }

View File

@ -61,30 +61,26 @@ class Accounts extends BaseApi
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->RecordNotFound();
} }
// Return results older than this id $request = self::getRequest([
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'max_id' => 0, // Return results older than this id
// Return results newer than this id 'since_id' => 0, // Return results newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'limit' => 40, // Maximum number of results. Defaults to 40. Max 40. Set to 0 in order to get all accounts without pagination.
// 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'];
$params = ['order' => ['contact-id' => true]]; $params = ['order' => ['contact-id' => true]];
if ($limit != 0) { if ($request['limit'] != 0) {
$params['limit'] = $limit; $params['limit'] = min($request['limit'], 40);
} }
$condition = ['gid' => $id]; $condition = ['gid' => $id];
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($min_id)) {

View File

@ -49,23 +49,22 @@ class Mutes extends BaseApi
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->RecordNotFound();
} }
// Return results older than this id $request = self::getRequest([
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'max_id' => 0, // Return results older than this id
// Return results newer than this id 'since_id' => 0, // Return results newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'limit' => 40, // Maximum number of results. Defaults to 40.
// Maximum number of results. Defaults to 40. ]);
$limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
$params = ['order' => ['cid' => true], 'limit' => $limit]; $params = ['order' => ['cid' => true], 'limit' => $request['limit']];
$condition = ['cid' => $id, 'ignored' => true, 'uid' => $uid]; $condition = ['cid' => $id, 'ignored' => true, 'uid' => $uid];
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($min_id)) {

View File

@ -50,64 +50,51 @@ class Notifications extends BaseApi
System::jsonExit(DI::mstdnNotification()->createFromNotifyId($id)); System::jsonExit(DI::mstdnNotification()->createFromNotifyId($id));
} }
$request = self::getRequest(['max_id' => 0, 'since_id' => 0, 'min_id' => 0, 'limit' => 20, $request = self::getRequest([
'exclude_types' => [], 'account_id' => 0, 'with_muted' => 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 (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 $params = ['order' => ['id' => true], 'limit' => $request['limit']];
$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];
$condition = ['uid' => $uid, 'seen' => false, 'type' => []]; $condition = ['uid' => $uid, 'seen' => false, 'type' => []];
if (!empty($account_id)) { if (!empty($request['account_id'])) {
$contact = Contact::getById($account_id, ['url']); $contact = Contact::getById($request['account_id'], ['url']);
if (!empty($contact['url'])) { if (!empty($contact['url'])) {
$condition['url'] = $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]); $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'], $condition['type'] = array_merge($condition['type'],
[Notification\Type::WALL, Notification\Type::COMMENT, Notification\Type::MAIL, Notification\Type::TAG_SELF, Notification\Type::POKE]); [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]); $condition['type'] = array_merge($condition['type'], [Notification\Type::SHARE]);
} }
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`id` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]); $condition = DBA::mergeConditions($condition, ["`id` > ?", $request['min_id']]);
$params['order'] = ['id']; $params['order'] = ['id'];
} }
@ -119,7 +106,7 @@ class Notifications extends BaseApi
$notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']); $notifications[] = DI::mstdnNotification()->createFromNotifyId($notification['id']);
} }
if (!empty($min_id)) { if (!empty($request['min_id'])) {
array_reverse($notifications); array_reverse($notifications);
} }

View File

@ -42,35 +42,33 @@ class Home extends BaseApi
self::login(self::SCOPE_READ); self::login(self::SCOPE_READ);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
// Return results older than id $request = self::getRequest([
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'max_id' => 0, // Return results older than id
// Return results newer than id 'since_id' => 0, // Return results newer than id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'min_id' => 0, // Return results immediately newer than id
// Return results immediately newer than id 'limit' => 20, // Maximum number of results to return. Defaults to 20.
$min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; 'local' => false, // Return only local statuses? Defaults to false.
// Maximum number of results to return. Defaults to 20. 'with_muted' => false, // Unknown parameter
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; ]);
// Return only local statuses? Defaults to false.
$local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true');
$params = ['order' => ['uri-id' => true], 'limit' => $limit]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
$condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'uid' => $uid]; $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`)"]); $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
} }
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
$params['order'] = ['uri-id']; $params['order'] = ['uri-id'];
} }
@ -83,7 +81,7 @@ class Home extends BaseApi
} }
DBA::close($items); DBA::close($items);
if (!empty($min_id)) { if (!empty($request['min_id'])) {
array_reverse($statuses); array_reverse($statuses);
} }

View File

@ -46,30 +46,29 @@ class ListTimeline extends BaseApi
DI::mstdnError()->UnprocessableEntity(); DI::mstdnError()->UnprocessableEntity();
} }
// Return results older than id $request = self::getRequest([
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'max_id' => 0, // Return results older than id
// Return results newer than id 'since_id' => 0, // Return results newer than id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; 'min_id' => 0, // Return results immediately newer than id
// Return results immediately newer than id 'limit' => 20, // Maximum number of results to return. Defaults to 20.
$min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; 'with_muted' => false, // Unknown parameter
// Maximum number of results to return. Defaults to 20. ]);
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
$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` = ?)", $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)",
$uid, GRAVITY_PARENT, GRAVITY_COMMENT, $parameters['id']]; $uid, GRAVITY_PARENT, GRAVITY_COMMENT, $parameters['id']];
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
$params['order'] = ['uri-id']; $params['order'] = ['uri-id'];
} }
@ -82,7 +81,7 @@ class ListTimeline extends BaseApi
} }
DBA::close($items); DBA::close($items);
if (!empty($min_id)) { if (!empty($request['min_id'])) {
array_reverse($statuses); array_reverse($statuses);
} }

View File

@ -41,49 +41,45 @@ class PublicTimeline extends BaseApi
*/ */
public static function rawContent(array $parameters = []) public static function rawContent(array $parameters = [])
{ {
// Show only local statuses? Defaults to false. $request = self::getRequest([
$local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true'); 'local' => false, // Show only local statuses? Defaults to false.
// Show only remote statuses? Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false.
$remote = (bool)!isset($_REQUEST['remote']) ? false : ($_REQUEST['remote'] == 'true'); 'only_media' => false, // Show only statuses with media attached? Defaults to false.
// Show only statuses with media attached? Defaults to false. 'max_id' => 0, // Return results older than this id
$only_media = (bool)!isset($_REQUEST['only_media']) ? false : ($_REQUEST['only_media'] == 'true'); 'since_id' => 0, // Return results newer than this id
// Return results older than this id 'min_id' => 0, // Return results immediately newer than this id
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; 'limit' => 20, // Maximum number of results to return. Defaults to 20.
// Return results newer than this id 'with_muted' => false, // Unknown parameter
$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'];
$params = ['order' => ['uri-id' => true], 'limit' => $limit]; $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
$condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => Item::PUBLIC, $condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => Item::PUBLIC,
'uid' => 0, 'network' => Protocol::FEDERATED]; '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`)"]); $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`)"]); $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 (?, ?, ?))", $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]); Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
} }
if (!empty($max_id)) { if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
} }
if (!empty($since_id)) { if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
} }
if (!empty($min_id)) { if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
$params['order'] = ['uri-id']; $params['order'] = ['uri-id'];
} }
@ -95,7 +91,7 @@ class PublicTimeline extends BaseApi
} }
DBA::close($items); DBA::close($items);
if (!empty($min_id)) { if (!empty($request['min_id'])) {
array_reverse($statuses); array_reverse($statuses);
} }