diff --git a/app/Transformer/Api/AccountTransformer.php b/app/Transformer/Api/AccountTransformer.php index 57d6bdb4c..96919f169 100644 --- a/app/Transformer/Api/AccountTransformer.php +++ b/app/Transformer/Api/AccountTransformer.php @@ -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); diff --git a/app/Transformer/Api/Mastodon/v1/AccountTransformer.php b/app/Transformer/Api/Mastodon/v1/AccountTransformer.php index 6c6576987..eb8406b9f 100644 --- a/app/Transformer/Api/Mastodon/v1/AccountTransformer.php +++ b/app/Transformer/Api/Mastodon/v1/AccountTransformer.php @@ -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(), + ]; + }); } }