Update InternalApiController, use DiscoverService for discoverPosts method

This commit is contained in:
Daniel Supernault 2021-12-19 00:48:05 -07:00
parent 493c5ca0ce
commit f36da7816f
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -41,6 +41,8 @@ use App\Services\ModLogService;
use App\Services\PublicTimelineService; use App\Services\PublicTimelineService;
use App\Services\SnowflakeService; use App\Services\SnowflakeService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Services\UserFilterService;
use App\Services\DiscoverService;
class InternalApiController extends Controller class InternalApiController extends Controller
{ {
@ -67,51 +69,21 @@ class InternalApiController extends Controller
public function discoverPosts(Request $request) public function discoverPosts(Request $request)
{ {
$profile = Auth::user()->profile; $pid = $request->user()->profile_id;
$pid = $profile->id; $filters = UserFilterService::filters($pid);
$following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(15), function() use ($pid) { $forYou = DiscoverService::getForYou();
return Follower::whereProfileId($pid)->pluck('following_id')->toArray(); $posts = $forYou->random(50)->map(function($post) {
}); return StatusService::get($post);
$filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(15), function() use($pid) { })
$private = Profile::whereIsPrivate(true) ->filter(function($post) use($filters) {
->orWhere('unlisted', true) return $post &&
->orWhere('status', '!=', null) isset($post['account']) &&
->pluck('id') isset($post['account']['id']) &&
->toArray(); !in_array($post['account']['id'], $filters);
$filters = UserFilter::whereUserId($pid) })
->whereFilterableType('App\Profile') ->take(12)
->whereIn('filter_type', ['mute', 'block']) ->values();
->pluck('filterable_id') return response()->json(compact('posts'));
->toArray();
return array_merge($private, $filters);
});
$following = array_merge($following, $filters);
$sql = config('database.default') !== 'pgsql';
$min_id = SnowflakeService::byDate(now()->subMonths(3));
$posts = Status::select(
'id',
'is_nsfw',
'profile_id',
'type',
'uri',
)
->whereNull('uri')
->whereIn('type', ['photo','photo:album', 'video'])
->whereIsNsfw(false)
->whereVisibility('public')
->whereNotIn('profile_id', $following)
->where('id', '>', $min_id)
->inRandomOrder()
->take(39)
->pluck('id');
$res = [
'posts' => $posts->map(function($post) {
return StatusService::get($post);
})
];
return response()->json($res);
} }
public function directMessage(Request $request, $profileId, $threadId) public function directMessage(Request $request, $profileId, $threadId)