[backend/masto-client] Add basic timeline endpoints
This commit is contained in:
parent
4c075154d3
commit
b9cfbe52fc
3 changed files with 70 additions and 2 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
17
Iceshrimp.Backend/Core/Helpers/QueryHelpers.cs
Normal file
17
Iceshrimp.Backend/Core/Helpers/QueryHelpers.cs
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue