diff options
Diffstat (limited to 'Utilities')
15 files changed, 390 insertions, 0 deletions
diff --git a/Utilities/LibMatrix.DebugDataValidationApi/Controllers/ValidationController.cs b/Utilities/LibMatrix.DebugDataValidationApi/Controllers/ValidationController.cs new file mode 100644 index 0000000..4dbee54 --- /dev/null +++ b/Utilities/LibMatrix.DebugDataValidationApi/Controllers/ValidationController.cs @@ -0,0 +1,26 @@ +using System.Text.Json; +using LibMatrix.Extensions; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.DebugDataValidationApi.Controllers; + +[ApiController] +[Route("/")] +public class ValidationController : ControllerBase { + private readonly ILogger<ValidationController> _logger; + + public ValidationController(ILogger<ValidationController> logger) { + _logger = logger; + } + + [HttpPost("/validate/{type}")] + public Task<bool> Get([FromRoute] string type, [FromBody] JsonElement content) { + var t = Type.GetType(type); + if (t is null) { + Console.WriteLine($"Type `{type}` does not exist!"); + throw new ArgumentException($"Unknown type {type}!"); + } + Console.WriteLine($"Validating {type}..."); + return Task.FromResult(content.FindExtraJsonElementFields(t, "$")); + } +} diff --git a/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj b/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj new file mode 100644 index 0000000..aad8ee0 --- /dev/null +++ b/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj @@ -0,0 +1,20 @@ +<Project Sdk="Microsoft.NET.Sdk.Web"> + + <PropertyGroup> + <TargetFramework>net7.0</TargetFramework> + <Nullable>enable</Nullable> + <ImplicitUsings>enable</ImplicitUsings> + <InvariantGlobalization>true</InvariantGlobalization> + <LangVersion>preview</LangVersion> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" /> + <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\LibMatrix\LibMatrix.csproj" /> + </ItemGroup> + +</Project> diff --git a/Utilities/LibMatrix.DebugDataValidationApi/Program.cs b/Utilities/LibMatrix.DebugDataValidationApi/Program.cs new file mode 100644 index 0000000..cf9dc55 --- /dev/null +++ b/Utilities/LibMatrix.DebugDataValidationApi/Program.cs @@ -0,0 +1,32 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddCors(options => +{ + options.AddPolicy( + "Open", + policy => policy.AllowAnyOrigin().AllowAnyHeader()); +}); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) { + app.UseSwagger(); + app.UseSwaggerUI(); +} + +// app.UseHttpsRedirection(); + +app.UseCors("Open"); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/Utilities/LibMatrix.DebugDataValidationApi/Properties/launchSettings.json b/Utilities/LibMatrix.DebugDataValidationApi/Properties/launchSettings.json new file mode 100644 index 0000000..fe668ce --- /dev/null +++ b/Utilities/LibMatrix.DebugDataValidationApi/Properties/launchSettings.json @@ -0,0 +1,32 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:9169", + "sslPort": 44321 + } + }, + "profiles": { + "Development": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5258", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Local": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5258", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Local" + } + } + } +} diff --git a/Utilities/LibMatrix.DebugDataValidationApi/appsettings.Development.json b/Utilities/LibMatrix.DebugDataValidationApi/appsettings.Development.json new file mode 100644 index 0000000..12c8ab9 --- /dev/null +++ b/Utilities/LibMatrix.DebugDataValidationApi/appsettings.Development.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Information", + "Microsoft.AspNetCore.Routing": "Warning", + "Microsoft.AspNetCore.Mvc": "Warning", + "Microsoft.AspNetCore.Cors": "Warning" + } + } +} diff --git a/Utilities/LibMatrix.DebugDataValidationApi/appsettings.json b/Utilities/LibMatrix.DebugDataValidationApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Utilities/LibMatrix.DebugDataValidationApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs new file mode 100644 index 0000000..42cdb6c --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs @@ -0,0 +1,44 @@ +using ArcaneLibs; +using ArcaneLibs.Extensions; +using LibMatrix.Helpers; +using LibMatrix.Homeservers; +using LibMatrix.Services; +using LibMatrix.StateEventTypes.Spec; +using LibMatrix.Utilities.Bot.Services; +using MediaModeratorPoC.Bot.Interfaces; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace LibMatrix.Utilities.Bot; + +public static class BotCommandInstaller { + public static IServiceCollection AddBotCommands(this IServiceCollection services) { + foreach (var commandClass in new ClassCollector<ICommand>().ResolveFromAllAccessibleAssemblies()) { + Console.WriteLine($"Adding command {commandClass.Name}"); + services.AddScoped(typeof(ICommand), commandClass); + } + + return services; + } + + public static IServiceCollection AddBot(this IServiceCollection services, bool withCommands = true) { + services.AddSingleton<LibMatrixBotConfiguration>(); + + services.AddScoped<AuthenticatedHomeserverGeneric>(x => { + var config = x.GetService<LibMatrixBotConfiguration>(); + var hsProvider = x.GetService<HomeserverProviderService>(); + var hs = hsProvider.GetAuthenticatedWithToken(config.Homeserver, config.AccessToken).Result; + + return hs; + }); + + if (withCommands) { + Console.WriteLine("Adding command handler..."); + services.AddBotCommands(); + services.AddHostedService<CommandListenerHostedService>(); + // services.AddSingleton<IHostedService, CommandListenerHostedService>(); + } + return services; + } +} diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs new file mode 100644 index 0000000..c975c8b --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs @@ -0,0 +1,22 @@ +using System.Text; +using LibMatrix.StateEventTypes.Spec; +using MediaModeratorPoC.Bot.Interfaces; +using Microsoft.Extensions.DependencyInjection; + +namespace MediaModeratorPoC.Bot.Commands; + +public class HelpCommand(IServiceProvider services) : ICommand { + public string Name { get; } = "help"; + public string Description { get; } = "Displays this help message"; + + public async Task Invoke(CommandContext ctx) { + var sb = new StringBuilder(); + sb.AppendLine("Available commands:"); + var commands = services.GetServices<ICommand>().ToList(); + foreach (var command in commands) { + sb.AppendLine($"- {command.Name}: {command.Description}"); + } + + await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData(messageType: "m.notice", body: sb.ToString())); + } +} diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs new file mode 100644 index 0000000..e7f3b10 --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs @@ -0,0 +1,13 @@ +using LibMatrix.StateEventTypes.Spec; +using MediaModeratorPoC.Bot.Interfaces; + +namespace MediaModeratorPoC.Bot.Commands; + +public class PingCommand : ICommand { + public string Name { get; } = "ping"; + public string Description { get; } = "Pong!"; + + public async Task Invoke(CommandContext ctx) { + await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData(body: "pong!")); + } +} diff --git a/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs b/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs new file mode 100644 index 0000000..d5b991a --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs @@ -0,0 +1,38 @@ +using System.Text.Json; +using ArcaneLibs.Extensions; +using LibMatrix.Interfaces.Services; +using Microsoft.Extensions.Logging; + +namespace MediaModeratorPoC.Bot; + +public class FileStorageProvider : IStorageProvider { + private readonly ILogger<FileStorageProvider> _logger; + + public string TargetPath { get; } + + /// <summary> + /// Creates a new instance of <see cref="FileStorageProvider" />. + /// </summary> + /// <param name="targetPath"></param> + public FileStorageProvider(string targetPath) { + new Logger<FileStorageProvider>(new LoggerFactory()).LogInformation("test"); + Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); + TargetPath = targetPath; + if(!Directory.Exists(targetPath)) { + Directory.CreateDirectory(targetPath); + } + } + + public async Task SaveObjectAsync<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); + + public async Task<T?> LoadObjectAsync<T>(string key) => JsonSerializer.Deserialize<T>(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); + + public Task<bool> ObjectExistsAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key))); + + public Task<List<string>> GetAllKeysAsync() => Task.FromResult(Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList()); + + public Task DeleteObjectAsync(string key) { + File.Delete(Path.Join(TargetPath, key)); + return Task.CompletedTask; + } +} diff --git a/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs new file mode 100644 index 0000000..0ad3e09 --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs @@ -0,0 +1,21 @@ +using LibMatrix.Homeservers; +using LibMatrix.Responses; +using LibMatrix.RoomTypes; +using LibMatrix.StateEventTypes.Spec; + +namespace MediaModeratorPoC.Bot.Interfaces; + +public class CommandContext { + public GenericRoom Room { get; set; } + public StateEventResponse MessageEvent { get; set; } + + public string MessageContentWithoutReply => + (MessageEvent.TypedContent as RoomMessageEventData)! + .Body.Split('\n') + .SkipWhile(x => x.StartsWith(">")) + .Aggregate((x, y) => $"{x}\n{y}"); + + public string CommandName => MessageContentWithoutReply.Split(' ')[0][1..]; + public string[] Args => MessageContentWithoutReply.Split(' ')[1..]; + public AuthenticatedHomeserverGeneric Homeserver { get; set; } +} diff --git a/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs new file mode 100644 index 0000000..a8fce94 --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs @@ -0,0 +1,12 @@ +namespace MediaModeratorPoC.Bot.Interfaces; + +public interface ICommand { + public string Name { get; } + public string Description { get; } + + public Task<bool> CanInvoke(CommandContext ctx) { + return Task.FromResult(true); + } + + public Task Invoke(CommandContext ctx); +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj new file mode 100644 index 0000000..db6570d --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj @@ -0,0 +1,21 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net7.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <LangVersion>preview</LangVersion> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\..\LibMatrix\LibMatrix.csproj" /> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" /> + <PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> + <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" /> + </ItemGroup> + + +</Project> diff --git a/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs new file mode 100644 index 0000000..118b4df --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs @@ -0,0 +1,10 @@ +using Microsoft.Extensions.Configuration; + +namespace LibMatrix.Utilities.Bot; + +public class LibMatrixBotConfiguration { + public LibMatrixBotConfiguration(IConfiguration config) => config.GetRequiredSection("LibMatrixBot").Bind(this); + public string Homeserver { get; set; } = ""; + public string AccessToken { get; set; } = ""; + public string Prefix { get; set; } = "?"; +} diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs new file mode 100644 index 0000000..d5e7dd6 --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs @@ -0,0 +1,79 @@ +using LibMatrix.Homeservers; +using LibMatrix.StateEventTypes.Spec; +using MediaModeratorPoC.Bot.Interfaces; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace LibMatrix.Utilities.Bot.Services; + +public class CommandListenerHostedService : IHostedService { + private readonly AuthenticatedHomeserverGeneric _hs; + private readonly ILogger<CommandListenerHostedService> _logger; + private readonly IEnumerable<ICommand> _commands; + private readonly LibMatrixBotConfiguration _config; + + private Task? _listenerTask; + + public CommandListenerHostedService(AuthenticatedHomeserverGeneric hs, ILogger<CommandListenerHostedService> logger, IServiceProvider services, + LibMatrixBotConfiguration config) { + logger.LogInformation("{} instantiated!", this.GetType().Name); + _hs = hs; + _logger = logger; + _config = config; + _logger.LogInformation("Getting commands..."); + _commands = services.GetServices<ICommand>(); + _logger.LogInformation("Got {} commands!", _commands.Count()); + } + + /// <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> + public Task StartAsync(CancellationToken cancellationToken) { + _listenerTask = Run(cancellationToken); + _logger.LogInformation("Command listener started (StartAsync)!"); + return Task.CompletedTask; + } + + private async Task? Run(CancellationToken cancellationToken) { + _logger.LogInformation("Starting command listener!"); + _hs.SyncHelper.TimelineEventHandlers.Add(async @event => { + var room = await _hs.GetRoom(@event.RoomId); + // _logger.LogInformation(eventResponse.ToJson(indent: false)); + if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventData message }) { + if (message is { MessageType: "m.text" }) { + var messageContentWithoutReply = message.Body.Split('\n', StringSplitOptions.RemoveEmptyEntries).SkipWhile(x=>x.StartsWith(">")).Aggregate((x, y) => $"{x}\n{y}"); + if (messageContentWithoutReply.StartsWith(_config.Prefix)) { + var command = _commands.FirstOrDefault(x => x.Name == messageContentWithoutReply.Split(' ')[0][_config.Prefix.Length..]); + if (command == null) { + await room.SendMessageEventAsync("m.room.message", + new RoomMessageEventData(messageType: "m.notice", body: "Command not found!")); + return; + } + + var ctx = new CommandContext { + Room = room, + MessageEvent = @event, + Homeserver = _hs + }; + if (await command.CanInvoke(ctx)) { + await command.Invoke(ctx); + } + else { + await room.SendMessageEventAsync("m.room.message", + new RoomMessageEventData(messageType: "m.notice", body: "You do not have permission to run this command!")); + } + } + } + } + }); + await _hs.SyncHelper.RunSyncLoop(cancellationToken: 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 Task StopAsync(CancellationToken cancellationToken) { + _logger.LogInformation("Shutting down command listener!"); + _listenerTask.Wait(cancellationToken); + return Task.CompletedTask; + } +} |