mirror of
https://github.com/friendica/friendica
synced 2025-05-11 11:44:12 +02:00
API: Reworked request parameter handling
This commit is contained in:
parent
dbd349b92e
commit
e5c1b90668
16 changed files with 233 additions and 265 deletions
|
@ -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)) {
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue