Update AccountTransformer

pull/7118/head
Daniel Supernault 2 weeks ago
parent 48750f7079
commit 94b8fea32a
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1

@ -5,7 +5,6 @@ namespace App\Transformer\Api;
use App\Models\Profile;
use App\Models\User;
use App\Models\UserSetting;
use App\Services\AccountService;
use App\Services\PronounService;
use Illuminate\Support\Facades\Cache;
use League\Fractal;
@ -26,7 +25,6 @@ class AccountTransformer extends Fractal\TransformerAbstract
return User::whereIsAdmin(true)->pluck('profile_id')->toArray();
});
$local = $profile->private_key != null;
$local = $profile->user_id && $profile->private_key != null;
$hideFollowing = false;
$hideFollowers = false;
@ -75,20 +73,38 @@ class AccountTransformer extends Fractal\TransformerAbstract
'location' => $profile->location,
];
if ($profile->moved_to_profile_id) {
$newProfile = AccountService::get($profile->moved_to_profile_id);
if ($newProfile && isset($newProfile['id'], $newProfile['acct'])) {
$res['moved'] = [
'id' => $newProfile['id'],
'acct' => $newProfile['acct'],
'avatar' => $newProfile['avatar'],
];
}
$moved = $this->resolveMoved($profile);
if ($moved) {
$res['moved'] = $moved;
}
return $res;
}
protected function resolveMoved(Profile $profile): ?array
{
$targetId = $profile->moved_to_profile_id;
if (! $targetId || (string) $targetId === (string) $profile->id) {
return null;
}
return Cache::remember('pf:acct-trans:moved:'.$targetId, 3600, function () use ($targetId) {
$target = Profile::find($targetId);
if (! $target) {
return null;
}
$targetLocal = $target->user_id && $target->private_key != null;
return [
'id' => (string) $target->id,
'acct' => $targetLocal ? $target->username : substr($target->username, 1),
'avatar' => $target->avatarUrl(),
];
});
}
protected function includeRelationship(Profile $profile)
{
return $this->item($profile, new RelationshipTransformer);

@ -3,7 +3,7 @@
namespace App\Transformer\Api\Mastodon\v1;
use App\Models\Profile;
use App\Services\AccountService;
use Illuminate\Support\Facades\Cache;
use League\Fractal;
class AccountTransformer extends Fractal\TransformerAbstract
@ -32,21 +32,34 @@ class AccountTransformer extends Fractal\TransformerAbstract
'statuses_count' => (int) $profile->statusCount(),
'last_status_at' => $profile->last_status_at?->toJSON(),
'emojis' => [],
'moved' => null,
'moved' => $this->resolveMoved($profile),
'fields' => [],
];
if ($profile->moved_to_profile_id) {
$newProfile = AccountService::get($profile->moved_to_profile_id);
if ($newProfile && isset($newProfile['id'], $newProfile['acct'])) {
$res['moved'] = [
'id' => $newProfile['id'],
'acct' => $newProfile['acct'],
'avatar' => $newProfile['avatar'],
];
}
return $res;
}
protected function resolveMoved(Profile $profile): ?array
{
$targetId = $profile->moved_to_profile_id;
if (! $targetId || (string) $targetId === (string) $profile->id) {
return null;
}
return $res;
return Cache::remember('pf:acct-trans:moved:'.$targetId, 3600, function () use ($targetId) {
$target = Profile::find($targetId);
if (! $target) {
return null;
}
$targetLocal = $target->user_id && $target->private_key != null;
return [
'id' => (string) $target->id,
'acct' => $targetLocal ? $target->username : substr($target->username, 1),
'avatar' => $target->avatarUrl(),
];
});
}
}

Loading…
Cancel
Save