✨ Marking views
This commit is contained in:
@ -58,9 +58,21 @@ public class PostController(
|
||||
var postsId = posts.Select(e => e.Id).ToList();
|
||||
var reactionMaps = await ps.GetPostReactionMapBatch(postsId);
|
||||
foreach (var post in posts)
|
||||
{
|
||||
post.ReactionsCount =
|
||||
reactionMaps.TryGetValue(post.Id, out var count) ? count : new Dictionary<string, int>();
|
||||
|
||||
// Track view for each post in the list
|
||||
if (currentUser != null)
|
||||
{
|
||||
await ps.IncreaseViewCount(post.Id, currentUser.Id.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
await ps.IncreaseViewCount(post.Id);
|
||||
}
|
||||
}
|
||||
|
||||
Response.Headers["X-Total"] = totalCount.ToString();
|
||||
|
||||
return Ok(posts);
|
||||
@ -85,6 +97,9 @@ public class PostController(
|
||||
|
||||
post.ReactionsCount = await ps.GetPostReactionMap(post.Id);
|
||||
|
||||
// Track view - use the account ID as viewer ID if user is logged in
|
||||
await ps.IncreaseViewCount(post.Id, currentUser?.Id.ToString());
|
||||
|
||||
return Ok(post);
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,9 @@ public class PostService(
|
||||
AppDatabase db,
|
||||
FileReferenceService fileRefService,
|
||||
IStringLocalizer<NotificationResource> localizer,
|
||||
IServiceScopeFactory factory
|
||||
IServiceScopeFactory factory,
|
||||
FlushBufferService flushBuffer,
|
||||
ICacheService cacheService
|
||||
)
|
||||
{
|
||||
private const string PostFileUsageIdentifier = "post";
|
||||
@ -365,6 +367,40 @@ public class PostService(
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Increases the view count for a post.
|
||||
/// Uses the flush buffer service to batch database updates for better performance.
|
||||
/// </summary>
|
||||
/// <param name="postId">The ID of the post to mark as viewed</param>
|
||||
/// <param name="viewerId">Optional viewer ID for unique view counting (anonymous if null)</param>
|
||||
/// <returns>Task representing the asynchronous operation</returns>
|
||||
public async Task IncreaseViewCount(Guid postId, string? viewerId = null)
|
||||
{
|
||||
// Check if this view is already counted in cache to prevent duplicate counting
|
||||
if (!string.IsNullOrEmpty(viewerId))
|
||||
{
|
||||
var cacheKey = $"post:view:{postId}:{viewerId}";
|
||||
var (found, _) = await cacheService.GetAsyncWithStatus<bool>(cacheKey);
|
||||
|
||||
if (found)
|
||||
{
|
||||
// Already viewed by this user recently, don't count again
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark as viewed in cache for 1 hour to prevent duplicate counting
|
||||
await cacheService.SetAsync(cacheKey, true, TimeSpan.FromHours(1));
|
||||
}
|
||||
|
||||
// Add view info to flush buffer
|
||||
flushBuffer.Enqueue(new PostViewInfo
|
||||
{
|
||||
PostId = postId,
|
||||
ViewerId = viewerId,
|
||||
ViewedAt = Instant.FromDateTimeUtc(DateTime.UtcNow)
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<List<Post>> LoadPublishers(List<Post> posts)
|
||||
{
|
||||
var publisherIds = posts
|
||||
|
10
DysonNetwork.Sphere/Post/PostViewInfo.cs
Normal file
10
DysonNetwork.Sphere/Post/PostViewInfo.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using NodaTime;
|
||||
|
||||
namespace DysonNetwork.Sphere.Post;
|
||||
|
||||
public class PostViewInfo
|
||||
{
|
||||
public Guid PostId { get; set; }
|
||||
public string? ViewerId { get; set; }
|
||||
public Instant ViewedAt { get; set; }
|
||||
}
|
Reference in New Issue
Block a user