From b2203fb464789a2021d3703eeb6f27bc4aaabce0 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 27 Jun 2025 16:14:25 +0800 Subject: [PATCH] :sparkles: Publisher's recommendation (discovery) --- .../Activity/ActivityService.cs | 49 +++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/DysonNetwork.Sphere/Activity/ActivityService.cs b/DysonNetwork.Sphere/Activity/ActivityService.cs index 4ce21a0..5188ce0 100644 --- a/DysonNetwork.Sphere/Activity/ActivityService.cs +++ b/DysonNetwork.Sphere/Activity/ActivityService.cs @@ -4,10 +4,6 @@ using DysonNetwork.Sphere.Post; using DysonNetwork.Sphere.Publisher; using Microsoft.EntityFrameworkCore; using NodaTime; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace DysonNetwork.Sphere.Activity; @@ -103,6 +99,16 @@ public class ActivityService( ).ToActivity()); } } + else if (cursor == null && Random.Shared.NextDouble() < 0.2) + { + var popularPublishers = await GetPopularPublishers(5); + if (popularPublishers.Count > 0) + { + activities.Add(new DiscoveryActivity( + popularPublishers.Select(x => new DiscoveryItem("publisher", x)).ToList() + ).ToActivity()); + } + } // Get publishers based on filter var filteredPublishers = filter switch @@ -166,4 +172,37 @@ public class ActivityService( return activities; } -} \ No newline at end of file + + private static double CalculatePopularity(List posts) + { + var score = posts.Sum(p => p.Upvotes - p.Downvotes); + var postCount = posts.Count; + return score + postCount; + } + + private async Task> GetPopularPublishers(int take) + { + var now = SystemClock.Instance.GetCurrentInstant(); + var recent = now.Minus(Duration.FromDays(7)); + + var posts = await db.Posts + .Where(p => p.PublishedAt > recent) + .ToListAsync(); + + var publisherIds = posts.Select(p => p.PublisherId).Distinct().ToList(); + var publishers = await db.Publishers.Where(p => publisherIds.Contains(p.Id)).ToListAsync(); + + var rankedPublishers = publishers + .Select(p => new + { + Publisher = p, + Rank = CalculatePopularity(posts.Where(post => post.PublisherId == p.Id).ToList()) + }) + .OrderByDescending(x => x.Rank) + .Select(x => x.Publisher) + .Take(take) + .ToList(); + + return rankedPublishers; + } +}