about summary refs log tree commit diff
path: root/ExampleBots/ModerationBot/FirstRunTasks.cs
blob: 83356bf3fc5c5e3db126b291481236456191cb5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using LibMatrix;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using ModerationBot.AccountData;

namespace ModerationBot;

public class FirstRunTasks {
    public static async Task<BotData> ConstructBotData(AuthenticatedHomeserverGeneric hs, ModerationBotConfiguration configuration, BotData? botdata) {
        botdata ??= new BotData();
        var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Rory&::ModerationBot - Control room", roomAliasName: "moderation-bot-control-room");
        creationContent.Invite = configuration.Admins;
        creationContent.CreationContent["type"] = "gay.rory.moderation_bot.control_room";

        if (botdata.ControlRoom is null)
            try {
                botdata.ControlRoom = (await hs.CreateRoom(creationContent)).RoomId;
            }
            catch (Exception e) {
                if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) {
                    Console.WriteLine(e);
                    throw;
                }

                creationContent.RoomAliasName += $"-{Guid.NewGuid()}";
                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 = "Rory&::ModerationBot - Log room";
        creationContent.RoomAliasName = "moderation-bot-log-room";
        creationContent.CreationContent["type"] = "gay.rory.moderation_bot.log_room";

        if (botdata.LogRoom is null)
            try {
                botdata.LogRoom = (await hs.CreateRoom(creationContent)).RoomId;
            }
            catch (Exception e) {
                if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) {
                    Console.WriteLine(e);
                    throw;
                }

                creationContent.RoomAliasName += $"-{Guid.NewGuid()}";
                botdata.LogRoom = (await hs.CreateRoom(creationContent)).RoomId;
            }

        creationContent.Name = "Rory&::ModerationBot - Policy room";
        creationContent.RoomAliasName = "moderation-bot-policy-room";
        creationContent.CreationContent["type"] = "gay.rory.moderation_bot.policy_room";

        if (botdata.DefaultPolicyRoom is null)
            try {
                botdata.DefaultPolicyRoom = (await hs.CreateRoom(creationContent)).RoomId;
            }
            catch (Exception e) {
                if (e is not MatrixException { ErrorCode: "M_ROOM_IN_USE" }) {
                    Console.WriteLine(e);
                    throw;
                }

                creationContent.RoomAliasName += $"-{Guid.NewGuid()}";
                botdata.DefaultPolicyRoom = (await hs.CreateRoom(creationContent)).RoomId;
            }

        await hs.SetAccountDataAsync("gay.rory.moderation_bot_data", botdata);

        return botdata;
    }
}