blob: 088fdcd812116ba56a78d0ab3894615b5fa40c8d (
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
140
141
142
143
144
145
146
147
148
149
|
@page "/Rooms/{RoomId}/Space"
@using System.Collections.ObjectModel
@using LibMatrix.RoomTypes
@using ArcaneLibs.Extensions
@using LibMatrix
@using MatrixUtils.Abstractions
<h3>Room manager - Viewing Space</h3>
<span>Add new room to space: </span>
<FancyTextBox @bind-Value="@NewRoomId"></FancyTextBox>
<button onclick="@AddNewRoom">Add</button>
<button onclick="@JoinAllRooms">Join all rooms</button>
@foreach (var room in Rooms) {
<RoomListItem RoomInfo="room" ShowOwnProfile="true"></RoomListItem>
}
<br/>
<details style="background: #0002;">
<summary style="background: #fff1;">State list</summary>
@foreach (var stateEvent in States.OrderBy(x => x.StateKey).ThenBy(x => x.Type)) {
<p>@stateEvent.StateKey/@stateEvent.Type:</p>
<pre>@stateEvent.RawContent.ToJson()</pre>
}
</details>
@code {
[Parameter]
public string RoomId { get; set; } = "invalid!!!!!!";
private GenericRoom? Room { get; set; }
private StateEventResponse[] States { get; set; } = Array.Empty<StateEventResponse>();
private List<RoomInfo> Rooms { get; } = new();
private List<string> ServersInSpace { get; } = new();
private string? NewRoomId { get; set; }
protected override async Task OnInitializedAsync() {
var hs = await RMUStorage.GetCurrentSessionOrNavigate();
if (hs is null) return;
Room = hs.GetRoom(RoomId.Replace('~', '.'));
var state = Room.GetFullStateAsync();
await foreach (var stateEvent in state) {
switch (stateEvent.Type) {
case "m.space.child": {
var roomId = stateEvent.StateKey;
var room = hs.GetRoom(roomId);
if (room is not null) {
Task.Run(async () => {
try {
Rooms.Add(new(Room, await room.GetFullStateAsListAsync()));
}
catch (MatrixException e) {
if (e is { ErrorCode: MatrixException.ErrorCodes.M_FORBIDDEN }) {
Rooms.Add(new(Room) {
RoomName = "M_FORBIDDEN"
});
}
}
});
}
break;
}
case "m.room.member": {
var serverName = stateEvent.StateKey.Split(':').Last();
if (!ServersInSpace.Contains(serverName)) {
ServersInSpace.Add(serverName);
}
break;
}
}
}
await base.OnInitializedAsync();
// var state = await Room.GetStateAsync("");
// if (state is not null) {
// // Console.WriteLine(state.Value.ToJson());
// States = state.Value.Deserialize<StateEventResponse[]>()!;
//
// foreach (var stateEvent in States) {
// if (stateEvent.Type == "m.space.child") {
// // if (stateEvent.Content.ToJson().Length < 5) return;
// var roomId = stateEvent.StateKey;
// var room = hs.GetRoom(roomId);
// if (room is not null) {
// Rooms.Add(room);
// }
// }
// else if (stateEvent.Type == "m.room.member") {
// var serverName = stateEvent.StateKey.Split(':').Last();
// if (!ServersInSpace.Contains(serverName)) {
// ServersInSpace.Add(serverName);
// }
// }
// }
// if(state.Value.TryGetProperty("Type", out var Type))
// {
// }
// else
// {
// //this is fine, apprently...
// //Console.WriteLine($"Room {room.RoomId} has no Content.Type in m.room.create!");
// }
// await base.OnInitializedAsync();
}
private async Task JoinAllRooms() {
// List<Task<RoomIdResponse>> tasks = Rooms.Select(room => room.JoinAsync(ServersInSpace.ToArray())).ToList();
// await Task.WhenAll(tasks);
foreach (var room in Rooms) {
await JoinRecursive(room.Room.RoomId);
}
}
private async Task JoinRecursive(string roomId) {
var room = Room!.Homeserver.GetRoom(roomId);
if (room is null) return;
try {
await room.JoinAsync(ServersInSpace.ToArray());
var joined = false;
while (!joined) {
var ce = await room.GetCreateEventAsync();
if(ce is null) continue;
if (ce.Type == "m.space") {
var children = room.AsSpace.GetChildrenAsync(false);
await foreach (var child in children) {
JoinRecursive(child.RoomId);
}
}
joined = true;
}
}
catch (Exception e) {
Console.WriteLine(e);
}
}
private async Task AddNewRoom() {
if (string.IsNullOrWhiteSpace(NewRoomId)) return;
await Room.AsSpace.AddChildByIdAsync(NewRoomId);
}
}
|