feat(backend): add member delete endpoint
This commit is contained in:
parent
a069d0ff15
commit
2b91723696
2 changed files with 46 additions and 1 deletions
|
@ -6,6 +6,7 @@ using Foxnouns.Backend.Middleware;
|
|||
using Foxnouns.Backend.Services;
|
||||
using Foxnouns.Backend.Utils;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Foxnouns.Backend.Controllers;
|
||||
|
||||
|
@ -14,7 +15,8 @@ public class MembersController(
|
|||
ILogger logger,
|
||||
DatabaseContext db,
|
||||
MemberRendererService memberRendererService,
|
||||
ISnowflakeGenerator snowflakeGenerator) : ApiControllerBase
|
||||
ISnowflakeGenerator snowflakeGenerator,
|
||||
AvatarUpdateJob avatarUpdate) : ApiControllerBase
|
||||
{
|
||||
private readonly ILogger _logger = logger.ForContext<MembersController>();
|
||||
|
||||
|
@ -74,5 +76,24 @@ public class MembersController(
|
|||
return Ok(memberRendererService.RenderMember(member, CurrentToken));
|
||||
}
|
||||
|
||||
[HttpDelete("/api/v2/users/@me/members/{memberRef}")]
|
||||
[Authorize("member.update")]
|
||||
public async Task<IActionResult> DeleteMemberAsync(string memberRef)
|
||||
{
|
||||
var member = await db.ResolveMemberAsync(CurrentUser!.Id, memberRef);
|
||||
var deleteCount = await db.Members.Where(m => m.UserId == CurrentUser!.Id && m.Id == member.Id)
|
||||
.ExecuteDeleteAsync();
|
||||
if (deleteCount == 0)
|
||||
{
|
||||
_logger.Warning("Successfully resolved member {Id} but could not delete them", member.Id);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
if (member.Avatar != null) await avatarUpdate.DeleteMemberAvatar(member.Id, member.Avatar);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
public record CreateMemberRequest(string Name, string? DisplayName, string? Bio, string? Avatar, bool? Unlisted);
|
||||
}
|
|
@ -5,6 +5,7 @@ using Foxnouns.Backend.Utils;
|
|||
using Hangfire;
|
||||
using Minio;
|
||||
using Minio.DataModel.Args;
|
||||
using Minio.Exceptions;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats.Webp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
|
@ -163,6 +164,29 @@ public class AvatarUpdateJob(DatabaseContext db, IMinioClient minio, Config conf
|
|||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a member's avatar. This should only be used when a member is in the process of being deleted, otherwise,
|
||||
/// <see cref="QueueUpdateMemberAvatar" /> with a <c>null</c> avatar should be used instead.
|
||||
/// </summary>
|
||||
public async Task DeleteMemberAvatar(Snowflake id, string hash) => await DeleteAvatar(MemberAvatarPath(id, hash));
|
||||
/// <summary>
|
||||
/// Deletes a user's avatar. This should only be used when a user is in the process of being deleted, otherwise,
|
||||
/// <see cref="QueueUpdateUserAvatar" /> with a <c>null</c> avatar should be used instead.
|
||||
/// </summary>
|
||||
public async Task DeleteUserAvatar(Snowflake id, string hash) => await DeleteAvatar(UserAvatarPath(id, hash));
|
||||
|
||||
private async Task DeleteAvatar(string path)
|
||||
{
|
||||
logger.Debug("Deleting avatar at path {Path}", path);
|
||||
try
|
||||
{
|
||||
await minio.RemoveObjectAsync(new RemoveObjectArgs().WithBucket(config.Storage.Bucket).WithObject(path));
|
||||
}
|
||||
catch (InvalidObjectNameException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Stream> ConvertAvatar(string uri)
|
||||
{
|
||||
if (!uri.StartsWith("data:image/"))
|
||||
|
|
Loading…
Reference in a new issue