mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
Refactor friend request and media services, remove console commands
Refactored the friend request command service and SignalR hub to return and broadcast FriendshipDto objects, improving real-time updates. Enhanced media upload and download logic to support file storage and retrieval, including database integration. Removed all command classes and related infrastructure from the Govor.Console project, streamlining the console client. Updated dependency injection and interfaces to reflect these changes.
This commit is contained in:
@@ -198,11 +198,25 @@ public class ChatsHub : Hub
|
||||
{
|
||||
var result = await _messageCommandService.EditMessageAsync(editMessageParam);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
if (!result.IsSuccess || result.OriginalMessage == null)
|
||||
return LogAndError<MessageEditResponse>(editor, request.MessageId, "Edit message error",
|
||||
result.Exception);
|
||||
|
||||
return HubResult<MessageEditResponse>.Ok();
|
||||
var response = new MessageEditResponse()
|
||||
{
|
||||
MessageId = result.messageId,
|
||||
EditorId = editor,
|
||||
RecipientId = result.OriginalMessage.RecipientId,
|
||||
RecipientType = result.OriginalMessage.RecipientType,
|
||||
NewEncryptedContent = request.NewEncryptedContent,
|
||||
EditedAt = editMessageParam.EditedAt,
|
||||
};
|
||||
|
||||
await NotifyClientsAboutEdit(response);
|
||||
|
||||
_logger.LogInformation("Message {MessageId} edited successfully by {editor}", request.MessageId, editor);
|
||||
|
||||
return HubResult<MessageEditResponse>.Ok(response);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
@@ -259,6 +273,20 @@ public class ChatsHub : Hub
|
||||
}
|
||||
}
|
||||
|
||||
private async Task NotifyClientsAboutEdit(MessageEditResponse response)
|
||||
{
|
||||
if (response.RecipientType == RecipientType.User)
|
||||
{
|
||||
await Clients.Group(response.EditorId.ToString()).SendAsync("MessageEdit", response);
|
||||
if (response.EditorId != response.RecipientId)
|
||||
await Clients.Group(response.RecipientId.ToString()).SendAsync("MessageEdit", response);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Clients.Group($"group_{response.RecipientId}").SendAsync("MessageEdit", response);
|
||||
}
|
||||
}
|
||||
|
||||
// Logging helpers
|
||||
private HubResult<T> LogAndError<T>(Guid userId, Guid targetId, string message, Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using AutoMapper;
|
||||
using Govor.Application.Exceptions.FriendsService;
|
||||
using Govor.Application.Interfaces.Friends;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Contracts.Responses.SignalR;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
@@ -11,12 +14,17 @@ public class FriendsHub : Hub
|
||||
private readonly ILogger<FriendsHub> _logger;
|
||||
private readonly IFriendRequestCommandService _friendRequestService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public FriendsHub(IFriendRequestCommandService friendRequestService, ICurrentUserService currentUserService, ILogger<FriendsHub> logger)
|
||||
public FriendsHub(IFriendRequestCommandService friendRequestService,
|
||||
ICurrentUserService currentUserService,
|
||||
ILogger<FriendsHub> logger,
|
||||
IMapper mapper)
|
||||
{
|
||||
_friendRequestService = friendRequestService;
|
||||
_currentUserService = currentUserService;
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
@@ -70,9 +78,10 @@ public class FriendsHub : Hub
|
||||
try
|
||||
{
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
await _friendRequestService.SendAsync(userId, targetUserId);
|
||||
await Clients.User(targetUserId.ToString())
|
||||
.SendAsync("FriendRequestReceived", userId);
|
||||
var friendship = await _friendRequestService.SendAsync(userId, targetUserId);
|
||||
|
||||
await Clients.Group(targetUserId.ToString())
|
||||
.SendAsync("FriendRequestReceived", _mapper.Map<FriendshipDto>(friendship));
|
||||
|
||||
_logger.LogInformation($"Friend request received for user {targetUserId} from {userId}.");
|
||||
return HubResult<object>.Created();
|
||||
@@ -104,9 +113,9 @@ public class FriendsHub : Hub
|
||||
try
|
||||
{
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
await _friendRequestService.AcceptAsync(friendshipId, userId);
|
||||
await Clients.User(userId.ToString())
|
||||
.SendAsync("FriendRequestAccepted", friendshipId);
|
||||
var friendship = await _friendRequestService.AcceptAsync(friendshipId, userId);
|
||||
await Clients.Group(userId.ToString())
|
||||
.SendAsync("FriendRequestAccepted", _mapper.Map<FriendshipDto>(friendship));
|
||||
|
||||
_logger.LogInformation($"Friend request accepted for user {userId} from {userId}.");
|
||||
return HubResult<object>.Ok();
|
||||
@@ -133,9 +142,9 @@ public class FriendsHub : Hub
|
||||
try
|
||||
{
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
await _friendRequestService.RejectAsync(friendshipId, userId);
|
||||
await Clients.User(userId.ToString())
|
||||
.SendAsync("FriendRequestRejected", friendshipId);
|
||||
var friendship = await _friendRequestService.RejectAsync(friendshipId, userId);
|
||||
await Clients.Group(userId.ToString())
|
||||
.SendAsync("FriendRequestRejected", _mapper.Map<FriendshipDto>(friendship));
|
||||
|
||||
_logger.LogInformation($"Friend request rejected for user {userId} from {userId}.");
|
||||
return HubResult<object>.Ok();
|
||||
|
||||
Reference in New Issue
Block a user