🐛 Fix get the posts repeatly
This commit is contained in:
parent
34902d0486
commit
8af2dddb45
@ -1,6 +1,4 @@
|
|||||||
using DysonNetwork.Sphere.Account;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using NodaTime;
|
using NodaTime;
|
||||||
using NodaTime.Text;
|
using NodaTime.Text;
|
||||||
|
|
||||||
@ -24,7 +22,8 @@ public class ActivityController(
|
|||||||
/// Besides, when users are logged in, it will also mix the other kinds of data and who're plying to them.
|
/// Besides, when users are logged in, it will also mix the other kinds of data and who're plying to them.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<List<Activity>>> ListActivities([FromQuery] string? cursor, [FromQuery] int take = 20)
|
public async Task<ActionResult<List<Activity>>> ListActivities([FromQuery] string? cursor,
|
||||||
|
[FromQuery] int take = 20)
|
||||||
{
|
{
|
||||||
Instant? cursorTimestamp = null;
|
Instant? cursorTimestamp = null;
|
||||||
if (!string.IsNullOrEmpty(cursor))
|
if (!string.IsNullOrEmpty(cursor))
|
||||||
@ -41,9 +40,8 @@ public class ActivityController(
|
|||||||
|
|
||||||
|
|
||||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||||
if (currentUserValue is not Account.Account currentUser)
|
return currentUserValue is not Account.Account currentUser
|
||||||
return Ok(await acts.GetActivitiesForAnyone(take, cursorTimestamp));
|
? Ok(await acts.GetActivitiesForAnyone(take, cursorTimestamp))
|
||||||
|
: Ok(await acts.GetActivities(take, cursorTimestamp, currentUser));
|
||||||
return Ok(await acts.GetActivities(take, cursorTimestamp, currentUser));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,7 +10,7 @@ public class ActivityService(AppDatabase db, RelationshipService rels, PostServi
|
|||||||
public async Task<List<Activity>> GetActivitiesForAnyone(int take, Instant? cursor)
|
public async Task<List<Activity>> GetActivitiesForAnyone(int take, Instant? cursor)
|
||||||
{
|
{
|
||||||
var activities = new List<Activity>();
|
var activities = new List<Activity>();
|
||||||
|
|
||||||
// Crunching up data
|
// Crunching up data
|
||||||
var posts = await db.Posts
|
var posts = await db.Posts
|
||||||
.Include(e => e.RepliedPost)
|
.Include(e => e.RepliedPost)
|
||||||
@ -18,7 +18,7 @@ public class ActivityService(AppDatabase db, RelationshipService rels, PostServi
|
|||||||
.Include(e => e.Categories)
|
.Include(e => e.Categories)
|
||||||
.Include(e => e.Tags)
|
.Include(e => e.Tags)
|
||||||
.Where(e => e.RepliedPostId == null)
|
.Where(e => e.RepliedPostId == null)
|
||||||
.Where(p => cursor == null || cursor > p.CreatedAt)
|
.Where(p => cursor == null || p.PublishedAt < cursor)
|
||||||
.OrderByDescending(p => p.PublishedAt)
|
.OrderByDescending(p => p.PublishedAt)
|
||||||
.FilterWithVisibility(null, [], isListing: true)
|
.FilterWithVisibility(null, [], isListing: true)
|
||||||
.Take(take)
|
.Take(take)
|
||||||
@ -31,22 +31,22 @@ public class ActivityService(AppDatabase db, RelationshipService rels, PostServi
|
|||||||
foreach (var post in posts)
|
foreach (var post in posts)
|
||||||
post.ReactionsCount =
|
post.ReactionsCount =
|
||||||
reactionMaps.TryGetValue(post.Id, out var count) ? count : new Dictionary<string, int>();
|
reactionMaps.TryGetValue(post.Id, out var count) ? count : new Dictionary<string, int>();
|
||||||
|
|
||||||
// Formatting data
|
// Formatting data
|
||||||
foreach (var post in posts)
|
foreach (var post in posts)
|
||||||
activities.Add(post.ToActivity());
|
activities.Add(post.ToActivity());
|
||||||
|
|
||||||
if (activities.Count == 0)
|
if (activities.Count == 0)
|
||||||
activities.Add(Activity.Empty());
|
activities.Add(Activity.Empty());
|
||||||
|
|
||||||
return activities;
|
return activities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Activity>> GetActivities(int take, Instant? cursor, Account.Account currentUser)
|
public async Task<List<Activity>> GetActivities(int take, Instant? cursor, Account.Account currentUser)
|
||||||
{
|
{
|
||||||
var activities = new List<Activity>();
|
var activities = new List<Activity>();
|
||||||
var userFriends = await rels.ListAccountFriends(currentUser);
|
var userFriends = await rels.ListAccountFriends(currentUser);
|
||||||
|
|
||||||
// Crunching data
|
// Crunching data
|
||||||
var posts = await db.Posts
|
var posts = await db.Posts
|
||||||
.Include(e => e.RepliedPost)
|
.Include(e => e.RepliedPost)
|
||||||
@ -54,7 +54,7 @@ public class ActivityService(AppDatabase db, RelationshipService rels, PostServi
|
|||||||
.Include(e => e.Categories)
|
.Include(e => e.Categories)
|
||||||
.Include(e => e.Tags)
|
.Include(e => e.Tags)
|
||||||
.Where(e => e.RepliedPostId == null)
|
.Where(e => e.RepliedPostId == null)
|
||||||
.Where(p => cursor == null || p.CreatedAt > cursor)
|
.Where(p => cursor == null || p.PublishedAt < cursor)
|
||||||
.OrderByDescending(p => p.PublishedAt)
|
.OrderByDescending(p => p.PublishedAt)
|
||||||
.FilterWithVisibility(currentUser, userFriends, isListing: true)
|
.FilterWithVisibility(currentUser, userFriends, isListing: true)
|
||||||
.Take(take)
|
.Take(take)
|
||||||
@ -67,11 +67,11 @@ public class ActivityService(AppDatabase db, RelationshipService rels, PostServi
|
|||||||
foreach (var post in posts)
|
foreach (var post in posts)
|
||||||
post.ReactionsCount =
|
post.ReactionsCount =
|
||||||
reactionMaps.TryGetValue(post.Id, out var count) ? count : new Dictionary<string, int>();
|
reactionMaps.TryGetValue(post.Id, out var count) ? count : new Dictionary<string, int>();
|
||||||
|
|
||||||
// Formatting data
|
// Formatting data
|
||||||
foreach (var post in posts)
|
foreach (var post in posts)
|
||||||
activities.Add(post.ToActivity());
|
activities.Add(post.ToActivity());
|
||||||
|
|
||||||
if (activities.Count == 0)
|
if (activities.Count == 0)
|
||||||
activities.Add(Activity.Empty());
|
activities.Add(Activity.Empty());
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ public class Post : ModelBase, IIdentifiedResource, IActivity
|
|||||||
{
|
{
|
||||||
return new Activity.Activity()
|
return new Activity.Activity()
|
||||||
{
|
{
|
||||||
CreatedAt = CreatedAt,
|
CreatedAt = PublishedAt ?? CreatedAt,
|
||||||
UpdatedAt = UpdatedAt,
|
UpdatedAt = UpdatedAt,
|
||||||
DeletedAt = DeletedAt,
|
DeletedAt = DeletedAt,
|
||||||
Id = Id,
|
Id = Id,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user