From 9f66d6b6407d205b48b7f76a89fc56f2a83b40ce Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 7 Feb 2023 22:02:23 -0700 Subject: [PATCH] Update ApiV1Controller, fix account statuses and bookmark pagination --- app/Http/Controllers/Api/ApiV1Controller.php | 43 +++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index aa616c0e6..c4d01d989 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -619,13 +619,20 @@ class ApiV1Controller extends Controller ->orderByDesc('id') ->get() ->map(function($s) use($user, $napi, $profile) { - $status = $napi ? StatusService::get($s->id, false) : StatusService::getMastodon($s->id, false); + try { + $status = $napi ? StatusService::get($s->id, false) : StatusService::getMastodon($s->id, false); + } catch (\Exception $e) { + $status = false; + } + if($profile) { $status['account'] = $profile; } if($user && $status) { $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); + $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); + $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); } return $status; }) @@ -2975,20 +2982,36 @@ class ApiV1Controller extends Controller $dir = $min_id ? '>' : '<'; $id = $min_id ?? $max_id; - $bookmarks = Bookmark::whereProfileId($pid) - ->when($id, function($query, $id) use($dir) { - return $query->where('status_id', $dir, $id); - }) - ->limit($limit) - ->pluck('status_id') - ->map(function($id) { - return \App\Services\StatusService::getMastodon($id); + $bookmarkQuery = Bookmark::whereProfileId($pid) + ->orderByDesc('id') + ->cursorPaginate($limit); + + $bookmarks = $bookmarkQuery->map(function($bookmark) { + return \App\Services\StatusService::getMastodon($bookmark->status_id); }) ->filter() ->values() ->toArray(); - return $this->json($bookmarks); + $links = null; + $headers = []; + + if($bookmarkQuery->nextCursor()) { + $links .= '<'.$bookmarkQuery->nextPageUrl().'&limit='.$limit.'>; rel="next"'; + } + + if($bookmarkQuery->previousCursor()) { + if($links != null) { + $links .= ', '; + } + $links .= '<'.$bookmarkQuery->previousPageUrl().'&limit='.$limit.'>; rel="prev"'; + } + + if($links) { + $headers = ['Link' => $links]; + } + + return $this->json($bookmarks, 200, $headers); } /**