[backend/api] Add NoteController & TimelineController

This commit is contained in:
Laura Hausmann 2024-02-18 02:05:35 +01:00
parent 5b9170d397
commit fabdb6cc25
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 75 additions and 0 deletions

View file

@ -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<IActionResult> 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()));
}
}

View file

@ -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<IActionResult> 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()));
}
}