Browse Source

Update StoryController, show active self stories on home timeline

Daniel Supernault 2 years ago
parent
commit
633351f6dc
2 changed files with 44 additions and 19 deletions
  1. 42 18
      app/Http/Controllers/StoryController.php
  2. 2 1
      app/Services/StoryService.php

+ 42 - 18
app/Http/Controllers/StoryController.php

@@ -37,28 +37,53 @@ class StoryController extends StoryComposeController
 		$pid = $request->user()->profile_id;
 
 		if(config('database.default') == 'pgsql') {
-			$s = Story::select('stories.*', 'followers.following_id')
-				->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
-				->where('followers.profile_id', $pid)
-				->where('stories.active', true)
+			$s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
+				return Story::select('stories.*', 'followers.following_id')
+					->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
+					->where('followers.profile_id', $pid)
+					->where('stories.active', true)
+					->get()
+					->map(function($s) {
+						$r  = new \StdClass;
+						$r->id = $s->id;
+						$r->profile_id = $s->profile_id;
+						$r->type = $s->type;
+						$r->path = $s->path;
+						return $r;
+					})
+					->unique('profile_id');
+			});
+
+		} else {
+			$s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
+				return Story::select('stories.*', 'followers.following_id')
+					->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
+					->where('followers.profile_id', $pid)
+					->where('stories.active', true)
+					->groupBy('followers.following_id')
+					->orderByDesc('id')
+					->get();
+			});
+		}
+
+		$self = Cache::remember('pf:stories:recent-self:' . $pid, 21600, function() use($pid) {
+			return Story::whereProfileId($pid)
+				->whereActive(true)
+				->orderByDesc('id')
+				->limit(1)
 				->get()
-				->map(function($s) {
+				->map(function($s) use($pid) {
 					$r  = new \StdClass;
 					$r->id = $s->id;
-					$r->profile_id = $s->profile_id;
+					$r->profile_id = $pid;
 					$r->type = $s->type;
 					$r->path = $s->path;
 					return $r;
-				})
-				->unique('profile_id');
-		} else {
-			$s = Story::select('stories.*', 'followers.following_id')
-				->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
-				->where('followers.profile_id', $pid)
-				->where('stories.active', true)
-				->groupBy('followers.following_id')
-				->orderByDesc('id')
-				->get();
+				});
+		});
+
+		if($self->count()) {
+			$s->prepend($self->first());
 		}
 
 		$res = $s->map(function($s) use($pid) {
@@ -93,7 +118,7 @@ class StoryController extends StoryComposeController
 		$profile = Profile::findOrFail($id);
 
 		if($authed != $profile->id && !FollowerService::follows($authed, $profile->id)) {
-			return [];
+			return abort([], 403);
 		}
 
 		$stories = Story::whereProfileId($profile->id)
@@ -164,7 +189,6 @@ class StoryController extends StoryComposeController
 		$publicOnly = (bool) $profile->followedBy($authed);
 		abort_if(!$publicOnly, 403);
 
-
 		$v = StoryView::firstOrCreate([
 			'story_id' => $id,
 			'profile_id' => $authed->id

+ 2 - 1
app/Services/StoryService.php

@@ -95,7 +95,8 @@ class StoryService
 
 	public static function delLatest($pid)
 	{
-		return Cache::forget(self::STORY_KEY . 'latest:pid-' . $pid);
+		Cache::forget(self::STORY_KEY . 'latest:pid-' . $pid);
+		return Cache::forget('pf:stories:recent-self:' . $pid);
 	}
 
 	public static function addSeen($pid, $sid)