2024-05-28 15:29:18 +02:00
|
|
|
using Foxnouns.Backend.Database;
|
2024-07-12 17:12:24 +02:00
|
|
|
using Foxnouns.Backend.Database.Models;
|
2024-07-08 19:03:04 +02:00
|
|
|
using Foxnouns.Backend.Jobs;
|
2024-05-28 15:29:18 +02:00
|
|
|
using Foxnouns.Backend.Middleware;
|
2024-05-28 17:09:50 +02:00
|
|
|
using Foxnouns.Backend.Services;
|
2024-07-12 17:12:24 +02:00
|
|
|
using Foxnouns.Backend.Utils;
|
2024-05-28 15:29:18 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-07-12 17:12:24 +02:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-05-28 15:29:18 +02:00
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Controllers;
|
|
|
|
|
|
|
|
[Route("/api/v2/users")]
|
2024-05-28 17:09:50 +02:00
|
|
|
public class UsersController(DatabaseContext db, UserRendererService userRendererService) : ApiControllerBase
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
|
|
|
[HttpGet("{userRef}")]
|
2024-07-12 17:12:24 +02:00
|
|
|
[ProducesResponseType<UserRendererService.UserResponse>(statusCode: StatusCodes.Status200OK)]
|
2024-07-08 19:03:04 +02:00
|
|
|
public async Task<IActionResult> GetUserAsync(string userRef)
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
2024-07-13 19:38:40 +02:00
|
|
|
var user = await db.ResolveUserAsync(userRef, CurrentToken);
|
2024-07-12 17:12:24 +02:00
|
|
|
return await GetUserInnerAsync(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<IActionResult> GetUserInnerAsync(User user)
|
|
|
|
{
|
|
|
|
return Ok(await userRendererService.RenderUserAsync(
|
|
|
|
user,
|
|
|
|
selfUser: CurrentUser,
|
|
|
|
token: CurrentToken,
|
|
|
|
renderMembers: true,
|
|
|
|
renderAuthMethods: true
|
|
|
|
));
|
2024-05-28 15:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPatch("@me")]
|
2024-07-12 17:12:24 +02:00
|
|
|
[Authorize("user.update")]
|
|
|
|
[ProducesResponseType<UserRendererService.UserResponse>(statusCode: StatusCodes.Status200OK)]
|
2024-07-08 19:03:04 +02:00
|
|
|
public async Task<IActionResult> UpdateUserAsync([FromBody] UpdateUserRequest req)
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
2024-07-12 17:12:24 +02:00
|
|
|
await using var tx = await db.Database.BeginTransactionAsync();
|
|
|
|
var user = await db.Users.FirstAsync(u => u.Id == CurrentUser!.Id);
|
|
|
|
|
|
|
|
if (req.Username != null && req.Username != user.Username)
|
|
|
|
{
|
|
|
|
ValidationUtils.ValidateUsername(req.Username);
|
|
|
|
user.Username = req.Username;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.HasProperty(nameof(req.DisplayName)))
|
|
|
|
{
|
|
|
|
ValidationUtils.ValidateDisplayName(req.DisplayName);
|
|
|
|
user.DisplayName = req.DisplayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.HasProperty(nameof(req.Bio)))
|
|
|
|
{
|
|
|
|
ValidationUtils.ValidateBio(req.Bio);
|
|
|
|
user.Bio = req.Bio;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.HasProperty(nameof(req.Avatar)))
|
|
|
|
{
|
|
|
|
ValidationUtils.ValidateAvatar(req.Avatar);
|
2024-07-08 19:03:04 +02:00
|
|
|
AvatarUpdateJob.QueueUpdateUserAvatar(CurrentUser!.Id, req.Avatar);
|
2024-07-12 17:12:24 +02:00
|
|
|
}
|
2024-07-08 19:03:04 +02:00
|
|
|
|
2024-07-12 17:12:24 +02:00
|
|
|
await db.SaveChangesAsync();
|
|
|
|
await tx.CommitAsync();
|
|
|
|
return Ok(await userRendererService.RenderUserAsync(user, CurrentUser, renderMembers: false,
|
|
|
|
renderAuthMethods: false));
|
2024-05-28 15:29:18 +02:00
|
|
|
}
|
|
|
|
|
2024-07-12 17:12:24 +02:00
|
|
|
public class UpdateUserRequest : PatchRequest
|
|
|
|
{
|
|
|
|
public string? Username { get; init; }
|
|
|
|
public string? DisplayName { get; init; }
|
|
|
|
public string? Bio { get; init; }
|
|
|
|
public string? Avatar { get; init; }
|
|
|
|
}
|
2024-05-28 15:29:18 +02:00
|
|
|
}
|