From b39f91b409caf5de508f8e4971bc967ebbcf5acc Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 7 Dec 2022 00:08:28 -0700 Subject: [PATCH] Update PublicApiController, refactor follower/following api endpoints to consume FollowerService instead of querying database --- app/Http/Controllers/PublicApiController.php | 42 ++++++-------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index 3a406e95..6c1c4cc4 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/app/Http/Controllers/PublicApiController.php @@ -747,7 +747,7 @@ class PublicApiController extends Controller public function accountFollowers(Request $request, $id) { abort_if(!$request->user(), 403); - $account = AccountService::get($id); + $account = AccountService::get($id, true); abort_if(!$account, 404); $pid = $request->user()->profile_id; @@ -762,24 +762,15 @@ class PublicApiController extends Controller return []; } - if($request->has('page') && $request->page >= 5) { + if($request->has('page') && $request->page >= 10) { return []; } } - $res = DB::table('followers') - ->select('id', 'profile_id', 'following_id') - ->whereFollowingId($account['id']) - ->orderByDesc('id') - ->simplePaginate(10) - ->map(function($follower) { - return AccountService::get($follower->profile_id); - }) - ->filter(function($account) { - return $account && isset($account['id']); - }) - ->values() - ->toArray(); + $res = collect(FollowerService::followersPaginate($account['id'], $request->input('page', 1))) + ->map(fn($id) => AccountService::get($id, true)) + ->filter() + ->values(); return response()->json($res); } @@ -787,7 +778,7 @@ class PublicApiController extends Controller public function accountFollowing(Request $request, $id) { abort_if(!$request->user(), 403); - $account = AccountService::get($id); + $account = AccountService::get($id, true); abort_if(!$account, 404); $pid = $request->user()->profile_id; @@ -802,24 +793,15 @@ class PublicApiController extends Controller return []; } - if($request->has('page') && $request->page >= 5) { + if($request->has('page') && $request->page >= 10) { return []; } } - $res = 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); - }) - ->filter(function($account) { - return $account && isset($account['id']); - }) - ->values() - ->toArray(); + $res = collect(FollowerService::followingPaginate($account['id'], $request->input('page', 1))) + ->map(fn($id) => AccountService::get($id, true)) + ->filter() + ->values(); return response()->json($res); }