diff --git a/Iceshrimp.Backend/Controllers/Mastodon/MastodonTimelineController.cs b/Iceshrimp.Backend/Controllers/Mastodon/MastodonTimelineController.cs index 34f5f98e..fe36f0fc 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/MastodonTimelineController.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/MastodonTimelineController.cs @@ -1,8 +1,10 @@ +using Iceshrimp.Backend.Controllers.Attributes; using Iceshrimp.Backend.Controllers.Mastodon.Renderers; using Iceshrimp.Backend.Controllers.Mastodon.Schemas; using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities; using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database.Tables; +using Iceshrimp.Backend.Core.Extensions; using Iceshrimp.Backend.Core.Helpers; using Iceshrimp.Backend.Core.Middleware; using Microsoft.AspNetCore.Mvc; @@ -26,14 +28,13 @@ public class MastodonTimelineController(DatabaseContext db, NoteRenderer noteRen [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable))] public async Task GetHomeTimeline() { var user = HttpContext.GetOauthUser() ?? throw new GracefulException("Failed to get user from HttpContext"); - var notes = await db.Notes - .WithIncludes() - .IsFollowedBy(user) - .OrderByIdDesc() - .Take(40) - .ToListAsync(); - - var res = await Task.WhenAll(notes.Select(async p => await noteRenderer.RenderAsync(p))); + var notes = db.Notes + .WithIncludes() + .IsFollowedBy(user) + .OrderByIdDesc() + .Take(40); + + var res = await notes.RenderAllForMastodonAsync(noteRenderer); return Ok(res); } @@ -42,14 +43,13 @@ public class MastodonTimelineController(DatabaseContext db, NoteRenderer noteRen [Produces("application/json")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable))] public async Task GetPublicTimeline() { - var notes = await db.Notes - .WithIncludes() - .HasVisibility(Note.NoteVisibility.Public) - .OrderByIdDesc() - .Take(40) - .ToListAsync(); - - var res = await Task.WhenAll(notes.Select(async p => await noteRenderer.RenderAsync(p))); + var notes = db.Notes + .WithIncludes() + .HasVisibility(Note.NoteVisibility.Public) + .OrderByIdDesc() + .Take(40); + + var res = await notes.RenderAllForMastodonAsync(noteRenderer); return Ok(res); } } \ No newline at end of file diff --git a/Iceshrimp.Backend/Core/Extensions/EnumerableExtensions.cs b/Iceshrimp.Backend/Core/Extensions/EnumerableExtensions.cs new file mode 100644 index 00000000..5908ac24 --- /dev/null +++ b/Iceshrimp.Backend/Core/Extensions/EnumerableExtensions.cs @@ -0,0 +1,16 @@ +using Iceshrimp.Backend.Controllers.Mastodon.Renderers; +using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities; +using Iceshrimp.Backend.Core.Database.Tables; + +namespace Iceshrimp.Backend.Core.Extensions; + +public static class EnumerableExtensions { + public static async Task> AwaitAllAsync(this IEnumerable> tasks) { + return await Task.WhenAll(tasks); + } + + public static async Task> RenderAllForMastodonAsync( + this IEnumerable notes, NoteRenderer renderer) { + return await notes.Select(async p => await renderer.RenderAsync(p)).AwaitAllAsync(); + } +} \ No newline at end of file