Skip to content

Commit 633351f

Browse files
committed
Update StoryController, show active self stories on home timeline
1 parent 73429ac commit 633351f

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

app/Http/Controllers/StoryController.php

+42-18
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,53 @@ public function recent(Request $request)
3737
$pid = $request->user()->profile_id;
3838

3939
if(config('database.default') == 'pgsql') {
40-
$s = Story::select('stories.*', 'followers.following_id')
41-
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
42-
->where('followers.profile_id', $pid)
43-
->where('stories.active', true)
40+
$s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
41+
return Story::select('stories.*', 'followers.following_id')
42+
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
43+
->where('followers.profile_id', $pid)
44+
->where('stories.active', true)
45+
->get()
46+
->map(function($s) {
47+
$r = new \StdClass;
48+
$r->id = $s->id;
49+
$r->profile_id = $s->profile_id;
50+
$r->type = $s->type;
51+
$r->path = $s->path;
52+
return $r;
53+
})
54+
->unique('profile_id');
55+
});
56+
57+
} else {
58+
$s = Cache::remember('pf:stories:recent-by-id:' . $pid, 900, function() use($pid) {
59+
return Story::select('stories.*', 'followers.following_id')
60+
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
61+
->where('followers.profile_id', $pid)
62+
->where('stories.active', true)
63+
->groupBy('followers.following_id')
64+
->orderByDesc('id')
65+
->get();
66+
});
67+
}
68+
69+
$self = Cache::remember('pf:stories:recent-self:' . $pid, 21600, function() use($pid) {
70+
return Story::whereProfileId($pid)
71+
->whereActive(true)
72+
->orderByDesc('id')
73+
->limit(1)
4474
->get()
45-
->map(function($s) {
75+
->map(function($s) use($pid) {
4676
$r = new \StdClass;
4777
$r->id = $s->id;
48-
$r->profile_id = $s->profile_id;
78+
$r->profile_id = $pid;
4979
$r->type = $s->type;
5080
$r->path = $s->path;
5181
return $r;
52-
})
53-
->unique('profile_id');
54-
} else {
55-
$s = Story::select('stories.*', 'followers.following_id')
56-
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
57-
->where('followers.profile_id', $pid)
58-
->where('stories.active', true)
59-
->groupBy('followers.following_id')
60-
->orderByDesc('id')
61-
->get();
82+
});
83+
});
84+
85+
if($self->count()) {
86+
$s->prepend($self->first());
6287
}
6388

6489
$res = $s->map(function($s) use($pid) {
@@ -93,7 +118,7 @@ public function profile(Request $request, $id)
93118
$profile = Profile::findOrFail($id);
94119

95120
if($authed != $profile->id && !FollowerService::follows($authed, $profile->id)) {
96-
return [];
121+
return abort([], 403);
97122
}
98123

99124
$stories = Story::whereProfileId($profile->id)
@@ -164,7 +189,6 @@ public function viewed(Request $request)
164189
$publicOnly = (bool) $profile->followedBy($authed);
165190
abort_if(!$publicOnly, 403);
166191

167-
168192
$v = StoryView::firstOrCreate([
169193
'story_id' => $id,
170194
'profile_id' => $authed->id

app/Services/StoryService.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public static function latest($pid)
9595

9696
public static function delLatest($pid)
9797
{
98-
return Cache::forget(self::STORY_KEY . 'latest:pid-' . $pid);
98+
Cache::forget(self::STORY_KEY . 'latest:pid-' . $pid);
99+
return Cache::forget('pf:stories:recent-self:' . $pid);
99100
}
100101

101102
public static function addSeen($pid, $sid)

0 commit comments

Comments
 (0)