kopia lustrzana https://github.com/pixelfed/pixelfed
Merge pull request #5863 from pixelfed/staging
Update ApiV1Controller, add pagination to conversations endpoint with…pull/5871/head
commit
1cbfd98ac4
|
|
@ -55,6 +55,7 @@
|
|||
- Update snowflake config, allow custom datacenter/worker ids ([806e210f1](https://github.com/pixelfed/pixelfed/commit/806e210f1))
|
||||
- Update ApiV1Controller, return empty statuses feed for private accounts instead of 403 response ([cce657d9c](https://github.com/pixelfed/pixelfed/commit/cce657d9c))
|
||||
- Update DM config, allow new users to send DMs by default, with a new env variable to enforce a 72h limit ([717f17cde](https://github.com/pixelfed/pixelfed/commit/717f17cde))
|
||||
- Update ApiV1Controller, add pagination to conversations endpoint with min/max/since id pagination and link header support ([244e86bad](https://github.com/pixelfed/pixelfed/commit/244e86bad))
|
||||
- ([](https://github.com/pixelfed/pixelfed/commit/))
|
||||
|
||||
## [v0.12.4 (2024-11-08)](https://github.com/pixelfed/pixelfed/compare/v0.12.4...dev)
|
||||
|
|
|
|||
|
|
@ -3007,13 +3007,22 @@ class ApiV1Controller extends Controller
|
|||
abort_unless($request->user()->tokenCan('read'), 403);
|
||||
|
||||
$this->validate($request, [
|
||||
'limit' => 'min:1|max:40',
|
||||
'limit' => 'sometimes|integer|min:1|max:40',
|
||||
'scope' => 'nullable|in:inbox,sent,requests',
|
||||
'min_id' => 'nullable|integer',
|
||||
'max_id' => 'nullable|integer',
|
||||
'since_id' => 'nullable|integer',
|
||||
]);
|
||||
|
||||
$limit = $request->input('limit', 20);
|
||||
$limit = $request->input('limit', 10);
|
||||
if ($limit > 20) {
|
||||
$limit = 20;
|
||||
}
|
||||
$scope = $request->input('scope', 'inbox');
|
||||
$user = $request->user();
|
||||
$min_id = $request->input('min_id');
|
||||
$max_id = $request->input('max_id');
|
||||
$since_id = $request->input('since_id');
|
||||
|
||||
if ($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id)) {
|
||||
return [];
|
||||
|
|
@ -3021,7 +3030,9 @@ class ApiV1Controller extends Controller
|
|||
|
||||
$pid = $user->profile_id;
|
||||
|
||||
if (config('database.default') == 'pgsql') {
|
||||
$isPgsql = config('database.default') == 'pgsql';
|
||||
|
||||
if ($isPgsql) {
|
||||
$dms = DirectMessage::when($scope === 'inbox', function ($q) use ($pid) {
|
||||
return $q->whereIsHidden(false)
|
||||
->where(function ($query) use ($pid) {
|
||||
|
|
@ -3057,9 +3068,27 @@ class ApiV1Controller extends Controller
|
|||
});
|
||||
}
|
||||
|
||||
$dms = $dms->orderByDesc('status_id')
|
||||
->simplePaginate($limit)
|
||||
->map(function ($dm) use ($pid) {
|
||||
if ($min_id) {
|
||||
$dms = $dms->where('id', '>', $min_id);
|
||||
}
|
||||
if ($max_id) {
|
||||
$dms = $dms->where('id', '<', $max_id);
|
||||
}
|
||||
if ($since_id) {
|
||||
$dms = $dms->where('id', '>', $since_id);
|
||||
}
|
||||
|
||||
$dms = $dms->orderByDesc('status_id')->orderBy('id');
|
||||
|
||||
$dmResults = $dms->limit($limit + 1)->get();
|
||||
|
||||
$hasNextPage = $dmResults->count() > $limit;
|
||||
|
||||
if ($hasNextPage) {
|
||||
$dmResults = $dmResults->take($limit);
|
||||
}
|
||||
|
||||
$transformedDms = $dmResults->map(function ($dm) use ($pid) {
|
||||
$from = $pid == $dm->to_id ? $dm->from_id : $dm->to_id;
|
||||
|
||||
return [
|
||||
|
|
@ -3084,7 +3113,37 @@ class ApiV1Controller extends Controller
|
|||
})
|
||||
->values();
|
||||
|
||||
return $this->json($dms);
|
||||
$links = [];
|
||||
|
||||
if (! $transformedDms->isEmpty()) {
|
||||
$baseUrl = url()->current().'?'.http_build_query(array_merge(
|
||||
$request->except(['min_id', 'max_id', 'since_id']),
|
||||
['limit' => $limit]
|
||||
));
|
||||
|
||||
$firstId = $transformedDms->first()['id'];
|
||||
$lastId = $transformedDms->last()['id'];
|
||||
|
||||
$firstLink = $baseUrl;
|
||||
$links[] = '<'.$firstLink.'>; rel="first"';
|
||||
|
||||
if ($hasNextPage) {
|
||||
$nextLink = $baseUrl.'&max_id='.$lastId;
|
||||
$links[] = '<'.$nextLink.'>; rel="next"';
|
||||
}
|
||||
|
||||
if ($max_id || $since_id) {
|
||||
$prevLink = $baseUrl.'&min_id='.$firstId;
|
||||
$links[] = '<'.$prevLink.'>; rel="prev"';
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($links)) {
|
||||
return response()->json($transformedDms->toArray())
|
||||
->header('Link', implode(', ', $links));
|
||||
}
|
||||
|
||||
return $this->json($transformedDms);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue