about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/Rooms/Index.razor
blob: 6cabe82e0460940abe9201844c1489424d26db8b (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
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
239
240
241
242
243
244
245
246
247
248
249
250
@page "/Rooms"
@using LibMatrix.Filters
@using LibMatrix.Helpers
@using LibMatrix.Extensions
@using LibMatrix.Responses
@using System.Collections.ObjectModel
@using System.Diagnostics
@using ArcaneLibs.Extensions
@using MatrixRoomUtils.Abstractions
@inject ILogger<Index> logger
<h3>Room list</h3>

<p>@Status</p>
<p>@Status2</p>

<LinkButton href="/Rooms/Create">Create new room</LinkButton>

<RoomList Rooms="Rooms" GlobalProfile="@GlobalProfile" @bind-StillFetching="RenderContents"></RoomList>

@code {
    private ObservableCollection<RoomInfo> Rooms { get; } = new();
    private UserProfileResponse GlobalProfile { get; set; }

    private AuthenticatedHomeserverGeneric? Homeserver { get; set; }

    private static 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.create",
                    "m.room.name",
                    "m.room.avatar",
                    "org.matrix.mjolnir.shortcode",
                    "m.room.power_levels",
                }
            },
            Timeline = new SyncFilter.RoomFilter.StateFilter {
                NotTypes = new List<string> { "*" },
                Limit = 1
            }
        }
    };

    // private static SyncFilter profileUpdateFilter = 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.member"
    //             },
    //             Senders = new()
    //         },
    //         Timeline = new SyncFilter.RoomFilter.StateFilter {
    //             NotTypes = new List<string> { "*" },
    //             Limit = 1
    //         }
    //     }
    // };

    private SyncHelper syncHelper;

    // SyncHelper profileSyncHelper;

    protected override async Task OnInitializedAsync() {
        Homeserver = await MRUStorage.GetCurrentSessionOrNavigate();
        if (Homeserver is null) return;
        var rooms = await Homeserver.GetJoinedRooms();
        // SemaphoreSlim _semaphore = new(160, 160);

        var roomTasks = rooms.Select(async room => {
            RoomInfo ri;
            // await _semaphore.WaitAsync();
            ri = new() { Room = room };
            await Task.WhenAll((filter.Room?.State?.Types ?? []).Select(x => ri.GetStateEvent(x)));
            return ri;
        }).ToAsyncEnumerable();

        await foreach (var room in roomTasks) {
            Rooms.Add(room);
            StateHasChanged();
            // await Task.Delay(50);
            // _semaphore.Release();
        }

        if (rooms.Count >= 150) RenderContents = true;

        GlobalProfile = await Homeserver.GetProfileAsync(Homeserver.WhoAmI.UserId);
        syncHelper = new SyncHelper(Homeserver, logger) {
            Timeout = 30000,
            Filter = filter,
            MinimumDelay = TimeSpan.FromMilliseconds(5000)
        };
        //  profileSyncHelper = new SyncHelper(Homeserver, logger) {
        //     Timeout = 10000,
        //     Filter = profileUpdateFilter,
        //     MinimumDelay = TimeSpan.FromMilliseconds(5000)
        //  };
        // profileUpdateFilter.Room.State.Senders.Add(Homeserver.WhoAmI.UserId);

        RunSyncLoop(syncHelper);
        // RunSyncLoop(profileSyncHelper);
        RunQueueProcessor();

        await base.OnInitializedAsync();
    }

    private async Task RunQueueProcessor() {
        var renderTimeSw = Stopwatch.StartNew();
        var isInitialSync = true;
        while (true) {
            try {
                while (queue.Count == 0) {
                    Console.WriteLine("Queue is empty, waiting...");
                    await Task.Delay(isInitialSync ? 100 : 2500);
                }

                Console.WriteLine($"Queue no longer empty after {renderTimeSw.Elapsed}!");

                int maxUpdates = 10;
                isInitialSync = false;
                while (maxUpdates-- > 0 && queue.TryDequeue(out var queueEntry)) {
                    var (roomId, roomData) = queueEntry;
                    Console.WriteLine($"Dequeued room {roomId}");
                    RoomInfo room;

                    if (Rooms.Any(x => x.Room.RoomId == roomId)) {
                        room = Rooms.First(x => x.Room.RoomId == roomId);
                        Console.WriteLine($"QueueWorker: {roomId} already known with {room.StateEvents?.Count ?? 0} state events");
                    }
                    else {
                        Console.WriteLine($"QueueWorker: encountered new room {roomId}!");
                        room = new RoomInfo() {
                            Room = Homeserver.GetRoom(roomId)
                        };
                        Rooms.Add(room);
                    }

                    if (room.StateEvents is null) {
                        Console.WriteLine($"QueueWorker: {roomId} does not have state events on record?");
                        throw new InvalidDataException("Somehow this is null???");
                    }

                    if (roomData.State?.Events is { Count: > 0 })
                        room.StateEvents.MergeStateEventLists(roomData.State.Events);
                    else {
                        Console.WriteLine($"QueueWorker: could not merge state for {room.Room.RoomId} as new data contains no state events!");
                    }
                }
                Console.WriteLine($"QueueWorker: {queue.Count} entries left in queue, {maxUpdates} maxUpdates left, RenderContents: {RenderContents}");
                    Status = $"Got {Rooms.Count} rooms so far! {queue.Count} entries in processing queue...";

                RenderContents |= queue.Count == 0;
                await Task.Delay(Rooms.Count);
            }
            catch (Exception e) {
                Console.WriteLine("QueueWorker exception: " + e);
            }
        }
    }

    private bool RenderContents { get; set; } = false;

    private string _status;

    public string Status {
        get => _status;
        set {
            _status = value;
            StateHasChanged();
        }
    }

    private string _status2;

    public string Status2 {
        get => _status2;
        set {
            _status2 = value;
            StateHasChanged();
        }
    }

    private Queue<KeyValuePair<string, SyncResponse.RoomsDataStructure.JoinedRoomDataStructure>> queue = new();

    private async Task RunSyncLoop(SyncHelper syncHelper) {
        Status = "Initial syncing...";
        Console.WriteLine("starting sync");

        var syncs = syncHelper.EnumerateSyncAsync();
        await foreach (var sync in syncs) {
            Console.WriteLine("trying sync");
            if (sync is null) continue;

            Status = $"Got sync with {sync.Rooms?.Join?.Count ?? 0} room updates, next batch: {sync.NextBatch}!";
            if (sync?.Rooms?.Join != null)
                foreach (var joinedRoom in sync.Rooms.Join)
                    if ( /*joinedRoom.Value.AccountData?.Events?.Count > 0 ||*/ joinedRoom.Value.State?.Events?.Count > 0) {
                        joinedRoom.Value.State.Events.RemoveAll(x => x.Type == "m.room.member" && x.StateKey != Homeserver.WhoAmI?.UserId);
                        // We can't trust servers to give us what we ask for, and this ruins performance
                        // Thanks, Conduit.
                        joinedRoom.Value.State.Events.RemoveAll(x => filter.Room?.State?.Types?.Contains(x.Type) == false);
                        if (filter.Room?.State?.NotSenders?.Any() ?? false)
                            joinedRoom.Value.State.Events.RemoveAll(x => filter.Room?.State?.NotSenders?.Contains(x.Sender) ?? false);

                        queue.Enqueue(joinedRoom);
                    }
            if (sync.Rooms.Leave is {Count: > 0})
                foreach (var leftRoom in sync.Rooms.Leave)
                    if (Rooms.Any(x => x.Room.RoomId == leftRoom.Key))
                        Rooms.Remove(Rooms.First(x => x.Room.RoomId == leftRoom.Key));

            Status = $"Got {Rooms.Count} rooms so far! {queue.Count} entries in processing queue... " +
                     $"{sync?.Rooms?.Join?.Count ?? 0} new updates!";

            Status2 = $"Next batch: {sync.NextBatch}";
        }
    }

}