From 5649873ab87456c36816b441992244adf7fb4ea9 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 2 Apr 2022 16:34:12 -0600 Subject: [PATCH] Update ApiV1Controller, add miissing pagination header --- app/Http/Controllers/Api/ApiV1Controller.php | 64 +++++++++++++++++--- 1 file changed, 56 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 964a52e8..7beb251a 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -1702,8 +1702,7 @@ class ApiV1Controller extends Controller return $status && isset($status['account']); }) ->take($limit) - ->values() - ->toArray(); + ->values(); } else { $res = Status::select( 'id', @@ -1734,11 +1733,35 @@ class ApiV1Controller extends Controller return $status && isset($status['account']); }) ->take($limit) - ->values() - ->toArray(); + ->values(); } - return $this->json($res); + $baseUrl = config('app.url') . '/api/v1/timelines/home?limit=' . $limit . '&'; + $minId = $res->map(function($s) { + return ['id' => $s['id']]; + })->min('id'); + $maxId = $res->map(function($s) { + return ['id' => $s['id']]; + })->max('id'); + + if($minId == $maxId) { + $minId = null; + } + + if($maxId) { + $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next"'; + } + + if($minId) { + $link = '<'.$baseUrl.'min_id='.$minId.'>; rel="prev"'; + } + + if($maxId && $minId) { + $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next",<'.$baseUrl.'min_id='.$minId.'>; rel="prev"'; + } + + $headers = isset($link) ? ['Link' => $link] : []; + return $this->json($res->toArray(), 200, $headers); } /** @@ -1792,10 +1815,35 @@ class ApiV1Controller extends Controller return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; }) ->take($limit) - ->values() - ->toArray(); + ->values(); + // ->toArray(); - return $this->json($res); + $baseUrl = config('app.url') . '/api/v1/timelines/public?limit=' . $limit . '&'; + $minId = $res->map(function($s) { + return ['id' => $s['id']]; + })->min('id'); + $maxId = $res->map(function($s) { + return ['id' => $s['id']]; + })->max('id'); + + if($minId == $maxId) { + $minId = null; + } + + if($maxId) { + $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next"'; + } + + if($minId) { + $link = '<'.$baseUrl.'min_id='.$minId.'>; rel="prev"'; + } + + if($maxId && $minId) { + $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next",<'.$baseUrl.'min_id='.$minId.'>; rel="prev"'; + } + + $headers = isset($link) ? ['Link' => $link] : []; + return $this->json($res->toArray(), 200, $headers); } /**