Update ApiV1Controller, map AccountService

This commit is contained in:
Daniel Supernault 2022-11-17 20:13:26 -07:00
parent 8b7121f9fb
commit e71972d8f1
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 17 additions and 8 deletions

View file

@ -3023,7 +3023,7 @@ class ApiV1Controller extends Controller
} }
if($sortBy == 'all' && !$request->has('cursor')) { if($sortBy == 'all' && !$request->has('cursor')) {
$ids = Cache::remember('status:replies:all:' . $id, 86400, function() use($id) { $ids = Cache::remember('status:replies:all:' . $id, 3600, function() use($id) {
return DB::table('statuses') return DB::table('statuses')
->where('in_reply_to_id', $id) ->where('in_reply_to_id', $id)
->orderBy('id') ->orderBy('id')
@ -3058,8 +3058,15 @@ class ApiV1Controller extends Controller
$status['favourited'] = LikeService::liked($pid, $post->id); $status['favourited'] = LikeService::liked($pid, $post->id);
return $status; return $status;
}) })
->map(function($post) {
if(isset($post['account']) && isset($post['account']['id'])) {
$account = AccountService::get($post['account']['id'], true);
$post['account'] = $account;
}
return $post;
})
->filter(function($post) { ->filter(function($post) {
return $post && isset($post['id']) && isset($post['account']); return $post && isset($post['id']) && isset($post['account']) && isset($post['account']['id']);
}) })
->values(); ->values();

View file

@ -19,19 +19,21 @@ class AccountService
public static function get($id, $softFail = false) public static function get($id, $softFail = false)
{ {
return Cache::remember(self::CACHE_KEY . $id, 43200, function() use($id, $softFail) { $res = Cache::remember(self::CACHE_KEY . $id, 43200, function() use($id) {
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$profile = Profile::find($id); $profile = Profile::find($id);
if(!$profile || $profile->status === 'delete') { if(!$profile || $profile->status === 'delete') {
if($softFail) {
return null; return null;
} }
abort(404);
}
$resource = new Fractal\Resource\Item($profile, new AccountTransformer()); $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return $fractal->createData($resource)->toArray(); return $fractal->createData($resource)->toArray();
}); });
if(!$res) {
return $softFail ? null : abort(404);
}
return $res;
} }
public static function getMastodon($id, $softFail = false) public static function getMastodon($id, $softFail = false)