Central function to process request parameters

This commit is contained in:
Michael 2021-05-18 06:31:22 +00:00
parent 32e7ddf21b
commit ce427b248e
3 changed files with 63 additions and 16 deletions

View file

@ -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];

View file

@ -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' => []];

View file

@ -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
*