Преглед изворни кода

Update PublicApiController, improve home timeline perf

Daniel Supernault пре 4 година
родитељ
комит
4fe42e5b57
1 измењених фајлова са 18 додато и 55 уклоњено
  1. 18 55
      app/Http/Controllers/PublicApiController.php

+ 18 - 55
app/Http/Controllers/PublicApiController.php

@@ -290,69 +290,32 @@ class PublicApiController extends Controller
         $max = $request->input('max_id');
         $max = $request->input('max_id');
         $limit = $request->input('limit') ?? 3;
         $limit = $request->input('limit') ?? 3;
         $user = $request->user();
         $user = $request->user();
-        $amin = SnowflakeService::byDate(now()->subDays(90));
-
-        $key = 'user:last_active_at:id:'.$user->id;
-        $ttl = now()->addMinutes(5);
-        Cache::remember($key, $ttl, function() use($user) {
-            $user->last_active_at = now();
-            $user->save();
-            return;
-        });
 
 
         $filtered = UserFilter::whereUserId($user->profile_id)
         $filtered = UserFilter::whereUserId($user->profile_id)
                   ->whereFilterableType('App\Profile')
                   ->whereFilterableType('App\Profile')
                   ->whereIn('filter_type', ['mute', 'block'])
                   ->whereIn('filter_type', ['mute', 'block'])
                   ->pluck('filterable_id')->toArray();
                   ->pluck('filterable_id')->toArray();
 
 
-        if($min || $max) {
-            $dir = $min ? '>' : '<';
-            $id = $min ?? $max;
-            $timeline = Status::select(
-                        'id',
-                        'profile_id',
-                        'type',
-                        'scope',
-                        'local'
-                      )->where('id', $dir, $id)
-                      ->where('id', '>', $amin)
-                      ->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
-                      ->whereNotIn('profile_id', $filtered)
-                      ->whereLocal(true)
-                      ->whereScope('public')
-                      ->orderBy('id', 'desc')
-                      ->limit($limit)
-                      ->get()
-                      ->map(function($s) use ($user) {
-                           $status = StatusService::get($s->id);
-                           $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
-                           return $status;
-                      });
-            $res = $timeline->toArray();
-        } else {
-            $timeline = Status::select(
-                        'id',
-                        'profile_id',
-                        'type',
-                        'scope',
-                        'local'
-                      )->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
-                      ->where('id', '>', $amin)
-                      ->whereNotIn('profile_id', $filtered)
-                      ->with('profile', 'hashtags', 'mentions')
-                      ->whereLocal(true)
-                      ->whereScope('public')
-                      ->orderBy('id', 'desc')
-                      ->limit($limit)
-                      ->get()
-                      ->map(function($s) use ($user) {
-                           $status = StatusService::get($s->id);
-                           $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
-                           return $status;
-                      });
-            $res = $timeline->toArray();
+        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::get($k);
+                $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
+                return $status;
+            })
+            ->toArray();
+
         return response()->json($res);
         return response()->json($res);
     }
     }