Add /api/v1/notifications endpoint

This commit is contained in:
Daniel Supernault 2019-09-26 01:37:22 -06:00
parent 9b130cfe9b
commit 39449f3638
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 46 additions and 2 deletions

View file

@ -19,12 +19,15 @@ use App\{
UserFilter, UserFilter,
}; };
use League\Fractal; use League\Fractal;
use App\Transformer\Api\{ use App\Transformer\Api\Mastodon\v1\{
AccountTransformer, AccountTransformer,
MediaTransformer, MediaTransformer,
RelationshipTransformer, NotificationTransformer,
StatusTransformer, StatusTransformer,
}; };
use App\Transformer\Api\{
RelationshipTransformer,
};
use App\Http\Controllers\FollowerController; use App\Http\Controllers\FollowerController;
use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter; use League\Fractal\Pagination\IlluminatePaginatorAdapter;
@ -1091,6 +1094,46 @@ class ApiV1Controller extends Controller
return response()->json($res); 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) public function statusById(Request $request, $id)
{ {
$status = Status::whereVisibility('public')->findOrFail($id); $status = Status::whereVisibility('public')->findOrFail($id);

View file

@ -114,6 +114,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
Route::get('mutes', 'Api\ApiV1Controller@accountMutes')->middleware('auth:api'); Route::get('mutes', 'Api\ApiV1Controller@accountMutes')->middleware('auth:api');
Route::post('accounts/{id}/mute', 'Api\ApiV1Controller@accountMuteById')->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::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::get('likes', 'ApiController@hydrateLikes');
// Route::post('media', 'ApiController@uploadMedia')->middleware('auth:api'); // Route::post('media', 'ApiController@uploadMedia')->middleware('auth:api');