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
|
@page "/Rooms"
@using LibMatrix.Filters
@using LibMatrix.Helpers
@using LibMatrix.Responses
@using LibMatrix.EventTypes.Spec.State
<h3>Room list</h3>
<p>@Status</p>
@if (RenderContents) {
<RoomList Rooms="Rooms" GlobalProfile="@GlobalProfile"></RoomList>
}
@code {
public List<RoomInfo> KnownRooms { get; set; } = new();
private List<RoomInfo> Rooms { get; set; } = new();
private ProfileResponseEventContent GlobalProfile { get; set; }
private SyncFilter filter = new() {
AccountData = new SyncFilter.EventFilter {
NotTypes = new List<string> { "*" },
Limit = 1
},
Presence = new SyncFilter.EventFilter {
NotTypes = new List<string> { "*" },
Limit = 1
},
Room = new SyncFilter.RoomFilter {
AccountData = new SyncFilter.RoomFilter.StateFilter {
NotTypes = new List<string> { "*" },
Limit = 1
},
Ephemeral = new SyncFilter.RoomFilter.StateFilter {
NotTypes = new List<string> { "*" },
Limit = 1
},
State = new SyncFilter.RoomFilter.StateFilter {
Types = new List<string> {
"m.room.name",
"m.room.avatar",
"m.room.create",
"org.matrix.mjolnir.shortcode",
"m.room.power_levels"
}
},
Timeline = new SyncFilter.RoomFilter.StateFilter {
NotTypes = new List<string> { "*" },
Limit = 1
}
}
};
protected override async Task OnInitializedAsync() {
var hs = await MRUStorage.GetCurrentSessionOrNavigate();
if (hs is null) return;
GlobalProfile = await hs.GetProfileAsync(hs.WhoAmI.UserId);
Status = "Syncing...";
SyncResult? sync = null;
string? nextBatch = null;
while (sync is null or { Rooms.Join.Count: >= 1}) {
sync = await hs.SyncHelper.Sync(since: nextBatch, filter: filter, timeout: 0);
nextBatch = sync?.NextBatch ?? nextBatch;
if (sync is null) continue;
Console.WriteLine($"Got sync, next batch: {nextBatch}!");
if (sync.Rooms is null) continue;
if (sync.Rooms.Join is null) continue;
foreach (var (roomId, roomData) in sync.Rooms.Join) {
RoomInfo room;
if (Rooms.Any(x => x.Room.RoomId == roomId)) {
room = Rooms.First(x => x.Room.RoomId == roomId);
}
else {
room = new RoomInfo {
Room = hs.GetRoom(roomId),
StateEvents = new List<StateEventResponse?>()
};
Rooms.Add(room);
KnownRooms.Add(room);
}
room.StateEvents.AddRange(roomData.State.Events);
}
Status = $"Got {Rooms.Count} rooms so far! Next batch: {nextBatch}";
StateHasChanged();
await Task.Delay(100);
}
Console.WriteLine("Sync done!");
Status = "Sync complete!";
foreach (var roomInfo in Rooms) {
if (!roomInfo.StateEvents.Any(x => x.Type == "m.room.name")) {
roomInfo.StateEvents.Add(new StateEventResponse {
Type = "m.room.name",
TypedContent = new RoomNameEventContent {
Name = roomInfo.Room.RoomId
}
});
}
if (!roomInfo.StateEvents.Any(x => x.Type == "m.room.avatar")) {
roomInfo.StateEvents.Add(new StateEventResponse {
Type = "m.room.avatar",
TypedContent = new RoomAvatarEventContent {
}
});
}
if (!roomInfo.StateEvents.Any(x => x.Type == "org.matrix.mjolnir.shortcode")) {
roomInfo.StateEvents.Add(new StateEventResponse {
Type = "org.matrix.mjolnir.shortcode"
});
}
}
Console.WriteLine("Set stub data!");
Status = "Set stub data!";
SemaphoreSlim semaphore = new(8, 8);
var memberTasks = Rooms.Select(async roomInfo => {
if (!roomInfo.StateEvents.Any(x => x.Type == "m.room.member" && x.StateKey == hs.WhoAmI.UserId)) {
await semaphore.WaitAsync();
roomInfo.StateEvents.Add(new StateEventResponse {
Type = "m.room.member",
StateKey = hs.WhoAmI.UserId,
TypedContent = await roomInfo.Room.GetStateAsync<RoomMemberEventContent>("m.room.member", hs.WhoAmI.UserId) ?? new RoomMemberEventContent {
Membership = "unknown"
}
});
semaphore.Release();
}
}).ToList();
await Task.WhenAll(memberTasks);
Console.WriteLine("Set all room member data!");
Status = "Set all room member data!";
// var res = await hs.SyncHelper.Sync(filter: filter);
// if (res is not null) {
// foreach (var (roomId, roomData) in res.Rooms.Join) {
// var room = new RoomInfo() {
// Room = hs.GetRoom(roomId),
// StateEvents = roomData.State.Events.Where(x => x.Type == "m.room.member" && x.StateKey == hs.WhoAmI.UserId).ToList()
// };
// Rooms.Add(room);
// }
// }
// Rooms = (await hs.GetJoinedRooms()).Select(x => new RoomInfo() { Room = x }).ToList();
RenderContents = true;
Status = "";
await base.OnInitializedAsync();
}
private bool RenderContents { get; set; }
private string _status;
public string Status {
get => _status;
set {
_status = value;
StateHasChanged();
}
}
}
|