blob: 4db2b5a7011bb62e4270bbdd199da938f29451d5 (
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
|
@page "/PolicyListEditor"
@using System.Text.Json.Serialization
@using MatrixRoomUtils.Core.Interfaces
@using MatrixRoomUtils.Core.StateEventTypes
@inject ILocalStorageService LocalStorage
@inject NavigationManager NavigationManager
<h3>Policy list editor - Room list</h3>
<hr/>
@if (PolicyRoomList.Count == 0) {
<p>No policy rooms found.</p>
<p>Loading progress: @checkedRoomCount/@totalRoomCount</p>
}
else {
@if (checkedRoomCount != totalRoomCount) {
<p>Loading progress: @checkedRoomCount/@totalRoomCount</p>
}
foreach (var s in PolicyRoomList) {
<a style="color: unset; text-decoration: unset;" href="/PolicyListEditor/@s.RoomId.Replace('.', '~')">
<RoomListItem RoomId="@s.RoomId">
<br/>
<span>Shortcode: @s.Shortcode</span>
</RoomListItem>
</a>
@* <a href="@(NavigationManager.Uri + "/" + s.RoomId.Replace('.', '~'))">[@s.Shortcode] @s.Name (@s.RoomId)</a> *@
@* <br/> *@
}
}
<div style="margin-bottom: 4em;"></div>
<LogView></LogView>
@code {
//get room list
// - sync withroom list filter
// Type = support.feline.msc3784
//support.feline.policy.lists.msc.v1
public List<PolicyRoomInfo> PolicyRoomList { get; set; } = new();
private int checkedRoomCount { get; set; }
private int totalRoomCount { get; set; }
protected override async Task OnInitializedAsync() {
await base.OnInitializedAsync();
var hs = await MRUStorage.GetCurrentSessionOrNavigate();
if (hs is null) return;
await EnumeratePolicyRooms();
Console.WriteLine("Policy list editor initialized!");
}
private async Task EnumeratePolicyRooms() {
var hs = await MRUStorage.GetCurrentSession();
var rooms = await hs.GetJoinedRooms();
totalRoomCount = rooms.Count;
StateHasChanged();
var semaphore = new SemaphoreSlim(8);
var tasks = new List<Task<PolicyRoomInfo?>>();
foreach (var room in rooms) {
tasks.Add(GetPolicyRoomInfo(room.RoomId, semaphore));
}
var results = await Task.WhenAll(tasks);
PolicyRoomList.AddRange(results.Where(x => x is not null).Select(x => x.Value));
Console.WriteLine($"Detected policy lists: {PolicyRoomList.ToJson()}");
}
private async Task<PolicyRoomInfo?> GetPolicyRoomInfo(string room, SemaphoreSlim semaphore) {
try {
await semaphore.WaitAsync();
var hs = await MRUStorage.GetCurrentSession();
PolicyRoomInfo roomInfo = new() {
RoomId = room
};
var r = await hs.GetRoom(room);
var shortcodeState = await r.GetStateAsync<MjolnirShortcodeEventData>("org.matrix.mjolnir.shortcode");
roomInfo.Shortcode = shortcodeState.Shortcode;
if (roomInfo.Shortcode is not null) {
roomInfo.Name = await r.GetNameAsync();
return roomInfo;
}
return null;
}
finally {
checkedRoomCount++;
StateHasChanged();
semaphore.Release();
}
}
public struct PolicyRoomInfo {
public
string RoomId { get; set; }
public
string? Shortcode { get; set; }
public
string? Name { get; set; }
}
}
|