Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return user relation json for friends listing endpoint #11582

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}