Update PublicApiController, improve accountStatuses api perf

This commit is contained in:
Daniel Supernault 2021-07-25 01:39:03 -06:00
parent c1f14f89f6
commit bce8edd994
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -656,6 +656,7 @@ class PublicApiController extends Controller
'limit' => 'nullable|integer|min:1|max:24' 'limit' => 'nullable|integer|min:1|max:24'
]); ]);
$user = $request->user();
$profile = Profile::whereNull('status')->findOrFail($id); $profile = Profile::whereNull('status')->findOrFail($id);
$limit = $request->limit ?? 9; $limit = $request->limit ?? 9;
@ -663,21 +664,21 @@ class PublicApiController extends Controller
$min_id = $request->min_id; $min_id = $request->min_id;
$scope = $request->only_media == true ? $scope = $request->only_media == true ?
['photo', 'photo:album', 'video', 'video:album'] : ['photo', 'photo:album', 'video', 'video:album'] :
['photo', 'photo:album', 'video', 'video:album', 'share', 'reply']; ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply', 'text'];
if($profile->is_private) { if($profile->is_private) {
if(!Auth::check()) { if(!$user) {
return response()->json([]); return response()->json([]);
} }
$pid = Auth::user()->profile->id; $pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) { $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id'); $following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray(); return $following->push($pid)->toArray();
}); });
$visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : []; $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
} else { } else {
if(Auth::check()) { if($user) {
$pid = Auth::user()->profile->id; $pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) { $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id'); $following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray(); return $following->push($pid)->toArray();
@ -688,84 +689,31 @@ class PublicApiController extends Controller
} }
} }
$tag = in_array('private', $visibility) ? 'private' : 'public';
if($min_id == 1 && $limit == 9 && $tag == 'public') {
$limit = 9;
$scope = ['photo', 'photo:album', 'video', 'video:album'];
$key = '_api:statuses:recent_9:'.$profile->id;
$res = Cache::remember($key, now()->addHours(24), function() use($profile, $scope, $visibility, $limit) {
$dir = '>';
$id = 1;
$timeline = Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'likes_count',
'reblogs_count',
'scope',
'visibility',
'local',
'place_id',
'comments_disabled',
'cw_summary',
'created_at',
'updated_at'
)->whereProfileId($profile->id)
->whereIn('type', $scope)
->where('id', $dir, $id)
->whereIn('visibility', $visibility)
->limit($limit)
->orderByDesc('id')
->get();
$resource = new Fractal\Resource\Collection($timeline, new StatusStatelessTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
});
return $res;
}
$dir = $min_id ? '>' : '<'; $dir = $min_id ? '>' : '<';
$id = $min_id ?? $max_id; $id = $min_id ?? $max_id;
$timeline = Status::select( $res = Status::select(
'id', 'id',
'uri',
'caption',
'rendered',
'profile_id', 'profile_id',
'type', 'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'likes_count',
'reblogs_count',
'scope', 'scope',
'visibility',
'local', 'local',
'place_id', 'created_at'
'comments_disabled', )
'cw_summary', ->whereProfileId($profile->id)
'created_at',
'updated_at'
)->whereProfileId($profile->id)
->whereIn('type', $scope) ->whereIn('type', $scope)
->where('id', $dir, $id) ->where('id', $dir, $id)
->whereIn('visibility', $visibility) ->whereIn('scope', $visibility)
->limit($limit) ->limit($limit)
->orderByDesc('id') ->orderByDesc('id')
->get(); ->get()
->map(function($s) use($user) {
$resource = new Fractal\Resource\Collection($timeline, new StatusStatelessTransformer()); $status = StatusService::get($s->id, false);
$res = $this->fractal->createData($resource)->toArray(); if($user) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); }
return $status;
});
return response()->json($res);
} }
} }