test to make server

This commit is contained in:
Artemy
2026-02-08 22:30:29 +07:00
parent cc2921d257
commit 0a43e35797
56 changed files with 949 additions and 442 deletions
@@ -0,0 +1,13 @@
namespace Govor.API.Hubs.Infrastructure;
public static class ChatHubConstants
{
public const string ReceiveMessage = "ReceiveMessage";
public const string MessageSent = "MessageSent";
public const string MessageRemoved = "MessageRemoved";
public const string MessageEdited = "MessageEdit";
public static string GetUserGroup(Guid userId) => userId.ToString();
public static string GetChatGroup(Guid groupId) => $"group_{groupId}";
public static string GetPrivateChat(Guid groupId) => $"private_{groupId}";
}
@@ -0,0 +1,73 @@
using Govor.Contracts.Responses.SignalR;
using Govor.Core.Models.Messages;
using Microsoft.AspNetCore.SignalR;
namespace Govor.API.Hubs.Infrastructure;
public class ChatNotificationService : IChatNotificationService
{
private readonly IHubContext<ChatsHub> _hubContext;
public ChatNotificationService(IHubContext<ChatsHub> hubContext)
{
_hubContext = hubContext;
}
public async Task NotifyMessageSentAsync(UserMessageResponse message)
{
if (message.RecipientType == RecipientType.User)
{
await _hubContext.Clients.Group(ChatHubConstants.GetPrivateChat(message.RecipientId))
.SendAsync(ChatHubConstants.ReceiveMessage, message);
}
else
{
await _hubContext.Clients.Group(ChatHubConstants.GetChatGroup(message.RecipientId))
.SendAsync(ChatHubConstants.ReceiveMessage, message);
}
// await _hubContext.Clients.Group(ChatHubConstants.GetUserGroup(message.SenderId))
// .SendAsync(ChatHubConstants.MessageSent, message);
}
public async Task NotifyMessageRemovedAsync(MessageRemovedResponse response)
{
await NotifyParticipantsAsync(
response.SenderId,
response.RecipientId,
response.RecipientType,
ChatHubConstants.MessageRemoved,
response);
}
public async Task NotifyMessageEditedAsync(MessageEditResponse response)
{
await NotifyParticipantsAsync(
response.EditorId,
response.RecipientId,
response.RecipientType,
ChatHubConstants.MessageEdited,
response);
}
private async Task NotifyParticipantsAsync(Guid initiatorId, Guid targetId, RecipientType type, string method, object payload)
{
if (type == RecipientType.User)
{
await _hubContext.Clients.Group(ChatHubConstants.GetUserGroup(initiatorId))
.SendAsync(method, payload);
if (initiatorId != targetId)
{
await _hubContext.Clients.Group(ChatHubConstants.GetUserGroup(targetId))
.SendAsync(method, payload);
}
}
else
{
// to groups
await _hubContext.Clients.Group(ChatHubConstants.GetChatGroup(targetId))
.SendAsync(method, payload);
}
}
}
@@ -0,0 +1,46 @@
using Govor.Application.Interfaces;
using Microsoft.AspNetCore.SignalR;
namespace Govor.API.Hubs.Infrastructure;
public class ConnectionManager : IConnectionManager
{
private readonly IUserGroupsGetterService _userGroupsGetterService;
private readonly IUserPrivateChatsGetterService _userPrivateChatsGetterService;
private readonly IHubContext<ChatsHub> _hubContext;
public ConnectionManager(IUserGroupsGetterService userGroupsGetterService, IUserPrivateChatsGetterService userPrivateChatsGetterService, IHubContext<ChatsHub> hubContext)
{
_userGroupsGetterService = userGroupsGetterService;
_userPrivateChatsGetterService = userPrivateChatsGetterService;
_hubContext = hubContext;
}
public async Task OnConnectedAsync(string connectionId, Guid userId)
{
// user
await _hubContext.Groups.AddToGroupAsync(connectionId, ChatHubConstants.GetUserGroup(userId));
// groups
var userGroups = await _userGroupsGetterService.GetUserGroupsAsync(userId);
foreach (var group in userGroups)
{
await _hubContext.Groups.AddToGroupAsync(connectionId, ChatHubConstants.GetChatGroup(group.Id));
}
// private chats
var chats = await _userPrivateChatsGetterService.GetUserChatsAsync(userId);
foreach (var group in chats)
{
await _hubContext.Groups.AddToGroupAsync(connectionId, ChatHubConstants.GetPrivateChat(group.Id));
}
}
public async Task OnDisconnectedAsync(string connectionId, Guid userId)
{
if (userId != Guid.Empty)
{
await _hubContext.Groups.RemoveFromGroupAsync(connectionId, ChatHubConstants.GetUserGroup(userId));
}
}
}
@@ -0,0 +1,10 @@
using Govor.Contracts.Responses.SignalR;
namespace Govor.API.Hubs.Infrastructure;
public interface IChatNotificationService
{
Task NotifyMessageSentAsync(UserMessageResponse message);
Task NotifyMessageRemovedAsync(MessageRemovedResponse response);
Task NotifyMessageEditedAsync(MessageEditResponse response);
}
@@ -0,0 +1,7 @@
namespace Govor.API.Hubs.Infrastructure;
public interface IConnectionManager
{
Task OnConnectedAsync(string connectionId, Guid userId);
Task OnDisconnectedAsync(string connectionId, Guid userId);
}