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 UserRelation instead of UserCompact for friends API #7768

Closed
wants to merge 3 commits into from
Closed
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
95 changes: 91 additions & 4 deletions app/Http/Controllers/FriendsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Auth;
use Request;

/**
* @group Friends
*/
class FriendsController extends Controller
{
public function __construct()
Expand All @@ -30,8 +33,96 @@ public function __construct()
return parent::__construct();
}

/**
* Get Friends
*
* Returns the authenticated user's friends list.
*
* ---
*
* ### Response Format
*
* A collection of [UserRelation](#userrelation) objects with `target` included. `target`s include `country`, `cover`, `groups`, and `support_level`.
*
* @response [
* {
* "target_id": 2,
* "relation_type": "friend",
* "mutual": false,
* "target": {
* "avatar_url": "https://a.ppy.sh/2?1537409912.jpeg",
* "country_code": "AU",
* "default_group": "default",
* "id": 2,
* "is_active": true,
* "is_bot": false,
* "is_deleted": false,
* "is_online": false,
* "is_supporter": true,
* "last_visit": "2021-06-21T06:55:47+00:00",
* "pm_friends_only": false,
* "profile_colour": "#3366FF",
* "username": "peppy",
* "country": {
* "code": "AU",
* "name": "Australia"
* },
* "cover": {
* "custom_url": "https://assets.ppy.sh/user-profile-covers/2/baba245ef60834b769694178f8f6d4f6166c5188c740de084656ad2b80f1eea7.jpeg",
* "url": "https://assets.ppy.sh/user-profile-covers/2/baba245ef60834b769694178f8f6d4f6166c5188c740de084656ad2b80f1eea7.jpeg",
* "id": null
* },
* "groups": [
* {
* "colour": "#0066FF",
* "has_listing": false,
* "has_playmodes": false,
* "id": 33,
* "identifier": "ppy",
* "is_probationary": false,
* "name": "ppy",
* "short_name": "PPY",
* "playmodes": null
* },
* {
* "colour": "#EB47D0",
* "has_listing": true,
* "has_playmodes": false,
* "id": 11,
* "identifier": "dev",
* "is_probationary": false,
* "name": "Developers",
* "short_name": "DEV",
* "playmodes": null
* }
* ],
* "support_level": 3
* }
* },
* // ...
* ]
*/
public function index()
{
if (is_api_request()) {
return json_collection(
auth()->user()
->relations()
->friends()
->withMutual()
Comment on lines +110 to +112
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make its own relation and load that instead.

// in User model
public function relationFriends()
{
  return $this->relations()->friends()->withMutual();
}

// in this controller action
// the loaded reloaded data will be used regardless (in json for api, in current user default json for html)
$relations = $currentUser->relationFriends;
$relations->load(['target' => fn ($q) => $q->...]);

if (is_api...) {
    ...
}

$user = $relations->pluck('target');

// in Transformers etc
$user->relationFriends

->with(['target' => fn ($query) => $query->eagerloadForListing()])
->get(),
'UserRelation',
[
'target',
'target.country',
'target.cover',
'target.groups',
'target.support_level',
Comment on lines +117 to +121
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static $userIncludes = [ ... ];

if (is_api_request()) {
  return json_collection(..., ..., [
    "target:mode({$currentMode})",
    ...array_map(..., $userIncludes),
  ]);
}

],
);
}

$currentUser = auth()->user();
$currentMode = default_mode();

Expand All @@ -52,10 +143,6 @@ public function index()
'support_level',
]);

if (is_api_request()) {
return $usersJson;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About statistics; identify scope is always implied. If anything, the check should be at default_mode function instead.

return ext_view('friends.index', compact('usersJson'));
}

Expand Down
22 changes: 22 additions & 0 deletions resources/views/docs/_structures/user_relation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## UserRelation
```json
{
"target_id": 3,
"relation_type": "friend",
"mutual": true
}
```

Field | Type | Notes
--------------|---------------------|------------
mutual | boolean | Always `false` for blocks.
relation_type | `block` \| `friend` | |
target_id | number | |

### Optional Attributes

The following are attributes which may be additionally included in responses. Relevant endpoints should list them if applicable.

Field | Type
-------|-----
target | [UserCompact](#usercompact)
3 changes: 3 additions & 0 deletions resources/views/docs/info.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@

## Breaking Changes

### 2021-06-21
- [`/friends`](#get-friends) returns a collection of [UserRelation](#userrelation) instead of [UserCompact](#usercompact).

### 2021-06-09
- `ranked_and_approved_beatmapset_count` and `unranked_beatmapset_count` attributes in [UserCompact](#usercompact) object have been deprecated and replaced with `ranked_beatmapset_count` and `pending_beatmapset_count` respectively.
- `ranked_and_approved` and `unranked` types in [Get User Beatmaps](#get-user-beatmaps) have been deprecated and replaced with `ranked` and `pending` respectively.
Expand Down