From 633351f6dcb15b5e09f87520ea750b8a2e3d3137 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Fri, 5 May 2023 06:42:17 -0600 Subject: [PATCH] Update StoryController, show active self stories on home timeline --- app/Http/Controllers/StoryController.php | 60 +++++++++++++++++------- app/Services/StoryService.php | 3 +- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/StoryController.php b/app/Http/Controllers/StoryController.php index 216fed8b4..5a9fb5530 100644 --- a/app/Http/Controllers/StoryController.php +++ b/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 diff --git a/app/Services/StoryService.php b/app/Services/StoryService.php index 6c1d95b8e..811837614 100644 --- a/app/Services/StoryService.php +++ b/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)