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; $pid = $request->user()->profile_id;
$pg = $request->input('pg'); $limit = $request->input('limit', 20);
if($pg == true) {
$timeago = Carbon::now()->subMonths(6); $since = $request->input('since_id');
$notifications = Notification::whereProfileId($pid) $min = $request->input('min_id');
->whereDate('created_at', '>', $timeago) $max = $request->input('max_id');
->latest()
->simplePaginate(10); if(!$since && !$min && !$max) {
$resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer()); $min = 1;
$res = $this->fractal->createData($resource)->toArray(); }
$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 { } else {
$this->validate($request, [ $res = NotificationService::getMin($pid, $min ?? $since, $limit);
'page' => 'nullable|integer|min:1|max:10', $ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit);
'limit' => 'nullable|integer|min:1|max:40' if(!empty($ids)) {
]); $maxId = max($ids);
$limit = $request->input('limit') ?? 10; $minId = min($ids);
$page = $request->input('page') ?? 1; }
$end = (int) $page * $limit; }
$start = (int) $end - $limit;
$res = NotificationService::get($pid, $start, $end); 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);