Update ApiControllers, use NotificationService

This commit is contained in:
Daniel Supernault 2021-07-25 02:12:30 -06:00
parent bce8edd994
commit f9516ac316
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 39 additions and 20 deletions

View file

@ -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?'; $baseUrl = config('app.url') . '/api/v1/notifications?';
if($minId == $maxId) { if($minId == $maxId) {

View file

@ -54,26 +54,40 @@ class BaseApiController extends Controller
public function notifications(Request $request) public function notifications(Request $request)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$pid = $request->user()->profile_id;
$pg = $request->input('pg'); $pid = $request->user()->profile_id;
if($pg == true) { $limit = $request->input('limit', 20);
$timeago = Carbon::now()->subMonths(6);
$notifications = Notification::whereProfileId($pid) $since = $request->input('since_id');
->whereDate('created_at', '>', $timeago) $min = $request->input('min_id');
->latest() $max = $request->input('max_id');
->simplePaginate(10);
$resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer()); if(!$since && !$min && !$max) {
$res = $this->fractal->createData($resource)->toArray(); $min = 1;
} else { }
$this->validate($request, [
'page' => 'nullable|integer|min:1|max:10', $maxId = null;
'limit' => 'nullable|integer|min:1|max:40' $minId = null;
]);
$limit = $request->input('limit') ?? 10; if($max) {
$page = $request->input('page') ?? 1; $res = NotificationService::getMax($pid, $max, $limit);
$end = (int) $page * $limit; $ids = NotificationService::getRankedMaxId($pid, $max, $limit);
$start = (int) $end - $limit; if(!empty($ids)) {
$res = NotificationService::get($pid, $start, $end); $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); return response()->json($res);