🐛 Fixes for sticker & sticker packs

This commit is contained in:
2025-05-11 22:13:13 +08:00
parent eab775e224
commit 3d5d4db3e3
8 changed files with 93 additions and 34 deletions

View File

@@ -230,6 +230,8 @@ public class PostController(AppDatabase db, PostService ps, RelationshipService
.FirstOrDefaultAsync();
if (post is null) return NotFound();
var isSelfReact = post.Publisher.AccountId is not null && post.Publisher.AccountId == currentUser.Id;
var isExistingReaction = await db.PostReactions
.AnyAsync(r => r.PostId == post.Id &&
r.Symbol == request.Symbol &&
@@ -241,7 +243,7 @@ public class PostController(AppDatabase db, PostService ps, RelationshipService
PostId = post.Id,
AccountId = currentUser.Id
};
var isRemoving = await ps.ModifyPostVotes(post, reaction, isExistingReaction);
var isRemoving = await ps.ModifyPostVotes(post, reaction, isExistingReaction, isSelfReact);
if (isRemoving) return NoContent();
return Ok(reaction);

View File

@@ -174,7 +174,7 @@ public class PostService(AppDatabase db, FileService fs, ActivityService act)
/// <param name="post">Post that modifying</param>
/// <param name="reaction">The new / target reaction adding / removing</param>
/// <param name="isRemoving">Indicate this operation is adding / removing</param>
public async Task<bool> ModifyPostVotes(Post post, PostReaction reaction, bool isRemoving)
public async Task<bool> ModifyPostVotes(Post post, PostReaction reaction, bool isRemoving, bool isSelfReact)
{
var isExistingReaction = await db.Set<PostReaction>()
.AnyAsync(r => r.PostId == post.Id && r.AccountId == reaction.AccountId);
@@ -193,6 +193,12 @@ public class PostService(AppDatabase db, FileService fs, ActivityService act)
return isRemoving;
}
if (isSelfReact)
{
await db.SaveChangesAsync();
return isRemoving;
}
switch (reaction.Attitude)
{
case PostReactionAttitude.Positive:

View File

@@ -26,6 +26,14 @@ public class PublisherController(AppDatabase db, PublisherService ps, FileServic
return Ok(publisher);
}
[HttpGet("{name}/stats")]
public async Task<ActionResult<PublisherService.PublisherStats>> GetPublisherStats(string name)
{
var stats = await ps.GetPublisherStats(name);
if (stats is null) return NotFound();
return Ok(stats);
}
[HttpGet]
[Authorize]
public async Task<ActionResult<List<Publisher>>> ListManagedPublishers()

View File

@@ -1,9 +1,11 @@
using DysonNetwork.Sphere.Storage;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using NodaTime;
namespace DysonNetwork.Sphere.Post;
public class PublisherService(AppDatabase db, FileService fs)
public class PublisherService(AppDatabase db, FileService fs, IMemoryCache cache)
{
public async Task<Publisher> CreateIndividualPublisher(
Account.Account account,
@@ -44,4 +46,50 @@ public class PublisherService(AppDatabase db, FileService fs)
}
// TODO Able to create organizational publisher when the realm system is completed
public class PublisherStats
{
public int PostsCreated { get; set; }
public int StickerPacksCreated { get; set; }
public int StickersCreated { get; set; }
public int UpvoteReceived { get; set; }
public int DownvoteReceived { get; set; }
}
private const string PublisherStatsCacheKey = "PublisherStats_{0}";
public async Task<PublisherStats?> GetPublisherStats(string name)
{
var cacheKey = string.Format(PublisherStatsCacheKey, name);
if (cache.TryGetValue(cacheKey, out PublisherStats? stats))
return stats;
var publisher = await db.Publishers.FirstOrDefaultAsync(e => e.Name == name);
if (publisher is null) return null;
var postsCount = await db.Posts.Where(e => e.Publisher.Id == publisher.Id).CountAsync();
var postsUpvotes = await db.PostReactions
.Where(r => r.Post.Publisher.Id == publisher.Id && r.Attitude == PostReactionAttitude.Positive)
.CountAsync();
var postsDownvotes = await db.PostReactions
.Where(r => r.Post.Publisher.Id == publisher.Id && r.Attitude == PostReactionAttitude.Negative)
.CountAsync();
var stickerPacksId = await db.StickerPacks.Where(e => e.Publisher.Id == publisher.Id).Select(e => e.Id).ToListAsync();
var stickerPacksCount = stickerPacksId.Count;
var stickersCount = await db.Stickers.Where(e => stickerPacksId.Contains(e.PackId)).CountAsync();
stats = new PublisherStats
{
PostsCreated = postsCount,
StickerPacksCreated = stickerPacksCount,
StickersCreated = stickersCount,
UpvoteReceived = postsUpvotes,
DownvoteReceived = postsDownvotes
};
cache.Set(cacheKey, stats, TimeSpan.FromMinutes(5));
return stats;
}
}