diff options
Diffstat (limited to 'MatrixRoomUtils.Bot')
-rw-r--r-- | MatrixRoomUtils.Bot/FileStorageProvider.cs | 31 | ||||
-rw-r--r-- | MatrixRoomUtils.Bot/MRUBot.cs | 60 | ||||
-rw-r--r-- | MatrixRoomUtils.Bot/MatrixRoomUtils.Bot.csproj | 4 | ||||
-rw-r--r-- | MatrixRoomUtils.Bot/Program.cs | 24 |
4 files changed, 117 insertions, 2 deletions
diff --git a/MatrixRoomUtils.Bot/FileStorageProvider.cs b/MatrixRoomUtils.Bot/FileStorageProvider.cs new file mode 100644 index 0000000..8d99828 --- /dev/null +++ b/MatrixRoomUtils.Bot/FileStorageProvider.cs @@ -0,0 +1,31 @@ +using System.Text.Json; +using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Interfaces.Services; + +namespace MatrixRoomUtils.Bot; + +public class FileStorageProvider : IStorageProvider { + public string TargetPath { get; } + + /// <summary> + /// Creates a new instance of <see cref="FileStorageProvider" />. + /// </summary> + /// <param name="targetPath"></param> + public FileStorageProvider(string targetPath) { + Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); + TargetPath = targetPath; + if(!Directory.Exists(targetPath)) { + Directory.CreateDirectory(targetPath); + } + } + + public async Task SaveObject<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), ObjectExtensions.ToJson(value)); + + public async Task<T?> LoadObject<T>(string key) => JsonSerializer.Deserialize<T>(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); + + public async Task<bool> ObjectExists(string key) => File.Exists(Path.Join(TargetPath, key)); + + public async Task<List<string>> GetAllKeys() => Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList(); + + public async Task DeleteObject(string key) => File.Delete(Path.Join(TargetPath, key)); +} \ No newline at end of file diff --git a/MatrixRoomUtils.Bot/MRUBot.cs b/MatrixRoomUtils.Bot/MRUBot.cs new file mode 100644 index 0000000..9b2a395 --- /dev/null +++ b/MatrixRoomUtils.Bot/MRUBot.cs @@ -0,0 +1,60 @@ +using System.CodeDom.Compiler; +using System.Diagnostics.CodeAnalysis; +using MatrixRoomUtils.Core; +using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Helpers; +using MatrixRoomUtils.Core.Responses; +using MatrixRoomUtils.Core.Services; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +public class MRUBot : IHostedService { + private readonly HomeserverProviderService _homeserverProviderService; + private readonly ILogger<MRUBot> _logger; + + public MRUBot(HomeserverProviderService homeserverProviderService, ILogger<MRUBot> logger) { + Console.WriteLine("MRUBot hosted service instantiated!"); + _homeserverProviderService = homeserverProviderService; + _logger = logger; + } + + /// <summary>Triggered when the application host is ready to start the service.</summary> + /// <param name="cancellationToken">Indicates that the start process has been aborted.</param> + [SuppressMessage("ReSharper", "FunctionNeverReturns")] + public async Task StartAsync(CancellationToken cancellationToken) { + var hs = await _homeserverProviderService.GetAuthenticatedWithToken("rory.gay", "syt_bXJ1Y29yZXRlc3Q_XKUmPswDGZBiLAmFfAut_1iO0KD"); + await (await hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync(); + // #pragma warning disable CS4014 + // Task.Run(async Task? () => { + // #pragma warning restore CS4014 + // while (true) { + // var rooms = await hs.GetJoinedRooms(); + // foreach (var room in rooms) { + // var states = await room.GetStateAsync<List<StateEventResponse>>(""); + // foreach (var state in states) { + // // Console.WriteLine( + // // $"{state.RoomId}: {state.Type}::{state.StateKey} = {ObjectExtensions.ToJson(state.Content, indent: false)}"); + // } + // } + // + // await Task.Delay(1000, cancellationToken); + // } + // }, cancellationToken); + #pragma warning disable CS4014 + Task.Run(async Task? () => { + #pragma warning restore CS4014 + SyncResult? sync = null; + while (true) { + sync = await hs.SyncHelper.Sync(sync?.NextBatch); + _logger.LogInformation($"Got sync, next batch: {sync?.NextBatch}!"); + } + }, cancellationToken); + + } + + /// <summary>Triggered when the application host is performing a graceful shutdown.</summary> + /// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param> + public async Task StopAsync(CancellationToken cancellationToken) { + Console.WriteLine("Shutting down bot!"); + } +} \ No newline at end of file diff --git a/MatrixRoomUtils.Bot/MatrixRoomUtils.Bot.csproj b/MatrixRoomUtils.Bot/MatrixRoomUtils.Bot.csproj index 8ff14c4..095d0f6 100644 --- a/MatrixRoomUtils.Bot/MatrixRoomUtils.Bot.csproj +++ b/MatrixRoomUtils.Bot/MatrixRoomUtils.Bot.csproj @@ -6,7 +6,7 @@ <LangVersion>preview</LangVersion> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> - <PublishAot>true</PublishAot> + <PublishAot>false</PublishAot> <InvariantGlobalization>true</InvariantGlobalization> <PublishTrimmed>true</PublishTrimmed> <PublishReadyToRun>true</PublishReadyToRun> @@ -21,7 +21,7 @@ </ItemGroup> <ItemGroup> - <PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> + <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0-preview.5.23280.8" /> </ItemGroup> </Project> diff --git a/MatrixRoomUtils.Bot/Program.cs b/MatrixRoomUtils.Bot/Program.cs index 83fa4f4..441003e 100644 --- a/MatrixRoomUtils.Bot/Program.cs +++ b/MatrixRoomUtils.Bot/Program.cs @@ -1,2 +1,26 @@ // See https://aka.ms/new-console-template for more information + +using MatrixRoomUtils.Bot; +using MatrixRoomUtils.Core.Services; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + Console.WriteLine("Hello, World!"); + +using IHost host = Host.CreateDefaultBuilder(args) + .ConfigureServices((_, services) => { + services.AddScoped<TieredStorageService>(x => + new( + cacheStorageProvider: new FileStorageProvider("data/cache/"), + dataStorageProvider: new FileStorageProvider("data/data/") + ) + ); + + services.AddRoryLibMatrixServices(); + + services.AddHostedService<MRUBot>(); + }) + .Build(); + + +await host.RunAsync(); \ No newline at end of file |