diff options
author | Emma [it/its]@Rory& <root@rory.gay> | 2023-10-11 15:53:52 +0200 |
---|---|---|
committer | Emma [it/its]@Rory& <root@rory.gay> | 2023-10-11 15:53:52 +0200 |
commit | ed42df73f1da60ddbfc11d5425a4955aece4acd0 (patch) | |
tree | 53eee9fd268f35982c09c4d450ccbca390e57a2b | |
parent | Changes? (diff) | |
download | OsuFederatedBeatmapApi-ed42df73f1da60ddbfc11d5425a4955aece4acd0.tar.xz |
Bot does not crash on startup when creating rooms
Diffstat (limited to '')
7 files changed, 90 insertions, 36 deletions
diff --git a/.idea/.idea.OsuFederatedBeatmapApi/.idea/vcs.xml b/.idea/.idea.OsuFederatedBeatmapApi/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.OsuFederatedBeatmapApi/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/LibMatrix b/LibMatrix -Subproject 1eec6bb14f08124e1e8d6f2e0b072862590f1ff +Subproject a98f16f1aaa72aa68278420a61d8ce897639416 diff --git a/OsuFederatedBeatmapApi/Commands/CreateBeatmapSetListCommand.cs b/OsuFederatedBeatmapApi/Commands/CreateBeatmapSetListCommand.cs new file mode 100644 index 0000000..eb00c3e --- /dev/null +++ b/OsuFederatedBeatmapApi/Commands/CreateBeatmapSetListCommand.cs @@ -0,0 +1,13 @@ +using LibMatrix.EventTypes.Spec; +using LibMatrix.Utilities.Bot.Interfaces; + +namespace OsuFederatedBeatmapApi.Commands; + +public class CreateBeatmapSetListCommand : ICommand { + public string Name { get; } = "create-beatmapset-list"; + public string Description { get; } = "Creates a beatmapset list and adds it to the list of tracked beatmapset lists"; + + public async Task Invoke(CommandContext ctx) { + await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); + } +} diff --git a/OsuFederatedBeatmapApi/Events/AccountData/BotData.cs b/OsuFederatedBeatmapApi/Events/AccountData/BotData.cs index 1eccf03..4e8af7b 100644 --- a/OsuFederatedBeatmapApi/Events/AccountData/BotData.cs +++ b/OsuFederatedBeatmapApi/Events/AccountData/BotData.cs @@ -4,11 +4,14 @@ namespace OsuFederatedBeatmapApi.Events.AccountData; public class BotData { [JsonPropertyName("control_room")] - public string ControlRoom { get; set; } = ""; + public string? ControlRoom { get; set; } [JsonPropertyName("log_room")] - public string? LogRoom { get; set; } = ""; + public string? LogRoom { get; set; } [JsonPropertyName("listed_repositories")] public List<string> ListedRepositories { get; set; } = new(); + + [JsonPropertyName("own_beatmap_repository_room")] + public string? OwnBeatmapRepositoryRoom { get; set; } } diff --git a/OsuFederatedBeatmapApi/OsuFederatedBeatmapApi.csproj b/OsuFederatedBeatmapApi/OsuFederatedBeatmapApi.csproj index 9c307b6..387808f 100644 --- a/OsuFederatedBeatmapApi/OsuFederatedBeatmapApi.csproj +++ b/OsuFederatedBeatmapApi/OsuFederatedBeatmapApi.csproj @@ -18,4 +18,5 @@ + </Project> diff --git a/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs index 04d64ad..b8cc635 100644 --- a/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs +++ b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs @@ -25,8 +25,11 @@ public class FederatedBeatmapApiBot(AuthenticatedHomeserverGeneric hs, } private async Task Run(CancellationToken cancellationToken) { - Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete); + if (Directory.Exists("bot_data/cache")) + Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete); + logger.LogInformation("Logged in as {}", hs.UserId); + await accountDataService.LoadAccountDataAsync(); var syncHelper = new SyncHelper(hs); List<string> admins = new(); @@ -45,8 +48,9 @@ public class FederatedBeatmapApiBot(AuthenticatedHomeserverGeneric hs, }, cancellationToken); #pragma warning restore CS4014 - foreach (var inviteTask in admins.Select(x=>accountDataService.ControlRoom.InviteUserAsync(x))) await inviteTask; - foreach (var inviteTask in admins.Select(x=>accountDataService.LogRoom.InviteUserAsync(x))) await inviteTask; + await accountDataService.ControlRoom.InviteUsersAsync(configuration.Admins, "You are marked as an administrator in the bot configuration!"); + await accountDataService.LogRoom.InviteUsersAsync(configuration.Admins, "You are marked as an administrator in the bot configuration!"); + await accountDataService.OwnBeatmapRepositoryRoom.InviteUsersAsync(configuration.Admins, "You are marked as an administrator in the bot configuration!"); syncHelper.InviteReceivedHandlers.Add(async Task (args) => { var inviteEvent = @@ -72,9 +76,7 @@ public class FederatedBeatmapApiBot(AuthenticatedHomeserverGeneric hs, logger.LogInformation( "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true)); - if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { - - } + if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { } } catch (Exception e) { logger.LogError("{}", e.ToString()); diff --git a/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs index 37b0d67..c1f33e0 100644 --- a/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs +++ b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs @@ -1,3 +1,5 @@ +using System.Collections.ObjectModel; +using ArcaneLibs.Extensions; using LibMatrix; using LibMatrix.EventTypes.Spec.State; using LibMatrix.Homeservers; @@ -7,13 +9,15 @@ using OsuFederatedBeatmapApi.Events.AccountData; namespace OsuFederatedBeatmapApi.Services; -public class FederatedBeatmapApiBotAccountDataService(ILogger<FederatedBeatmapApiBotAccountDataService> logger, AuthenticatedHomeserverGeneric hs, FederatedBeatmapApiBotConfiguration configuration) { +public class FederatedBeatmapApiBotAccountDataService(ILogger<FederatedBeatmapApiBotAccountDataService> logger, AuthenticatedHomeserverGeneric hs, + FederatedBeatmapApiBotConfiguration configuration) { private const string BotDataKey = "gay.rory.federated_beatmap_repository_bot_data"; - public GenericRoom LogRoom; - public GenericRoom ControlRoom; - public List<GenericRoom> ListedRepositories; - private BotData botData; + public GenericRoom LogRoom = null!; + public GenericRoom ControlRoom = null!; + public GenericRoom OwnBeatmapRepositoryRoom = null!; + public ObservableCollection<GenericRoom> ListedRepositories = null!; + private BotData botData = null!; public async Task LoadAccountDataAsync() { try { @@ -26,37 +30,62 @@ public class FederatedBeatmapApiBotAccountDataService(ILogger<FederatedBeatmapAp } botData = new BotData(); - var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Beatmap Repository - Control room", roomAliasName: "beatmap-repo-control-room"); + } + + if (string.IsNullOrWhiteSpace(botData.ControlRoom)) { + var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Beatmap Repository - Control room", roomAliasName: $"{hs.UserLocalpart}-control-room"); creationContent.Invite = configuration.Admins; creationContent.CreationContent["type"] = "gay.rory.federated_beatmap_repository.control_room"; - botData.ControlRoom = (await hs.CreateRoom(creationContent)).RoomId; - - //set access rules to allow joining via control room - creationContent.InitialState.Add(new StateEvent { - Type = "m.room.join_rules", - StateKey = "", - TypedContent = new RoomJoinRulesEventContent { - JoinRule = "knock_restricted", - Allow = new() { - new RoomJoinRulesEventContent.AllowEntry { - Type = "m.room_membership", - RoomId = botData.ControlRoom - } - } - } - }); - - creationContent.Name = "Beatmap Repository - Log room"; - creationContent.RoomAliasName = "beatmap-repo-log-room"; + botData.ControlRoom = (await hs.CreateRoom(creationContent, true, true, true)).RoomId; + } + + //this doesnt work on conduit yet :c + //set access rules to allow joining via control room + // creationContent.InitialState.Add(new StateEvent { + // Type = "m.room.join_rules", + // StateKey = "", + // TypedContent = new RoomJoinRulesEventContent { + // JoinRule = "knock_restricted", + // Allow = new() { + // new RoomJoinRulesEventContent.AllowEntry { + // Type = "m.room_membership", + // RoomId = botData.ControlRoom + // } + // } + // } + // }); + + if (string.IsNullOrWhiteSpace(botData.LogRoom)) { + var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Beatmap Repository - Log room", roomAliasName: $"{hs.UserLocalpart}-log-room"); + creationContent.Invite = configuration.Admins; creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.log_room"; - botData.LogRoom = (await hs.CreateRoom(creationContent)).RoomId; + botData.LogRoom = (await hs.CreateRoom(creationContent, true, true, true)).RoomId; + } - await hs.SetAccountData(BotDataKey, botData); + if (string.IsNullOrWhiteSpace(botData.OwnBeatmapRepositoryRoom)) { + var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Beatmap Repository - Own beatmap repository room", roomAliasName: $"{hs.UserLocalpart}-beatmap-repo-room"); + creationContent.Invite = configuration.Admins; + creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.beatmap_repository_room"; + botData.OwnBeatmapRepositoryRoom = (await hs.CreateRoom(creationContent, true, true, true)).RoomId; } + // await hs.SetAccountData(BotDataKey, botData); + LogRoom = hs.GetRoom(botData.LogRoom ?? botData.ControlRoom); ControlRoom = hs.GetRoom(botData.ControlRoom); - ListedRepositories = botData.ListedRepositories.Select(hs.GetRoom).ToList(); + OwnBeatmapRepositoryRoom = hs.GetRoom(botData.OwnBeatmapRepositoryRoom); + ListedRepositories = new ObservableCollection<GenericRoom>(botData.ListedRepositories.Select(hs.GetRoom).ToList()); + ListedRepositories.CollectionChanged += async (_, _) => await UpdateAccountData(); + if (!ListedRepositories.Any(x => x.RoomId == OwnBeatmapRepositoryRoom.RoomId)) + ListedRepositories.Add(OwnBeatmapRepositoryRoom); + + await UpdateAccountData(); + } + + private async Task UpdateAccountData() { + botData.ListedRepositories = ListedRepositories.Select(x => x.RoomId).ToList(); + await hs.SetAccountData(BotDataKey, botData); + logger.LogInformation("Updated bot account data: {}", botData.ToJson(indent: true, ignoreNull: false)); } } |