[backend/core] Switch to universal home timeline query

This commit is contained in:
Laura Hausmann 2024-06-26 18:02:37 +02:00
parent 7a57862048
commit 46644b2ab1
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 8 additions and 31 deletions

View file

@ -31,11 +31,10 @@ public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer, C
public async Task<IActionResult> GetHomeTimeline(MastodonPaginationQuery query)
{
var user = HttpContext.GetUserOrFail();
var heuristic = await QueryableTimelineExtensions.GetHeuristic(user, db, cache);
var res = await db.Notes
.IncludeCommonProperties()
.FilterByFollowingAndOwn(user, db, heuristic)
.FilterByFollowingAndOwn(user, db)
.EnsureVisibleFor(user)
.FilterHidden(user, db, filterHiddenListMembers: true)
.Paginate(query, ControllerContext)

View file

@ -29,9 +29,8 @@ public class TimelineController(DatabaseContext db, CacheService cache, NoteRend
public async Task<IActionResult> GetHomeTimeline(PaginationQuery pq)
{
var user = HttpContext.GetUserOrFail();
var heuristic = await QueryableTimelineExtensions.GetHeuristic(user, db, cache);
var notes = await db.Notes.IncludeCommonProperties()
.FilterByFollowingAndOwn(user, db, heuristic)
.FilterByFollowingAndOwn(user, db)
.EnsureVisibleFor(user)
.FilterHidden(user, db, filterHiddenListMembers: true)
.Paginate(pq, ControllerContext)

View file

@ -12,12 +12,14 @@ public static class QueryableTimelineExtensions
private const int Cutoff = 250;
public static IQueryable<Note> FilterByFollowingAndOwn(
this IQueryable<Note> query, User user, DatabaseContext db, int heuristic
this IQueryable<Note> query, User user, DatabaseContext db
)
{
return heuristic < Cutoff
? query.FollowingAndOwnLowFreq(user, db)
: query.Where(note => note.User == user || note.User.IsFollowedBy(user));
return query.Where(note => db.Followings
.Where(p => p.Follower == user)
.Select(p => p.FolloweeId)
.Concat(new[] { user.Id })
.Contains(note.UserId));
}
private static IQueryable<Note> FollowingAndOwnLowFreq(this IQueryable<Note> query, User user, DatabaseContext db)
@ -26,27 +28,4 @@ public static class QueryableTimelineExtensions
.Select(p => p.FolloweeId)
.Concat(new[] { user.Id })
.Contains(note.UserId));
public static async Task<int> GetHeuristic(User user, DatabaseContext db, CacheService cache)
{
return await cache.FetchValueAsync($"following-query-heuristic:{user.Id}",
TimeSpan.FromHours(24), FetchHeuristic);
[SuppressMessage("ReSharper", "EntityFramework.UnsupportedServerSideFunctionCall")]
async Task<int> FetchHeuristic()
{
var latestNote = await db.Notes.OrderByDescending(p => p.Id)
.Select(p => new { p.CreatedAt })
.FirstOrDefaultAsync() ??
new { CreatedAt = DateTime.UtcNow };
//TODO: maybe we should express this as a ratio between matching and non-matching posts
return await db.Notes
.Where(p => p.CreatedAt > latestNote.CreatedAt - TimeSpan.FromDays(7))
.FollowingAndOwnLowFreq(user, db)
//.Select(p => new { })
.Take(Cutoff + 1)
.CountAsync();
}
}
}