diff --git a/app/Http/Controllers/FriendsController.php b/app/Http/Controllers/FriendsController.php index f2bc0f67622..896cc2648fe 100644 --- a/app/Http/Controllers/FriendsController.php +++ b/app/Http/Controllers/FriendsController.php @@ -12,6 +12,9 @@ use Auth; use Request; +/** + * @group Friends + */ class FriendsController extends Controller { public function __construct() @@ -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() + ->with(['target' => fn ($query) => $query->eagerloadForListing()]) + ->get(), + 'UserRelation', + [ + 'target', + 'target.country', + 'target.cover', + 'target.groups', + 'target.support_level', + ], + ); + } + $currentUser = auth()->user(); $currentMode = default_mode(); @@ -52,10 +143,6 @@ public function index() 'support_level', ]); - if (is_api_request()) { - return $usersJson; - } - return ext_view('friends.index', compact('usersJson')); } diff --git a/resources/views/docs/_structures/user_relation.md b/resources/views/docs/_structures/user_relation.md new file mode 100644 index 00000000000..682e5be29b2 --- /dev/null +++ b/resources/views/docs/_structures/user_relation.md @@ -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) diff --git a/resources/views/docs/info.blade.php b/resources/views/docs/info.blade.php index fcdd46fc48a..3860dd8c33d 100644 --- a/resources/views/docs/info.blade.php +++ b/resources/views/docs/info.blade.php @@ -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.