1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2025-02-21 21:57:06 +00:00

Update PublicApiController, refactor follower/following api endpoints to consume FollowerService instead of querying database

This commit is contained in:
Daniel Supernault 2022-12-07 00:08:28 -07:00
parent f46b01af51
commit b39f91b409
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -747,7 +747,7 @@ class PublicApiController extends Controller
public function accountFollowers(Request $request, $id) public function accountFollowers(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$account = AccountService::get($id); $account = AccountService::get($id, true);
abort_if(!$account, 404); abort_if(!$account, 404);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -762,24 +762,15 @@ class PublicApiController extends Controller
return []; return [];
} }
if($request->has('page') && $request->page >= 5) { if($request->has('page') && $request->page >= 10) {
return []; return [];
} }
} }
$res = DB::table('followers') $res = collect(FollowerService::followersPaginate($account['id'], $request->input('page', 1)))
->select('id', 'profile_id', 'following_id') ->map(fn($id) => AccountService::get($id, true))
->whereFollowingId($account['id']) ->filter()
->orderByDesc('id') ->values();
->simplePaginate(10)
->map(function($follower) {
return AccountService::get($follower->profile_id);
})
->filter(function($account) {
return $account && isset($account['id']);
})
->values()
->toArray();
return response()->json($res); return response()->json($res);
} }
@ -787,7 +778,7 @@ class PublicApiController extends Controller
public function accountFollowing(Request $request, $id) public function accountFollowing(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$account = AccountService::get($id); $account = AccountService::get($id, true);
abort_if(!$account, 404); abort_if(!$account, 404);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -802,24 +793,15 @@ class PublicApiController extends Controller
return []; return [];
} }
if($request->has('page') && $request->page >= 5) { if($request->has('page') && $request->page >= 10) {
return []; return [];
} }
} }
$res = DB::table('followers') $res = collect(FollowerService::followingPaginate($account['id'], $request->input('page', 1)))
->select('id', 'profile_id', 'following_id') ->map(fn($id) => AccountService::get($id, true))
->whereProfileId($account['id']) ->filter()
->orderByDesc('id') ->values();
->simplePaginate(10)
->map(function($follower) {
return AccountService::get($follower->following_id);
})
->filter(function($account) {
return $account && isset($account['id']);
})
->values()
->toArray();
return response()->json($res); return response()->json($res);
} }