[backend/api] Add pagination data to note likes and note renotes responses

This commit is contained in:
Laura Hausmann 2024-09-28 01:13:25 +02:00 committed by Lilian
parent 2078ce3747
commit 55530f482d
No known key found for this signature in database
4 changed files with 48 additions and 42 deletions

View file

@ -217,9 +217,10 @@ public class NoteController(
[HttpGet("{id}/likes")] [HttpGet("{id}/likes")]
[Authenticate] [Authenticate]
[Authorize] [Authorize]
[LinkPagination(20, 40)]
[ProducesResults(HttpStatusCode.OK)] [ProducesResults(HttpStatusCode.OK)]
[ProducesErrors(HttpStatusCode.NotFound)] [ProducesErrors(HttpStatusCode.NotFound)]
public async Task<IEnumerable<UserResponse>> GetNoteLikes(string id) public async Task<IEnumerable<UserResponse>> GetNoteLikes(string id, PaginationQuery pq)
{ {
var user = HttpContext.GetUser(); var user = HttpContext.GetUser();
var note = await db.Notes var note = await db.Notes
@ -231,10 +232,12 @@ public class NoteController(
var users = await db.NoteLikes var users = await db.NoteLikes
.Where(p => p.Note == note) .Where(p => p.Note == note)
.Include(p => p.User.UserProfile) .Include(p => p.User.UserProfile)
.Select(p => p.User) .Paginate(pq, ControllerContext)
.Wrap(p => p.User)
.ToListAsync(); .ToListAsync();
return await userRenderer.RenderMany(users); HttpContext.SetPaginationData(users);
return await userRenderer.RenderMany(users.Select(p => p.Entity));
} }
[HttpPost("{id}/renote")] [HttpPost("{id}/renote")]
@ -278,9 +281,10 @@ public class NoteController(
[HttpGet("{id}/renotes")] [HttpGet("{id}/renotes")]
[Authenticate] [Authenticate]
[Authorize] [Authorize]
[LinkPagination(20, 40)]
[ProducesResults(HttpStatusCode.OK)] [ProducesResults(HttpStatusCode.OK)]
[ProducesErrors(HttpStatusCode.NotFound)] [ProducesErrors(HttpStatusCode.NotFound)]
public async Task<IEnumerable<UserResponse>> GetRenotes(string id) public async Task<IEnumerable<UserResponse>> GetRenotes(string id, PaginationQuery pq)
{ {
var user = HttpContext.GetUser(); var user = HttpContext.GetUser();
var note = await db.Notes var note = await db.Notes
@ -294,10 +298,12 @@ public class NoteController(
.EnsureVisibleFor(user) .EnsureVisibleFor(user)
.Include(p => p.User.UserProfile) .Include(p => p.User.UserProfile)
.FilterHidden(user, db) .FilterHidden(user, db)
.Select(p => p.User) .Paginate(pq, ControllerContext)
.Wrap(p => p.User)
.ToListAsync(); .ToListAsync();
return await userRenderer.RenderMany(users); HttpContext.SetPaginationData(users);
return await userRenderer.RenderMany(users.Select(p => p.Entity));
} }
[HttpGet("{id}/quotes")] [HttpGet("{id}/quotes")]

View file

@ -8,7 +8,7 @@ namespace Iceshrimp.Backend.Core.Database.Tables;
[Index(nameof(UserId))] [Index(nameof(UserId))]
[Index(nameof(NoteId))] [Index(nameof(NoteId))]
[Index(nameof(UserId), nameof(NoteId), IsUnique = true)] [Index(nameof(UserId), nameof(NoteId), IsUnique = true)]
public class NoteLike public class NoteLike : IEntity
{ {
[Key] [Key]
[Column("id")] [Column("id")]

View file

@ -360,6 +360,13 @@ public static class QueryableExtensions
return Paginate(query, pq, filter.DefaultLimit, filter.MaxLimit); return Paginate(query, pq, filter.DefaultLimit, filter.MaxLimit);
} }
public static IQueryable<EntityWrapper<TResult>> Wrap<TSource, TResult>(
this IQueryable<TSource> query, Expression<Func<TSource, TResult>> predicate
) where TSource : IEntity
{
return query.Select(p => new EntityWrapper<TResult> { Id = p.Id, Entity = predicate.Compile().Invoke(p) });
}
public static IQueryable<Note> HasVisibility(this IQueryable<Note> query, Note.NoteVisibility visibility) public static IQueryable<Note> HasVisibility(this IQueryable<Note> query, Note.NoteVisibility visibility)
{ {
return query.Where(note => note.Visibility == visibility); return query.Where(note => note.Visibility == visibility);

View file

@ -1,7 +0,0 @@
namespace Iceshrimp.Shared.Schemas.Web;
public class PaginationWrapper<T>
{
public required string Id { get; set; }
public required T Entity { get; set; }
}