mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-06 23:34:13 +02:00
Add core models, DTOs, and services for EchoHub chat application
- Created User, Channel, Message, and ServerInfo models with necessary properties. - Added DTOs for user profiles, server information, and message handling. - Implemented JWT authentication service for user login and token generation. - Developed Entity Framework Core DbContext for database interactions. - Introduced SignalR ChatHub for real-time messaging and presence tracking. - Implemented file storage and image to ASCII conversion services. - Set up CORS and middleware for API endpoints. - Created launch settings and development configuration for server and web projects.
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace EchoHub.Server.Services;
|
||||
|
||||
public class PresenceTracker
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, (Guid userId, string username)> _connections = new();
|
||||
private readonly ConcurrentDictionary<string, HashSet<string>> _userConnections = new();
|
||||
private readonly ConcurrentDictionary<string, HashSet<string>> _userChannels = new();
|
||||
|
||||
private readonly object _lock = new();
|
||||
|
||||
public void UserConnected(string connectionId, Guid userId, string username)
|
||||
{
|
||||
_connections[connectionId] = (userId, username);
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_userConnections.TryGetValue(username, out var connections))
|
||||
{
|
||||
connections = new HashSet<string>();
|
||||
_userConnections[username] = connections;
|
||||
}
|
||||
|
||||
connections.Add(connectionId);
|
||||
}
|
||||
}
|
||||
|
||||
public string? UserDisconnected(string connectionId)
|
||||
{
|
||||
if (!_connections.TryRemove(connectionId, out var userInfo))
|
||||
return null;
|
||||
|
||||
var username = userInfo.username;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (_userConnections.TryGetValue(username, out var connections))
|
||||
{
|
||||
connections.Remove(connectionId);
|
||||
|
||||
if (connections.Count == 0)
|
||||
{
|
||||
_userConnections.TryRemove(username, out _);
|
||||
_userChannels.TryRemove(username, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return username;
|
||||
}
|
||||
|
||||
public void JoinChannel(string username, string channelName)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_userChannels.TryGetValue(username, out var channels))
|
||||
{
|
||||
channels = new HashSet<string>();
|
||||
_userChannels[username] = channels;
|
||||
}
|
||||
|
||||
channels.Add(channelName);
|
||||
}
|
||||
}
|
||||
|
||||
public void LeaveChannel(string username, string channelName)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_userChannels.TryGetValue(username, out var channels))
|
||||
{
|
||||
channels.Remove(channelName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> GetOnlineUsersInChannel(string channelName)
|
||||
{
|
||||
var users = new List<string>();
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
foreach (var (username, channels) in _userChannels)
|
||||
{
|
||||
if (channels.Contains(channelName))
|
||||
{
|
||||
users.Add(username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
public List<string> GetChannelsForUser(string username)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_userChannels.TryGetValue(username, out var channels))
|
||||
{
|
||||
return channels.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public bool IsOnline(string username)
|
||||
{
|
||||
return _userConnections.TryGetValue(username, out var connections) && connections.Count > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user