about summary refs log tree commit diff
path: root/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-10-10 21:24:28 +0200
committerEmma [it/its]@Rory& <root@rory.gay>2023-10-10 21:44:30 +0200
commit06be7bed0c640c21503f75ce834a6fde2fc905ba (patch)
treeeff4ef92267dd79ce308ec2df348ae6ef50e8ecc /OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs
downloadOsuFederatedBeatmapApi-06be7bed0c640c21503f75ce834a6fde2fc905ba.tar.xz
Initial commit
Diffstat (limited to 'OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs')
-rw-r--r--OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs60
1 files changed, 60 insertions, 0 deletions
diff --git a/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs
new file mode 100644
index 0000000..0f4aa6a
--- /dev/null
+++ b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBotAccountDataService.cs
@@ -0,0 +1,60 @@
+using LibMatrix;
+using LibMatrix.EventTypes.Spec.State;
+using LibMatrix.Homeservers;
+using LibMatrix.Responses;
+using LibMatrix.RoomTypes;
+using OsuFederatedBeatmapApi.Events.AccountData;
+
+namespace OsuFederatedBeatmapApi.Services;
+
+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;
+    private BotData botData;
+
+    public async Task LoadAccountData() {
+        try {
+            botData = await hs.GetAccountDataAsync<BotData>(BotDataKey);
+        }
+        catch (Exception e) {
+            if (e is not MatrixException { ErrorCode: "M_NOT_FOUND" }) {
+                logger.LogError("{}", e.ToString());
+                throw;
+            }
+
+            botData = new BotData();
+            var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Beatmap Repository - Control room", roomAliasName: "beatmap-repo-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";
+            creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.log_room";
+            botData.LogRoom = (await hs.CreateRoom(creationContent)).RoomId;
+
+            await hs.SetAccountData(BotDataKey, botData);
+        }
+
+        LogRoom = hs.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+        ControlRoom = hs.GetRoom(botData.ControlRoom);
+    }
+}