mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
was added firebase nitifying
This commit is contained in:
@@ -1,16 +1,29 @@
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.PushNotifications;
|
||||
using Govor.Contracts.Responses.SignalR;
|
||||
using Govor.Core.Models.Messages;
|
||||
using Govor.Core.Repositories.PrivateChats;
|
||||
using Govor.Core.Repositories.PushTokens;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace Govor.API.Hubs.Infrastructure;
|
||||
|
||||
public class ChatNotificationService : IChatNotificationService
|
||||
{
|
||||
private readonly IHubContext<ChatsHub> _hubContext;
|
||||
|
||||
public ChatNotificationService(IHubContext<ChatsHub> hubContext)
|
||||
private readonly IHubContext<ChatsHub> _hubContext;
|
||||
private readonly IPrivateChatsRepository _privateChatsRepository;
|
||||
private readonly IPushNotificationService _notificationService;
|
||||
private readonly IProfileService _profileService;
|
||||
public ChatNotificationService(
|
||||
IProfileService profileService,
|
||||
IHubContext<ChatsHub> hubContext,
|
||||
IPushNotificationService notificationService,
|
||||
IPrivateChatsRepository privateChatsRepository)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
_profileService = profileService;
|
||||
_notificationService = notificationService;
|
||||
_privateChatsRepository = privateChatsRepository;
|
||||
}
|
||||
|
||||
public async Task NotifyMessageSentAsync(UserMessageResponse message)
|
||||
@@ -19,6 +32,8 @@ private readonly IHubContext<ChatsHub> _hubContext;
|
||||
{
|
||||
await _hubContext.Clients.Group(ChatHubConstants.GetPrivateChat(message.RecipientId))
|
||||
.SendAsync(ChatHubConstants.ReceiveMessage, message);
|
||||
|
||||
await NotifyMessageReceivedInPrivateChatAsync(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -30,6 +45,19 @@ private readonly IHubContext<ChatsHub> _hubContext;
|
||||
// .SendAsync(ChatHubConstants.MessageSent, message);
|
||||
}
|
||||
|
||||
private async Task NotifyMessageReceivedInPrivateChatAsync(UserMessageResponse message)
|
||||
{
|
||||
var privateChat = await _privateChatsRepository.GetByIdAsync(message.RecipientId);
|
||||
|
||||
var text = message.EncryptedContent.Substring(0, Math.Min(40, message.EncryptedContent.Length));
|
||||
var userId = message.SenderId == privateChat.UserAId ? privateChat.UserAId : privateChat.UserBId;
|
||||
|
||||
var profile = await _profileService.GetUserProfileAsync(userId);
|
||||
var title = profile.Username;
|
||||
|
||||
await _notificationService.SendToUserAsync(userId, title,text, "chat_messages");
|
||||
}
|
||||
|
||||
public async Task NotifyMessageRemovedAsync(MessageRemovedResponse response)
|
||||
{
|
||||
await NotifyParticipantsAsync(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Govor.Application.Interfaces;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
@@ -7,11 +8,17 @@ public class ConnectionManager : IConnectionManager
|
||||
{
|
||||
private readonly IUserGroupsGetterService _userGroupsGetterService;
|
||||
private readonly IUserPrivateChatsGetterService _userPrivateChatsGetterService;
|
||||
private readonly IConnectionStore _connectionStore;
|
||||
private readonly IHubContext<ChatsHub> _hubContext;
|
||||
|
||||
public ConnectionManager(IUserGroupsGetterService userGroupsGetterService, IUserPrivateChatsGetterService userPrivateChatsGetterService, IHubContext<ChatsHub> hubContext)
|
||||
public ConnectionManager(
|
||||
IUserGroupsGetterService userGroupsGetterService,
|
||||
IConnectionStore connectionStore,
|
||||
IUserPrivateChatsGetterService userPrivateChatsGetterService,
|
||||
IHubContext<ChatsHub> hubContext)
|
||||
{
|
||||
_userGroupsGetterService = userGroupsGetterService;
|
||||
_connectionStore = connectionStore;
|
||||
_userPrivateChatsGetterService = userPrivateChatsGetterService;
|
||||
_hubContext = hubContext;
|
||||
}
|
||||
@@ -20,7 +27,8 @@ public class ConnectionManager : IConnectionManager
|
||||
{
|
||||
// user
|
||||
await _hubContext.Groups.AddToGroupAsync(connectionId, ChatHubConstants.GetUserGroup(userId));
|
||||
|
||||
_connectionStore.AddConnection(userId, connectionId);
|
||||
|
||||
// groups
|
||||
var userGroups = await _userGroupsGetterService.GetUserGroupsAsync(userId);
|
||||
foreach (var group in userGroups)
|
||||
@@ -41,6 +49,7 @@ public class ConnectionManager : IConnectionManager
|
||||
if (userId != Guid.Empty)
|
||||
{
|
||||
await _hubContext.Groups.RemoveFromGroupAsync(connectionId, ChatHubConstants.GetUserGroup(userId));
|
||||
_connectionStore.RemoveConnection(userId, connectionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Govor.Application.Interfaces;
|
||||
|
||||
namespace Govor.API.Hubs.Infrastructure;
|
||||
|
||||
public class ConnectionStore : IConnectionStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<Guid, HashSet<string>> _connections = new();
|
||||
|
||||
public void AddConnection(Guid userId, string connectionId)
|
||||
{
|
||||
var conns = _connections.GetOrAdd(userId, _ => new HashSet<string>());
|
||||
lock (conns) conns.Add(connectionId);
|
||||
}
|
||||
|
||||
public void RemoveConnection(Guid userId, string connectionId)
|
||||
{
|
||||
if (_connections.TryGetValue(userId, out var conns))
|
||||
{
|
||||
lock (conns)
|
||||
{
|
||||
conns.Remove(connectionId);
|
||||
if (!conns.Any()) _connections.TryRemove(userId, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetConnections(Guid userId)
|
||||
{
|
||||
return _connections.TryGetValue(userId, out var conns) ? conns.ToList() : Enumerable.Empty<string>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user