[backend/masto-client] Include navigation properties for note queries

This commit is contained in:
Laura Hausmann 2024-02-02 20:19:56 +01:00
parent f230d95b5b
commit a3fd46bb96
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 11 additions and 0 deletions

View file

@ -27,6 +27,7 @@ public class MastodonTimelineController(DatabaseContext db, NoteRenderer noteRen
public async Task<IActionResult> GetHomeTimeline() {
var user = HttpContext.GetOauthUser() ?? throw new GracefulException("Failed to get user from HttpContext");
var notes = await db.Notes
.WithIncludes()
.IsFollowedBy(user)
.OrderByIdDesc()
.Take(40)
@ -41,6 +42,7 @@ public class MastodonTimelineController(DatabaseContext db, NoteRenderer noteRen
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<Status>))]
public async Task<IActionResult> GetPublicTimeline() {
var notes = await db.Notes
.WithIncludes()
.HasVisibility(Note.NoteVisibility.Public)
.OrderByIdDesc()
.Take(40)

View file

@ -1,8 +1,17 @@
using Iceshrimp.Backend.Core.Database.Tables;
using Microsoft.EntityFrameworkCore;
namespace Iceshrimp.Backend.Core.Helpers;
public static class QueryHelpers {
public static IQueryable<Note> WithIncludes(this IQueryable<Note> query) {
return query.Include(p => p.User)
.Include(p => p.Renote)
.ThenInclude(p => p != null ? p.User : null)
.Include(p => p.Reply)
.ThenInclude(p => p != null ? p.User : null);
}
public static IQueryable<Note> HasVisibility(this IQueryable<Note> query, Note.NoteVisibility visibility) {
return query.Where(note => note.Visibility == visibility);
}