mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-29 17:53:16 +00:00
Update ApiV1Controller, add cursor pagination and pagination link headers to account/{id}/followers and account/{id}/following endpoints with legacy support for page=
simple pagination
This commit is contained in:
parent
eb2fe9d05b
commit
713aa5fd7d
1 changed files with 100 additions and 14 deletions
|
@ -467,6 +467,10 @@ class ApiV1Controller extends Controller
|
||||||
$account = AccountService::get($id);
|
$account = AccountService::get($id);
|
||||||
abort_if(!$account, 404);
|
abort_if(!$account, 404);
|
||||||
$pid = $request->user()->profile_id;
|
$pid = $request->user()->profile_id;
|
||||||
|
$this->validate($request, [
|
||||||
|
'limit' => 'sometimes|integer|min:1|max:80'
|
||||||
|
]);
|
||||||
|
$limit = $request->input('limit', 10);
|
||||||
|
|
||||||
if(intval($pid) !== intval($account['id'])) {
|
if(intval($pid) !== intval($account['id'])) {
|
||||||
if($account['locked']) {
|
if($account['locked']) {
|
||||||
|
@ -479,18 +483,21 @@ class ApiV1Controller extends Controller
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if($request->has('page') && $request->page >= 5) {
|
if($request->has('page') && $request->user()->is_admin == false) {
|
||||||
|
$page = (int) $request->input('page');
|
||||||
|
if(($page * $limit) >= 100) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if($request->has('page')) {
|
||||||
$res = DB::table('followers')
|
$res = DB::table('followers')
|
||||||
->select('id', 'profile_id', 'following_id')
|
->select('id', 'profile_id', 'following_id')
|
||||||
->whereFollowingId($account['id'])
|
->whereFollowingId($account['id'])
|
||||||
->orderByDesc('id')
|
->orderByDesc('id')
|
||||||
->simplePaginate(10)
|
->simplePaginate($limit)
|
||||||
->map(function($follower) {
|
->map(function($follower) {
|
||||||
return AccountService::getMastodon($follower->profile_id);
|
return AccountService::getMastodon($follower->profile_id, true);
|
||||||
})
|
})
|
||||||
->filter(function($account) {
|
->filter(function($account) {
|
||||||
return $account && isset($account['id']);
|
return $account && isset($account['id']);
|
||||||
|
@ -501,6 +508,42 @@ class ApiV1Controller extends Controller
|
||||||
return $this->json($res);
|
return $this->json($res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$paginator = DB::table('followers')
|
||||||
|
->select('id', 'profile_id', 'following_id')
|
||||||
|
->whereFollowingId($account['id'])
|
||||||
|
->orderByDesc('id')
|
||||||
|
->cursorPaginate($limit)
|
||||||
|
->withQueryString();
|
||||||
|
|
||||||
|
$link = null;
|
||||||
|
|
||||||
|
if($paginator->onFirstPage()) {
|
||||||
|
if($paginator->hasMorePages()) {
|
||||||
|
$link = '<'.$paginator->nextPageUrl().'>; rel="prev"';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if($paginator->previousPageUrl()) {
|
||||||
|
$link = '<'.$paginator->previousPageUrl().'>; rel="next"';
|
||||||
|
}
|
||||||
|
|
||||||
|
if($paginator->hasMorePages()) {
|
||||||
|
$link .= ($link ? ',' : '') . '<'.$paginator->nextPageUrl().'>; rel="prev"';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$res = $paginator->map(function($follower) {
|
||||||
|
return AccountService::get($follower->profile_id, true);
|
||||||
|
})
|
||||||
|
->filter(function($account) {
|
||||||
|
return $account && isset($account['id']);
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
$headers = isset($link) ? ['Link' => $link] : [];
|
||||||
|
return $this->json($res, 200, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /api/v1/accounts/{id}/following
|
* GET /api/v1/accounts/{id}/following
|
||||||
*
|
*
|
||||||
|
@ -514,6 +557,10 @@ class ApiV1Controller extends Controller
|
||||||
$account = AccountService::get($id);
|
$account = AccountService::get($id);
|
||||||
abort_if(!$account, 404);
|
abort_if(!$account, 404);
|
||||||
$pid = $request->user()->profile_id;
|
$pid = $request->user()->profile_id;
|
||||||
|
$this->validate($request, [
|
||||||
|
'limit' => 'sometimes|integer|min:1|max:80'
|
||||||
|
]);
|
||||||
|
$limit = $request->input('limit', 10);
|
||||||
|
|
||||||
if(intval($pid) !== intval($account['id'])) {
|
if(intval($pid) !== intval($account['id'])) {
|
||||||
if($account['locked']) {
|
if($account['locked']) {
|
||||||
|
@ -526,18 +573,56 @@ class ApiV1Controller extends Controller
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if($request->has('page') && $request->page >= 5) {
|
if($request->has('page') && $request->user()->is_admin == false) {
|
||||||
|
$page = (int) $request->input('page');
|
||||||
|
if(($page * $limit) >= 100) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($request->has('page')) {
|
||||||
$res = DB::table('followers')
|
$res = DB::table('followers')
|
||||||
->select('id', 'profile_id', 'following_id')
|
->select('id', 'profile_id', 'following_id')
|
||||||
->whereProfileId($account['id'])
|
->whereProfileId($account['id'])
|
||||||
->orderByDesc('id')
|
->orderByDesc('id')
|
||||||
->simplePaginate(10)
|
->simplePaginate($limit)
|
||||||
->map(function($follower) {
|
->map(function($follower) {
|
||||||
return AccountService::get($follower->following_id);
|
return AccountService::get($follower->following_id, true);
|
||||||
|
})
|
||||||
|
->filter(function($account) {
|
||||||
|
return $account && isset($account['id']);
|
||||||
|
})
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
return $this->json($res);
|
||||||
|
}
|
||||||
|
|
||||||
|
$paginator = DB::table('followers')
|
||||||
|
->select('id', 'profile_id', 'following_id')
|
||||||
|
->whereProfileId($account['id'])
|
||||||
|
->orderByDesc('id')
|
||||||
|
->cursorPaginate($limit)
|
||||||
|
->withQueryString();
|
||||||
|
|
||||||
|
$link = null;
|
||||||
|
|
||||||
|
if($paginator->onFirstPage()) {
|
||||||
|
if($paginator->hasMorePages()) {
|
||||||
|
$link = '<'.$paginator->nextPageUrl().'>; rel="prev"';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if($paginator->previousPageUrl()) {
|
||||||
|
$link = '<'.$paginator->previousPageUrl().'>; rel="next"';
|
||||||
|
}
|
||||||
|
|
||||||
|
if($paginator->hasMorePages()) {
|
||||||
|
$link .= ($link ? ',' : '') . '<'.$paginator->nextPageUrl().'>; rel="prev"';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$res = $paginator->map(function($follower) {
|
||||||
|
return AccountService::get($follower->following_id, true);
|
||||||
})
|
})
|
||||||
->filter(function($account) {
|
->filter(function($account) {
|
||||||
return $account && isset($account['id']);
|
return $account && isset($account['id']);
|
||||||
|
@ -545,7 +630,8 @@ class ApiV1Controller extends Controller
|
||||||
->values()
|
->values()
|
||||||
->toArray();
|
->toArray();
|
||||||
|
|
||||||
return $this->json($res);
|
$headers = isset($link) ? ['Link' => $link] : [];
|
||||||
|
return $this->json($res, 200, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue