blob: d4a200f007f4f6b4471b9d5822933dccf131e006 (
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
|
using System.Diagnostics.CodeAnalysis;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using LibMatrix.RoomTypes;
using ModerationBot.AccountData;
namespace ModerationBot.Services;
public class ModerationBotRoomProvider(AuthenticatedHomeserverGeneric hs, ModerationBotConfiguration cfg) {
private BotData? _botData;
public BotData? BotData {
get {
if (BotDataExpiry >= DateTime.UtcNow) return _botData;
Console.WriteLine("BotData expired!");
return null;
}
set {
_botData = value;
Console.WriteLine("BotData updated!");
BotDataExpiry = DateTime.UtcNow.AddMinutes(5);
}
}
private DateTime BotDataExpiry { get; set; }
[MemberNotNull(nameof(BotData))]
private async Task<BotData> GetBotDataAsync() {
try {
BotData ??= await hs.GetAccountDataAsync<BotData>(BotData.EventId);
}
catch (Exception e) {
Console.WriteLine(e);
await hs.SetAccountDataAsync(BotData.EventId, new BotData());
return await GetBotDataAsync();
}
if (BotData == null)
throw new NullReferenceException("BotData is null!");
return BotData;
}
public async Task<GenericRoom> GetControlRoomAsync() {
var botData = await GetBotDataAsync();
if (botData.ControlRoom == null) {
var createRoomRequest = CreateRoomRequest.CreatePrivate(hs, "Rory&::ModerationBot - Control Room");
createRoomRequest.Invite = cfg.Admins;
var newRoom = await hs.CreateRoom(createRoomRequest, true, true, true);
BotData.ControlRoom = newRoom.RoomId;
await hs.SetAccountDataAsync(BotData.EventId, BotData);
}
return hs.GetRoom(BotData.ControlRoom!);
}
public async Task<GenericRoom> GetLogRoomAsync() {
var botData = await GetBotDataAsync();
if (botData.LogRoom == null) {
var controlRoom = await GetControlRoomAsync();
var createRoomRequest = CreateRoomRequest.CreatePrivate(hs, "Rory&::ModerationBot - Log Room");
createRoomRequest.Invite = (await controlRoom.GetMembersListAsync()).Select(x=>x.StateKey).ToList();
var newRoom = await hs.CreateRoom(createRoomRequest, true, true, true);
BotData.LogRoom = newRoom.RoomId;
await hs.SetAccountDataAsync(BotData.EventId, BotData);
}
return hs.GetRoom(BotData.LogRoom!);
}
public async Task<GenericRoom?> GetDefaultPolicyRoomAsync() {
var botData = await GetBotDataAsync();
return string.IsNullOrWhiteSpace(botData.DefaultPolicyRoom) ? null : hs.GetRoom(BotData.DefaultPolicyRoom!);
}
}
|