瀏覽代碼

Update ApiV1Controller, fix timelline pagination

Daniel Supernault 3 年之前
父節點
當前提交
a5cdc28b6c
共有 1 個文件被更改,包括 61 次插入58 次删除
  1. 61 58
      app/Http/Controllers/Api/ApiV1Controller.php

+ 61 - 58
app/Http/Controllers/Api/ApiV1Controller.php

@@ -1684,7 +1684,7 @@ class ApiV1Controller extends Controller
 			->whereIn('profile_id', $following)
 			->whereIn('profile_id', $following)
 			->whereIn('visibility',['public', 'unlisted', 'private'])
 			->whereIn('visibility',['public', 'unlisted', 'private'])
 			->latest()
 			->latest()
-			->take($limit)
+			->take(($limit * 2))
 			->get()
 			->get()
 			->map(function($s) use($pid) {
 			->map(function($s) use($pid) {
 				$status = StatusService::getMastodon($s['id']);
 				$status = StatusService::getMastodon($s['id']);
@@ -1701,6 +1701,7 @@ class ApiV1Controller extends Controller
 			->filter(function($status) {
 			->filter(function($status) {
 				return $status && isset($status['account']);
 				return $status && isset($status['account']);
 			})
 			})
+			->take($limit)
 			->values()
 			->values()
 			->toArray();
 			->toArray();
 		} else {
 		} else {
@@ -1715,7 +1716,7 @@ class ApiV1Controller extends Controller
 			->whereIn('profile_id', $following)
 			->whereIn('profile_id', $following)
 			->whereIn('visibility',['public', 'unlisted', 'private'])
 			->whereIn('visibility',['public', 'unlisted', 'private'])
 			->latest()
 			->latest()
-			->take($limit)
+			->take(($limit * 2))
 			->get()
 			->get()
 			->map(function($s) use($pid) {
 			->map(function($s) use($pid) {
 				$status = StatusService::getMastodon($s['id']);
 				$status = StatusService::getMastodon($s['id']);
@@ -1732,6 +1733,7 @@ class ApiV1Controller extends Controller
 			->filter(function($status) {
 			->filter(function($status) {
 				return $status && isset($status['account']);
 				return $status && isset($status['account']);
 			})
 			})
+			->take($limit)
 			->values()
 			->values()
 			->toArray();
 			->toArray();
 		}
 		}
@@ -1739,6 +1741,63 @@ class ApiV1Controller extends Controller
 		return $this->json($res);
 		return $this->json($res);
 	}
 	}
 
 
+	/**
+	 * GET /api/v1/timelines/public
+	 *
+	 *
+	 * @return StatusTransformer
+	 */
+	public function timelinePublic(Request $request)
+	{
+		$this->validate($request,[
+		  'min_id'      => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
+		  'max_id'      => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
+		  'limit'       => 'nullable|integer|max:100'
+		]);
+
+		$min = $request->input('min_id');
+		$max = $request->input('max_id');
+		$limit = $request->input('limit') ?? 20;
+		$user = $request->user();
+        $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
+
+		Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
+			if(PublicTimelineService::count() == 0) {
+				PublicTimelineService::warmCache(true, 400);
+			}
+		});
+
+		if ($max) {
+			$feed = PublicTimelineService::getRankedMaxId($max, $limit + 5);
+		} else if ($min) {
+			$feed = PublicTimelineService::getRankedMinId($min, $limit + 5);
+		} else {
+			$feed = PublicTimelineService::get(0, $limit + 5);
+		}
+
+		$res = collect($feed)
+		->map(function($k) use($user) {
+			$status = StatusService::getMastodon($k);
+			if(!$status || !isset($status['account']) || !isset($status['account']['id'])) {
+				return false;
+			}
+
+			if($user) {
+				$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
+				$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $status['id']);
+			}
+			return $status;
+		})
+		->filter(function($s) use($filtered) {
+			return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
+		})
+		->take($limit)
+		->values()
+		->toArray();
+
+		return $this->json($res);
+	}
+
 	/**
 	/**
 	 * GET /api/v1/conversations
 	 * GET /api/v1/conversations
 	 *
 	 *
@@ -1805,62 +1864,6 @@ class ApiV1Controller extends Controller
 		return $this->json($dms);
 		return $this->json($dms);
 	}
 	}
 
 
-	/**
-	 * GET /api/v1/timelines/public
-	 *
-	 *
-	 * @return StatusTransformer
-	 */
-	public function timelinePublic(Request $request)
-	{
-		$this->validate($request,[
-		  'min_id'      => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
-		  'max_id'      => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
-		  'limit'       => 'nullable|integer|max:100'
-		]);
-
-		$min = $request->input('min_id');
-		$max = $request->input('max_id');
-		$limit = $request->input('limit') ?? 20;
-		$user = $request->user();
-        $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
-
-		Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
-			if(PublicTimelineService::count() == 0) {
-				PublicTimelineService::warmCache(true, 400);
-			}
-		});
-
-		if ($max) {
-			$feed = PublicTimelineService::getRankedMaxId($max, $limit);
-		} else if ($min) {
-			$feed = PublicTimelineService::getRankedMinId($min, $limit);
-		} else {
-			$feed = PublicTimelineService::get(0, $limit);
-		}
-
-		$res = collect($feed)
-		->map(function($k) use($user) {
-			$status = StatusService::getMastodon($k);
-			if(!$status || !isset($status['account']) || !isset($status['account']['id'])) {
-				return false;
-			}
-
-			if($user) {
-				$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
-				$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $status['id']);
-			}
-			return $status;
-		})
-		->filter(function($s) use($filtered) {
-			return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
-		})
-		->values()
-		->toArray();
-
-		return $this->json($res);
-	}
-
 	/**
 	/**
 	 * GET /api/v1/statuses/{id}
 	 * GET /api/v1/statuses/{id}
 	 *
 	 *