Update public timeline api, add experimental cache

This commit is contained in:
Daniel Supernault 2021-10-21 19:02:15 -06:00
parent 37abcf3898
commit 192553ff77
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
7 changed files with 113 additions and 28 deletions

View file

@ -58,7 +58,8 @@ use App\Services\{
RelationshipService, RelationshipService,
SearchApiV2Service, SearchApiV2Service,
StatusService, StatusService,
MediaBlocklistService MediaBlocklistService,
UserFilterService
}; };
use App\Util\Lexer\Autolink; use App\Util\Lexer\Autolink;
@ -1487,6 +1488,7 @@ class ApiV1Controller extends Controller
$max = $request->input('max_id'); $max = $request->input('max_id');
$limit = $request->input('limit') ?? 3; $limit = $request->input('limit') ?? 3;
$user = $request->user(); $user = $request->user();
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() { Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
if(PublicTimelineService::count() == 0) { if(PublicTimelineService::count() == 0) {
@ -1511,6 +1513,9 @@ class ApiV1Controller extends Controller
} }
return $status; return $status;
}) })
->filter(function($s) use($filtered) {
return in_array($s['account']['id'], $filtered) == false;
})
->toArray(); ->toArray();
return response()->json($res); return response()->json($res);
} }

View file

@ -56,7 +56,7 @@ class LikeController extends Controller
} }
Cache::forget('status:'.$status->id.':likedby:userid:'.$user->id); Cache::forget('status:'.$status->id.':likedby:userid:'.$user->id);
StatusService::del($status->id); StatusService::refresh($status->id);
if ($request->ajax()) { if ($request->ajax()) {
$response = ['code' => 200, 'msg' => 'Like saved', 'count' => 0]; $response = ['code' => 200, 'msg' => 'Like saved', 'count' => 0];

View file

@ -39,7 +39,6 @@ use App\Jobs\StatusPipeline\NewStatusPipeline;
use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter; use League\Fractal\Pagination\IlluminatePaginatorAdapter;
class PublicApiController extends Controller class PublicApiController extends Controller
{ {
protected $fractal; protected $fractal;
@ -288,7 +287,75 @@ class PublicApiController extends Controller
$max = $request->input('max_id'); $max = $request->input('max_id');
$limit = $request->input('limit') ?? 3; $limit = $request->input('limit') ?? 3;
$user = $request->user(); $user = $request->user();
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
if(config('exp.cached_public_timeline') == false) {
if($min || $max) {
$dir = $min ? '>' : '<';
$id = $min ?? $max;
$timeline = Status::select(
'id',
'profile_id',
'type',
'scope',
'local'
)
->where('id', $dir, $id)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereLocal(true)
->whereScope('public')
->orderBy('id', 'desc')
->limit($limit)
->get()
->map(function($s) use ($user) {
$status = StatusService::getFull($s->id, $user->profile_id);
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
return $status;
})
->filter(function($s) use($filtered) {
return in_array($s['account']['id'], $filtered) == false;
});
$res = $timeline->toArray();
} else {
$timeline = Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'reply_count',
'comments_disabled',
'created_at',
'place_id',
'likes_count',
'reblogs_count',
'updated_at'
)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->with('profile', 'hashtags', 'mentions')
->whereLocal(true)
->whereScope('public')
->orderBy('id', 'desc')
->limit($limit)
->get()
->map(function($s) use ($user) {
$status = StatusService::getFull($s->id, $user->profile_id);
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
return $status;
})
->filter(function($s) use($filtered) {
return in_array($s['account']['id'], $filtered) == false;
});
$res = $timeline->toArray();
}
} else {
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() { Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
if(PublicTimelineService::count() == 0) { if(PublicTimelineService::count() == 0) {
PublicTimelineService::warmCache(true, 400); PublicTimelineService::warmCache(true, 400);
@ -312,7 +379,11 @@ class PublicApiController extends Controller
} }
return $status; return $status;
}) })
->filter(function($s) use($filtered) {
return in_array($s['account']['id'], $filtered) == false;
})
->toArray(); ->toArray();
}
return response()->json($res); return response()->json($res);
} }

View file

@ -59,7 +59,7 @@ class LikePipeline implements ShouldQueue
return; return;
} }
StatusService::del($status->id); StatusService::refresh($status->id);
if($status->url && $actor->domain == null) { if($status->url && $actor->domain == null) {
return $this->remoteLikeDeliver(); return $this->remoteLikeDeliver();

View file

@ -63,7 +63,7 @@ class UnlikePipeline implements ShouldQueue
$status->likes_count = $count - 1; $status->likes_count = $count - 1;
$status->save(); $status->save();
StatusService::del($status->id); StatusService::refresh($status->id);
if($actor->id !== $status->profile_id && $status->url && $actor->domain == null) { if($actor->id !== $status->profile_id && $status->url && $actor->domain == null) {
$this->remoteLikeDeliver(); $this->remoteLikeDeliver();

View file

@ -62,4 +62,12 @@ class StatusService {
Cache::forget(self::key($id, false)); Cache::forget(self::key($id, false));
return Cache::forget(self::key($id)); return Cache::forget(self::key($id));
} }
public static function refresh($id)
{
Cache::forget(self::key($id, false));
Cache::forget(self::key($id, true));
self::get($id, false);
self::get($id, true);
}
} }

View file

@ -6,5 +6,6 @@ return [
'rec' => false, 'rec' => false,
'loops' => false, 'loops' => false,
'top' => env('EXP_TOP', false), 'top' => env('EXP_TOP', false),
'polls' => env('EXP_POLLS', false) 'polls' => env('EXP_POLLS', false),
'cached_public_timeline' => env('EXP_CPT', false),
]; ];