From 163e2a94f600ffe0f982e3f605264ff2f2fe312b Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Fri, 9 Feb 2024 16:33:14 +0100 Subject: Apply syntax style to LibMatrix side projects --- .../Controllers/ValidationController.cs | 4 +- .../LibMatrix.DebugDataValidationApi.csproj | 6 +-- .../LibMatrix.DebugDataValidationApi/Program.cs | 2 +- .../Bot/Commands/CmdCommand.cs | 8 ++-- .../Bot/Commands/DbgAniRainbowTest.cs | 14 ++---- .../Bot/Commands/HelpCommand.cs | 6 +-- .../Bot/Commands/PingCommand.cs | 6 +-- Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs | 28 +++++------ .../Bot/DevTestBotConfiguration.cs | 6 +-- .../Bot/FileStorageProvider.cs | 6 +-- .../Bot/Interfaces/CommandContext.cs | 2 +- .../Bot/Interfaces/ICommand.cs | 6 +-- .../Bot/StartupTasks/ServerRoomSizeCalulator.cs | 14 ++---- .../LibMatrix.DevTestBot.csproj | 56 +++++++++++----------- Utilities/LibMatrix.DevTestBot/Program.cs | 10 ++-- .../Properties/launchSettings.json | 1 - .../appsettings.Development.json | 12 ++--- Utilities/LibMatrix.DevTestBot/appsettings.json | 22 ++++----- ...LibMatrix.JsonSerializerContextGenerator.csproj | 4 +- .../Program.cs | 2 +- .../AppServiceConfiguration.cs | 2 +- .../LibMatrix.Utilities.Bot/BotCommandInstaller.cs | 3 +- .../Commands/HelpCommand.cs | 8 ++-- .../Commands/PingCommand.cs | 6 +-- .../LibMatrix.Utilities.Bot/FileStorageProvider.cs | 6 +-- .../Interfaces/CommandContext.cs | 2 +- .../LibMatrix.Utilities.Bot/Interfaces/ICommand.cs | 6 +-- .../LibMatrix.Utilities.Bot.csproj | 8 ++-- .../LibMatrixBotConfiguration.cs | 2 +- .../Services/CommandListenerHostedService.cs | 18 ++++--- 30 files changed, 121 insertions(+), 155 deletions(-) (limited to 'Utilities') diff --git a/Utilities/LibMatrix.DebugDataValidationApi/Controllers/ValidationController.cs b/Utilities/LibMatrix.DebugDataValidationApi/Controllers/ValidationController.cs index 1b93614..3420e56 100644 --- a/Utilities/LibMatrix.DebugDataValidationApi/Controllers/ValidationController.cs +++ b/Utilities/LibMatrix.DebugDataValidationApi/Controllers/ValidationController.cs @@ -7,7 +7,6 @@ namespace LibMatrix.DebugDataValidationApi.Controllers; [ApiController] [Route("/")] public class ValidationController(ILogger logger) : ControllerBase { - [HttpPost("/validate/{type}")] public Task Get([FromRoute] string type, [FromBody] JsonElement content) { var t = Type.GetType(type); @@ -15,7 +14,8 @@ public class ValidationController(ILogger logger) : Contro logger.LogWarning($"Type `{type}` does not exist!"); throw new ArgumentException($"Unknown type {type}!"); } + logger.LogInformation($"Validating {type}..."); return Task.FromResult(content.FindExtraJsonElementFields(t, "$")); } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj b/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj index e14b7a1..24fd617 100644 --- a/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj +++ b/Utilities/LibMatrix.DebugDataValidationApi/LibMatrix.DebugDataValidationApi.csproj @@ -9,12 +9,12 @@ - - + + - + diff --git a/Utilities/LibMatrix.DebugDataValidationApi/Program.cs b/Utilities/LibMatrix.DebugDataValidationApi/Program.cs index 2c324d8..1d913b4 100644 --- a/Utilities/LibMatrix.DebugDataValidationApi/Program.cs +++ b/Utilities/LibMatrix.DebugDataValidationApi/Program.cs @@ -28,4 +28,4 @@ app.UseAuthorization(); app.MapControllers(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs index e690890..89a9033 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/CmdCommand.cs @@ -8,9 +8,7 @@ public class CmdCommand : ICommand { public string Name => "cmd"; public string Description => "Runs a command on the host system"; - public Task CanInvoke(CommandContext ctx) { - return Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay") || ctx.MessageEvent.Sender.EndsWith(":conduit.rory.gay")); - } + public Task CanInvoke(CommandContext ctx) => Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay") || ctx.MessageEvent.Sender.EndsWith(":conduit.rory.gay")); public async Task Invoke(CommandContext ctx) { var cmd = ctx.Args.Aggregate("\"", (current, arg) => current + arg + " "); @@ -27,7 +25,7 @@ public class CmdCommand : ICommand { // .Split("\n").ToList(); var msg = ""; - EventIdResponse? msgId = await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { + var msgId = await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent { FormattedBody = $"Waiting for command output...", Body = msg.RemoveAnsi(), Format = "m.notice" @@ -69,4 +67,4 @@ public class CmdCommand : ICommand { // } // } } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs index c526847..f75c863 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/DbgAniRainbowTest.cs @@ -11,26 +11,22 @@ public class DbgAniRainbowTest(IServiceProvider services, HomeserverProviderServ public string Name { get; } = "ani-rainbow"; public string Description { get; } = "[Debug] animated rainbow :)"; - public async Task CanInvoke(CommandContext ctx) { - return ctx.Room.RoomId == "!hLEefBaYvNfJwcTjmt:rory.gay"; - } + public async Task CanInvoke(CommandContext ctx) => ctx.Room.RoomId == "!hLEefBaYvNfJwcTjmt:rory.gay"; public async Task Invoke(CommandContext ctx) { //255 long string // var rainbow = "🟥🟧🟨🟩🟦🟪"; var rainbow = "M"; var chars = rainbow; - for (var i = 0; i < 76; i++) { - chars += rainbow[i % rainbow.Length]; - } + for (var i = 0; i < 76; i++) chars += rainbow[i % rainbow.Length]; Task.Run(async () => { - int i = 0; - var msg = new MessageBuilder(msgType: "m.notice").WithRainbowString(chars).Build(); + var i = 0; + var msg = new MessageBuilder("m.notice").WithRainbowString(chars).Build(); var msgEvent = await ctx.Room.SendMessageEventAsync(msg); while (true) { - msg = new MessageBuilder(msgType: "m.notice").WithRainbowString(chars, offset: i * 5).Build(); + msg = new MessageBuilder("m.notice").WithRainbowString(chars, offset: i * 5).Build(); if (i % 50 == 0) { msg.NewContent = null; msg.RelatesTo = null; diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs index 23c4fe2..7ecbeb3 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/HelpCommand.cs @@ -13,10 +13,8 @@ public class HelpCommand(IServiceProvider services) : ICommand { var sb = new StringBuilder(); sb.AppendLine("Available commands:"); var commands = services.GetServices().ToList(); - foreach (var command in commands) { - sb.AppendLine($"- {command.Name}: {command.Description}"); - } + foreach (var command in commands) sb.AppendLine($"- {command.Name}: {command.Description}"); await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: sb.ToString())); } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs index ba242fe..85c86a3 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Commands/PingCommand.cs @@ -7,7 +7,5 @@ public class PingCommand : ICommand { public string Name { get; } = "ping"; public string Description { get; } = "Pong!"; - public async Task Invoke(CommandContext ctx) { - await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); - } -} + public async Task Invoke(CommandContext ctx) => await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs index 678df27..fa80bfd 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs @@ -20,7 +20,7 @@ public class DevTestBot : IHostedService { public DevTestBot(HomeserverProviderService homeserverProviderService, ILogger logger, DevTestBotConfiguration configuration, IServiceProvider services) { - logger.LogInformation("{} instantiated!", this.GetType().Name); + logger.LogInformation("{} instantiated!", GetType().Name); _homeserverProviderService = homeserverProviderService; _logger = logger; _configuration = configuration; @@ -49,7 +49,7 @@ public class DevTestBot : IHostedService { var syncHelper = new SyncHelper(hs); - await (hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync(); + await hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay").JoinAsync(); // foreach (var room in await hs.GetJoinedRooms()) { // if(room.RoomId is "!OGEhHVWSdvArJzumhm:matrix.org") continue; @@ -65,29 +65,28 @@ public class DevTestBot : IHostedService { x.Type == "m.room.member" && x.StateKey == hs.UserId); _logger.LogInformation( $"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventContent).Reason}"); - if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender == "@mxidupwitch:the-apothecary.club") { + if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender == "@mxidupwitch:the-apothecary.club") try { var senderProfile = await hs.GetProfileAsync(inviteEvent.Sender); - await (hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!"); + await hs.GetRoom(args.Key).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!"); } catch (Exception e) { _logger.LogError("{}", e.ToString()); - await (hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e); + await hs.GetRoom(args.Key).LeaveAsync("I was unable to join the room: " + e); } - } }); syncHelper.TimelineEventHandlers.Add(async @event => { _logger.LogInformation( - "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: false, ignoreNull: true)); + "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(false, true)); var room = hs.GetRoom(@event.RoomId); // _logger.LogInformation(eventResponse.ToJson(indent: false)); - if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { + if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) if (message is { MessageType: "m.text" } && message.Body.StartsWith(_configuration.Prefix)) { var command = _commands.FirstOrDefault(x => x.Name == message.Body.Split(' ')[0][_configuration.Prefix.Length..]); if (command == null) { await room.SendMessageEventAsync( - new RoomMessageEventContent(messageType: "m.text", body: "Command not found!")); + new RoomMessageEventContent("m.text", "Command not found!")); return; } @@ -95,15 +94,12 @@ public class DevTestBot : IHostedService { Room = room, MessageEvent = @event }; - if (await command.CanInvoke(ctx)) { + if (await command.CanInvoke(ctx)) await command.Invoke(ctx); - } - else { + else await room.SendMessageEventAsync( - new RoomMessageEventContent(messageType: "m.text", body: "You do not have permission to run this command!")); - } + new RoomMessageEventContent("m.text", "You do not have permission to run this command!")); } - } }); await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken); } @@ -114,4 +110,4 @@ public class DevTestBot : IHostedService { _logger.LogInformation("Shutting down bot!"); return Task.CompletedTask; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBotConfiguration.cs b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBotConfiguration.cs index ef203cd..dcbc670 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBotConfiguration.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBotConfiguration.cs @@ -3,10 +3,8 @@ using Microsoft.Extensions.Configuration; namespace LibMatrix.ExampleBot.Bot; public class DevTestBotConfiguration { - public DevTestBotConfiguration(IConfiguration config) { - config.GetRequiredSection("Bot").Bind(this); - } + public DevTestBotConfiguration(IConfiguration config) => config.GetRequiredSection("Bot").Bind(this); public string Homeserver { get; set; } = ""; public string AccessToken { get; set; } = ""; public string Prefix { get; set; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/FileStorageProvider.cs b/Utilities/LibMatrix.DevTestBot/Bot/FileStorageProvider.cs index 2c014de..cc866e6 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/FileStorageProvider.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/FileStorageProvider.cs @@ -18,9 +18,7 @@ public class FileStorageProvider : IStorageProvider { new Logger(new LoggerFactory()).LogInformation("test"); Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); TargetPath = targetPath; - if (!Directory.Exists(targetPath)) { - Directory.CreateDirectory(targetPath); - } + if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath); } public async Task SaveObjectAsync(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); @@ -35,4 +33,4 @@ public class FileStorageProvider : IStorageProvider { File.Delete(Path.Join(TargetPath, key)); return Task.CompletedTask; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/CommandContext.cs b/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/CommandContext.cs index 6dbb7f9..90a95e4 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/CommandContext.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/CommandContext.cs @@ -8,4 +8,4 @@ public class CommandContext { public StateEventResponse MessageEvent { get; set; } public string CommandName => (MessageEvent.TypedContent as RoomMessageEventContent).Body.Split(' ')[0][1..]; public string[] Args => (MessageEvent.TypedContent as RoomMessageEventContent).Body.Split(' ')[1..]; -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/ICommand.cs b/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/ICommand.cs index 2ba5a27..a6dc8da 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/ICommand.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/Interfaces/ICommand.cs @@ -4,9 +4,7 @@ public interface ICommand { public string Name { get; } public string Description { get; } - public Task CanInvoke(CommandContext ctx) { - return Task.FromResult(true); - } + public Task CanInvoke(CommandContext ctx) => Task.FromResult(true); public Task Invoke(CommandContext ctx); -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs b/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs index 0c04d27..253eb37 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs @@ -36,23 +36,19 @@ public class ServerRoomSizeCalulator : IHostedService { throw; } - await (hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay")).JoinAsync(); + await hs.GetRoom("!DoHEdFablOLjddKWIp:rory.gay").JoinAsync(); Dictionary totalRoomSize = new(); foreach (var room in await hs.GetJoinedRooms()) { var stateList = room.GetFullStateAsync().ToBlockingEnumerable().ToList(); var roomSize = stateList.Count; - if (roomSize > 10000) { - await File.AppendAllLinesAsync("large_rooms.txt", new[] { $"{{ \"{room.RoomId}\", {roomSize} }}," }, cancellationToken); - } + if (roomSize > 10000) await File.AppendAllLinesAsync("large_rooms.txt", new[] { $"{{ \"{room.RoomId}\", {roomSize} }}," }, cancellationToken); var roomHs = room.RoomId.Split(":")[1]; - if (totalRoomSize.ContainsKey(roomHs)) { + if (totalRoomSize.ContainsKey(roomHs)) totalRoomSize[roomHs] += roomSize; - } - else { + else totalRoomSize.Add(roomHs, roomSize); - } _logger.LogInformation($"Got room state for {room.RoomId}!"); } @@ -66,4 +62,4 @@ public class ServerRoomSizeCalulator : IHostedService { _logger.LogInformation("Shutting down bot!"); return Task.CompletedTask; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj b/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj index c0be661..6518c67 100644 --- a/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj +++ b/Utilities/LibMatrix.DevTestBot/LibMatrix.DevTestBot.csproj @@ -1,33 +1,33 @@  - - Exe - net8.0 - preview - enable - enable - false - true - LibMatrix.ExampleBot - - - - - - - + + Exe + net8.0 + preview + enable + enable + false + true + LibMatrix.ExampleBot + + + + + + + - - - + + + - - - - - - - Always - - + + + + + + + Always + + diff --git a/Utilities/LibMatrix.DevTestBot/Program.cs b/Utilities/LibMatrix.DevTestBot/Program.cs index b53bbfb..eb5ad76 100644 --- a/Utilities/LibMatrix.DevTestBot/Program.cs +++ b/Utilities/LibMatrix.DevTestBot/Program.cs @@ -11,10 +11,10 @@ Console.WriteLine("Hello, World!"); var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) => { // services.AddScoped(x => - // new TieredStorageService( - // cacheStorageProvider: new FileStorageProvider("bot_data/cache/"), - // dataStorageProvider: new FileStorageProvider("bot_data/data/") - // ) + // new TieredStorageService( + // cacheStorageProvider: new FileStorageProvider("bot_data/cache/"), + // dataStorageProvider: new FileStorageProvider("bot_data/data/") + // ) // ); services.AddScoped(); services.AddRoryLibMatrixServices(); @@ -27,4 +27,4 @@ var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) => { services.AddHostedService(); }).UseConsoleLifetime().Build(); -await host.RunAsync(); +await host.RunAsync(); \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/Properties/launchSettings.json b/Utilities/LibMatrix.DevTestBot/Properties/launchSettings.json index 997e294..6c504ff 100644 --- a/Utilities/LibMatrix.DevTestBot/Properties/launchSettings.json +++ b/Utilities/LibMatrix.DevTestBot/Properties/launchSettings.json @@ -5,7 +5,6 @@ "commandName": "Project", "dotnetRunMessages": true, "environmentVariables": { - } }, "Development": { diff --git a/Utilities/LibMatrix.DevTestBot/appsettings.Development.json b/Utilities/LibMatrix.DevTestBot/appsettings.Development.json index 27bbd50..f999bc2 100644 --- a/Utilities/LibMatrix.DevTestBot/appsettings.Development.json +++ b/Utilities/LibMatrix.DevTestBot/appsettings.Development.json @@ -1,9 +1,9 @@ { - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" } + } } \ No newline at end of file diff --git a/Utilities/LibMatrix.DevTestBot/appsettings.json b/Utilities/LibMatrix.DevTestBot/appsettings.json index 6613979..db64c22 100644 --- a/Utilities/LibMatrix.DevTestBot/appsettings.json +++ b/Utilities/LibMatrix.DevTestBot/appsettings.json @@ -1,14 +1,14 @@ { - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - }, - "Bot": { - "Homeserver": "rory.gay", - "AccessToken": "syt_xxxxxxxxxxxxxxxxx", - "Prefix": "!" + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" } + }, + "Bot": { + "Homeserver": "rory.gay", + "AccessToken": "syt_xxxxxxxxxxxxxxxxx", + "Prefix": "!" + } } \ No newline at end of file diff --git a/Utilities/LibMatrix.JsonSerializerContextGenerator/LibMatrix.JsonSerializerContextGenerator.csproj b/Utilities/LibMatrix.JsonSerializerContextGenerator/LibMatrix.JsonSerializerContextGenerator.csproj index b90bf20..35cb9f8 100644 --- a/Utilities/LibMatrix.JsonSerializerContextGenerator/LibMatrix.JsonSerializerContextGenerator.csproj +++ b/Utilities/LibMatrix.JsonSerializerContextGenerator/LibMatrix.JsonSerializerContextGenerator.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/Utilities/LibMatrix.JsonSerializerContextGenerator/Program.cs b/Utilities/LibMatrix.JsonSerializerContextGenerator/Program.cs index 0c0277b..f33efeb 100644 --- a/Utilities/LibMatrix.JsonSerializerContextGenerator/Program.cs +++ b/Utilities/LibMatrix.JsonSerializerContextGenerator/Program.cs @@ -23,7 +23,7 @@ stream.WriteString("\n\nnamespace LibMatrix.Generated;\n\n[JsonSourceGenerationO // // """))); -stream.WriteString(string.Join('\n', eventContentTypes//.DistinctBy(x => x.Namespace) +stream.WriteString(string.Join('\n', eventContentTypes //.DistinctBy(x => x.Namespace) .Select(x => $"[JsonSerializable(typeof({x.Name}))]"))); stream.WriteString("\ninternal partial class EventTypeSerializerContext : JsonSerializerContext { }"); diff --git a/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs index d8d1094..afda89e 100644 --- a/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs +++ b/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs @@ -84,4 +84,4 @@ public class AppServiceConfiguration { return yaml; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs index 25a8d92..eb67424 100644 --- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs +++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs @@ -34,6 +34,7 @@ public static class BotCommandInstaller { services.AddHostedService(); // services.AddSingleton(); } + return services; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs index 4fe1038..9937b3c 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs @@ -13,10 +13,8 @@ public class HelpCommand(IServiceProvider services) : ICommand { var sb = new StringBuilder(); sb.AppendLine("Available commands:"); var commands = services.GetServices().ToList(); - foreach (var command in commands) { - sb.AppendLine($"- {command.Name}: {command.Description}"); - } + foreach (var command in commands) sb.AppendLine($"- {command.Name}: {command.Description}"); - await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(messageType: "m.notice", body: sb.ToString())); + await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent("m.notice", sb.ToString())); } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs index 16712ea..b5fb868 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs @@ -7,7 +7,5 @@ public class PingCommand : ICommand { public string Name { get; } = "ping"; public string Description { get; } = "Pong!"; - public async Task Invoke(CommandContext ctx) { - await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); - } -} + public async Task Invoke(CommandContext ctx) => await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs b/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs index b66fbf5..b762937 100644 --- a/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs +++ b/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs @@ -14,9 +14,7 @@ public class FileStorageProvider : IStorageProvider { public FileStorageProvider(string targetPath) { Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); TargetPath = targetPath; - if (!Directory.Exists(targetPath)) { - Directory.CreateDirectory(targetPath); - } + if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath); } public async Task SaveObjectAsync(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); @@ -31,4 +29,4 @@ public class FileStorageProvider : IStorageProvider { File.Delete(Path.Join(TargetPath, key)); return Task.CompletedTask; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs index 94ea846..e65f86d 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs @@ -19,4 +19,4 @@ public class CommandContext { public required AuthenticatedHomeserverGeneric Homeserver { get; set; } public async Task Reply(RoomMessageEventContent content) => await Room.SendMessageEventAsync(content); -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs index 82439e8..453a8fe 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs @@ -4,9 +4,7 @@ public interface ICommand { public string Name { get; } public string Description { get; } - public Task CanInvoke(CommandContext ctx) { - return Task.FromResult(true); - } + public Task CanInvoke(CommandContext ctx) => 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 index e16c00c..89ea5af 100644 --- a/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj @@ -8,13 +8,13 @@ - + - - - + + + diff --git a/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs index 27ce06b..d607637 100644 --- a/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs @@ -8,4 +8,4 @@ public class LibMatrixBotConfiguration { public string AccessToken { get; set; } = ""; public string Prefix { get; set; } = "?"; public string? LogRoom { get; set; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs index 9949631..11ee740 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs @@ -18,7 +18,7 @@ public class CommandListenerHostedService : IHostedService { public CommandListenerHostedService(AuthenticatedHomeserverGeneric hs, ILogger logger, IServiceProvider services, LibMatrixBotConfiguration config) { - logger.LogInformation("{} instantiated!", this.GetType().Name); + logger.LogInformation("{} instantiated!", GetType().Name); _hs = hs; _logger = logger; _config = config; @@ -44,7 +44,7 @@ public class CommandListenerHostedService : IHostedService { try { var room = _hs.GetRoom(@event.RoomId); // _logger.LogInformation(eventResponse.ToJson(indent: false)); - if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { + if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent 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}"); @@ -52,7 +52,7 @@ public class CommandListenerHostedService : IHostedService { var command = _commands.FirstOrDefault(x => x.Name == messageContentWithoutReply.Split(' ')[0][_config.Prefix.Length..]); if (command == null) { await room.SendMessageEventAsync( - new RoomMessageEventContent(messageType: "m.notice", body: "Command not found!")); + new RoomMessageEventContent("m.notice", "Command not found!")); return; } @@ -62,7 +62,7 @@ public class CommandListenerHostedService : IHostedService { Homeserver = _hs }; - if (await command.CanInvoke(ctx)) { + if (await command.CanInvoke(ctx)) try { await command.Invoke(ctx); } @@ -70,14 +70,11 @@ public class CommandListenerHostedService : IHostedService { await room.SendMessageEventAsync( MessageFormatter.FormatException("An error occurred during the execution of this command", e)); } - } - else { + else await room.SendMessageEventAsync( - new RoomMessageEventContent(messageType: "m.notice", body: "You do not have permission to run this command!")); - } + new RoomMessageEventContent("m.notice", "You do not have permission to run this command!")); } } - } } catch (Exception e) { _logger.LogError(e, "Error in command listener!"); @@ -94,6 +91,7 @@ public class CommandListenerHostedService : IHostedService { _logger.LogError("Could not shut down command listener task because it was null!"); return; } + await _listenerTask.WaitAsync(cancellationToken); } -} +} \ No newline at end of file -- cgit 1.4.1