mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
6d1c53beeb
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
71 lines
1.9 KiB
C#
71 lines
1.9 KiB
C#
using System.Collections.Concurrent;
|
|
|
|
namespace Govor.Application.Users.UserOnlineStatus;
|
|
|
|
public class OnlineUserStore : IOnlineUserStore
|
|
{
|
|
private readonly ConcurrentDictionary<Guid, HashSet<string>> _userConnections = new();
|
|
|
|
public bool AddConnection(Guid userId, string connectionId)
|
|
{
|
|
bool isFirstConnection = false;
|
|
|
|
_userConnections.AddOrUpdate(userId,
|
|
_ => {
|
|
isFirstConnection = true;
|
|
return new HashSet<string> { connectionId };
|
|
},
|
|
(_, connections) => {
|
|
lock (connections)
|
|
{
|
|
if (connections.Count == 0) isFirstConnection = true;
|
|
connections.Add(connectionId);
|
|
}
|
|
return connections;
|
|
});
|
|
|
|
return isFirstConnection;
|
|
}
|
|
|
|
public bool RemoveConnection(Guid userId, string connectionId)
|
|
{
|
|
bool isLastConnection = false;
|
|
|
|
if (_userConnections.TryGetValue(userId, out var connections))
|
|
{
|
|
lock (connections)
|
|
{
|
|
connections.Remove(connectionId);
|
|
if (connections.Count == 0)
|
|
{
|
|
isLastConnection = true;
|
|
_userConnections.TryRemove(userId, out _);
|
|
}
|
|
}
|
|
}
|
|
|
|
return isLastConnection;
|
|
}
|
|
|
|
public IEnumerable<string> GetConnections(Guid userId)
|
|
{
|
|
if (_userConnections.TryGetValue(userId, out var connections))
|
|
{
|
|
lock (connections)
|
|
{
|
|
return connections.ToList();
|
|
}
|
|
}
|
|
return Enumerable.Empty<string>();
|
|
}
|
|
|
|
public bool IsOnline(Guid userId)
|
|
{
|
|
return _userConnections.TryGetValue(userId, out var connections) && connections.Count > 0;
|
|
}
|
|
|
|
public IReadOnlyCollection<Guid> GetAllOnlineUsers()
|
|
{
|
|
return _userConnections.Keys.ToList();
|
|
}
|
|
} |