mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +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:
@@ -29,7 +29,7 @@ public class FriendshipsController : Controller
|
||||
{
|
||||
_logger.LogInformation("Get all friendships by administrator");
|
||||
var result = await _friendshipsRepository.GetAllAsync();
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
return Ok(result);
|
||||
}
|
||||
catch (NotFoundException ex)
|
||||
{
|
||||
@@ -53,7 +53,7 @@ public class FriendshipsController : Controller
|
||||
{
|
||||
_logger.LogInformation($"Get user's {userId} all friendships by administrator");
|
||||
var result = await _friendshipsRepository.FindByUserIdAsync(userId);
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
return Ok(result);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
@@ -89,21 +89,4 @@ public class FriendshipsController : Controller
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
private List<UserDto> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Username = user.Username,
|
||||
Description = user.Description,
|
||||
WasOnline = user.WasOnline,
|
||||
IconId = user.IconId
|
||||
}).ToList();
|
||||
|
||||
private List<FriendshipDto> BuildFriendshipDtos(IEnumerable<Friendship> friendships) => friendships.Select(f => new FriendshipDto
|
||||
{
|
||||
Id = f.Id,
|
||||
Status = f.Status,
|
||||
AddresseeId = f.AddresseeId,
|
||||
RequesterId = f.RequesterId,
|
||||
}).ToList();
|
||||
}
|
||||
@@ -56,7 +56,11 @@ public class FriendsRequestQueryController : Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetResponsesAsync(_currentUserService.GetCurrentUserId());
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
|
||||
_logger.LogInformation("Getting responses by user {userId}", userId);
|
||||
|
||||
var result = await _friendsService.GetResponsesAsync(userId);
|
||||
var response = _mapper.Map<List<FriendshipDto>>(result);
|
||||
|
||||
return Ok(response);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Application.Interfaces.Medias;
|
||||
using Govor.Contracts.Requests;
|
||||
using Govor.Core.Models;
|
||||
@@ -15,27 +16,67 @@ public class MediaController : Controller
|
||||
{
|
||||
private readonly ILogger<MediaController> _logger;
|
||||
private readonly IMediaService _mediaService;
|
||||
|
||||
public MediaController(ILogger<MediaController> logger, IMediaService mediaService)
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
public MediaController(
|
||||
ILogger<MediaController> logger,
|
||||
IMediaService mediaService,
|
||||
ICurrentUserService currentUserService)
|
||||
{
|
||||
_logger = logger;
|
||||
_mediaService = mediaService;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
[HttpPost("upload")]
|
||||
[RequestSizeLimit(100_000_000)]// ~100MB
|
||||
[RequestSizeLimit(100_000_000)] // ~100MB
|
||||
public async Task<IActionResult> Upload([FromForm] MediaUploadRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _mediaService.UploadMediaAsync(new Media(request.Data, request.FileName, request.Type,
|
||||
request.MimeType, request.EncryptedKey));
|
||||
|
||||
return Ok(result);
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
// Чтение байт из IFormFile
|
||||
using var memoryStream = new MemoryStream();
|
||||
await request.Data.CopyToAsync(memoryStream);
|
||||
byte[] fileBytes = memoryStream.ToArray();
|
||||
|
||||
var media = new Media(
|
||||
_currentUserService.GetCurrentUserId(),
|
||||
DateTime.UtcNow,
|
||||
fileBytes,
|
||||
request.FileName,
|
||||
request.Type,
|
||||
request.MimeType,
|
||||
request.EncryptedKey
|
||||
);
|
||||
|
||||
var result = await _mediaService.UploadMediaAsync(media);
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex);
|
||||
_logger.LogError(ex, "Error uploading media");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("download/{id}")]
|
||||
public async Task<IActionResult> Download(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var media = await _mediaService.GetMediaByIdAsync(id);
|
||||
return File(media.Data, media.MineType, media.FileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error downloading media");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.Authentication;
|
||||
using Govor.Application.Interfaces.Friends;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Application.Interfaces.Medias;
|
||||
using Govor.Application.Interfaces.Messages;
|
||||
using Govor.Application.Services;
|
||||
using Govor.Application.Services.Authentication;
|
||||
@@ -65,6 +66,7 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
|
||||
services.AddScoped<IUserGroupsService, UserGroupsService>();
|
||||
services.AddScoped<IMessagesLoader, MessagesLoader>();
|
||||
services.AddScoped<IMediaService, MediaService>();
|
||||
|
||||
// Auto Mapper
|
||||
services.AddAutoMapper(typeof(MappingProfile));
|
||||
|
||||
@@ -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