From 96afc3e7819b6f41fe9b8b090fef93c0af0a1dbc Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 9 Jan 2021 02:41:17 -0700 Subject: [PATCH 1/3] Update Controllers, add last_active_at logic --- app/Http/Controllers/Api/ApiV1Controller.php | 20 ++++++++++++++++++ app/Http/Controllers/PublicApiController.php | 22 +++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index dbf8f6116..2ecd3ed05 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -1320,6 +1320,15 @@ class ApiV1Controller extends Controller $min = $request->input('min_id'); $max = $request->input('max_id'); $limit = $request->input('limit') ?? 3; + $user = $request->user(); + + $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; + }); $pid = $request->user()->profile_id; @@ -1424,6 +1433,15 @@ class ApiV1Controller extends Controller $min = $request->input('min_id'); $max = $request->input('max_id'); $limit = $request->input('limit') ?? 3; + $user = $request->user(); + + $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; + }); if($min || $max) { $dir = $min ? '>' : '<'; @@ -1452,6 +1470,7 @@ class ApiV1Controller extends Controller ->with('profile', 'hashtags', 'mentions') ->where('id', $dir, $id) ->whereScope('public') + ->where('created_at', '>', now()->subDays(14)) ->latest() ->limit($limit) ->get(); @@ -1479,6 +1498,7 @@ class ApiV1Controller extends Controller ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album']) ->with('profile', 'hashtags', 'mentions') ->whereScope('public') + ->where('created_at', '>', now()->subDays(14)) ->latest() ->simplePaginate($limit); } diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index 3ba2e01da..92e3130ad 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/app/Http/Controllers/PublicApiController.php @@ -272,8 +272,17 @@ class PublicApiController extends Controller $min = $request->input('min_id'); $max = $request->input('max_id'); $limit = $request->input('limit') ?? 3; + $user = $request->user(); - $filtered = UserFilter::whereUserId(Auth::user()->profile_id) + $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) ->whereFilterableType('App\Profile') ->whereIn('filter_type', ['mute', 'block']) ->pluck('filterable_id')->toArray(); @@ -305,6 +314,7 @@ class PublicApiController extends Controller ->whereNotIn('profile_id', $filtered) ->whereLocal(true) ->whereScope('public') + ->where('created_at', '>', now()->subDays(14)) ->orderBy('created_at', 'desc') ->limit($limit) ->get(); @@ -333,6 +343,7 @@ class PublicApiController extends Controller ->with('profile', 'hashtags', 'mentions') ->whereLocal(true) ->whereScope('public') + ->where('created_at', '>', now()->subDays(14)) ->orderBy('created_at', 'desc') ->simplePaginate($limit); } @@ -360,6 +371,15 @@ class PublicApiController extends Controller $min = $request->input('min_id'); $max = $request->input('max_id'); $limit = $request->input('limit') ?? 3; + $user = $request->user(); + + $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; + }); // TODO: Use redis for timelines // $timeline = Timeline::build()->local(); From b2fe219eeca0495f7326942a1c575a422604a10c Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 11 Jan 2021 00:31:21 -0700 Subject: [PATCH 2/3] Update DiscoverController, add albums and video to discover trending api --- app/Http/Controllers/DiscoverController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/DiscoverController.php b/app/Http/Controllers/DiscoverController.php index 53c7e8caa..dbad2f3f8 100644 --- a/app/Http/Controllers/DiscoverController.php +++ b/app/Http/Controllers/DiscoverController.php @@ -170,26 +170,26 @@ class DiscoverController extends Controller public function trendingApi(Request $request) { $this->validate($request, [ - 'range' => 'nullable|string|in:daily,monthly,alltime' + 'range' => 'nullable|string|in:daily,monthly' ]); $range = $request->filled('range') ? $request->input('range') == 'alltime' ? '-1' : ($request->input('range') == 'daily' ? 1 : 31) : 1; - $key = ':api:discover:trending:v2:range:' . $range; + $key = ':api:discover:trending:v2.1:range:' . $range; $ttl = now()->addHours(2); $res = Cache::remember($key, $ttl, function() use($range) { if($range == '-1') { $res = Status::whereScope('public') - ->whereType('photo') + ->whereIn('type', ['photo', 'photo:album', 'video']) ->whereIsNsfw(false) ->orderBy('likes_count','desc') ->take(12) ->get(); } else { $res = Status::whereScope('public') - ->whereType('photo') + ->whereIn('type', ['photo', 'photo:album', 'video']) ->whereIsNsfw(false) ->orderBy('likes_count','desc') ->take(12) From a443694dc0802ff9598f7e453697359787f064b5 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 11 Jan 2021 18:47:16 -0700 Subject: [PATCH 3/3] Update PublicApiController --- app/Http/Controllers/PublicApiController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index 92e3130ad..1402940ef 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/app/Http/Controllers/PublicApiController.php @@ -310,11 +310,11 @@ class PublicApiController extends Controller 'created_at', 'updated_at' )->where('id', $dir, $id) - ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) + ->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->whereNotIn('profile_id', $filtered) ->whereLocal(true) ->whereScope('public') - ->where('created_at', '>', now()->subDays(14)) + ->where('created_at', '>', now()->subMonths(3)) ->orderBy('created_at', 'desc') ->limit($limit) ->get(); @@ -338,12 +338,12 @@ class PublicApiController extends Controller 'likes_count', 'reblogs_count', 'updated_at' - )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) + )->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->whereNotIn('profile_id', $filtered) ->with('profile', 'hashtags', 'mentions') ->whereLocal(true) ->whereScope('public') - ->where('created_at', '>', now()->subDays(14)) + ->where('created_at', '>', now()->subMonths(3)) ->orderBy('created_at', 'desc') ->simplePaginate($limit); } @@ -429,7 +429,7 @@ class PublicApiController extends Controller 'reblogs_count', 'created_at', 'updated_at' - )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) + )->whereIn('type', ['text','photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->with('profile', 'hashtags', 'mentions') ->where('id', $dir, $id) ->whereIn('profile_id', $following) @@ -458,7 +458,7 @@ class PublicApiController extends Controller 'reblogs_count', 'created_at', 'updated_at' - )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) + )->whereIn('type', ['text','photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->with('profile', 'hashtags', 'mentions') ->whereIn('profile_id', $following) ->whereNotIn('profile_id', $filtered)