mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
ChatGroup work
This commit is contained in:
+37
-15
@@ -2,6 +2,7 @@ using Govor.API.Services;
|
||||
using Govor.Application.Interfaces.Messages;
|
||||
using Govor.Application.Interfaces.Messages.Parameters;
|
||||
using Govor.Contracts.Requests.SignalR;
|
||||
using Govor.Contracts.Responses.SignalR;
|
||||
using Govor.Core.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
@@ -26,12 +27,14 @@ public class ChatsHub : Hub
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
var userId = GetUserId();
|
||||
|
||||
if (userId != Guid.Empty)
|
||||
{
|
||||
// Binding ConnectionId to UserId
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, userId.ToString());
|
||||
_logger.LogInformation("User {UserId} connected with ConnectionId {ConnectionId}", userId, Context.ConnectionId);
|
||||
}
|
||||
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
@@ -56,6 +59,33 @@ public class ChatsHub : Hub
|
||||
|
||||
}
|
||||
|
||||
public async Task SendGroup(GroupMessageRequest request)
|
||||
{
|
||||
var senderId = GetUserId();
|
||||
|
||||
// Создание сообщения
|
||||
var message = new SendMessage(
|
||||
EncryptContent: request.EncryptedContent,
|
||||
ReplyToMessageId: request.ReplyToMessageId,
|
||||
FromUserId: senderId,
|
||||
RecipientId: request.GroupId,
|
||||
SendAt: DateTime.UtcNow,
|
||||
Media: request.MediaAttachments?.Select(f => new SendMedia(
|
||||
f.MediaId, f.EncryptedKey, f.Type, f.MimeType)) ?? Array.Empty<SendMedia>());
|
||||
|
||||
var result = await _groupService.SendMessageAsync(message);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
throw result.Exception!;
|
||||
|
||||
// Шлём всем участникам группы, кроме отправителя
|
||||
await Clients.GroupExcept($"group_{request.GroupId}", Context.ConnectionId)
|
||||
.SendAsync("ReceiveGroupMessage", message);
|
||||
|
||||
// Отправителю тоже
|
||||
await Clients.Caller.SendAsync("ReceiveGroupMessage", message);
|
||||
}
|
||||
|
||||
public async Task Send(MessageRequest request)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.EncryptedContent))
|
||||
@@ -79,10 +109,13 @@ public class ChatsHub : Hub
|
||||
Media: request.MediaAttachments?.Select(f => new SendMedia(
|
||||
f.MediaId, f.EncryptedKey, f.Type, f.MimeType)) ?? Array.Empty<SendMedia>());
|
||||
|
||||
if (request.RecipientType == RecipientType.User)
|
||||
{
|
||||
await SendUser(message);
|
||||
}
|
||||
Result result = await _chatService.SendMessageAsync(message);
|
||||
if(result.IsSuccess == false)
|
||||
throw result.Exception;
|
||||
|
||||
// Sending a message to the sender and recipient
|
||||
await Clients.Group(message.RecipientId.ToString()).SendAsync("Receive", message);
|
||||
await Clients.Group(message.FromUserId.ToString()).SendAsync("Receive", message);
|
||||
// TODO: Send to Group
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -92,17 +125,6 @@ public class ChatsHub : Hub
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using Govor.Application.Interfaces.Messages;
|
||||
using Govor.Core.Models;
|
||||
|
||||
namespace Govor.API.Services;
|
||||
|
||||
public interface IGroupService
|
||||
public interface IGroupService : IMessageSendingService, IMessageManagementService
|
||||
{
|
||||
ChatGroup GetGroupByInvite(string code);
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
namespace Govor.Application.Interfaces.Messages.Parameters;
|
||||
|
||||
public record Result(bool IsSuccess, Exception Exception);
|
||||
public record Result(bool IsSuccess, Exception Exception, Guid messageId);
|
||||
@@ -48,11 +48,12 @@ public class PrivateChatService : IChatService
|
||||
};
|
||||
|
||||
await _messages.AddAsync(message);
|
||||
return new Result(true, null);
|
||||
|
||||
return new Result(true, null, messageId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new Result(false, ex);
|
||||
return new Result(false, ex, Guid.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Govor.Contracts.Requests.SignalR;
|
||||
|
||||
public record GroupMessageRequest()
|
||||
{
|
||||
public Guid GroupId { get; init; }
|
||||
public string EncryptedContent { get; init; } = string.Empty;
|
||||
public Guid? ReplyToMessageId { get; set; }
|
||||
public List<MediaReference> MediaAttachments { get; set; } = new();
|
||||
}
|
||||
@@ -5,7 +5,6 @@ namespace Govor.Contracts.Requests.SignalR;
|
||||
public record MessageRequest
|
||||
{
|
||||
public Guid RecipientId { get; init; }
|
||||
public RecipientType RecipientType { get; set; }
|
||||
public string EncryptedContent { get; init; } = string.Empty;
|
||||
public Guid? ReplyToMessageId { get; set; }
|
||||
public List<MediaReference> MediaAttachments { get; set; } = new();
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using Govor.Contracts.Requests.SignalR;
|
||||
|
||||
namespace Govor.Contracts.Responses.SignalR;
|
||||
|
||||
public record UserMessageResponse
|
||||
{
|
||||
public Guid Id { get; init; }
|
||||
public Guid SenderId { get; init; }
|
||||
public string EncryptedContent { get; init; } = string.Empty;
|
||||
public Guid? ReplyToMessageId { get; set; }
|
||||
public List<MediaReference> MediaReferences { get; init; } = new List<MediaReference>();
|
||||
}
|
||||
@@ -118,7 +118,7 @@ public class MessagesRepository : IMessagesRepository
|
||||
.SetProperty(m => m.ReplyToMessageId, message.ReplyToMessageId)
|
||||
.SetProperty(m => m.MessageViews, message.MessageViews)
|
||||
.SetProperty(m => m.MediaAttachments, message.MediaAttachments)
|
||||
|
||||
|
||||
);
|
||||
|
||||
if (rowsAffected == 0)
|
||||
|
||||
Reference in New Issue
Block a user