Add online pinging and friendship verification features

Introduces OnlinePingingController and related integration/unit tests for user online status updates. Adds PingHandlerService with memory cache throttling, IPingHandlerService interface, and service registration. Implements VerifyFriendship service and interface for friendship validation, with exception handling. Refactors CurrentUserService for improved user ID extraction and testability. Updates ChatsHub to verify friendship before messaging. Cleans up MediaController and optimizes UsersRepository queries by removing unnecessary includes.
This commit is contained in:
Artemy
2025-07-02 19:16:49 +07:00
parent 0669614a5e
commit 565d3249e5
14 changed files with 483 additions and 32 deletions
-9
View File
@@ -14,7 +14,6 @@ public class MediaController : Controller
{
private readonly ILogger<MediaController> _logger;
private readonly IStorageService _storageService;
private readonly IMediaAttachmentsRepository _repository;
public MediaController(ILogger<MediaController> logger, IStorageService storageService)
{
@@ -31,14 +30,6 @@ public class MediaController : Controller
var url = await _storageService.SaveAsync(request.Data,request.FileName);
var mediaId = Guid.NewGuid();
_repository.AddAsync(new MediaAttachments()
{
Id = mediaId,
FilePath = url,
EncryptedKey = request.EncryptedKey,
MimeType = request.MimeType,
Type = request.Type,
});
return Ok(mediaId);
}
@@ -0,0 +1,57 @@
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Core.Repositories.Users;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers;
[ApiController]
[Route("api/online")]
[Authorize(Roles = "User,Admin")]
public class OnlinePingingController : Controller
{
private readonly ILogger<OnlinePingingController> _logger;
private readonly IPingHandlerService _ping;
private readonly ICurrentUserService _currentUserService;
public OnlinePingingController(ILogger<OnlinePingingController> logger,
IPingHandlerService ping,
ICurrentUserService currentUserService)
{
_logger = logger;
_ping = ping;
_currentUserService = currentUserService;
}
[HttpPatch("ping")]// api/online/ping
public async Task<IActionResult> Ping()
{
try
{
_logger.LogInformation("Ping...");
var id = _currentUserService.GetCurrentUserId();
await _ping.Ping(id);
_logger.LogInformation($"Ping from user {id} processed successfully");
return Ok();
}
catch (InvalidOperationException e)
{
_logger.LogError(e, e.Message);
return BadRequest("User can't be found in our database.");
}
catch (UnauthorizedAccessException e)
{
_logger.LogError(e, e.Message);
return Forbid(e.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return StatusCode(500, new { error = "Failed to send friend request." });
}
}
}
@@ -43,6 +43,9 @@ public static class ConfigurationProgramExtensions
services.AddHttpContextAccessor(); // it's very important for CurrentUserService
services.AddScoped<ICurrentUserService, CurrentUserService>();
services.AddMemoryCache();
services.AddScoped<IPingHandlerService, PingHandlerService>();
}
public static void AddRepositories(this IServiceCollection services)
+8 -5
View File
@@ -1,4 +1,5 @@
using System.Security.Claims;
using Govor.Application.Interfaces;
using Govor.Contracts.Requests.SignalR;
using Govor.Core.Models;
using Govor.Core.Repositories.Users;
@@ -13,6 +14,7 @@ namespace Govor.API.Hubs;
public class ChatsHub : Hub
{
private readonly IUsersRepository _usersRepository;
private readonly IVerifyFriendship _verifyFriendship;
private readonly ILogger<ChatsHub> _logger;
public ChatsHub(IUsersRepository usersRepository, ILogger<ChatsHub> logger)
@@ -60,21 +62,22 @@ public class ChatsHub : Hub
var senderId = GetUserId();
// Проверка существования получателя
/*try
// Проверка существования получателя и установленной дружбы
try
{
await _usersRepository.FindByIdAsync(toUserId);
await _usersRepository.FindByIdAsync(request.RecipientId);
await _verifyFriendship.VerifyAsync(senderId, request.RecipientId);
}
catch (NotFoundByKeyException<User> ex)
{
_logger.LogWarning("Recipient user {ToUserId} not found", toUserId);
_logger.LogWarning("Recipient user {ToUserId} not found", request.RecipientId);
throw;
}
catch (ArgumentException ex)
{
_logger.LogWarning("Invalid recipient userId received from user {UserId}", GetUserId());
throw;
}*/
}
try
{