Add order/limit building func and fix reverse order requirement on min_id

This commit is contained in:
Hank Grabowski 2023-02-22 11:27:54 -05:00
parent 9c5f3ae58f
commit a67fe069e8
2 changed files with 37 additions and 6 deletions

View File

@ -57,11 +57,12 @@ class Home extends BaseApi
'friendica_order' => TimelineOrderByTypes::ID,
], $request);
$params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
$condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'uid' => $uid];
$condition = $this->addPagingConditions($request, $condition);
$params = $this->buildOrderAndLimitParams($request);
if ($request['local']) {
$condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);

View File

@ -105,12 +105,11 @@ class BaseApi extends BaseModule
}
/**
* Processes data from GET requests and sets defaults
* Processes data from GET requests and sets paging conditions
*
* @param array $defaults Associative array of expected request keys and their default typed value. A null
* value will remove the request key from the resulting value array.
* @param array $request Custom REQUEST array, superglobal instead
* @return array request data
* @param array $request Custom REQUEST array
* @param array $condition Existing conditions to merge
* @return array paging data condition parameters data
* @throws \Exception
*/
public function addPagingConditions(array $request, array $condition): array
@ -151,6 +150,37 @@ class BaseApi extends BaseModule
return $condition;
}
/**
* Processes data from GET requests and sets paging conditions
*
* @param array $request Custom REQUEST array
* @param array $params Existing $params element to build on
* @return array ordering data added to the params blocks that was passed in
* @throws \Exception
*/
public function buildOrderAndLimitParams(array $request, array $params = []): array
{
$requested_order = $request['friendica_order'];
switch ($requested_order) {
case TimelineOrderByTypes::CREATED:
$order_field = 'created';
break;
case TimelineOrderByTypes::ID:
default:
$order_field = 'uri-id';
}
if(!empty($request['min_id'])) {
$params['order'] = [$order_field];
} else {
$params['order'] = [$order_field => true];
}
$params['limit']= $request['limit'];
return $params;
}
/**
* Processes data from GET requests and sets defaults
*