Skip to content

Commit

Permalink
Return user relation json for friends listing endpoint
Browse files Browse the repository at this point in the history
Requires specifying api version 20241022 or higher.
  • Loading branch information
nanaya committed Oct 22, 2024
1 parent cea8921 commit 95de3f2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
32 changes: 22 additions & 10 deletions app/Http/Controllers/FriendsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\User;
use App\Models\UserRelation;
use App\Transformers\UserCompactTransformer;
use App\Transformers\UserRelationTransformer;
use Auth;
use Exception;

Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ public function getAttribute($key)
'rankHighests',
'rankHistories',
'receivedKudosu',
'relationFriends',
'relations',
'replaysWatchedCounts',
'reportedIn',
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/Transformers/UserCompactTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
12 changes: 10 additions & 2 deletions app/Transformers/UserRelationTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace App\Transformers;

use App\Models\UserRelation;
use League\Fractal\ParamBag;

class UserRelationTransformer extends TransformerAbstract
{
Expand All @@ -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);
}
}

0 comments on commit 95de3f2

Please sign in to comment.