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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -5,13 +5,14 @@ namespace HermesSocketServer.Services | ||||
|     public class DatabaseService : BackgroundService | ||||
|     { | ||||
|         private readonly VoiceStore _voices; | ||||
|         private readonly ChatterStore _chatters; | ||||
|         private readonly UserStore _users; | ||||
|         private readonly ServerConfiguration _configuration; | ||||
|         private readonly Serilog.ILogger _logger; | ||||
|  | ||||
|         public DatabaseService(VoiceStore voices, ChatterStore chatters, ServerConfiguration configuration, Serilog.ILogger logger) { | ||||
|         public DatabaseService(VoiceStore voices, UserStore users, ServerConfiguration configuration, Serilog.ILogger logger) | ||||
|         { | ||||
|             _voices = voices; | ||||
|             _chatters = chatters; | ||||
|             _users = users; | ||||
|             _configuration = configuration; | ||||
|             _logger = logger; | ||||
|         } | ||||
| @@ -20,16 +21,19 @@ namespace HermesSocketServer.Services | ||||
|         { | ||||
|             _logger.Information("Loading TTS voices..."); | ||||
|             await _voices.Load(); | ||||
|             _logger.Information("Loading TTS chatters' voice."); | ||||
|             await _chatters.Load(); | ||||
|             _logger.Information("Loading users..."); | ||||
|             await _users.Load(); | ||||
|  | ||||
|             await Task.Run(async () => | ||||
|             { | ||||
|                 var tasks = new List<Task>(); | ||||
|                 await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds)); | ||||
|                 while (true) | ||||
|                 { | ||||
|                     await Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds)); | ||||
|                     await _voices.Save(); | ||||
|                     await _chatters.Save(); | ||||
|                     tasks.Add(_voices.Save()); | ||||
|                     tasks.Add(_users.Save()); | ||||
|                     tasks.Add(Task.Delay(TimeSpan.FromSeconds(_configuration.Database.SaveDelayInSeconds))); | ||||
|                     await Task.WhenAll(tasks); | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user