🐛 Fix inconsistent between authorized feed and unauthorized feed

This commit is contained in:
LittleSheep 2025-07-03 00:43:02 +08:00
parent 19b1e957dd
commit 3a72347432

View File

@ -24,7 +24,8 @@ public class ActivityService(
return (score + 1) / Math.Pow(hours + 2, 1.8); return (score + 1) / Math.Pow(hours + 2, 1.8);
} }
public async Task<List<Activity>> GetActivitiesForAnyone(int take, Instant? cursor, HashSet<string>? debugInclude = null) public async Task<List<Activity>> GetActivitiesForAnyone(int take, Instant? cursor,
HashSet<string>? debugInclude = null)
{ {
var activities = new List<Activity>(); var activities = new List<Activity>();
debugInclude ??= new HashSet<string>(); debugInclude ??= new HashSet<string>();
@ -61,11 +62,9 @@ public class ActivityService(
.OrderBy(_ => EF.Functions.Random()) .OrderBy(_ => EF.Functions.Random())
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
if (article != null) if (article == null) continue;
{ recentArticles.Add(article);
recentArticles.Add(article); if (recentArticles.Count >= 5) break; // Limit to 5 articles
if (recentArticles.Count >= 5) break; // Limit to 5 articles
}
} }
if (recentArticles.Count > 0) if (recentArticles.Count > 0)
@ -155,16 +154,29 @@ public class ActivityService(
if (debugInclude.Contains("articles") || Random.Shared.NextDouble() < 0.2) if (debugInclude.Contains("articles") || Random.Shared.NextDouble() < 0.2)
{ {
var recentArticlesQuery = db.WebArticles var recentFeedIds = await db.WebArticles
.Take(20); // Get a larger pool for randomization .GroupBy(a => a.FeedId)
.OrderByDescending(g => g.Max(a => a.PublishedAt))
.Take(10) // Get recent 10 distinct feeds
.Select(g => g.Key)
.ToListAsync();
// Apply random ordering 50% of the time // For each feed, get one random article
if (Random.Shared.NextDouble() < 0.5) var recentArticles = new List<WebArticle>();
recentArticlesQuery = recentArticlesQuery.OrderBy(_ => EF.Functions.Random()); var random = new Random();
else
recentArticlesQuery = recentArticlesQuery.OrderByDescending(a => a.PublishedAt);
var recentArticles = await recentArticlesQuery.Take(5).ToListAsync(); foreach (var feedId in recentFeedIds.OrderBy(_ => random.Next()))
{
var article = await db.WebArticles
.Include(a => a.Feed)
.Where(a => a.FeedId == feedId)
.OrderBy(_ => EF.Functions.Random())
.FirstOrDefaultAsync();
if (article == null) continue;
recentArticles.Add(article);
if (recentArticles.Count >= 5) break; // Limit to 5 articles
}
if (recentArticles.Count > 0) if (recentArticles.Count > 0)
{ {