From 1b8783a99f69dd55ff66b6f5d8b9fffffe45d3b0 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 10 Feb 2019 21:49:54 -0700 Subject: [PATCH] Update Api Controllers --- .../Controllers/Api/BaseApiController.php | 11 +-- app/Http/Controllers/PublicApiController.php | 81 ++++++++++++++++++- 2 files changed, 81 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Api/BaseApiController.php b/app/Http/Controllers/Api/BaseApiController.php index c890f3846..7fdbfad38 100644 --- a/app/Http/Controllers/Api/BaseApiController.php +++ b/app/Http/Controllers/Api/BaseApiController.php @@ -141,10 +141,9 @@ class BaseApiController extends Controller $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, 200, [], JSON_PRETTY_PRINT); + return response()->json($res); } public function followSuggestions(Request $request) @@ -302,12 +301,4 @@ 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); - } } diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index ba132585a..a6323b710 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/app/Http/Controllers/PublicApiController.php @@ -340,7 +340,8 @@ class PublicApiController extends Controller abort_if(!Auth::check(), 403); $this->validate($request, [ - 'id' => 'required|array|min:1|max:20' + 'id' => 'required|array|min:1|max:20', + 'id.*' => 'required|integer' ]); $ids = collect($request->input('id')); $filtered = $ids->filter(function($v) { @@ -351,4 +352,82 @@ class PublicApiController extends Controller $res = $this->fractal->createData($fractal)->toArray(); return response()->json($res); } + + public function account(Request $request, $id) + { + $profile = Profile::whereNull('status')->findOrFail($id); + $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); + $res = $this->fractal->createData($resource)->toArray(); + + return response()->json($res); + } + + public function accountFollowers(Request $request, $id) + { + $profile = Profile::findOrFail($id); + $followers = $profile->followers; + $resource = new Fractal\Resource\Collection($followers, new AccountTransformer()); + $res = $this->fractal->createData($resource)->toArray(); + + return response()->json($res); + } + + public function accountFollowing(Request $request, $id) + { + $profile = Profile::findOrFail($id); + $following = $profile->following; + $resource = new Fractal\Resource\Collection($following, new AccountTransformer()); + $res = $this->fractal->createData($resource)->toArray(); + + return response()->json($res); + } + + public function accountStatuses(Request $request, $id) + { + $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($limit); + } + $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer()); + $res = $this->fractal->createData($resource)->toArray(); + + return response()->json($res); + } }