From 0fa768556aca00f4346ccd71917fad048def6323 Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 30 May 2024 08:22:50 +0000 Subject: Move around some projects, further cleanup pending --- .../LibMatrix.TestDataGenerator/Bot/DataFetcher.cs | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Utilities/LibMatrix.TestDataGenerator/Bot/DataFetcher.cs (limited to 'Utilities/LibMatrix.TestDataGenerator/Bot/DataFetcher.cs') diff --git a/Utilities/LibMatrix.TestDataGenerator/Bot/DataFetcher.cs b/Utilities/LibMatrix.TestDataGenerator/Bot/DataFetcher.cs new file mode 100644 index 0000000..66b8a03 --- /dev/null +++ b/Utilities/LibMatrix.TestDataGenerator/Bot/DataFetcher.cs @@ -0,0 +1,59 @@ +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; +using LibMatrix.Homeservers; +using LibMatrix.RoomTypes; +using LibMatrix.Utilities.Bot; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously + +namespace TestDataGenerator.Bot; + +public class DataFetcher(AuthenticatedHomeserverGeneric hs, ILogger logger, LibMatrixBotConfiguration botConfiguration) : IHostedService { + private Task? _listenerTask; + + private GenericRoom? _logRoom; + + /// Triggered when the application host is ready to start the service. + /// Indicates that the start process has been aborted. + public async Task StartAsync(CancellationToken cancellationToken) { + _listenerTask = Run(cancellationToken); + logger.LogInformation("Bot started!"); + } + + private async Task Run(CancellationToken cancellationToken) { + Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete); + _logRoom = hs.GetRoom(botConfiguration.LogRoom!); + + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: "Test data collector started!")); + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: "Fetching rooms...")); + + var rooms = await hs.GetJoinedRooms(); + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: $"Fetched {rooms.Count} rooms!")); + + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: "Fetching room data...")); + + var roomAliasTasks = rooms.Select(room => room.GetCanonicalAliasAsync()).ToAsyncEnumerable(); + List> aliasResolutionTasks = new(); + await foreach (var @event in roomAliasTasks) + if (@event?.Alias != null) { + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: $"Fetched room alias {@event.Alias}!")); + aliasResolutionTasks.Add(Task.Run(async () => { + var alias = await hs.ResolveRoomAliasAsync(@event.Alias); + return (@event.Alias, alias.RoomId); + }, cancellationToken)); + } + + var aliasResolutionTaskEnumerator = aliasResolutionTasks.ToAsyncEnumerable(); + await foreach (var result in aliasResolutionTaskEnumerator) + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: $"Resolved room alias {result.Item1} to {result.Item2}!")); + } + + /// Triggered when the application host is performing a graceful shutdown. + /// Indicates that the shutdown process should no longer be graceful. + public async Task StopAsync(CancellationToken cancellationToken) { + logger.LogInformation("Shutting down bot!"); + _listenerTask?.Dispose(); + } +} \ No newline at end of file -- cgit 1.4.1