From fabdb6cc25b4e23e1f2bc9283fd4b5dec4ae0236 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sun, 18 Feb 2024 02:05:35 +0100 Subject: [PATCH] [backend/api] Add NoteController & TimelineController --- .../Controllers/NoteController.cs | 34 +++++++++++++++ .../Controllers/TimelineController.cs | 41 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 Iceshrimp.Backend/Controllers/NoteController.cs create mode 100644 Iceshrimp.Backend/Controllers/TimelineController.cs diff --git a/Iceshrimp.Backend/Controllers/NoteController.cs b/Iceshrimp.Backend/Controllers/NoteController.cs new file mode 100644 index 00000000..667b1dff --- /dev/null +++ b/Iceshrimp.Backend/Controllers/NoteController.cs @@ -0,0 +1,34 @@ +using Iceshrimp.Backend.Controllers.Renderers; +using Iceshrimp.Backend.Controllers.Schemas; +using Iceshrimp.Backend.Core.Database; +using Iceshrimp.Backend.Core.Extensions; +using Iceshrimp.Backend.Core.Middleware; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.RateLimiting; +using Microsoft.EntityFrameworkCore; + +namespace Iceshrimp.Backend.Controllers; + +[ApiController] +[Produces("application/json")] +[EnableRateLimiting("sliding")] +[Route("/api/iceshrimp/v1/note")] +public class NoteController(DatabaseContext db) : Controller +{ + [HttpGet("{id}")] + [Authenticate] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(NoteResponse))] + [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))] + public async Task GetNote(string id) + { + var user = HttpContext.GetUser(); + var note = await db.Notes.Where(p => p.Id == id) + .IncludeCommonProperties() + .EnsureVisibleFor(user) + .PrecomputeVisibilities(user) + .FirstOrDefaultAsync() ?? + throw GracefulException.NotFound("User not found"); + + return Ok(NoteRenderer.RenderOne(note.EnforceRenoteReplyVisibility())); + } +} \ No newline at end of file diff --git a/Iceshrimp.Backend/Controllers/TimelineController.cs b/Iceshrimp.Backend/Controllers/TimelineController.cs new file mode 100644 index 00000000..ac994586 --- /dev/null +++ b/Iceshrimp.Backend/Controllers/TimelineController.cs @@ -0,0 +1,41 @@ +using Iceshrimp.Backend.Controllers.Renderers; +using Iceshrimp.Backend.Controllers.Schemas; +using Iceshrimp.Backend.Core.Database; +using Iceshrimp.Backend.Core.Extensions; +using Iceshrimp.Backend.Core.Middleware; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.RateLimiting; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Distributed; + +namespace Iceshrimp.Backend.Controllers; + +[ApiController] +[Produces("application/json")] +[EnableRateLimiting("sliding")] +[Route("/api/iceshrimp/v1/timeline")] +public class TimelineController(DatabaseContext db, IDistributedCache cache) : Controller +{ + [HttpGet("home")] + [Authenticate, Authorize] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(NoteResponse))] + [ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(ErrorResponse))] + [ProducesResponseType(StatusCodes.Status403Forbidden, Type = typeof(ErrorResponse))] + [ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))] + public async Task GetHomeTimeline(PaginationQuery pq) + { + var user = HttpContext.GetUserOrFail(); + var heuristic = await QueryableExtensions.GetHeuristic(user, db, cache); + var notes = await db.Notes.IncludeCommonProperties() + .FilterByFollowingAndOwn(user, db, heuristic) + .EnsureVisibleFor(user) + .FilterHiddenListMembers(user) + .FilterBlocked(user) + .FilterMuted(user) + .Paginate(pq, ControllerContext) + .PrecomputeVisibilities(user) + .ToListAsync(); + + return Ok(NoteRenderer.RenderMany(notes.EnforceRenoteReplyVisibility())); + } +} \ No newline at end of file