2024-07-14 21:25:23 +02:00
|
|
|
using EntityFramework.Exceptions.Common;
|
2024-07-13 19:38:40 +02:00
|
|
|
using Foxnouns.Backend.Database;
|
|
|
|
using Foxnouns.Backend.Database.Models;
|
2024-07-14 21:25:23 +02:00
|
|
|
using Foxnouns.Backend.Jobs;
|
2024-07-13 19:38:40 +02:00
|
|
|
using Foxnouns.Backend.Middleware;
|
|
|
|
using Foxnouns.Backend.Services;
|
2024-07-14 21:25:23 +02:00
|
|
|
using Foxnouns.Backend.Utils;
|
2024-07-13 19:38:40 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Controllers;
|
|
|
|
|
|
|
|
[Route("/api/v2/users/{userRef}/members")]
|
|
|
|
public class MembersController(
|
|
|
|
ILogger logger,
|
|
|
|
DatabaseContext db,
|
|
|
|
MemberRendererService memberRendererService,
|
|
|
|
ISnowflakeGenerator snowflakeGenerator) : ApiControllerBase
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger = logger.ForContext<MembersController>();
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
[ProducesResponseType<IEnumerable<MemberRendererService.PartialMember>>(StatusCodes.Status200OK)]
|
|
|
|
public async Task<IActionResult> GetMembersAsync(string userRef)
|
|
|
|
{
|
|
|
|
var user = await db.ResolveUserAsync(userRef, CurrentToken);
|
|
|
|
return Ok(await memberRendererService.RenderUserMembersAsync(user, CurrentToken));
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{memberRef}")]
|
|
|
|
[ProducesResponseType<MemberRendererService.MemberResponse>(StatusCodes.Status200OK)]
|
|
|
|
public async Task<IActionResult> GetMemberAsync(string userRef, string memberRef)
|
|
|
|
{
|
|
|
|
var member = await db.ResolveMemberAsync(userRef, memberRef, CurrentToken);
|
|
|
|
return Ok(memberRendererService.RenderMember(member, CurrentToken));
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("/api/v2/users/@me/members")]
|
|
|
|
[ProducesResponseType<MemberRendererService.MemberResponse>(StatusCodes.Status200OK)]
|
|
|
|
[Authorize("member.create")]
|
|
|
|
public async Task<IActionResult> CreateMemberAsync([FromBody] CreateMemberRequest req)
|
|
|
|
{
|
2024-07-14 21:25:23 +02:00
|
|
|
ValidationUtils.Validate([
|
|
|
|
("name", ValidationUtils.ValidateMemberName(req.Name)),
|
|
|
|
("display_name", ValidationUtils.ValidateDisplayName(req.DisplayName)),
|
|
|
|
("bio", ValidationUtils.ValidateBio(req.Bio)),
|
|
|
|
("avatar", ValidationUtils.ValidateAvatar(req.Avatar))
|
|
|
|
]);
|
2024-07-13 19:38:40 +02:00
|
|
|
|
|
|
|
var member = new Member
|
|
|
|
{
|
|
|
|
Id = snowflakeGenerator.GenerateSnowflake(),
|
2024-07-14 21:25:23 +02:00
|
|
|
User = CurrentUser!,
|
2024-07-13 19:38:40 +02:00
|
|
|
Name = req.Name,
|
2024-07-14 21:25:23 +02:00
|
|
|
DisplayName = req.DisplayName,
|
|
|
|
Bio = req.Bio,
|
|
|
|
Unlisted = req.Unlisted ?? false
|
2024-07-13 19:38:40 +02:00
|
|
|
};
|
|
|
|
db.Add(member);
|
|
|
|
|
|
|
|
_logger.Debug("Creating member {MemberName} ({Id}) for {UserId}", member.Name, member.Id, CurrentUser!.Id);
|
|
|
|
|
2024-07-14 21:25:23 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
catch (UniqueConstraintException)
|
|
|
|
{
|
|
|
|
_logger.Debug("Could not create member {Id} due to name conflict", member.Id);
|
|
|
|
throw new ApiError.BadRequest("A member with that name already exists", "name", req.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.Avatar != null) AvatarUpdateJob.QueueUpdateMemberAvatar(member.Id, req.Avatar);
|
2024-07-13 19:38:40 +02:00
|
|
|
|
|
|
|
return Ok(memberRendererService.RenderMember(member, CurrentToken));
|
|
|
|
}
|
|
|
|
|
2024-07-14 21:25:23 +02:00
|
|
|
public record CreateMemberRequest(string Name, string? DisplayName, string? Bio, string? Avatar, bool? Unlisted);
|
2024-07-13 19:38:40 +02:00
|
|
|
}
|