From e1c5a99ab39239f98e3bb88f2633d0f1ed81aa6b Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 7 Feb 2025 17:14:41 +0100 Subject: [PATCH] [backend/api] Fix remote emoji lookup pagination --- .../Controllers/Web/EmojiController.cs | 32 +++++++++++++++++-- .../Core/Extensions/QueryableExtensions.cs | 23 ++++++------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/Iceshrimp.Backend/Controllers/Web/EmojiController.cs b/Iceshrimp.Backend/Controllers/Web/EmojiController.cs index d4b2136d..cb69d2b2 100644 --- a/Iceshrimp.Backend/Controllers/Web/EmojiController.cs +++ b/Iceshrimp.Backend/Controllers/Web/EmojiController.cs @@ -48,14 +48,14 @@ public class EmojiController( .ToListAsync(); } - [HttpGet("remote/{host?}")] + [HttpGet("remote")] [Authorize("role:moderator")] [RestPagination(100, 500)] [ProducesResults(HttpStatusCode.OK)] - public async Task>> GetRemoteEmoji(string? host, PaginationQuery pq) + public async Task>> GetRemoteEmoji(PaginationQuery pq) { var res = await db.Emojis - .Where(p => host == null ? p.Host != null : p.Host == host) + .Where(p => p.Host != null) .Select(p => new EmojiResponse { Id = p.Id, @@ -67,6 +67,32 @@ public class EmojiController( License = p.License, 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>> 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(); return HttpContext.CreatePaginationWrapper(pq, res); diff --git a/Iceshrimp.Backend/Core/Extensions/QueryableExtensions.cs b/Iceshrimp.Backend/Core/Extensions/QueryableExtensions.cs index 99c07443..9bbd4bb4 100644 --- a/Iceshrimp.Backend/Core/Extensions/QueryableExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/QueryableExtensions.cs @@ -10,6 +10,7 @@ using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.EntityFrameworkCore.Extensions; +using Iceshrimp.Shared.Helpers; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -22,7 +23,7 @@ public static class QueryableExtensions MastodonPaginationQuery pq, int defaultLimit, int maxLimit - ) where T : IEntity + ) where T : IIdentifiable { if (pq.Limit is < 1) throw GracefulException.BadRequest("Limit cannot be less than 1"); @@ -56,7 +57,7 @@ public static class QueryableExtensions MastodonPaginationQuery pq, int defaultLimit, int maxLimit - ) where T : IEntity + ) where T : IIdentifiable { if (pq.Limit is < 1) throw GracefulException.BadRequest("Limit cannot be less than 1"); @@ -90,7 +91,7 @@ public static class QueryableExtensions MastodonPaginationQuery pq, int defaultLimit, int maxLimit - ) where T : IEntity + ) where T : IIdentifiable { if (pq.Limit is < 1) throw GracefulException.BadRequest("Limit cannot be less than 1"); @@ -148,7 +149,7 @@ public static class QueryableExtensions PaginationQuery pq, int defaultLimit, int maxLimit - ) where T : IEntity + ) where T : IIdentifiable { if (pq.Limit is < 1) throw GracefulException.BadRequest("Limit cannot be less than 1"); @@ -173,7 +174,7 @@ public static class QueryableExtensions this IQueryable query, MastodonPaginationQuery pq, ControllerContext context - ) where T : IEntity + ) where T : IIdentifiable { var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata(); if (attr == null) @@ -187,7 +188,7 @@ public static class QueryableExtensions MastodonPaginationQuery pq, int defaultLimit, int maxLimit - ) where T : IEntity + ) where T : IIdentifiable { if (pq.Limit is < 1) throw GracefulException.BadRequest("Limit cannot be less than 1"); @@ -199,7 +200,7 @@ public static class QueryableExtensions this IQueryable query, MastodonPaginationQuery pq, ControllerContext context - ) where T : IEntity + ) where T : IIdentifiable { var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata(); if (attr == null) @@ -213,7 +214,7 @@ public static class QueryableExtensions Expression> predicate, MastodonPaginationQuery pq, ControllerContext context - ) where T : IEntity + ) where T : IIdentifiable { var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata(); if (attr == null) @@ -227,7 +228,7 @@ public static class QueryableExtensions Expression> predicate, MastodonPaginationQuery pq, ControllerContext context - ) where T : IEntity + ) where T : IIdentifiable { var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata(); if (attr == null) @@ -240,7 +241,7 @@ public static class QueryableExtensions this IQueryable query, PaginationQuery pq, ControllerContext context - ) where T : IEntity + ) where T : IIdentifiable { var attr = context.HttpContext.GetEndpoint()?.Metadata.GetMetadata(); if (attr == null) @@ -251,7 +252,7 @@ public static class QueryableExtensions public static IQueryable> Wrap( this IQueryable query, Expression> predicate - ) where TSource : IEntity + ) where TSource : IIdentifiable { return query.Select(p => new EntityWrapper { Id = p.Id, Entity = predicate.Compile().Invoke(p) }); }