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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
@page "/PolicyLists"
@using ArcaneLibs
@using ArcaneLibs.Extensions
@using LibMatrix
@using LibMatrix.EventTypes
@using LibMatrix.EventTypes.Common
@using LibMatrix.EventTypes.Spec.State.Policy
@using LibMatrix.Helpers
@using LibMatrix.Responses
@using LibMatrix.RoomTypes
@inject ILogger<Index> logger
<h3>
<span>Policy lists </span>
<LinkButton OnClickAsync="@(() => {
ShowPolicyListCreationWindow = true;
return Task.CompletedTask;
})">
<span class="oi oi-plus" aria-hidden="true"> Create</span>
</LinkButton>
</h3>
@if (!string.IsNullOrWhiteSpace(Status)) {
<p>@Status</p>
}
@if (!string.IsNullOrWhiteSpace(Status2)) {
<p>@Status2</p>
}
<hr/>
<table class="table table-striped table-hover table-bordered align-middle" aria-busy="@isLoading">
<thead>
<tr>
<th>Room name</th>
<th>Policies</th>
<th/>
</tr>
</thead>
<tbody>
@foreach (var room in Rooms.OrderByDescending(x => x.PolicyCounts.Sum(y => y.Value))) {
<tr>
<td style="padding-right: 24px;">
<span>@room.RoomName</span>
@if (room.IsLegacy) {
<span style="color: red;"> (legacy)</span>
}
<br/>
@if (!string.IsNullOrWhiteSpace(room.Shortcode)) {
<span style="font-size: 0.8em;">@room.Shortcode</span>
}
else {
<span style="color: red;">(no shortcode)</span>
}
</td>
<td>
<span>@(room.PolicyCounts.GetValueOrDefault(RoomInfo.PolicyType.User) ?? 0) user policies</span><br/>
<span>@(room.PolicyCounts.GetValueOrDefault(RoomInfo.PolicyType.Server) ?? 0) server policies</span><br/>
<span>@(room.PolicyCounts.GetValueOrDefault(RoomInfo.PolicyType.Room) ?? 0) room policies</span><br/>
</td>
<td>
<LinkButton href="@($"/Rooms/{room.Room.RoomId}/Policies")">
<span class="oi oi-pencil" aria-hidden="true"> View/edit policies</span>
</LinkButton>
</td>
</tr>
}
</tbody>
</table>
@if (ShowPolicyListCreationWindow && Homeserver != null) {
<ModalWindow Title="New policy list">
@if (!string.IsNullOrWhiteSpace(_roomBuilder.Avatar.Url)) {
<MxcAvatar Homeserver="@Homeserver" MxcUri="@_roomBuilder.Avatar.Url" Circular="true" Size="4" SizeUnit="em"/>
}
else {
<img class="avatar" style="height: 4em; width: 4em; border-radius: 50%;" src="@IdenticonGenerator.GenerateAsDataUri(Homeserver.WhoAmI.UserId)"/>
}
<div style="display: inline-block; vertical-align: middle; padding-left: 1em;">
<FancyTextBox @bind-Value="@_roomBuilder.Name.Name"></FancyTextBox>
<br/>
<span>#</span>
<FancyTextBox @bind-Value="@_roomBuilder.AliasLocalPart"></FancyTextBox>
<span>:@Homeserver!.ServerName</span>
<br/>
<FancyTextBox @bind-Value="@_roomBuilder.Avatar.Url"></FancyTextBox>
<InputFile OnChange="@RoomIconFilePicked"></InputFile>
</div>
<br/>
<span>Bot shortcode: </span>
<FancyTextBox @bind-Value="@_shortcodeEvent.Shortcode"></FancyTextBox>
<br/>
<LinkButton OnClickAsync="@CreatePolicyList">Create</LinkButton>
</ModalWindow>
}
@code {
private List<RoomInfo> Rooms { get; } = [];
private AuthenticatedHomeserverGeneric? Homeserver { get; set; }
protected override async Task OnInitializedAsync() {
Homeserver = await sessionStore.GetCurrentHomeserver(navigateOnFailure: true);
if (Homeserver is null) return;
isLoading = true;
Status = "Fetching rooms...";
List<Task> _tasks = [];
await foreach (var room in Homeserver.GetJoinedRoomsByType("support.feline.policy.lists.msc.v1")) {
// roomsByType.Add(room);
Status2 = $"Found {room.RoomId} (MSC3784)...";
_tasks.Add(Task.Run(async () => {
Rooms.Add(await RoomInfo.FromRoom(room));
StateHasChanged();
}));
}
await Task.WhenAll(_tasks);
isLoading = false;
Status = "";
Status2 = "";
}
private async Task ScanLegacyLists() {
isLoading = true;
Status = "Searching for legacy lists...";
var rooms = (await Homeserver.GetJoinedRooms())
.Where(x => !Rooms.Any(y => y.Room.RoomId == x.RoomId))
.Select(async room => {
var state = await room.GetFullStateAsListAsync();
var policies = state
.Where(x => PolicyRoom.SpecPolicyEventTypes.Contains(x.Type))
.ToList();
if (policies.Count == 0) return null;
Status2 = $"Found legacy list {room.RoomId}...";
return await RoomInfo.FromRoom(room, state, true);
}).ToAsyncResultEnumerable();
await foreach (var room in rooms) {
if (room is not null) {
Rooms.Add(room);
StateHasChanged();
}
}
isLoading = false;
Status = "";
Status2 = "";
}
private string? Status {
get;
set {
field = value;
StateHasChanged();
}
}
private string? Status2 {
get;
set {
field = value;
StateHasChanged();
}
}
private bool ShowPolicyListCreationWindow {
get;
set {
field = value;
StateHasChanged();
}
} = true;
private class RoomInfo {
public GenericRoom Room { get; set; }
public string RoomName { get; set; }
public string? Shortcode { get; set; }
public Dictionary<PolicyType, int?> PolicyCounts { get; set; }
public bool IsLegacy { get; set; }
public enum PolicyType {
User,
Room,
Server
}
public static async Task<RoomInfo> FromRoom(GenericRoom room, List<MatrixEventResponse>? state = null, bool legacy = false) {
state ??= await room.GetFullStateAsListAsync();
return new RoomInfo() {
Room = room,
IsLegacy = legacy,
RoomName = await room.GetNameAsync()
?? (await room.GetCanonicalAliasAsync())?.Alias
?? (await room.GetStateOrNullAsync<MjolnirShortcodeEventContent>(MjolnirShortcodeEventContent.EventId))?.Shortcode
?? room.RoomId,
Shortcode = (await room.GetStateOrNullAsync<MjolnirShortcodeEventContent>(MjolnirShortcodeEventContent.EventId))?.Shortcode,
PolicyCounts = new() {
{ PolicyType.User, state.Count(x => PolicyRoom.UserPolicyEventTypes.Contains(x.Type)) },
{ PolicyType.Server, state.Count(x => PolicyRoom.ServerPolicyEventTypes.Contains(x.Type)) },
{ PolicyType.Room, state.Count(x => PolicyRoom.RoomPolicyEventTypes.Contains(x.Type)) }
}
};
}
}
private readonly RoomBuilder _roomBuilder = new() {
Type = "support.feline.policy.lists.msc.v1",
Name = new() { Name = "New policy list" },
AliasLocalPart = "policies"
};
private readonly MjolnirShortcodeEventContent _shortcodeEvent = new() {
Shortcode = "policy-list"
};
private bool isLoading = true;
private static readonly SvgIdenticonGenerator IdenticonGenerator = new();
private async Task RoomIconFilePicked(InputFileChangeEventArgs obj) {
var res = await Homeserver!.UploadFile(obj.File.Name, obj.File.OpenReadStream(), obj.File.ContentType);
Console.WriteLine(res);
_roomBuilder.Avatar.Url = res;
StateHasChanged();
}
private async Task CreatePolicyList() {
var room = await _roomBuilder.Create(Homeserver!);
Status = $"Created policy list {room.RoomId} ({room.GetNameAsync()})";
await room.SendStateEventAsync(MjolnirShortcodeEventContent.EventId, _shortcodeEvent);
NavigationManager.NavigateTo($"/Rooms/{room.RoomId}/Policies");
}
}
|