hot fix for friends controller

This commit is contained in:
Artemy
2025-07-03 19:46:53 +07:00
parent 565d3249e5
commit d170c6270f
16 changed files with 193 additions and 49 deletions
+5 -5
View File
@@ -57,7 +57,7 @@ public class FriendsController : Controller
try
{
await _friendsService.SendFriendRequestAsync(targetUserId, _currentUserService.GetCurrentUserId());
await _friendsService.SendFriendRequestAsync(_currentUserService.GetCurrentUserId(), targetUserId);
return Ok(new { message = "Friend request sent successfully." });
}
catch (InvalidOperationException ex)
@@ -98,14 +98,14 @@ public class FriendsController : Controller
}
[HttpPost("accept")]
public async Task<IActionResult> AcceptFriend(Guid requesterId)
public async Task<IActionResult> AcceptFriend(Guid friendshipId)
{
if (requesterId == Guid.Empty)
if (friendshipId == Guid.Empty)
return BadRequest("Requester ID is invalid");
try
{
await _friendsService.AcceptFriendRequestAsync(requesterId, _currentUserService.GetCurrentUserId());
await _friendsService.AcceptFriendRequestAsync(friendshipId, _currentUserService.GetCurrentUserId());
return Ok(new { message = "Friend request accepted." });
}
catch (InvalidOperationException ex)
@@ -136,7 +136,7 @@ public class FriendsController : Controller
catch (InvalidOperationException ex)
{
_logger.LogError(ex, ex.Message);
return BadRequest(new { error = "User data not found." });
return Ok(Array.Empty<UserDto>());
}
catch (Exception ex)
{
@@ -54,4 +54,10 @@ public class OnlinePingingController : Controller
return StatusCode(500, new { error = "Failed to send friend request." });
}
}
[HttpGet("is-online")]
public async Task<IActionResult> IsOnline(Guid userId)
{
return BadRequest();
}
}
+42 -33
View File
@@ -1,11 +1,9 @@
using System.Security.Claims;
using Govor.Application.Interfaces;
using Govor.API.Services;
using Govor.Application.Interfaces.Messages;
using Govor.Application.Interfaces.Messages.Parameters;
using Govor.Contracts.Requests.SignalR;
using Govor.Core.Models;
using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
namespace Govor.API.Hubs;
@@ -13,14 +11,16 @@ namespace Govor.API.Hubs;
[Authorize]
public class ChatsHub : Hub
{
private readonly IUsersRepository _usersRepository;
private readonly IVerifyFriendship _verifyFriendship;
private readonly ILogger<ChatsHub> _logger;
public ChatsHub(IUsersRepository usersRepository, ILogger<ChatsHub> logger)
private readonly IChatService _chatService;
private readonly IGroupService _groupService;
public ChatsHub(ILogger<ChatsHub> logger,
IChatService chatService
)
{
_usersRepository = usersRepository;
_logger = logger;
_chatService = chatService;
}
public override async Task OnConnectedAsync()
@@ -28,7 +28,7 @@ public class ChatsHub : Hub
var userId = GetUserId();
if (userId != Guid.Empty)
{
// Привязываем ConnectionId к UserId
// Binding ConnectionId to UserId
await Groups.AddToGroupAsync(Context.ConnectionId, userId.ToString());
_logger.LogInformation("User {UserId} connected with ConnectionId {ConnectionId}", userId, Context.ConnectionId);
}
@@ -46,6 +46,11 @@ public class ChatsHub : Hub
await base.OnDisconnectedAsync(exception);
}
public async Task Remove(Guid recipientId, Guid messageId)
{
}
public async Task Edit(string newMessage, Guid messageId)
{
@@ -53,7 +58,6 @@ public class ChatsHub : Hub
public async Task Send(MessageRequest request)
{
// Валидация входных данных
if (string.IsNullOrWhiteSpace(request.EncryptedContent))
{
_logger.LogWarning("Empty message received from user {UserId}", GetUserId());
@@ -62,38 +66,43 @@ public class ChatsHub : Hub
var senderId = GetUserId();
// Проверка существования получателя и установленной дружбы
try
{
await _usersRepository.FindByIdAsync(request.RecipientId);
await _verifyFriendship.VerifyAsync(senderId, request.RecipientId);
}
catch (NotFoundByKeyException<User> ex)
{
_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
{
_logger.LogInformation("Message sent from {SenderId} to {RecipientId} at {UtcNow}", senderId, request.RecipientId, DateTime.UtcNow);
var message = new SendMessage(
EncryptContent: request.EncryptedContent,
ReplyToMessageId: request.ReplyToMessageId,
FromUserId: senderId,
RecipientId: request.RecipientId,
SendAt: DateTime.UtcNow,
Media: request.MediaAttachments?.Select(f => new SendMedia(
f.MediaId, f.EncryptedKey, f.Type, f.MimeType)) ?? Array.Empty<SendMedia>());
// Отправка сообщения отправителю и получателю
//await Clients.Group(request.RecipientId.ToString()).SendAsync("Receive", message, senderId);
// Clients.Group(senderId.ToString()).SendAsync("Receive", message, senderId);
if (request.RecipientType == RecipientType.User)
{
await SendUser(message);
}
// TODO: Send to Group
}
catch (Exception ex)
{
//_logger.LogError(ex, "Error sending message from {SenderId} to {RecipientId}", senderId, toUserId);
_logger.LogError(ex, "Error sending message from {SenderId} to {RecipientId}", senderId, request.RecipientId);
throw;
}
}
private async Task SendUser(SendMessage sendMessage)
{
Result result = await _chatService.SendMessageAsync(sendMessage);
if(result.IsSuccess == false)
throw result.Exception;
// Sending a message to the sender and recipient
await Clients.Group(sendMessage.RecipientId.ToString()).SendAsync("Receive", sendMessage);
await Clients.Group(sendMessage.FromUserId.ToString()).SendAsync("Receive", sendMessage);
}
private Guid GetUserId()
{
var userIdClaim = Context.User?.FindFirst("userID")?.Value;