43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
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;
|
|
|
|
|
|
[Route("messages")]
|
|
public class PrivateMessageController(MessageService messagesService, ILogger<PrivateMessageController> logger) : Controller
|
|
{
|
|
[HttpPost("send")]
|
|
[EnableRateLimiting("contact")]
|
|
|
|
public async Task<IActionResult> Send([FromForm] SendPrivateMessageRequest req, string? returnUrl)
|
|
{
|
|
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);
|
|
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);
|
|
}
|
|
} |