From f9516ac316824f5829d3c5799b1011b3cc570f7d Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 25 Jul 2021 02:12:30 -0600 Subject: [PATCH] Update ApiControllers, use NotificationService --- app/Http/Controllers/Api/ApiV1Controller.php | 5 ++ .../Controllers/Api/BaseApiController.php | 54 ++++++++++++------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 6a4593823..b2ce2f85a 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -1316,6 +1316,11 @@ class ApiV1Controller extends Controller } } + if(empty($res) && !Cache::has('pf:services:notifications:hasSynced:'.$pid)) { + Cache::put('pf:services:notifications:hasSynced:'.$pid, 1, 1209600); + NotificationService::warmCache($pid, 400, true); + } + $baseUrl = config('app.url') . '/api/v1/notifications?'; if($minId == $maxId) { diff --git a/app/Http/Controllers/Api/BaseApiController.php b/app/Http/Controllers/Api/BaseApiController.php index 5e02a2880..0e62013f4 100644 --- a/app/Http/Controllers/Api/BaseApiController.php +++ b/app/Http/Controllers/Api/BaseApiController.php @@ -54,26 +54,40 @@ class BaseApiController extends Controller public function notifications(Request $request) { abort_if(!$request->user(), 403); - $pid = $request->user()->profile_id; - $pg = $request->input('pg'); - if($pg == true) { - $timeago = Carbon::now()->subMonths(6); - $notifications = Notification::whereProfileId($pid) - ->whereDate('created_at', '>', $timeago) - ->latest() - ->simplePaginate(10); - $resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer()); - $res = $this->fractal->createData($resource)->toArray(); - } else { - $this->validate($request, [ - 'page' => 'nullable|integer|min:1|max:10', - 'limit' => 'nullable|integer|min:1|max:40' - ]); - $limit = $request->input('limit') ?? 10; - $page = $request->input('page') ?? 1; - $end = (int) $page * $limit; - $start = (int) $end - $limit; - $res = NotificationService::get($pid, $start, $end); + + $pid = $request->user()->profile_id; + $limit = $request->input('limit', 20); + + $since = $request->input('since_id'); + $min = $request->input('min_id'); + $max = $request->input('max_id'); + + if(!$since && !$min && !$max) { + $min = 1; + } + + $maxId = null; + $minId = null; + + if($max) { + $res = NotificationService::getMax($pid, $max, $limit); + $ids = NotificationService::getRankedMaxId($pid, $max, $limit); + if(!empty($ids)) { + $maxId = max($ids); + $minId = min($ids); + } + } else { + $res = NotificationService::getMin($pid, $min ?? $since, $limit); + $ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit); + if(!empty($ids)) { + $maxId = max($ids); + $minId = min($ids); + } + } + + if(empty($res) && !Cache::has('pf:services:notifications:hasSynced:'.$pid)) { + Cache::put('pf:services:notifications:hasSynced:'.$pid, 1, 1209600); + NotificationService::warmCache($pid, 400, true); } return response()->json($res);