[backend/masto-client] Paginate note likes & renotes correctly (ISH-362)

This commit is contained in:
Laura Hausmann 2024-06-15 17:27:21 +02:00
parent 946eed1b03
commit 364d0c54a0
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -1,5 +1,6 @@
using System.Net.Mime; using System.Net.Mime;
using AsyncKeyedLock; using AsyncKeyedLock;
using Iceshrimp.Backend.Controllers.Attributes;
using Iceshrimp.Backend.Controllers.Mastodon.Attributes; using Iceshrimp.Backend.Controllers.Mastodon.Attributes;
using Iceshrimp.Backend.Controllers.Mastodon.Renderers; using Iceshrimp.Backend.Controllers.Mastodon.Renderers;
using Iceshrimp.Backend.Controllers.Mastodon.Schemas; using Iceshrimp.Backend.Controllers.Mastodon.Schemas;
@ -544,9 +545,10 @@ public class StatusController(
[HttpGet("{id}/favourited_by")] [HttpGet("{id}/favourited_by")]
[Authenticate("read:statuses")] [Authenticate("read:statuses")]
[LinkPagination(40, 80)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<AccountEntity>))] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<AccountEntity>))]
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(MastodonErrorResponse))] [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(MastodonErrorResponse))]
public async Task<IActionResult> GetNoteLikes(string id) public async Task<IActionResult> GetNoteLikes(string id, MastodonPaginationQuery pq)
{ {
var user = HttpContext.GetUser(); var user = HttpContext.GetUser();
if (security.Value.PublicPreview == Enums.PublicPreview.Lockdown && user == null) if (security.Value.PublicPreview == Enums.PublicPreview.Lockdown && user == null)
@ -561,19 +563,23 @@ public class StatusController(
if (security.Value.PublicPreview <= Enums.PublicPreview.Restricted && note.UserHost != null && user == null) if (security.Value.PublicPreview <= Enums.PublicPreview.Restricted && note.UserHost != null && user == null)
throw GracefulException.Forbidden("Public preview is disabled on this instance"); throw GracefulException.Forbidden("Public preview is disabled on this instance");
var res = await db.NoteLikes.Where(p => p.Note == note) var likes = await db.NoteLikes.Where(p => p.Note == note)
.Include(p => p.User.UserProfile) .Include(p => p.User.UserProfile)
.Select(p => p.User) .Select(p => new EntityWrapper<User> { Id = p.Id, Entity = p.User })
.RenderAllForMastodonAsync(userRenderer); .Paginate(pq, ControllerContext)
.ToListAsync();
HttpContext.SetPaginationData(likes);
var res = await userRenderer.RenderManyAsync(likes.Select(p => p.Entity));
return Ok(res); return Ok(res);
} }
[HttpGet("{id}/reblogged_by")] [HttpGet("{id}/reblogged_by")]
[Authenticate("read:statuses")] [Authenticate("read:statuses")]
[LinkPagination(40, 80)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<AccountEntity>))] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<AccountEntity>))]
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(MastodonErrorResponse))] [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(MastodonErrorResponse))]
public async Task<IActionResult> GetNoteRenotes(string id) public async Task<IActionResult> GetNoteRenotes(string id, MastodonPaginationQuery pq)
{ {
var user = HttpContext.GetUser(); var user = HttpContext.GetUser();
if (security.Value.PublicPreview == Enums.PublicPreview.Lockdown && user == null) if (security.Value.PublicPreview == Enums.PublicPreview.Lockdown && user == null)
@ -589,13 +595,16 @@ public class StatusController(
if (security.Value.PublicPreview <= Enums.PublicPreview.Restricted && note.UserHost != null && user == null) if (security.Value.PublicPreview <= Enums.PublicPreview.Restricted && note.UserHost != null && user == null)
throw GracefulException.Forbidden("Public preview is disabled on this instance"); throw GracefulException.Forbidden("Public preview is disabled on this instance");
var res = await db.Notes var renotes = await db.Notes
.Where(p => p.Renote == note && p.IsPureRenote) .Where(p => p.Renote == note && p.IsPureRenote)
.EnsureVisibleFor(user) .EnsureVisibleFor(user)
.Include(p => p.User.UserProfile) .Include(p => p.User.UserProfile)
.Select(p => p.User) .Select(p => new EntityWrapper<User> { Id = p.Id, Entity = p.User })
.RenderAllForMastodonAsync(userRenderer); .Paginate(pq, ControllerContext)
.ToListAsync();
HttpContext.SetPaginationData(renotes);
var res = await userRenderer.RenderManyAsync(renotes.Select(p => p.Entity));
return Ok(res); return Ok(res);
} }