Compare commits
	
		
			2 Commits
		
	
	
		
			bc2e87c56f
			...
			3ee04d0b24
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 3ee04d0b24 | |||
| 7f110313e9 | 
| @@ -23,7 +23,7 @@ public class ActivityService( | |||||||
|         var postTime = post.PublishedAt ?? post.CreatedAt; |         var postTime = post.PublishedAt ?? post.CreatedAt; | ||||||
|         var hours = (now - postTime).TotalHours; |         var hours = (now - postTime).TotalHours; | ||||||
|         // Add 1 to score to prevent negative results for posts with more downvotes than upvotes |         // Add 1 to score to prevent negative results for posts with more downvotes than upvotes | ||||||
|         return (score + 1) / Math.Pow(hours + 2, 1.8); |         return (score + 1) / Math.Pow(hours + 1.5, 2.0); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public async Task<List<Activity>> GetActivitiesForAnyone( |     public async Task<List<Activity>> GetActivitiesForAnyone( | ||||||
| @@ -61,8 +61,10 @@ public class ActivityService( | |||||||
|                 if (discovery != null) |                 if (discovery != null) | ||||||
|                     interleaved.Add(discovery); |                     interleaved.Add(discovery); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             interleaved.Add(post.ToActivity()); |             interleaved.Add(post.ToActivity()); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         activities.AddRange(interleaved); |         activities.AddRange(interleaved); | ||||||
|  |  | ||||||
|         if (activities.Count == 0) |         if (activities.Count == 0) | ||||||
| @@ -127,8 +129,10 @@ public class ActivityService( | |||||||
|                 if (discovery != null) |                 if (discovery != null) | ||||||
|                     interleaved.Add(discovery); |                     interleaved.Add(discovery); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             interleaved.Add(post.ToActivity()); |             interleaved.Add(post.ToActivity()); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         activities.AddRange(interleaved); |         activities.AddRange(interleaved); | ||||||
|  |  | ||||||
|         if (activities.Count == 0) |         if (activities.Count == 0) | ||||||
| @@ -210,19 +214,19 @@ public class ActivityService( | |||||||
|  |  | ||||||
|     private async Task<Activity?> GetShuffledPostsActivity(int count = 5) |     private async Task<Activity?> GetShuffledPostsActivity(int count = 5) | ||||||
|     { |     { | ||||||
|         var posts = await db.Posts |         var postsQuery = db.Posts | ||||||
|             .Include(p => p.Categories) |             .Include(p => p.Categories) | ||||||
|             .Include(p => p.Tags) |             .Include(p => p.Tags) | ||||||
|             .Include(p => p.Realm) |             .Include(p => p.Realm) | ||||||
|             .Where(p => p.RepliedPostId == null) |             .Where(p => p.RepliedPostId == null) | ||||||
|             .OrderBy(_ => EF.Functions.Random()) |             .OrderBy(_ => EF.Functions.Random()) | ||||||
|             .Take(count) |             .Take(count); | ||||||
|             .ToListAsync(); |  | ||||||
|  |  | ||||||
|         if (posts.Count == 0) |         var posts = await GetAndProcessPosts(postsQuery, trackViews: false); | ||||||
|             return null; |  | ||||||
|  |  | ||||||
|         return new DiscoveryActivity(posts.Select(x => new DiscoveryItem("post", x)).ToList()).ToActivity(); |         return posts.Count == 0 | ||||||
|  |             ? null | ||||||
|  |             : new DiscoveryActivity(posts.Select(x => new DiscoveryItem("post", x)).ToList()).ToActivity(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private async Task<Activity?> GetArticleDiscoveryActivity(int count = 5, int feedSampleSize = 10) |     private async Task<Activity?> GetArticleDiscoveryActivity(int count = 5, int feedSampleSize = 10) | ||||||
|   | |||||||
| @@ -11,8 +11,7 @@ namespace DysonNetwork.Sphere.Post; | |||||||
| public enum PostType | public enum PostType | ||||||
| { | { | ||||||
|     Moment, |     Moment, | ||||||
|     Article, |     Article | ||||||
|     Video |  | ||||||
| } | } | ||||||
|  |  | ||||||
| public enum PostVisibility | public enum PostVisibility | ||||||
|   | |||||||
| @@ -818,19 +818,40 @@ public partial class PostService( | |||||||
|             var periodStart = today.InUtc().Date.AtStartOfDayInZone(DateTimeZone.Utc).ToInstant().Minus(Duration.FromDays(1)); |             var periodStart = today.InUtc().Date.AtStartOfDayInZone(DateTimeZone.Utc).ToInstant().Minus(Duration.FromDays(1)); | ||||||
|             var periodEnd = today.InUtc().Date.AtStartOfDayInZone(DateTimeZone.Utc).ToInstant(); |             var periodEnd = today.InUtc().Date.AtStartOfDayInZone(DateTimeZone.Utc).ToInstant(); | ||||||
|              |              | ||||||
|             var reactSocialPoints = await db.PostReactions |             var postsInPeriod = await db.Posts | ||||||
|                 .Include(e => e.Post) |                 .Where(e => e.Visibility == PostVisibility.Public) | ||||||
|                 .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) |                 .Select(e => e.Id) | ||||||
|  |                 .ToListAsync(); | ||||||
|  |  | ||||||
|  |             var reactionScores = await db.PostReactions | ||||||
|  |                 .Where(e => postsInPeriod.Contains(e.PostId)) | ||||||
|                 .GroupBy(e => e.PostId) |                 .GroupBy(e => e.PostId) | ||||||
|                 .Select(e => new |                 .Select(e => new | ||||||
|                 { |                 { | ||||||
|                     PostId = e.Key, |                     PostId = e.Key, | ||||||
|                     Count = e.Sum(r => r.Attitude == PostReactionAttitude.Positive ? 1 : -1) |                     Score = e.Sum(r => r.Attitude == PostReactionAttitude.Positive ? 1 : -1) | ||||||
|  |                 }) | ||||||
|  |                 .ToDictionaryAsync(e => e.PostId, e => e.Score); | ||||||
|  |  | ||||||
|  |             var repliesCounts = await db.Posts | ||||||
|  |                 .Where(p => p.RepliedPostId != null && postsInPeriod.Contains(p.RepliedPostId.Value)) | ||||||
|  |                 .GroupBy(p => p.RepliedPostId!.Value) | ||||||
|  |                 .ToDictionaryAsync( | ||||||
|  |                     g => g.Key, | ||||||
|  |                     g => g.Count() | ||||||
|  |                 ); | ||||||
|  |  | ||||||
|  |             var reactSocialPoints = postsInPeriod | ||||||
|  |                 .Select(postId => new | ||||||
|  |                 { | ||||||
|  |                     PostId = postId, | ||||||
|  |                     Count = (reactionScores.TryGetValue(postId, out var rScore) ? rScore : 0) + | ||||||
|  |                             (repliesCounts.TryGetValue(postId, out var repCount) ? repCount : 0) | ||||||
|                 }) |                 }) | ||||||
|                 .OrderByDescending(e => e.Count) |                 .OrderByDescending(e => e.Count) | ||||||
|                 .Take(5) |                 .Take(5) | ||||||
|                 .ToDictionaryAsync(e => e.PostId, e => e.Count); |                 .ToDictionary(e => e.PostId, e => e.Count); | ||||||
|  |  | ||||||
|             featuredIds = reactSocialPoints.Select(e => e.Key).ToList(); |             featuredIds = reactSocialPoints.Select(e => e.Key).ToList(); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user