From 8722f830406565b76df354408105f430cf422894 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 21 Jan 2019 13:01:36 -0700 Subject: [PATCH] Update BaseApiController --- .../Controllers/Api/BaseApiController.php | 61 ++++++++++++++++--- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/BaseApiController.php b/app/Http/Controllers/Api/BaseApiController.php index c87dd1983..c890f3846 100644 --- a/app/Http/Controllers/Api/BaseApiController.php +++ b/app/Http/Controllers/Api/BaseApiController.php @@ -13,7 +13,8 @@ use App\{ Avatar, Notification, Media, - Profile + Profile, + Status }; use App\Transformer\Api\{ AccountTransformer, @@ -23,6 +24,7 @@ use App\Transformer\Api\{ }; use League\Fractal; use League\Fractal\Serializer\ArraySerializer; +use League\Fractal\Pagination\IlluminatePaginatorAdapter; use App\Jobs\AvatarPipeline\AvatarOptimize; use App\Jobs\ImageOptimizePipeline\ImageOptimize; use App\Jobs\VideoPipeline\{ @@ -97,18 +99,52 @@ class BaseApiController extends Controller public function accountStatuses(Request $request, $id) { - $pid = Auth::user()->profile->id; - $profile = Profile::findOrFail($id); - $statuses = $profile->statuses(); - if($pid === $profile->id) { - $statuses = $statuses->orderBy('id', 'desc')->paginate(20); + $this->validate($request, [ + 'only_media' => 'nullable', + 'pinned' => 'nullable', + 'exclude_replies' => 'nullable', + 'max_id' => 'nullable|integer|min:1', + 'since_id' => 'nullable|integer|min:1', + 'min_id' => 'nullable|integer|min:1', + 'limit' => 'nullable|integer|min:1|max:24' + ]); + $limit = $request->limit ?? 20; + $max_id = $request->max_id ?? false; + $min_id = $request->min_id ?? false; + $since_id = $request->since_id ?? false; + $only_media = $request->only_media ?? false; + $user = Auth::user(); + $account = Profile::findOrFail($id); + $statuses = $account->statuses()->getQuery(); + if($only_media == true) { + $statuses = $statuses + ->whereHas('media') + ->whereNull('in_reply_to_id') + ->whereNull('reblog_of_id'); + } + if($id == $account->id && !$max_id && !$min_id && !$since_id) { + $statuses = $statuses->orderBy('id', 'desc') + ->paginate($limit); + } else if($since_id) { + $statuses = $statuses->where('id', '>', $since_id) + ->orderBy('id', 'DESC') + ->paginate($limit); + } else if($min_id) { + $statuses = $statuses->where('id', '>', $min_id) + ->orderBy('id', 'ASC') + ->paginate($limit); + } else if($max_id) { + $statuses = $statuses->where('id', '<', $max_id) + ->orderBy('id', 'DESC') + ->paginate($limit); } else { - $statuses = $statuses->whereVisibility('public')->orderBy('id', 'desc')->paginate(20); + $statuses = $statuses->whereVisibility('public')->orderBy('id', 'desc')->paginate($limit); } $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer()); + //$resource->setPaginator(new IlluminatePaginatorAdapter($statuses)); $res = $this->fractal->createData($resource)->toArray(); - return response()->json($res); + return response()->json($res, 200, [], JSON_PRETTY_PRINT); } public function followSuggestions(Request $request) @@ -265,4 +301,13 @@ class BaseApiController extends Controller return response()->json($res); } + + public function showAccount(Request $request, $id) + { + $profile = Profile::whereNull('domain')->whereNull('status')->findOrFail($id); + $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); + $res = $this->fractal->createData($resource)->toArray(); + + return response()->json($res); + } }