Files
Govor/Govor.API/Hubs/Infrastructure/PrivateChatGroupManager.cs
T
2026-03-01 16:48:06 +07:00

41 lines
1.4 KiB
C#

using Govor.API.Hubs;
using Govor.API.Hubs.Infrastructure;
using Govor.Application.Interfaces;
using Govor.Core.Models;
using Microsoft.AspNetCore.SignalR;
namespace Govor.API.Hubs.Infrastructure;
public class PrivateChatGroupManager : IPrivateChatGroupManager
{
private readonly IConnectionStore _connectionStore;
private readonly IHubContext<ChatsHub> _hubContext;
public PrivateChatGroupManager(IConnectionStore connectionStore, IHubContext<ChatsHub> hubContext)
{
_connectionStore = connectionStore;
_hubContext = hubContext;
}
public async Task AddUsersToPrivateChatGroupAsync(PrivateChat privateChat)
{
var connectionsA = _connectionStore.GetConnections(privateChat.UserAId);
var connectionsB = _connectionStore.GetConnections(privateChat.UserBId);
foreach (var conn in connectionsA.Concat(connectionsB))
{
await _hubContext.Groups.AddToGroupAsync(conn, ChatHubConstants.GetPrivateChat(privateChat.Id));
}
}
public async Task RemoveUsersFromPrivateChatGroupAsync(PrivateChat privateChat)
{
var connectionsA = _connectionStore.GetConnections(privateChat.UserAId);
var connectionsB = _connectionStore.GetConnections(privateChat.UserBId);
foreach (var conn in connectionsA.Concat(connectionsB))
{
await _hubContext.Groups.RemoveFromGroupAsync(conn, ChatHubConstants.GetPrivateChat(privateChat.Id));
}
}
}