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 /OsuFederatedBeatmapApi/Services | |
parent | Changes? (diff) | |
download | OsuFederatedBeatmapApi-ed42df73f1da60ddbfc11d5425a4955aece4acd0.tar.xz |
Bot does not crash on startup when creating rooms
Diffstat (limited to 'OsuFederatedBeatmapApi/Services')
-rw-r--r-- | OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs | 14 | ||||
-rw-r--r-- | OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs | 85 |
2 files changed, 65 insertions, 34 deletions
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)); } } |