Files
StatiKosev/Controllers/PrivateMessageController.cs
T

43 lines
1.5 KiB
C#
Raw Normal View History

using StatiKosev.Services;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using StatiKosev.Data;
using StatiKosev.Logging;
namespace StatiKosev.Controllers;
2026-10-28 11:10:46 +02:00
[Route("messages")]
2026-10-28 11:10:46 +02:00
public class PrivateMessageController(MessageService messagesService, ILogger<PrivateMessageController> logger) : Controller
{
[HttpPost("send")]
[EnableRateLimiting("contact")]
2026-10-28 11:10:46 +02:00
public async Task<IActionResult> Send([FromForm] SendPrivateMessageRequest req, string? returnUrl)
{
2026-10-28 11:10:46 +02:00
var back = Url.IsLocalUrl(returnUrl) ? returnUrl : "/";
if (!ModelState.IsValid)
{
TempData["Status"] = "The message must be between 1 and 2000 characters.";
TempData["StatusType"] = "error";
return LocalRedirect(back);
}
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
2026-10-28 11:10:46 +02:00
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);
2026-10-28 11:10:46 +02:00
TempData["Status"] = "Sent, thank you!";
TempData["StatusType"] = "success";
return LocalRedirect(back);
}
}