From 05c6d67c0399242fcf4391b0b31d2791aeae2458 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 12 Aug 2025 12:13:39 +0800 Subject: [PATCH] :necktie: Refactor the featured post algo --- DysonNetwork.Sphere/Post/PostService.cs | 14 ++++++++------ DysonNetwork.sln.DotSettings.user | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/DysonNetwork.Sphere/Post/PostService.cs b/DysonNetwork.Sphere/Post/PostService.cs index 87ba8a0..cc3133c 100644 --- a/DysonNetwork.Sphere/Post/PostService.cs +++ b/DysonNetwork.Sphere/Post/PostService.cs @@ -740,14 +740,16 @@ public partial class PostService( if (featuredIds is null) { + // The previous day highest rated posts var today = SystemClock.Instance.GetCurrentInstant(); - var periodStart = today.InUtc().Date.AtStartOfDayInZone(DateTimeZone.Utc).ToInstant().Minus(Duration.FromDays(7)); - var periodEnd = today.InUtc().Date.PlusDays(1).AtStartOfDayInZone(DateTimeZone.Utc).ToInstant(); - + var periodStart = today.InUtc().Date.AtStartOfDayInZone(DateTimeZone.Utc).ToInstant().Minus(Duration.FromDays(1)); + var periodEnd = today.InUtc().Date.AtStartOfDayInZone(DateTimeZone.Utc).ToInstant(); + var reactSocialPoints = await db.PostReactions .Include(e => e.Post) .Where(e => e.Post.Visibility == PostVisibility.Public) .Where(e => e.CreatedAt >= periodStart && e.CreatedAt < periodEnd) + .Where(e => e.Post.CreatedAt >= periodStart && e.Post.CreatedAt < periodEnd) .GroupBy(e => e.PostId) .Select(e => new { @@ -755,12 +757,12 @@ public partial class PostService( Count = e.Sum(r => r.Attitude == PostReactionAttitude.Positive ? 1 : -1) }) .OrderByDescending(e => e.Count) - .Take(3) + .Take(5) .ToDictionaryAsync(e => e.PostId, e => e.Count); featuredIds = reactSocialPoints.Select(e => e.Key).ToList(); - await cache.SetAsync(FeaturedPostCacheKey, featuredIds, TimeSpan.FromMinutes(5)); + await cache.SetAsync(FeaturedPostCacheKey, featuredIds, TimeSpan.FromHours(24)); } var posts = await db.Posts @@ -810,4 +812,4 @@ public static class PostQueryExtensions (e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) || publishersId.Contains(e.PublisherId)); } -} \ No newline at end of file +} diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index c4becba..77d72ed 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -104,6 +104,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded