浏览代码

Update ApiControllers, use NotificationService

Daniel Supernault 4 年之前
父节点
当前提交
f9516ac316
共有 2 个文件被更改,包括 39 次插入20 次删除
  1. 5 0
      app/Http/Controllers/Api/ApiV1Controller.php
  2. 34 20
      app/Http/Controllers/Api/BaseApiController.php

+ 5 - 0
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) {

+ 34 - 20
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);