checkpoint

This commit is contained in:
2026-10-28 11:10:46 +02:00
parent c5c9d74937
commit cce5e348b3
10 changed files with 333 additions and 46 deletions
+24 -16
View File
@@ -7,29 +7,37 @@ using StatiKosev.Data;
using StatiKosev.Logging;
namespace StatiKosev.Controllers;
[Route("messages")]
public class PrivateMessageController(MessageService messagesService, ILogger<PrivateMessageController> logger) : ControllerBase
public class PrivateMessageController(MessageService messagesService, ILogger<PrivateMessageController> logger) : Controller
{
[HttpPost("send")]
[EnableRateLimiting("contact")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Send(SendPrivateMessageRequest req, CancellationToken ct)
public async Task<IActionResult> Send([FromForm] SendPrivateMessageRequest req, string? returnUrl)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var userAgent = Request.Headers["User-Agent"].ToString();
if (userId == null)
await messagesService.SendAnonymousAsync(req.Body, userAgent);
else
var back = Url.IsLocalUrl(returnUrl) ? returnUrl : "/";
if (!ModelState.IsValid)
{
var senderUsername = User.FindFirstValue(userId);
if (senderUsername is null)
{
logger.LogWarning("User {UserId} has no name claim", userId);
return Unauthorized();
}
await messagesService.SendAsUserAsync(req.Body, userId, senderUsername, userAgent);
TempData["Status"] = "The message must be between 1 and 2000 characters.";
TempData["StatusType"] = "error";
return LocalRedirect(back);
}
return Accepted();
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var senderUsername = User.Identity?.Name;
var userAgent = Request.Headers.UserAgent.ToString();
logger.LogInformation("Received private message from {UserId}", userId ?? "anonymous");
if (userId is null || senderUsername is null || req.Anonymous)
await messagesService.SendAnonymousAsync(req.Body, userAgent);
else
await messagesService.SendAsUserAsync(req.Body, userId, senderUsername, userAgent);
TempData["Status"] = "Sent, thank you!";
TempData["StatusType"] = "success";
return LocalRedirect(back);
}
}