diff --git a/app/Http/Controllers/FriendsController.php b/app/Http/Controllers/FriendsController.php index 40383979c97..d8046a3fb2d 100644 --- a/app/Http/Controllers/FriendsController.php +++ b/app/Http/Controllers/FriendsController.php @@ -9,6 +9,7 @@ use App\Models\User; use App\Models\UserRelation; use App\Transformers\UserCompactTransformer; +use App\Transformers\UserRelationTransformer; use Auth; use Exception; @@ -35,23 +36,34 @@ public function index() $currentUser = auth()->user(); $currentMode = default_mode(); - $friends = $currentUser - ->friends() - ->with(UserCompactTransformer::listIncludesPreload($currentMode)) - ->orderBy('username', 'asc') - ->get(); + $relationFriends = $currentUser->relationFriends->sortBy('username'); + $relationFriends->load(array_map( + fn ($userPreload) => "target.{$userPreload}", + UserCompactTransformer::listIncludesPreload($currentMode), + )); + + $isApi = is_api_request(); + + if ($isApi && api_version() >= 20241022) { + return json_collection($relationFriends, new UserRelationTransformer(), [ + "target:ruleset({$currentMode})", + ...array_map( + fn ($userInclude) => "target.{$userInclude}", + UserCompactTransformer::LIST_INCLUDES, + ), + ]); + } + $friends = $relationFriends->pluck('target'); $usersJson = json_collection( $friends, (new UserCompactTransformer())->setMode($currentMode), UserCompactTransformer::LIST_INCLUDES ); - if (is_api_request()) { - return $usersJson; - } - - return ext_view('friends.index', compact('usersJson')); + return $isApi + ? $usersJson + : ext_view('friends.index', compact('usersJson')); } public function store() diff --git a/app/Models/User.php b/app/Models/User.php index da10756d17b..5a03d87eb1d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -923,6 +923,7 @@ public function getAttribute($key) 'rankHighests', 'rankHistories', 'receivedKudosu', + 'relationFriends', 'relations', 'replaysWatchedCounts', 'reportedIn', @@ -1518,6 +1519,11 @@ public function usernameChangeHistoryPublic() ->orderBy('timestamp', 'ASC'); } + public function relationFriends(): HasMany + { + return $this->relations()->friends()->withMutual(); + } + public function relations() { return $this->hasMany(UserRelation::class); diff --git a/app/Transformers/UserCompactTransformer.php b/app/Transformers/UserCompactTransformer.php index 461eba7f7af..c0f628e601d 100644 --- a/app/Transformers/UserCompactTransformer.php +++ b/app/Transformers/UserCompactTransformer.php @@ -255,7 +255,7 @@ public function includeFollowerCount(User $user) public function includeFriends(User $user) { return $this->collection( - $user->relations()->friends()->withMutual()->get(), + $user->relationFriends, new UserRelationTransformer() ); } diff --git a/app/Transformers/UserRelationTransformer.php b/app/Transformers/UserRelationTransformer.php index 7e0819e17eb..291bec3cffa 100644 --- a/app/Transformers/UserRelationTransformer.php +++ b/app/Transformers/UserRelationTransformer.php @@ -6,6 +6,7 @@ namespace App\Transformers; use App\Models\UserRelation; +use League\Fractal\ParamBag; class UserRelationTransformer extends TransformerAbstract { @@ -23,8 +24,15 @@ public function transform(UserRelation $userRelation) ]; } - public function includeTarget(UserRelation $userRelation) + public function includeTarget(UserRelation $userRelation, ?ParamBag $params = null) { - return $this->item($userRelation->target, new UserCompactTransformer()); + $transformer = new UserCompactTransformer(); + + $rulesetName = $params?->get('ruleset')[0]; + if ($rulesetName !== null) { + $transformer->setMode($rulesetName); + } + + return $this->item($userRelation->target, $transformer); } }