Redid stores. Added user store. Added Channel Manager, to manage data from a single channel.
This commit is contained in:
56
Services/ChannelManager.cs
Normal file
56
Services/ChannelManager.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Concurrent;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
|
||||
namespace HermesSocketServer.Services
|
||||
{
|
||||
public class ChannelManager
|
||||
{
|
||||
private readonly UserStore _users;
|
||||
private readonly Database _database;
|
||||
private readonly Serilog.ILogger _logger;
|
||||
private readonly IDictionary<string, Channel> _channels;
|
||||
|
||||
public ChannelManager(UserStore users, Database database, Serilog.ILogger logger)
|
||||
{
|
||||
_users = users;
|
||||
_database = database;
|
||||
_logger = logger;
|
||||
_channels = new ConcurrentDictionary<string, Channel>();
|
||||
}
|
||||
|
||||
|
||||
public async Task Add(string userId)
|
||||
{
|
||||
var user = _users.Get(userId);
|
||||
if (user == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (_channels.ContainsKey(userId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var chatters = new ChatterStore(userId, _database, _logger);
|
||||
await chatters.Load();
|
||||
|
||||
var channel = new Channel()
|
||||
{
|
||||
Id = userId,
|
||||
User = user,
|
||||
Chatters = chatters
|
||||
};
|
||||
|
||||
_channels.Add(userId, channel);
|
||||
}
|
||||
|
||||
public Channel? Get(string channelId)
|
||||
{
|
||||
if (_channels.TryGetValue(channelId, out var channel))
|
||||
return channel;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user