[backend/masto-client] Add basic timeline endpoints

This commit is contained in:
Laura Hausmann 2024-02-02 20:05:27 +01:00
parent 4c075154d3
commit b9cfbe52fc
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 70 additions and 2 deletions

View file

@ -0,0 +1,51 @@
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.Helpers;
using Iceshrimp.Backend.Core.Middleware;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.EntityFrameworkCore;
namespace Iceshrimp.Backend.Controllers.Mastodon;
[ApiController]
[Tags("Mastodon")]
[Route("/api/v1/timelines")]
[AuthenticateOauth]
[EnableRateLimiting("sliding")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(MastodonErrorResponse))]
[ProducesResponseType(StatusCodes.Status403Forbidden, Type = typeof(MastodonErrorResponse))]
public class MastodonTimelineController(DatabaseContext db, NoteRenderer noteRenderer) : Controller {
[AuthorizeOauth("read:statuses")]
[HttpGet("home")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<Status>))]
public async Task<IActionResult> GetHomeTimeline() {
var user = HttpContext.GetOauthUser() ?? throw new GracefulException("Failed to get user from HttpContext");
var notes = await db.Notes
.IsFollowedBy(user)
.OrderByIdDesc()
.Take(40)
.ToListAsync();
return Ok(notes);
}
[AuthorizeOauth("read:statuses")]
[HttpGet("public")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<Status>))]
public async Task<IActionResult> GetPublicTimeline() {
var notes = await db.Notes
.HasVisibility(Note.NoteVisibility.Public)
.OrderByIdDesc()
.Take(40)
.ToListAsync();
return Ok(notes);
}
}

View file

@ -13,13 +13,13 @@ namespace Iceshrimp.Backend.Controllers.Mastodon;
[AuthenticateOauth]
[EnableRateLimiting("sliding")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(MastodonErrorResponse))]
[ProducesResponseType(StatusCodes.Status403Forbidden, Type = typeof(MastodonErrorResponse))]
public class MastodonUserController(UserRenderer userRenderer) : Controller {
[AuthorizeOauth("read:accounts")]
[HttpGet("verify_credentials")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Account))]
[ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(MastodonErrorResponse))]
[ProducesResponseType(StatusCodes.Status403Forbidden, Type = typeof(MastodonErrorResponse))]
public async Task<IActionResult> VerifyUserCredentials() {
var user = HttpContext.GetOauthUser() ?? throw new GracefulException("Failed to get user from HttpContext");
var res = await userRenderer.RenderAsync(user);

View file

@ -0,0 +1,17 @@
using Iceshrimp.Backend.Core.Database.Tables;
namespace Iceshrimp.Backend.Core.Helpers;
public static class QueryHelpers {
public static IQueryable<Note> HasVisibility(this IQueryable<Note> query, Note.NoteVisibility visibility) {
return query.Where(note => note.Visibility == visibility);
}
public static IQueryable<Note> IsFollowedBy(this IQueryable<Note> query, User user) {
return query.Where(note => note.User.FollowingFollowees.Any(following => following.Follower == user));
}
public static IQueryable<Note> OrderByIdDesc(this IQueryable<Note> query) {
return query.OrderByDescending(note => note.Id);
}
}