mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-03 20:01:28 +00:00
Add online users endpoint to user API (#789)
* Add /api/v1/users/online endpoint * Fix chaining style * Return empty list if none online * Return by ascending login time and limit to 50 * Improve pagination clamping * Correct login time sort
This commit is contained in:
parent
ecdce826fb
commit
d1e21b4e18
1 changed files with 26 additions and 2 deletions
|
@ -96,8 +96,7 @@ public class UserEndpoints : ApiEndpointController
|
|||
[HttpPost("user/inviteToken/{username}")]
|
||||
public async Task<IActionResult> CreateUserInviteToken([FromRoute] string? username)
|
||||
{
|
||||
if (!Configuration.ServerConfiguration.Instance.Authentication.RegistrationEnabled)
|
||||
return this.NotFound();
|
||||
if (!Configuration.ServerConfiguration.Instance.Authentication.RegistrationEnabled) return this.NotFound();
|
||||
|
||||
string? authHeader = this.Request.Headers["Authorization"];
|
||||
if (string.IsNullOrWhiteSpace(authHeader)) return this.NotFound();
|
||||
|
@ -125,4 +124,29 @@ public class UserEndpoints : ApiEndpointController
|
|||
|
||||
return this.Ok(token.Token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of online users and returns it.
|
||||
/// </summary>
|
||||
/// <param name="page">Page number. Increments skip value by clamp value</param>
|
||||
/// <param name="clamp">Number of entries to clamp response to</param>
|
||||
/// <returns>Array of online users ordered by login time</returns>
|
||||
/// <response code="200">List of users</response>
|
||||
[HttpGet("users/online")]
|
||||
[ProducesResponseType(typeof(ApiUser), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetOnlineUsers(int page = 0, int clamp = 50)
|
||||
{
|
||||
if (clamp > 100) return this.BadRequest();
|
||||
|
||||
List<ApiUser> onlineUsers = (await this.database.Users
|
||||
.Where(u => u.PermissionLevel != PermissionLevel.Banned)
|
||||
.Where(u => u.ProfileVisibility == PrivacyType.All)
|
||||
.Where(u => u.GetStatus(this.database).StatusType == StatusType.Online)
|
||||
.OrderByDescending(u => u.LastLogin)
|
||||
.Skip(page * clamp)
|
||||
.Take(clamp)
|
||||
.ToListAsync()).ToSerializableList(ApiUser.CreateFromEntity);
|
||||
|
||||
return this.Ok(onlineUsers);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue