[backend/api] Fix unpaginated remote emoji lookup
This commit is contained in:
parent
014f35af6c
commit
e5c2edf8cb
5 changed files with 49 additions and 31 deletions
|
@ -1,8 +1,10 @@
|
|||
using System.Net;
|
||||
using System.Net.Mime;
|
||||
using Iceshrimp.Backend.Controllers.Shared.Attributes;
|
||||
using Iceshrimp.Backend.Controllers.Shared.Schemas;
|
||||
using Iceshrimp.Backend.Core.Configuration;
|
||||
using Iceshrimp.Backend.Core.Database;
|
||||
using Iceshrimp.Backend.Core.Extensions;
|
||||
using Iceshrimp.Backend.Core.Middleware;
|
||||
using Iceshrimp.Backend.Core.Services;
|
||||
using Iceshrimp.Shared.Schemas.Web;
|
||||
|
@ -46,25 +48,28 @@ public class EmojiController(
|
|||
.ToListAsync();
|
||||
}
|
||||
|
||||
[HttpGet("remote")]
|
||||
[HttpGet("remote/{host?}")]
|
||||
[Authorize("role:moderator")]
|
||||
[RestPagination(100, 500)]
|
||||
[ProducesResults(HttpStatusCode.OK)]
|
||||
public async Task<IEnumerable<EmojiResponse>> GetRemoteEmoji()
|
||||
public async Task<PaginationWrapper<List<EmojiResponse>>> GetRemoteEmoji(string? host, PaginationQuery pq)
|
||||
{
|
||||
return await db.Emojis
|
||||
.Where(p => p.Host != null)
|
||||
.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
|
||||
})
|
||||
.ToListAsync();
|
||||
var res = await db.Emojis
|
||||
.Where(p => host == null ? p.Host != null : 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
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return HttpContext.CreatePaginationWrapper(pq, res);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
|
@ -72,8 +77,8 @@ public class EmojiController(
|
|||
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||
public async Task<EmojiResponse> GetEmoji(string id)
|
||||
{
|
||||
var emoji = await db.Emojis.FirstOrDefaultAsync(p => p.Id == id) ??
|
||||
throw GracefulException.NotFound("Emoji not found");
|
||||
var emoji = await db.Emojis.FirstOrDefaultAsync(p => p.Id == id)
|
||||
?? throw GracefulException.NotFound("Emoji not found");
|
||||
|
||||
return new EmojiResponse
|
||||
{
|
||||
|
@ -155,8 +160,8 @@ public class EmojiController(
|
|||
public async Task<EmojiResponse> UpdateEmoji(string id, UpdateEmojiRequest request)
|
||||
{
|
||||
var emoji = await emojiSvc.UpdateLocalEmojiAsync(id, request.Name, request.Aliases, request.Category,
|
||||
request.License, request.Sensitive) ??
|
||||
throw GracefulException.NotFound("Emoji not found");
|
||||
request.License, request.Sensitive)
|
||||
?? throw GracefulException.NotFound("Emoji not found");
|
||||
|
||||
return new EmojiResponse
|
||||
{
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
using Iceshrimp.Shared.Helpers;
|
||||
|
||||
namespace Iceshrimp.Backend.Core.Database;
|
||||
|
||||
public interface IEntity
|
||||
{
|
||||
public string Id { get; }
|
||||
}
|
||||
public interface IEntity : IIdentifiable;
|
||||
|
||||
public class EntityWrapper<T> : IEntity
|
||||
{
|
||||
public required T Entity { get; init; }
|
||||
public required string Id { get; init; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ using Iceshrimp.Backend.Controllers.Shared.Attributes;
|
|||
using Iceshrimp.Backend.Controllers.Shared.Schemas;
|
||||
using Iceshrimp.Backend.Core.Database;
|
||||
using Iceshrimp.Backend.Core.Middleware;
|
||||
using Iceshrimp.Shared.Helpers;
|
||||
using Iceshrimp.Shared.Schemas.Web;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
|
||||
|
@ -10,7 +11,7 @@ namespace Iceshrimp.Backend.Core.Extensions;
|
|||
public static partial class HttpContextExtensions
|
||||
{
|
||||
public static PaginationWrapper<TData> CreatePaginationWrapper<TData>(
|
||||
this HttpContext ctx, PaginationQuery query, IEnumerable<IEntity> paginationData, TData data
|
||||
this HttpContext ctx, PaginationQuery query, IEnumerable<IIdentifiable> paginationData, TData data
|
||||
)
|
||||
{
|
||||
var attr = ctx.GetEndpoint()?.Metadata.GetMetadata<RestPaginationAttribute>();
|
||||
|
@ -37,7 +38,7 @@ public static partial class HttpContextExtensions
|
|||
|
||||
public static PaginationWrapper<TData> CreatePaginationWrapper<TData>(
|
||||
this HttpContext ctx, PaginationQuery query, TData data
|
||||
) where TData : IEnumerable<IEntity>
|
||||
) where TData : IEnumerable<IIdentifiable>
|
||||
{
|
||||
return CreatePaginationWrapper(ctx, query, data, data);
|
||||
}
|
||||
|
|
|
@ -10,8 +10,11 @@ internal class EmojiControllerModel(ApiClient api)
|
|||
public Task<List<EmojiResponse>> GetAllEmojiAsync() =>
|
||||
api.CallAsync<List<EmojiResponse>>(HttpMethod.Get, "/emoji");
|
||||
|
||||
public Task<List<EmojiResponse>> GetRemoteEmojiAsync() =>
|
||||
api.CallAsync<List<EmojiResponse>>(HttpMethod.Get, "/emoji/remote");
|
||||
public Task<PaginationWrapper<List<EmojiResponse>>> GetRemoteEmojiAsync(PaginationQuery pq) =>
|
||||
api.CallAsync<PaginationWrapper<List<EmojiResponse>>>(HttpMethod.Get, "/emoji/remote", pq);
|
||||
|
||||
public Task<PaginationWrapper<List<EmojiResponse>>> GetRemoteEmojiAsync(string instance, PaginationQuery pq) =>
|
||||
api.CallAsync<PaginationWrapper<List<EmojiResponse>>>(HttpMethod.Get, $"/emoji/remote/{instance}", pq);
|
||||
|
||||
public Task<EmojiResponse> UploadEmojiAsync(IBrowserFile file, string? name) =>
|
||||
api.CallAsync<EmojiResponse>(HttpMethod.Post, "/emoji",
|
||||
|
@ -31,4 +34,4 @@ internal class EmojiControllerModel(ApiClient api)
|
|||
|
||||
public Task<EmojiResponse?> GetEmojiAsync(string id) =>
|
||||
api.CallNullableAsync<EmojiResponse>(HttpMethod.Get, $"/emoji/{id}");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -106,7 +106,17 @@
|
|||
{
|
||||
State = State.Loading;
|
||||
|
||||
Emojis = Source == "remote" ? await Api.Emoji.GetRemoteEmojiAsync() : await Api.Emoji.GetAllEmojiAsync();
|
||||
if (Source == "remote")
|
||||
{
|
||||
//TODO: impolement proper pagination
|
||||
var res = await Api.Emoji.GetRemoteEmojiAsync(new PaginationQuery());
|
||||
Emojis = res.Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
Emojis = await Api.Emoji.GetAllEmojiAsync();
|
||||
}
|
||||
|
||||
if (Emojis.Count == 0)
|
||||
{
|
||||
State = State.Empty;
|
||||
|
|
Loading…
Add table
Reference in a new issue