Skip to content

Commit

Permalink
✨ allow searching explicitly for username or displayname
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu committed Dec 28, 2024
1 parent 2543eef commit d08fea7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
42 changes: 38 additions & 4 deletions app/Http/Controllers/API/v1/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,15 @@ public function destroyMute(int $userId): JsonResponse {

/**
* @OA\Get(
* path="/user/search/{query}",
* path="/user/search/{?query}",
* operationId="searchUsers",
* tags={"User"},
* summary="Get paginated statuses for single user",
* description="Returns paginated statuses of a single user specified by the username",
* @OA\Parameter (
* name="query",
* in="path",
* description="username",
* description="If this is given, the search will be performed on the username and (display)name (or-search)",
* example="Gertrud123",
* ),
* @OA\Parameter (
Expand All @@ -453,6 +453,16 @@ public function destroyMute(int $userId): JsonResponse {
* in="query",
* @OA\Schema(type="integer")
* ),
* @OA\Parameter (
* name="username",
* in="query",
* description="Search for parts username",
* ),
* @OA\Parameter (
* name="name",
* in="query",
* description="Search for parts of users (display)name",
* ),
* @OA\Response(
* response=200,
* description="successful operation",
Expand All @@ -473,9 +483,33 @@ public function destroyMute(int $userId): JsonResponse {
* )
*
*/
public function search(string $query): AnonymousResourceCollection|JsonResponse {
public function search(Request $request, ?string $query = null): AnonymousResourceCollection|JsonResponse {
try {
return UserResource::collection(BackendUserBackend::searchUser($query));
$validated = $request->validate([
'username' => ['nullable', 'string', 'max:255'],
'name' => ['nullable', 'string', 'max:255'],
]);
if (empty($validated) && isset($query)) {
// if no specific search criteria is given, search for the query in username and display_name
return UserResource::collection(BackendUserBackend::searchUser($query));
}

if (empty($validated)) {
return response()->json(null, 400);
}

$users = User::query();

if (isset($validated['username'])) {
$users->where('username', 'like', "%{$validated['username']}%");
}

if (isset($validated['name'])) {
$users->where('name', 'like', "%{$validated['name']}%");
}

return UserResource::collection($users->simplePaginate(10));

} catch (InvalidArgumentException) {
return $this->sendError(['message' => __('messages.exception.general')], 400);
}
Expand Down
10 changes: 5 additions & 5 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@
Route::delete('/{userId}/follow', [FollowController::class, 'destroyFollow']);
});
Route::group(['middleware' => ['scope:write-followers']], static function() {
Route::delete('removeFollower', [FollowController::class, 'removeFollower']); // TODO remove after 2024-10
Route::delete('removeFollower', [FollowController::class, 'removeFollower']); // TODO remove after 2024-10
Route::delete('rejectFollowRequest', [FollowController::class, 'rejectFollowRequest']); // TODO remove after 2024-10
Route::put('approveFollowRequest', [FollowController::class, 'approveFollowRequest']); // TODO remove after 2024-10
Route::put('approveFollowRequest', [FollowController::class, 'approveFollowRequest']); // TODO remove after 2024-10
});
Route::group(['middleware' => ['scope:write-blocks']], static function() {
Route::post('/{userId}/block', [UserController::class, 'createBlock']);
Route::delete('/{userId}/block', [UserController::class, 'destroyBlock']);
Route::post('/{userId}/mute', [UserController::class, 'createMute']);
Route::delete('/{userId}/mute', [UserController::class, 'destroyMute']);
});
Route::get('search/{query}', [UserController::class, 'search'])->middleware(['scope:read-search']);
Route::get('search/{query?}', [UserController::class, 'search'])->middleware(['scope:read-search']);
Route::get('statuses/active', [StatusController::class, 'getActiveStatus'])
->middleware(['scope:read-statuses']);
});
Expand Down Expand Up @@ -160,9 +160,9 @@
Route::delete('token', [TokenController::class, 'revokeToken']); //TODO: undocumented endpoint - document when stable
});
Route::group(['middleware' => ['scope:read-settings-followers']], static function() {
Route::get('followers', [FollowController::class, 'getFollowers']); // TODO remove after 2024-10
Route::get('followers', [FollowController::class, 'getFollowers']); // TODO remove after 2024-10
Route::get('follow-requests', [FollowController::class, 'getFollowRequests']); // TODO remove after 2024-10
Route::get('followings', [FollowController::class, 'getFollowings']); // TODO remove after 2024-10
Route::get('followings', [FollowController::class, 'getFollowings']); // TODO remove after 2024-10
});
});
Route::group(['prefix' => 'webhooks'], static function() {
Expand Down
14 changes: 12 additions & 2 deletions storage/api-docs/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4359,7 +4359,7 @@
]
}
},
"/user/search/{query}": {
"/user/search/{?query}": {
"get": {
"tags": [
"User"
Expand All @@ -4371,7 +4371,7 @@
{
"name": "query",
"in": "path",
"description": "username",
"description": "If this is given, the search will be performed on the username and (display)name (or-search)",
"example": "Gertrud123"
},
{
Expand All @@ -4382,6 +4382,16 @@
"schema": {
"type": "integer"
}
},
{
"name": "username",
"in": "query",
"description": "Search for Username"
},
{
"name": "name",
"in": "query",
"description": "Search for parts of users (display)name"
}
],
"responses": {
Expand Down

0 comments on commit d08fea7

Please sign in to comment.