Редактирование и плеер

This commit is contained in:
Халимов Рустам
2026-04-08 15:23:10 +03:00
parent 3905094ff4
commit 8399d32490
5 changed files with 98 additions and 8 deletions

View File

@@ -0,0 +1,69 @@
using Knot.Contracts.Messaging.Application.Abstractions;
using Knot.Contracts.Messaging.Domain;
using Knot.Contracts.Conversations.Application.Abstractions;
using Knot.Contracts.Conversations.Domain;
using Knot.Modules.Conversations.Infrastructure.SignalR;
using Knot.Shared.Kernel;
using MediatR;
using Microsoft.AspNetCore.SignalR;
namespace Knot.Modules.Conversations.Application.Messages.Edit;
public sealed record EditMessageCommand(
Guid MessageId,
Guid ChatId,
Guid UserId,
string Content) : ICommand;
public sealed class EditMessageCommandHandler : ICommandHandler<EditMessageCommand>
{
private readonly IMessageRepository _messageRepository;
private readonly IChatsUnitOfWork _unitOfWork;
private readonly IHubContext<ChatHub> _hubContext;
public EditMessageCommandHandler(
IMessageRepository messageRepository,
IChatsUnitOfWork unitOfWork,
IHubContext<ChatHub> hubContext)
{
_messageRepository = messageRepository;
_unitOfWork = unitOfWork;
_hubContext = hubContext;
}
public async Task<Result> Handle(EditMessageCommand request, CancellationToken cancellationToken)
{
var message = await _messageRepository.GetByIdAsync(request.MessageId, cancellationToken);
if (message is null)
{
return Result.Failure(new Error("Message.NotFound", "Message not found."));
}
if (message.SenderId != request.UserId)
{
return Result.Failure(new Error("Message.Forbidden", "You can only edit your own messages."));
}
if (message.ChatId != request.ChatId)
{
return Result.Failure(new Error("Message.InvalidChat", "Message does not belong to this chat."));
}
message.Edit(request.Content);
await _messageRepository.UpdateAsync(message, cancellationToken);
await _unitOfWork.SaveChangesAsync(cancellationToken);
// Notify clients
await _hubContext.Clients.Group(request.ChatId.ToString()).SendAsync("message_edited", new
{
messageId = message.Id,
chatId = message.ChatId,
content = message.Content,
isEdited = true
});
return Result.Success();
}
}

View File

@@ -16,6 +16,7 @@ using Knot.Contracts.Auth.Application.Abstractions;
using Knot.Modules.Conversations.Application.Messages.Pin;
using Knot.Modules.Conversations.Application.Messages.Unpin;
using Knot.Modules.Conversations.Application.Messages.Vote;
using Knot.Modules.Conversations.Application.Messages.Edit;
using Knot.Modules.Conversations.Application.DTOs;
using Knot.Contracts.Messaging.Application.Abstractions;
using Knot.Contracts.Messaging.Domain;
@@ -284,6 +285,17 @@ public sealed class ChatHub : Hub
});
}
[HubMethodName("edit_message")]
public async Task EditMessage(EditMessageHubRequest request)
{
var command = new EditMessageCommand(request.MessageId, request.ChatId, _userContext.UserId, request.Content);
var result = await _sender.Send(command);
if (result.IsFailure)
{
throw new HubException(result.Error.Description);
}
}
[HubMethodName("vote_poll")]
public async Task VotePoll(VotePollRequest request)
{
@@ -856,6 +868,7 @@ public sealed class ChatHub : Hub
public record GroupRenegotiateAnswerRequest(string ChatId, string TargetUserId, object Answer);
public record FriendSignalRequest(string FriendId);
public record VotePollRequest(Guid MessageId, Guid ChatId, Guid OptionId);
public record EditMessageHubRequest(Guid MessageId, Guid ChatId, string Content);
public class CallSession
{