blob: 51f8e1bd48a2dd453ceee2f7e368ac732bc77973 (
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
@page "/Moderation/DraupnirProtectedRoomsEditor"
@page "/Tools/Moderation/DraupnirProtectedRoomsEditor"
@page "/Tools/Moderation/Draupnir/ProtectedRoomsEditor"
@using System.Text.Json.Serialization
@using LibMatrix
@using LibMatrix.EventTypes.Interop.Draupnir
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.RoomTypes
<h3>Edit Draupnir protected rooms</h3>
<hr/>
<p><b>Note:</b> You will need to restart Draupnir after applying changes!</p>
<p>Minor note: This <i>should</i> also work with Mjolnir, but this hasn't been tested, and as such functionality cannot be guaranteed.</p>
@if (data is not null) {
<div class="row">
<div class="col-12">
<details>
<summary>Currently protected room IDs</summary>
<ul>
@foreach (var room in data.Rooms) {
<li>@room</li>
}
</ul>
</details>
<hr/>
<h4>Tickyboxes</h4>
<table class="table">
<thead>
<tr>
<th></th> @* Checkbox column *@
<th>Kick?</th> @* PL > kick *@
<th>Ban?</th> @* PL > ban *@
<th>ACL?</th> @* PL > m.room.server_acls event *@
<th>Room ID</th>
<th>Room name</th>
</tr>
</thead>
<tbody>
@foreach (var room in Rooms.OrderBy(x => x.RoomName)) {
<tr>
<td>
<input type="checkbox" @bind="room.IsProtected"/>
</td>
<td>@(room.PowerLevels.Kick <= room.PowerLevels.GetUserPowerLevel(hs.UserId) ? "X" : "")</td>
<td>@(room.PowerLevels.Ban <= room.PowerLevels.GetUserPowerLevel(hs.UserId) ? "X" : "")</td>
<td>@(room.PowerLevels.UserHasStatePermission(hs.UserId, RoomServerACLEventContent.EventId) ? "X" : "")</td>
<td>@room.Room.RoomId</td>
<td>@room.RoomName</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
<br/>
<LinkButton OnClick="@Apply">Apply</LinkButton>
@code {
private DraupnirProtectedRoomsData data { get; set; } = new();
private List<EditorRoomInfo> Rooms { get; set; } = new();
private AuthenticatedHomeserverGeneric hs { get; set; }
protected override async Task OnInitializedAsync() {
hs = await RMUStorage.GetCurrentSessionOrNavigate();
if (hs is null) return;
data = await hs.GetAccountDataAsync<DraupnirProtectedRoomsData>(DraupnirProtectedRoomsData.EventId);
StateHasChanged();
var tasks = (await hs.GetJoinedRooms()).Select(async room => {
var plTask = room.GetPowerLevelsAsync();
var roomNameTask = room.GetNameOrFallbackAsync();
var EditorRoomInfo = new EditorRoomInfo {
Room = room,
IsProtected = data.Rooms.Contains(room.RoomId),
RoomName = await roomNameTask,
PowerLevels = await plTask
};
Rooms.Add(EditorRoomInfo);
StateHasChanged();
return Task.CompletedTask;
}).ToList();
await Task.WhenAll(tasks);
await Task.Delay(500);
foreach (var protectedRoomId in data.Rooms) {
if (Rooms.Any(x => x.Room.RoomId == protectedRoomId)) continue;
var room = hs.GetRoom(protectedRoomId);
var editorRoomInfo = new EditorRoomInfo {
Room = room,
IsProtected = true
};
try {
var pl = await room.GetPowerLevelsAsync();
editorRoomInfo.PowerLevels = pl;
}
catch (MatrixException e) {
Console.WriteLine($"Failed to get power levels for {room.RoomId}: {e}");
}
try {
editorRoomInfo.RoomName = await room.GetNameOrFallbackAsync();
}
catch (MatrixException e) {
Console.WriteLine($"Failed to get name for {room.RoomId}: {e}");
}
try {
var membership = await room.GetStateEventOrNullAsync(hs.UserId);
if (membership is not null) {
editorRoomInfo.RoomName = $"(!! {membership.ContentAs<RoomMemberEventContent>()?.Membership ?? "null"} !!) {editorRoomInfo.RoomName}";
}
}
catch (MatrixException e) {
Console.WriteLine($"Failed to get membership for {room.RoomId}: {e}");
}
Rooms.Add(editorRoomInfo);
}
StateHasChanged();
}
private class EditorRoomInfo {
public GenericRoom Room { get; set; }
public bool IsProtected { get; set; }
public string RoomName { get; set; }
public RoomPowerLevelEventContent PowerLevels { get; set; }
}
private async Task Apply() {
Console.WriteLine(string.Join('\n', Rooms.Where(x => x.IsProtected).Select(x => x.Room.RoomId)));
data.Rooms = Rooms.Where(x => x.IsProtected).Select(x => x.Room.RoomId).ToList();
await hs.SetAccountDataAsync("org.matrix.mjolnir.protected_rooms", data);
}
}
|