Skip to content

Commit

Permalink
Update ApiV1Controller, add cursor pagination and pagination link hea…
Browse files Browse the repository at this point in the history
…ders to account/{id}/followers and account/{id}/following endpoints with legacy support for `page=` simple pagination
  • Loading branch information
dansup committed Mar 5, 2023
1 parent eb2fe9d commit 713aa5f
Showing 1 changed file with 100 additions and 14 deletions.
114 changes: 100 additions & 14 deletions app/Http/Controllers/Api/ApiV1Controller.php
Expand Up @@ -467,6 +467,10 @@ public function accountFollowersById(Request $request, $id)
$account = AccountService::get($id);
abort_if(!$account, 404);
$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($account['locked']) {
Expand All @@ -479,26 +483,65 @@ public function accountFollowersById(Request $request, $id)
return [];
}

if($request->has('page') && $request->page >= 5) {
return [];
if($request->has('page') && $request->user()->is_admin == false) {
$page = (int) $request->input('page');
if(($page * $limit) >= 100) {
return [];
}
}
}
if($request->has('page')) {
$res = DB::table('followers')
->select('id', 'profile_id', 'following_id')
->whereFollowingId($account['id'])
->orderByDesc('id')
->simplePaginate($limit)
->map(function($follower) {
return AccountService::getMastodon($follower->profile_id, true);
})
->filter(function($account) {
return $account && isset($account['id']);
})
->values()
->toArray();

return $this->json($res);
}

$res = DB::table('followers')
$paginator = DB::table('followers')
->select('id', 'profile_id', 'following_id')
->whereFollowingId($account['id'])
->orderByDesc('id')
->simplePaginate(10)
->map(function($follower) {
return AccountService::getMastodon($follower->profile_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();

return $this->json($res);
$headers = isset($link) ? ['Link' => $link] : [];
return $this->json($res, 200, $headers);
}

/**
Expand All @@ -514,6 +557,10 @@ public function accountFollowingById(Request $request, $id)
$account = AccountService::get($id);
abort_if(!$account, 404);
$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($account['locked']) {
Expand All @@ -526,26 +573,65 @@ public function accountFollowingById(Request $request, $id)
return [];
}

if($request->has('page') && $request->page >= 5) {
return [];
if($request->has('page') && $request->user()->is_admin == false) {
$page = (int) $request->input('page');
if(($page * $limit) >= 100) {
return [];
}
}
}

$res = DB::table('followers')
if($request->has('page')) {
$res = DB::table('followers')
->select('id', 'profile_id', 'following_id')
->whereProfileId($account['id'])
->orderByDesc('id')
->simplePaginate($limit)
->map(function($follower) {
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')
->simplePaginate(10)
->map(function($follower) {
return AccountService::get($follower->following_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) {
return $account && isset($account['id']);
})
->values()
->toArray();

return $this->json($res);
$headers = isset($link) ? ['Link' => $link] : [];
return $this->json($res, 200, $headers);
}

/**
Expand Down

0 comments on commit 713aa5f

Please sign in to comment.