checkpoint

This commit is contained in:
2026-10-28 13:23:35 +02:00
parent cce5e348b3
commit 33dad8ca5a
26 changed files with 1949 additions and 49 deletions
+24 -8
View File
@@ -1,26 +1,30 @@
using StatiKosev.Services;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.Extensions.Localization;
using StatiKosev.Data;
using StatiKosev.Logging;
using StatiKosev.Services;
namespace StatiKosev.Controllers;
[Route("messages")]
public class PrivateMessageController(MessageService messagesService, ILogger<PrivateMessageController> logger) : Controller
public class PrivateMessageController(
MessageService messagesService,
IStringLocalizer<StringValues> strings,
ILogger<PrivateMessageController> logger) : Controller
{
//form post from _MessageMe, redirects back to the page it came from
[HttpPost("send")]
[EnableRateLimiting("contact")]
public async Task<IActionResult> Send([FromForm] SendPrivateMessageRequest req, string? returnUrl)
[ValidateAntiForgeryToken]
public async Task<IActionResult> Send([FromForm] SendPrivateMessageRequest req, [FromForm] string? returnUrl)
{
var back = Url.IsLocalUrl(returnUrl) ? returnUrl : "/";
if (!ModelState.IsValid)
{
TempData["Status"] = "The message must be between 1 and 2000 characters.";
TempData["Status"] = strings["MessageInvalid"].Value;
TempData["StatusType"] = "error";
return LocalRedirect(back);
}
@@ -36,8 +40,20 @@ public class PrivateMessageController(MessageService messagesService, ILogger<Pr
else
await messagesService.SendAsUserAsync(req.Body, userId, senderUsername, userAgent);
TempData["Status"] = "Sent, thank you!";
TempData["Status"] = strings["MessageSent"].Value;
TempData["StatusType"] = "success";
return LocalRedirect(back);
}
}
//JSON for the Inbox page (AJAX)
[HttpGet("")]
[Authorize(Policy = "AdminOnly")]
public async Task<IActionResult> Inbox(bool unreadOnly = false) =>
Ok(await messagesService.GetInboxAsync(unreadOnly));
[HttpPost("{id:int}/read")]
[Authorize(Policy = "AdminOnly")]
[ValidateAntiForgeryToken] // token comes in the RequestVerificationToken header
public async Task<IActionResult> MarkRead(int id) =>
await messagesService.MarkReadAsync(id) == 0 ? NotFound() : NoContent();
}