From e8d8dcbb2de2bffde1c4a78df3171a760598e8d4 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 20 Aug 2025 14:00:15 +0800 Subject: [PATCH] :lipstick: Better sticker marketplace listing --- DysonNetwork.Sphere/Sticker/Sticker.cs | 3 +- .../Sticker/StickerController.cs | 46 ++++++++++++------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/DysonNetwork.Sphere/Sticker/Sticker.cs b/DysonNetwork.Sphere/Sticker/Sticker.cs index c3d5c70..dd125e0 100644 --- a/DysonNetwork.Sphere/Sticker/Sticker.cs +++ b/DysonNetwork.Sphere/Sticker/Sticker.cs @@ -32,7 +32,8 @@ public class StickerPack : ModelBase [MaxLength(4096)] public string Description { get; set; } = string.Empty; [MaxLength(128)] public string Prefix { get; set; } = null!; - public List Stickers { get; set; } = new(); + public List Stickers { get; set; } = []; + [JsonIgnore] public List Ownerships { get; set; } = []; public Guid PublisherId { get; set; } public Publisher.Publisher Publisher { get; set; } = null!; diff --git a/DysonNetwork.Sphere/Sticker/StickerController.cs b/DysonNetwork.Sphere/Sticker/StickerController.cs index c548c6e..b3f7cc7 100644 --- a/DysonNetwork.Sphere/Sticker/StickerController.cs +++ b/DysonNetwork.Sphere/Sticker/StickerController.cs @@ -10,7 +10,12 @@ namespace DysonNetwork.Sphere.Sticker; [ApiController] [Route("/api/stickers")] -public class StickerController(AppDatabase db, StickerService st, FileService.FileServiceClient files) : ControllerBase +public class StickerController( + AppDatabase db, + StickerService st, + Publisher.PublisherService ps, + FileService.FileServiceClient files +) : ControllerBase { private async Task _CheckStickerPackPermissions( Guid packId, @@ -26,12 +31,8 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi return NotFound("Sticker pack not found"); var accountId = Guid.Parse(currentUser.Id); - var member = await db.PublisherMembers - .FirstOrDefaultAsync(m => m.AccountId == accountId && m.PublisherId == pack.PublisherId); - if (member is null) + if (!await ps.IsMemberWithRole(accountId, pack.PublisherId, requiredRole)) return StatusCode(403, "You are not a member of this publisher"); - if (member.Role < requiredRole) - return StatusCode(403, $"You need to be at least a {requiredRole} to perform this action"); return Ok(); } @@ -40,19 +41,29 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi public async Task>> ListStickerPacks( [FromQuery] int offset = 0, [FromQuery] int take = 20, - [FromQuery] string? pubName = null + [FromQuery(Name = "pub")] string? pubName = null, + [FromQuery(Name = "order")] string? order = null ) { Publisher.Publisher? publisher = null; if (pubName is not null) publisher = await db.Publishers.FirstOrDefaultAsync(p => p.Name == pubName); - var totalCount = await db.StickerPacks - .If(publisher is not null, q => q.Where(f => f.PublisherId == publisher!.Id)) + var queryable = db.StickerPacks + .If(publisher is not null, q => q.Where(f => f.PublisherId == publisher!.Id)); + + if (order is not null) + { + queryable = order switch + { + "usage" => queryable.OrderByDescending(p => p.Ownerships.Count), + _ => queryable.OrderByDescending(p => p.CreatedAt) + }; + } + + var totalCount = await queryable .CountAsync(); - var packs = await db.StickerPacks - .If(publisher is not null, q => q.Where(f => f.PublisherId == publisher!.Id)) - .OrderByDescending(e => e.CreatedAt) + var packs = await queryable .Skip(offset) .Take(take) .ToListAsync(); @@ -240,7 +251,8 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); - var permissionCheck = await _CheckStickerPackPermissions(packId, currentUser, Publisher.PublisherMemberRole.Editor); + var permissionCheck = + await _CheckStickerPackPermissions(packId, currentUser, Publisher.PublisherMemberRole.Editor); if (permissionCheck is not OkResult) return permissionCheck; @@ -275,7 +287,8 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized(); - var permissionCheck = await _CheckStickerPackPermissions(packId, currentUser, Publisher.PublisherMemberRole.Editor); + var permissionCheck = + await _CheckStickerPackPermissions(packId, currentUser, Publisher.PublisherMemberRole.Editor); if (permissionCheck is not OkResult) return permissionCheck; @@ -305,7 +318,8 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi if (request.ImageId is null) return BadRequest("Image is required."); - var permissionCheck = await _CheckStickerPackPermissions(packId, currentUser, Publisher.PublisherMemberRole.Editor); + var permissionCheck = + await _CheckStickerPackPermissions(packId, currentUser, Publisher.PublisherMemberRole.Editor); if (permissionCheck is not OkResult) return permissionCheck; @@ -396,4 +410,4 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi return NoContent(); } -} +} \ No newline at end of file