diff --git a/Iceshrimp.Backend/Controllers/Pleroma/FrontendController.cs b/Iceshrimp.Backend/Controllers/Pleroma/FrontendController.cs index 9f549bdc..bf983e8f 100644 --- a/Iceshrimp.Backend/Controllers/Pleroma/FrontendController.cs +++ b/Iceshrimp.Backend/Controllers/Pleroma/FrontendController.cs @@ -3,11 +3,9 @@ using System.Net.Mime; using Iceshrimp.Backend.Controllers.Mastodon.Attributes; using Iceshrimp.Backend.Controllers.Pleroma.Schemas; using Iceshrimp.Backend.Controllers.Shared.Attributes; -using Iceshrimp.Backend.Core.Configuration; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; -using Microsoft.Extensions.Options; namespace Iceshrimp.Backend.Controllers.Pleroma; @@ -19,7 +17,7 @@ public class FrontendController : ControllerBase { [HttpGet("/api/pleroma/frontend_configurations")] [ProducesResults(HttpStatusCode.OK)] - public FrontendConfigurationsResponse GetFrontendConfigurations([FromServices] IOptionsSnapshot config) + public FrontendConfigurationsResponse GetFrontendConfigurations() { return new FrontendConfigurationsResponse(); } diff --git a/Iceshrimp.Backend/Controllers/Pleroma/NotificationController.cs b/Iceshrimp.Backend/Controllers/Pleroma/NotificationController.cs new file mode 100644 index 00000000..f64aefae --- /dev/null +++ b/Iceshrimp.Backend/Controllers/Pleroma/NotificationController.cs @@ -0,0 +1,62 @@ +using System.Net; +using System.Net.Mime; +using Iceshrimp.Backend.Controllers.Mastodon.Attributes; +using Iceshrimp.Backend.Controllers.Mastodon.Renderers; +using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities; +using Iceshrimp.Backend.Controllers.Pleroma.Schemas; +using Iceshrimp.Backend.Controllers.Pleroma.Schemas.Entities; +using Iceshrimp.Backend.Controllers.Shared.Attributes; +using Iceshrimp.Backend.Core.Database; +using Iceshrimp.Backend.Core.Database.Tables; +using Iceshrimp.Backend.Core.Extensions; +using Iceshrimp.Backend.Core.Middleware; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.RateLimiting; +using Microsoft.EntityFrameworkCore; + +namespace Iceshrimp.Backend.Controllers.Pleroma; + +[MastodonApiController] +[Authenticate] +[EnableCors("mastodon")] +[EnableRateLimiting("sliding")] +[Produces(MediaTypeNames.Application.Json)] +public class NotificationController(DatabaseContext db, NotificationRenderer notificationRenderer) : ControllerBase +{ + [HttpPost("/api/v1/pleroma/notifications/read")] + [Authorize("read:notifications")] + [ProducesResults(HttpStatusCode.OK)] + public async Task> MarkNotificationsAsRead([FromHybrid] PleromaNotificationSchemas.ReadNotificationsRequest request) + { + var user = HttpContext.GetUserOrFail(); + + if (request.Id != null && request.MaxId != null) + throw GracefulException.BadRequest("id and max_id are mutually exclusive."); + + var q = db.Notifications + .IncludeCommonProperties() + .Include(p => p.Notifier) + .Include(p => p.Note) + .Where(p => p.Notifiee == user) + .Where(p => p.Notifier != null) + .Where(p => !p.IsRead) + .EnsureNoteVisibilityFor(p => p.Note, user) + .OrderByDescending(n => n.MastoId) + .Take(80); + + if (request.Id != null) + q = q.Where(n => n.MastoId == request.Id); + else if (request.MaxId != null) + q = q.Where(n => n.MastoId <= request.MaxId); + else + throw GracefulException.BadRequest("One of id or max_id are required."); + + var notifications = await q.ToListAsync(); + foreach (var notif in notifications) + notif.IsRead = true; + + await db.SaveChangesAsync(); + return (await notificationRenderer.RenderManyAsync(notifications, user, isPleroma: true)).ToList(); + } +} diff --git a/Iceshrimp.Backend/Controllers/Pleroma/Schemas/NotificationSchemas.cs b/Iceshrimp.Backend/Controllers/Pleroma/Schemas/NotificationSchemas.cs new file mode 100644 index 00000000..6b3689ca --- /dev/null +++ b/Iceshrimp.Backend/Controllers/Pleroma/Schemas/NotificationSchemas.cs @@ -0,0 +1,13 @@ +using J = System.Text.Json.Serialization.JsonPropertyNameAttribute; +using B = Microsoft.AspNetCore.Mvc.BindPropertyAttribute; + +namespace Iceshrimp.Backend.Controllers.Pleroma.Schemas; + +public abstract class PleromaNotificationSchemas +{ + public class ReadNotificationsRequest + { + [B(Name = "id")] [J("id")] public long? Id { get; set; } + [B(Name = "max_id")] [J("max_id")] public long? MaxId { get; set; } + } +} \ No newline at end of file