From aee9b994e373efcdeaba9462cfe933cbda1a7046 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 19 Dec 2021 04:07:40 -0700 Subject: [PATCH] Improve DiscoverService --- app/Http/Controllers/Api/ApiV1Controller.php | 2 +- .../Controllers/InternalApiController.php | 2 +- app/Services/DiscoverService.php | 30 +++++++++---------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 7b8dfa403..43cf046b4 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -2446,7 +2446,7 @@ class ApiV1Controller extends Controller $pid = $request->user()->profile_id; $filters = UserFilterService::filters($pid); $forYou = DiscoverService::getForYou(); - $posts = $forYou->random(50)->map(function($post) { + $posts = $forYou->take(50)->map(function($post) { return StatusService::get($post); }) ->filter(function($post) use($filters) { diff --git a/app/Http/Controllers/InternalApiController.php b/app/Http/Controllers/InternalApiController.php index a8586d12d..059e5124f 100644 --- a/app/Http/Controllers/InternalApiController.php +++ b/app/Http/Controllers/InternalApiController.php @@ -72,7 +72,7 @@ class InternalApiController extends Controller $pid = $request->user()->profile_id; $filters = UserFilterService::filters($pid); $forYou = DiscoverService::getForYou(); - $posts = $forYou->random(50)->map(function($post) { + $posts = $forYou->take(50)->map(function($post) { return StatusService::get($post); }) ->filter(function($post) use($filters) { diff --git a/app/Services/DiscoverService.php b/app/Services/DiscoverService.php index 6f4acfc01..ae6bce3b0 100644 --- a/app/Services/DiscoverService.php +++ b/app/Services/DiscoverService.php @@ -3,28 +3,26 @@ namespace App\Services; use Illuminate\Support\Facades\Cache; -use App\Status; +use Illuminate\Support\Facades\DB; class DiscoverService { public static function getDailyIdPool() { $min_id = SnowflakeService::byDate(now()->subMonths(3)); - return Status::select( - 'id', - 'is_nsfw', - 'profile_id', - 'type', - 'uri', - ) - ->whereNull('uri') - ->whereType('photo') - ->whereIsNsfw(false) - ->whereVisibility('public') - ->where('id', '>', $min_id) - ->inRandomOrder() - ->take(300) - ->pluck('id'); + $sqld = config('database.default') == 'mysql'; + return DB::table('statuses') + ->whereNull('uri') + ->whereType('photo') + ->whereIsNsfw(false) + ->whereVisibility('public') + ->when($sqld, function($q, $sqld) { + return $q->groupBy('profile_id'); + }) + ->where('id', '>', $min_id) + ->inRandomOrder() + ->take(300) + ->pluck('id'); } public static function getForYou()