💄 Better sticker marketplace listing

This commit is contained in:
2025-08-20 14:00:15 +08:00
parent 3b679d6134
commit e8d8dcbb2d
2 changed files with 32 additions and 17 deletions

View File

@@ -32,7 +32,8 @@ public class StickerPack : ModelBase
[MaxLength(4096)] public string Description { get; set; } = string.Empty; [MaxLength(4096)] public string Description { get; set; } = string.Empty;
[MaxLength(128)] public string Prefix { get; set; } = null!; [MaxLength(128)] public string Prefix { get; set; } = null!;
public List<Sticker> Stickers { get; set; } = new(); public List<Sticker> Stickers { get; set; } = [];
[JsonIgnore] public List<StickerPackOwnership> Ownerships { get; set; } = [];
public Guid PublisherId { get; set; } public Guid PublisherId { get; set; }
public Publisher.Publisher Publisher { get; set; } = null!; public Publisher.Publisher Publisher { get; set; } = null!;

View File

@@ -10,7 +10,12 @@ namespace DysonNetwork.Sphere.Sticker;
[ApiController] [ApiController]
[Route("/api/stickers")] [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<IActionResult> _CheckStickerPackPermissions( private async Task<IActionResult> _CheckStickerPackPermissions(
Guid packId, Guid packId,
@@ -26,12 +31,8 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi
return NotFound("Sticker pack not found"); return NotFound("Sticker pack not found");
var accountId = Guid.Parse(currentUser.Id); var accountId = Guid.Parse(currentUser.Id);
var member = await db.PublisherMembers if (!await ps.IsMemberWithRole(accountId, pack.PublisherId, requiredRole))
.FirstOrDefaultAsync(m => m.AccountId == accountId && m.PublisherId == pack.PublisherId);
if (member is null)
return StatusCode(403, "You are not a member of this publisher"); 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(); return Ok();
} }
@@ -40,19 +41,29 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi
public async Task<ActionResult<List<StickerPack>>> ListStickerPacks( public async Task<ActionResult<List<StickerPack>>> ListStickerPacks(
[FromQuery] int offset = 0, [FromQuery] int offset = 0,
[FromQuery] int take = 20, [FromQuery] int take = 20,
[FromQuery] string? pubName = null [FromQuery(Name = "pub")] string? pubName = null,
[FromQuery(Name = "order")] string? order = null
) )
{ {
Publisher.Publisher? publisher = null; Publisher.Publisher? publisher = null;
if (pubName is not null) if (pubName is not null)
publisher = await db.Publishers.FirstOrDefaultAsync(p => p.Name == pubName); publisher = await db.Publishers.FirstOrDefaultAsync(p => p.Name == pubName);
var totalCount = await db.StickerPacks var queryable = db.StickerPacks
.If(publisher is not null, q => q.Where(f => f.PublisherId == publisher!.Id)) .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(); .CountAsync();
var packs = await db.StickerPacks var packs = await queryable
.If(publisher is not null, q => q.Where(f => f.PublisherId == publisher!.Id))
.OrderByDescending(e => e.CreatedAt)
.Skip(offset) .Skip(offset)
.Take(take) .Take(take)
.ToListAsync(); .ToListAsync();
@@ -240,7 +251,8 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi
if (HttpContext.Items["CurrentUser"] is not Account currentUser) if (HttpContext.Items["CurrentUser"] is not Account currentUser)
return Unauthorized(); 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) if (permissionCheck is not OkResult)
return permissionCheck; return permissionCheck;
@@ -275,7 +287,8 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi
if (HttpContext.Items["CurrentUser"] is not Account currentUser) if (HttpContext.Items["CurrentUser"] is not Account currentUser)
return Unauthorized(); 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) if (permissionCheck is not OkResult)
return permissionCheck; return permissionCheck;
@@ -305,7 +318,8 @@ public class StickerController(AppDatabase db, StickerService st, FileService.Fi
if (request.ImageId is null) if (request.ImageId is null)
return BadRequest("Image is required."); 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) if (permissionCheck is not OkResult)
return permissionCheck; return permissionCheck;