From 39449f363840a2c064a0435dbac07cabb29260bd Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 26 Sep 2019 01:37:22 -0600 Subject: [PATCH] Add /api/v1/notifications endpoint --- app/Http/Controllers/Api/ApiV1Controller.php | 47 +++++++++++++++++++- routes/web.php | 1 + 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 4d501595..0693827c 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -19,12 +19,15 @@ use App\{ UserFilter, }; use League\Fractal; -use App\Transformer\Api\{ +use App\Transformer\Api\Mastodon\v1\{ AccountTransformer, MediaTransformer, - RelationshipTransformer, + NotificationTransformer, StatusTransformer, }; +use App\Transformer\Api\{ + RelationshipTransformer, +}; use App\Http\Controllers\FollowerController; use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Pagination\IlluminatePaginatorAdapter; @@ -1091,6 +1094,46 @@ class ApiV1Controller extends Controller return response()->json($res); } + /** + * GET /api/v1/notifications + * + * + * @return App\Transformer\Api\NotificationTransformer + */ + public function accountNotifications(Request $request) + { + abort_if(!$request->user(), 403); + $this->validate($request, [ + 'page' => 'nullable|integer|min:1|max:10', + 'limit' => 'nullable|integer|min:1|max:40', + 'max_id' => 'nullable|integer|min:1', + 'min_id' => 'nullable|integer|min:0', + ]); + $pid = $request->user()->profile_id; + $limit = $request->input('limit') ?? 20; + $timeago = now()->subMonths(6); + $min = $request->input('min_id'); + $max = $request->input('max_id'); + if($min || $max) { + $dir = $min ? '>' : '<'; + $id = $min ?? $max; + $notifications = Notification::whereProfileId($pid) + ->whereDate('created_at', '>', $timeago) + ->latest() + ->where('id', $dir, $id) + ->limit($limit) + ->get(); + } else { + $notifications = Notification::whereProfileId($pid) + ->whereDate('created_at', '>', $timeago) + ->latest() + ->simplePaginate($limit); + } + $resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer()); + $res = $this->fractal->createData($resource)->toArray(); + return response()->json($res); + } + public function statusById(Request $request, $id) { $status = Status::whereVisibility('public')->findOrFail($id); diff --git a/routes/web.php b/routes/web.php index d295d360..bcfcefd9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -114,6 +114,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::get('mutes', 'Api\ApiV1Controller@accountMutes')->middleware('auth:api'); Route::post('accounts/{id}/mute', 'Api\ApiV1Controller@accountMuteById')->middleware('auth:api'); Route::post('accounts/{id}/unmute', 'Api\ApiV1Controller@accountUnmuteById')->middleware('auth:api'); + Route::get('notifications', 'Api\ApiV1Controller@accountNotifications')->middleware('auth:api'); // Route::get('likes', 'ApiController@hydrateLikes'); // Route::post('media', 'ApiController@uploadMedia')->middleware('auth:api');