From 1fb7e2b2c97b3a15b17bd4ccabc7d6ec71451212 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 31 Jan 2021 13:45:22 -0700 Subject: [PATCH] Update AccountController, add mutes and blocks endpoint to pixelfed api --- app/Http/Controllers/AccountController.php | 66 ++++++++++++++++++++++ routes/web.php | 2 + 2 files changed, 68 insertions(+) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index d0c3d4563..85ee74281 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -22,6 +22,10 @@ use App\{ User, UserFilter }; +use League\Fractal; +use League\Fractal\Serializer\ArraySerializer; +use League\Fractal\Pagination\IlluminatePaginatorAdapter; +use App\Transformer\Api\Mastodon\v1\AccountTransformer; class AccountController extends Controller { @@ -487,4 +491,66 @@ class AccountController extends Controller public function accountRestored(Request $request) { } + + public function accountMutes(Request $request) + { + abort_if(!$request->user(), 403); + + $this->validate($request, [ + 'limit' => 'nullable|integer|min:1|max:40' + ]); + + $user = $request->user(); + $limit = $request->input('limit') ?? 40; + + $mutes = UserFilter::whereUserId($user->profile_id) + ->whereFilterableType('App\Profile') + ->whereFilterType('mute') + ->simplePaginate($limit) + ->pluck('filterable_id'); + + $accounts = Profile::find($mutes); + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Collection($accounts, new AccountTransformer()); + $res = $fractal->createData($resource)->toArray(); + $url = $request->url(); + $page = $request->input('page', 1); + $next = $page < 40 ? $page + 1 : 40; + $prev = $page > 1 ? $page - 1 : 1; + $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"'; + return response()->json($res, 200, ['Link' => $links]); + } + + public function accountBlocks(Request $request) + { + abort_if(!$request->user(), 403); + + $this->validate($request, [ + 'limit' => 'nullable|integer|min:1|max:40', + 'page' => 'nullable|integer|min:1|max:10' + ]); + + $user = $request->user(); + $limit = $request->input('limit') ?? 40; + + $blocked = UserFilter::select('filterable_id','filterable_type','filter_type','user_id') + ->whereUserId($user->profile_id) + ->whereFilterableType('App\Profile') + ->whereFilterType('block') + ->simplePaginate($limit) + ->pluck('filterable_id'); + + $profiles = Profile::findOrFail($blocked); + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer()); + $res = $fractal->createData($resource)->toArray(); + $url = $request->url(); + $page = $request->input('page', 1); + $next = $page < 40 ? $page + 1 : 40; + $prev = $page > 1 ? $page - 1 : 1; + $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"'; + return response()->json($res, 200, ['Link' => $links]); + } } diff --git a/routes/web.php b/routes/web.php index fb51d6f36..1a40f506d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -172,6 +172,8 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::get('newsroom/timeline', 'NewsroomController@timelineApi'); Route::post('newsroom/markasread', 'NewsroomController@markAsRead'); Route::get('favourites', 'Api\BaseApiController@accountLikes'); + Route::get('mutes', 'AccountController@accountMutes'); + Route::get('blocks', 'AccountController@accountBlocks'); }); Route::group(['prefix' => 'v2'], function() {