Update ApiV1Controller, add network timeline support via NetworkTimelineService

This commit is contained in:
Daniel Supernault 2022-06-09 05:11:39 -06:00
parent 37f97ccc2d
commit f54fd6e9cc
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -62,6 +62,7 @@ use App\Services\{
FollowerService, FollowerService,
InstanceService, InstanceService,
LikeService, LikeService,
NetworkTimelineService,
NotificationService, NotificationService,
MediaPathService, MediaPathService,
PublicTimelineService, PublicTimelineService,
@ -1899,28 +1900,46 @@ class ApiV1Controller extends Controller
$this->validate($request,[ $this->validate($request,[
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
'limit' => 'nullable|integer|max:100' 'limit' => 'nullable|integer|max:100',
'remote' => 'sometimes'
]); ]);
$min = $request->input('min_id'); $min = $request->input('min_id');
$max = $request->input('max_id'); $max = $request->input('max_id');
$limit = $request->input('limit') ?? 20; $limit = $request->input('limit') ?? 20;
$user = $request->user(); $user = $request->user();
$remote = $request->has('remote');
$filtered = $user ? UserFilterService::filters($user->profile_id) : []; $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() { if($remote && config('instance.timeline.network.cached')) {
if(PublicTimelineService::count() == 0) { Cache::remember('api:v1:timelines:network:cache_check', 10368000, function() {
PublicTimelineService::warmCache(true, 400); if(NetworkTimelineService::count() == 0) {
} NetworkTimelineService::warmCache(true, config('instance.timeline.network.cache_dropoff'));
}); }
});
if ($max) { if ($max) {
$feed = PublicTimelineService::getRankedMaxId($max, $limit + 5); $feed = NetworkTimelineService::getRankedMaxId($max, $limit + 5);
} else if ($min) { } else if ($min) {
$feed = PublicTimelineService::getRankedMinId($min, $limit + 5); $feed = NetworkTimelineService::getRankedMinId($min, $limit + 5);
} else { } else {
$feed = PublicTimelineService::get(0, $limit + 5); $feed = NetworkTimelineService::get(0, $limit + 5);
} }
} else {
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
if(PublicTimelineService::count() == 0) {
PublicTimelineService::warmCache(true, 400);
}
});
if ($max) {
$feed = PublicTimelineService::getRankedMaxId($max, $limit + 5);
} else if ($min) {
$feed = PublicTimelineService::getRankedMinId($min, $limit + 5);
} else {
$feed = PublicTimelineService::get(0, $limit + 5);
}
}
$res = collect($feed) $res = collect($feed)
->map(function($k) use($user) { ->map(function($k) use($user) {
@ -1943,6 +1962,9 @@ class ApiV1Controller extends Controller
// ->toArray(); // ->toArray();
$baseUrl = config('app.url') . '/api/v1/timelines/public?limit=' . $limit . '&'; $baseUrl = config('app.url') . '/api/v1/timelines/public?limit=' . $limit . '&';
if($remote) {
$baseUrl .= 'remote=1&';
}
$minId = $res->map(function($s) { $minId = $res->map(function($s) {
return ['id' => $s['id']]; return ['id' => $s['id']];
})->min('id'); })->min('id');