[backend/api] Add bite back endpoint

This commit is contained in:
pancakes 2024-10-19 15:22:48 +10:00 committed by Iceshrimp development
parent fbbc50e158
commit 30ff0d77b2
3 changed files with 52 additions and 1 deletions

View file

@ -6,6 +6,7 @@ using Iceshrimp.Backend.Controllers.Web.Renderers;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Extensions; using Iceshrimp.Backend.Core.Extensions;
using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Middleware;
using Iceshrimp.Backend.Core.Services;
using Iceshrimp.Shared.Schemas.Web; using Iceshrimp.Shared.Schemas.Web;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting; using Microsoft.AspNetCore.RateLimiting;
@ -19,8 +20,28 @@ namespace Iceshrimp.Backend.Controllers.Web;
[EnableRateLimiting("sliding")] [EnableRateLimiting("sliding")]
[Route("/api/iceshrimp/misc")] [Route("/api/iceshrimp/misc")]
[Produces(MediaTypeNames.Application.Json)] [Produces(MediaTypeNames.Application.Json)]
public class MiscController(DatabaseContext db, NoteRenderer noteRenderer) : ControllerBase public class MiscController(DatabaseContext db, NoteRenderer noteRenderer, BiteService biteSvc) : ControllerBase
{ {
[HttpPost("bite_back/{id}")]
[Authenticate]
[Authorize]
[ProducesResults(HttpStatusCode.OK)]
[ProducesErrors(HttpStatusCode.BadRequest, HttpStatusCode.NotFound)]
public async Task BiteBack(string id)
{
var user = HttpContext.GetUserOrFail();
var target = await db.Bites
.IncludeCommonProperties()
.Where(p => p.Id == id)
.FirstOrDefaultAsync()
?? throw GracefulException.NotFound("Bite not found");
if (user.Id != (target.TargetUserId ?? target.TargetNote?.UserId ?? target.TargetBite?.UserId))
throw GracefulException.BadRequest("You can only bite back at a user who bit you");
await biteSvc.BiteAsync(user, target);
}
[HttpGet("muted_threads")] [HttpGet("muted_threads")]
[LinkPagination(20, 40)] [LinkPagination(20, 40)]
[ProducesResults(HttpStatusCode.OK)] [ProducesResults(HttpStatusCode.OK)]

View file

@ -717,6 +717,13 @@ public static class QueryableExtensions
return query.Include(p => p.UserProfile); return query.Include(p => p.UserProfile);
} }
public static IQueryable<Bite> IncludeCommonProperties(this IQueryable<Bite> query)
{
return query.Include(p => p.TargetNote)
.Include(p => p.TargetBite)
.Include(p => p.User);
}
public static IQueryable<FollowRequest> IncludeCommonProperties(this IQueryable<FollowRequest> query) public static IQueryable<FollowRequest> IncludeCommonProperties(this IQueryable<FollowRequest> query)
{ {
return query.Include(p => p.Follower.UserProfile) return query.Include(p => p.Follower.UserProfile)

View file

@ -8,6 +8,29 @@ namespace Iceshrimp.Backend.Core.Services;
public partial class BiteService(DatabaseContext db, ActivityPub.ActivityRenderer activityRenderer, ActivityPub.ActivityDeliverService deliverSvc, NotificationService notificationSvc, IOptions<Config.InstanceSection> config) public partial class BiteService(DatabaseContext db, ActivityPub.ActivityRenderer activityRenderer, ActivityPub.ActivityDeliverService deliverSvc, NotificationService notificationSvc, IOptions<Config.InstanceSection> config)
{ {
public async Task BiteAsync(User user, Bite target)
{
var bite = new Bite
{
Id = IdHelpers.GenerateSlowflakeId(),
CreatedAt = DateTime.UtcNow,
User = user,
TargetBite = target
};
bite.Uri = bite.GetPublicUri(config.Value);
await db.Bites.AddAsync(bite);
await db.SaveChangesAsync();
if (target.UserHost != null)
{
var activity = activityRenderer.RenderBite(bite, target.Uri ?? target.GetPublicUri(config.Value), target.User);
await deliverSvc.DeliverToAsync(activity, user, target.User);
}
await notificationSvc.GenerateBiteNotification(bite);
}
public async Task BiteAsync(User user, Note target) public async Task BiteAsync(User user, Note target)
{ {
var bite = new Bite var bite = new Bite