[backend/api] Fix remote emoji lookup pagination

This commit is contained in:
Laura Hausmann 2025-02-07 17:14:41 +01:00
parent a16aca11d2
commit e1c5a99ab3
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 41 additions and 14 deletions

View file

@ -48,14 +48,14 @@ public class EmojiController(
.ToListAsync(); .ToListAsync();
} }
[HttpGet("remote/{host?}")] [HttpGet("remote")]
[Authorize("role:moderator")] [Authorize("role:moderator")]
[RestPagination(100, 500)] [RestPagination(100, 500)]
[ProducesResults(HttpStatusCode.OK)] [ProducesResults(HttpStatusCode.OK)]
public async Task<PaginationWrapper<List<EmojiResponse>>> GetRemoteEmoji(string? host, PaginationQuery pq) public async Task<PaginationWrapper<List<EmojiResponse>>> GetRemoteEmoji(PaginationQuery pq)
{ {
var res = await db.Emojis var res = await db.Emojis
.Where(p => host == null ? p.Host != null : p.Host == host) .Where(p => p.Host != null)
.Select(p => new EmojiResponse .Select(p => new EmojiResponse
{ {
Id = p.Id, Id = p.Id,
@ -67,6 +67,32 @@ public class EmojiController(
License = p.License, License = p.License,
Sensitive = p.Sensitive Sensitive = p.Sensitive
}) })
.Paginate(pq, ControllerContext)
.ToListAsync();
return HttpContext.CreatePaginationWrapper(pq, res);
}
[HttpGet("remote/{host}")]
[Authorize("role:moderator")]
[RestPagination(100, 500)]
[ProducesResults(HttpStatusCode.OK)]
public async Task<PaginationWrapper<List<EmojiResponse>>> GetRemoteEmojiByHost(string host, PaginationQuery pq)
{
var res = await db.Emojis
.Where(p => p.Host == host)
.Select(p => new EmojiResponse
{
Id = p.Id,
Name = p.Name,
Uri = p.Uri,
Aliases = p.Aliases,
Category = p.Host,
PublicUrl = p.GetAccessUrl(instance.Value),
License = p.License,
Sensitive = p.Sensitive
})
.Paginate(pq, ControllerContext)
.ToListAsync(); .ToListAsync();
return HttpContext.CreatePaginationWrapper(pq, res); return HttpContext.CreatePaginationWrapper(pq, res);

View file

@ -10,6 +10,7 @@ using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Middleware;
using Iceshrimp.EntityFrameworkCore.Extensions; using Iceshrimp.EntityFrameworkCore.Extensions;
using Iceshrimp.Shared.Helpers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -22,7 +23,7 @@ public static class QueryableExtensions
MastodonPaginationQuery pq, MastodonPaginationQuery pq,
int defaultLimit, int defaultLimit,
int maxLimit int maxLimit
) where T : IEntity ) where T : IIdentifiable
{ {
if (pq.Limit is < 1) if (pq.Limit is < 1)
throw GracefulException.BadRequest("Limit cannot be less than 1"); throw GracefulException.BadRequest("Limit cannot be less than 1");
@ -56,7 +57,7 @@ public static class QueryableExtensions
MastodonPaginationQuery pq, MastodonPaginationQuery pq,
int defaultLimit, int defaultLimit,
int maxLimit int maxLimit
) where T : IEntity ) where T : IIdentifiable
{ {
if (pq.Limit is < 1) if (pq.Limit is < 1)
throw GracefulException.BadRequest("Limit cannot be less than 1"); throw GracefulException.BadRequest("Limit cannot be less than 1");
@ -90,7 +91,7 @@ public static class QueryableExtensions
MastodonPaginationQuery pq, MastodonPaginationQuery pq,
int defaultLimit, int defaultLimit,
int maxLimit int maxLimit
) where T : IEntity ) where T : IIdentifiable
{ {
if (pq.Limit is < 1) if (pq.Limit is < 1)
throw GracefulException.BadRequest("Limit cannot be less than 1"); throw GracefulException.BadRequest("Limit cannot be less than 1");
@ -148,7 +149,7 @@ public static class QueryableExtensions
PaginationQuery pq, PaginationQuery pq,
int defaultLimit, int defaultLimit,
int maxLimit int maxLimit
) where T : IEntity ) where T : IIdentifiable
{ {
if (pq.Limit is < 1) if (pq.Limit is < 1)
throw GracefulException.BadRequest("Limit cannot be less than 1"); throw GracefulException.BadRequest("Limit cannot be less than 1");
@ -173,7 +174,7 @@ public static class QueryableExtensions
this IQueryable<T> query, this IQueryable<T> query,
MastodonPaginationQuery pq, MastodonPaginationQuery pq,
ControllerContext context ControllerContext context
) where T : IEntity ) where T : IIdentifiable
{ {
var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>(); var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>();
if (attr == null) if (attr == null)
@ -187,7 +188,7 @@ public static class QueryableExtensions
MastodonPaginationQuery pq, MastodonPaginationQuery pq,
int defaultLimit, int defaultLimit,
int maxLimit int maxLimit
) where T : IEntity ) where T : IIdentifiable
{ {
if (pq.Limit is < 1) if (pq.Limit is < 1)
throw GracefulException.BadRequest("Limit cannot be less than 1"); throw GracefulException.BadRequest("Limit cannot be less than 1");
@ -199,7 +200,7 @@ public static class QueryableExtensions
this IQueryable<T> query, this IQueryable<T> query,
MastodonPaginationQuery pq, MastodonPaginationQuery pq,
ControllerContext context ControllerContext context
) where T : IEntity ) where T : IIdentifiable
{ {
var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>(); var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>();
if (attr == null) if (attr == null)
@ -213,7 +214,7 @@ public static class QueryableExtensions
Expression<Func<T, string>> predicate, Expression<Func<T, string>> predicate,
MastodonPaginationQuery pq, MastodonPaginationQuery pq,
ControllerContext context ControllerContext context
) where T : IEntity ) where T : IIdentifiable
{ {
var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>(); var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>();
if (attr == null) if (attr == null)
@ -227,7 +228,7 @@ public static class QueryableExtensions
Expression<Func<T, long>> predicate, Expression<Func<T, long>> predicate,
MastodonPaginationQuery pq, MastodonPaginationQuery pq,
ControllerContext context ControllerContext context
) where T : IEntity ) where T : IIdentifiable
{ {
var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>(); var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>();
if (attr == null) if (attr == null)
@ -240,7 +241,7 @@ public static class QueryableExtensions
this IQueryable<T> query, this IQueryable<T> query,
PaginationQuery pq, PaginationQuery pq,
ControllerContext context ControllerContext context
) where T : IEntity ) where T : IIdentifiable
{ {
var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>(); var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata<IPaginationAttribute>();
if (attr == null) if (attr == null)
@ -251,7 +252,7 @@ public static class QueryableExtensions
public static IQueryable<EntityWrapper<TResult>> Wrap<TSource, TResult>( public static IQueryable<EntityWrapper<TResult>> Wrap<TSource, TResult>(
this IQueryable<TSource> query, Expression<Func<TSource, TResult>> predicate this IQueryable<TSource> query, Expression<Func<TSource, TResult>> predicate
) where TSource : IEntity ) where TSource : IIdentifiable
{ {
return query.Select(p => new EntityWrapper<TResult> { Id = p.Id, Entity = predicate.Compile().Invoke(p) }); return query.Select(p => new EntityWrapper<TResult> { Id = p.Id, Entity = predicate.Compile().Invoke(p) });
} }