about summary refs log tree commit diff
path: root/ModerationClient/ViewModels/ClientViewModel.cs
blob: a2da5be45657a5db7d38eda72abe85b43e3bcb6f (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using ArcaneLibs;
using ArcaneLibs.Collections;
using ArcaneLibs.Extensions;
using LibMatrix;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.EventTypes.Spec.State.RoomInfo;
using LibMatrix.Helpers;
using LibMatrix.Responses;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ModerationClient.Models.SpaceTreeNodes;
using ModerationClient.Services;

namespace ModerationClient.ViewModels;

public partial class ClientViewModel : ViewModelBase {
    public ClientViewModel(ILogger<ClientViewModel> logger, MatrixAuthenticationService authService, CommandLineConfiguration cfg) {
        _logger = logger;
        _authService = authService;
        _cfg = cfg;
        DisplayedSpaces.Add(_allRoomsNode = new AllRoomsSpaceNode(this));
        DisplayedSpaces.Add(DirectMessages = new SpaceNode(false) { Name = "Direct messages" });
        _ = Task.Run(Run).ContinueWith(x => {
            if (x.IsFaulted) {
                Status = "Critical error running client view model: " + x.Exception?.Message;
                _logger.LogError(x.Exception, "Error running client view model.");
            }
        });
    }

    private readonly ILogger<ClientViewModel> _logger;
    private readonly MatrixAuthenticationService _authService;
    private readonly CommandLineConfiguration _cfg;
    private readonly SpaceNode _allRoomsNode;
    public ObservableCollection<SpaceNode> DisplayedSpaces { get; } = [];
    public ObservableDictionary<string, RoomNode> AllRooms { get; } = new();
    public SpaceNode DirectMessages { get; }

    public bool Paused { get; set; } = true;

    public string Status {
        get => field + " " + DateTime.Now;
        set => SetProperty(ref field, value);
    } = "Loading...";

    [field: AllowNull, MaybeNull]
    public SpaceNode CurrentSpace {
        get => field ?? _allRoomsNode;
        set => SetProperty(ref field, value);
    }

    public RoomNode? CurrentRoom {
        get;
        set {
            if (SetProperty(ref field, value)) OnPropertyChanged(nameof(CurrentRoomViewModel));
        }
    }

    public RoomViewModel? CurrentRoomViewModel => CurrentRoom is not null ? new RoomViewModel(_authService.Homeserver!, CurrentRoom) : null;

    public async Task Run() {
        Console.WriteLine("Running client view model loop...");
        ArgumentNullException.ThrowIfNull(_authService.Homeserver, nameof(_authService.Homeserver));
        // var sh = new SyncStateResolver(_authService.Homeserver, _logger, storageProvider: new FileStorageProvider(Path.Combine(_cfg.ProfileDirectory, "syncCache")));
        var store = new FileStorageProvider(Path.Combine(_cfg.ProfileDirectory, "syncCache"));
        var mediaCache = new FileStorageProvider(Path.Combine(_cfg.ProfileDirectory, "mediaCache"));
        Console.WriteLine($"Sync store at {store.TargetPath}");

        var sh = new SyncHelper(_authService.Homeserver, new NullLogger<SyncHelper>(), storageProvider: store) {
            // MinimumDelay = TimeSpan.FromSeconds(1)
        };
        Console.WriteLine("Sync helper created.");

        var unoptimised = await sh.GetUnoptimisedStoreCount(); // this is slow, so we cache
        //optimise - we create a new scope here to make ssr go out of scope
        if (unoptimised > 100) {
            Console.WriteLine("RUN - Optimising sync store...");
            Status = "Optimising sync store, please wait...";
            var ssr = new SyncStateResolver(_authService.Homeserver, _logger, storageProvider: store);
            Console.WriteLine("Created sync state resolver...");
            Status = "Optimising sync store, please wait... Creating new snapshot...";
            await ssr.OptimiseStore((remaining, total) => {
                if (remaining % (remaining / 10) == 0)
                    Status = $"Optimising sync store, please wait... {remaining}/{total} remaining...";
            });
            Status = "Optimising sync store, please wait... Deleting old intermediate snapshots...";
            await ssr.RemoveOldSnapshots();
            for (int gen = 0; gen < GC.MaxGeneration; gen++) {
                Status = $"Collecting garbage #{gen}: {Util.BytesToString(GC.GetGCMemoryInfo().TotalCommittedBytes)}";
                await Task.Delay(1000);
            }

            Status = $"Collecting garbage: {GC.GetTotalMemory(true) / 1024 / 1024}MB";
            await Task.Delay(1000);
        }

        GC.Collect();
        unoptimised = 0;
        Status = "Doing initial sync...";
        var currentSyncRes = "init";
        var lastGc = DateTime.Now;
        await foreach (var res in sh.EnumerateSyncAsync()) {
            var sw = Stopwatch.StartNew();
            //log thing

            // Program.Beep((short)250, 0);
            Status = $"Processing sync... {res.NextBatch}";

            if (res.Rooms?.Leave is { Count: > 0 }) {
                var maxKeyLength = res.Rooms.Leave.Keys.Max(x => x.Length);
                foreach (var (key, value) in res.Rooms?.Leave ?? []) {
                    List<StateEventResponse> events = [..value.Timeline?.Events ?? [], ..value.State?.Events ?? []];
                    var ownEvents = events.Where(x => x is { StateKey: "@emma:rory.gay", Type: RoomMemberEventContent.EventId }).ToList();
                    var maxSenderLength = ownEvents.Max(x => x.Sender!.Length);
                    foreach (var evt in ownEvents) {
                        var ct = evt.ContentAs<RoomMemberEventContent>()!;
                        var reasonString = string.IsNullOrWhiteSpace(ct.Reason)
                            ? ""
                            : $" (reason: {ct.Reason})";
                        Console.WriteLine($"Room {key.PadLeft(maxKeyLength)} removed: {evt.StateKey} by {evt.Sender!.PadLeft(maxSenderLength)} at {DateTimeOffset.FromUnixTimeMilliseconds((long)evt.OriginServerTs!)}, membership: {ct.Membership,6}{reasonString}");
                    }
                }
            }

            if (res.Rooms?.Leave is { Count: > 0 })
                Console.WriteLine($"Processed {res.Rooms?.Leave?.Count} left rooms");
            // await Task.Delay(10000);

            var applySw = Stopwatch.StartNew();
            await ApplySyncChanges(res);
            applySw.Stop();

            Program.Beep(0, 0);
            if (DateTime.Now - lastGc > TimeSpan.FromMinutes(1)) {
                lastGc = DateTime.Now;
                GC.Collect();
            }

            Console.WriteLine($"Processed sync {currentSyncRes} in {sw.ElapsedMilliseconds}ms (applied in: {applySw.ElapsedMilliseconds}ms)");
            if (Paused) {
                Status = "Sync loop interrupted... Press pause/break to resume.";
                while (Paused) await Task.Delay(1000);
            }
            else Status = $"Syncing... {unoptimised++} unoptimised sync responses, last={currentSyncRes}...";

            currentSyncRes = res.NextBatch;
        }
    }

    private async Task ApplySyncChanges(SyncResponse newSync) {
        await ApplySpaceChanges(newSync);
        if (newSync.AccountData?.Events?.FirstOrDefault(x => x.Type == "m.direct") is { } evt) {
            await ApplyDirectMessagesChanges(evt);
        }
    }

    private async Task ApplySpaceChanges(SyncResponse newSync) {
        List<Task> tasks = [];
        foreach (var room in newSync.Rooms?.Join ?? []) {
            if (!AllRooms.ContainsKey(room.Key)) {
                // AllRooms.Add(room.Key, new RoomNode { Name = "Loading..." });
                AllRooms.Add(room.Key, new RoomNode { Name = "", RoomID = room.Key });
            }

            var nameEvent = room.Value.State?.Events?.FirstOrDefault(x => x is { Type: "m.room.name", StateKey: "" });
            if (nameEvent != null)
                AllRooms[room.Key].Name = (nameEvent?.TypedContent as RoomNameEventContent)?.Name ?? "";

            if (string.IsNullOrWhiteSpace(AllRooms[room.Key].Name)) {
                AllRooms[room.Key].Name = "Loading...";
                tasks.Add(_authService.Homeserver!.GetRoom(room.Key).GetNameOrFallbackAsync().ContinueWith(r => {
                    if (r.IsFaulted) {
                        _logger.LogError(r.Exception, "Error getting room name for {RoomKey}", room.Key);
                        return AllRooms[room.Key].Name = "Error loading room name";
                    }

                    return AllRooms[room.Key].Name = r.Result;
                }));
                // Status = $"Getting room name for {room.Key}...";
                // AllRooms[room.Key].Name = await _authService.Homeserver!.GetRoom(room.Key).GetNameOrFallbackAsync();
            }

            if (room.Value.Timeline?.Events is not null) {
                foreach (var evt in room.Value.Timeline.Events) {
                    AllRooms[room.Key].Timeline.Add(evt);
                }
            }

            if (room.Value.State?.Events is not null) {
                foreach (var evt in room.Value.State.Events) {
                    AllRooms[room.Key].State.Add(evt);
                }
            }
        }

        await AwaitTasks(tasks, "Waiting for {0}/{1} tasks while applying room changes...");
    }

    private ExpiringSemaphoreCache<UserProfileResponse> _profileCache = new();

    private async Task ApplyDirectMessagesChanges(StateEventResponse evt) {
        _logger.LogCritical("Direct messages updated!");
        var dms = evt.RawContent.Deserialize<Dictionary<string, string[]?>>();
        List<Task> tasks = [];
        foreach (var (userId, roomIds) in dms) {
            if (roomIds is null || roomIds.Length == 0) continue;
            var space = DirectMessages.ChildSpaces.FirstOrDefault(x => x.RoomID == userId);
            if (space is null) {
                space = new SpaceNode { Name = userId, RoomID = userId };
                // tasks.Add(_authService.Homeserver!.GetProfileAsync(userId)
                // .ContinueWith(r => space.Name = string.IsNullOrWhiteSpace(r.Result.DisplayName) ? userId : r.Result.DisplayName));
                tasks.Add(_profileCache.GetOrAdd(userId, async () => await _authService.Homeserver!.GetProfileAsync(userId), TimeSpan.FromMinutes(5))
                    .ContinueWith(r => {
                        if (!r.IsFaulted)
                            return string.IsNullOrWhiteSpace(r.Result.DisplayName) ? userId : r.Result.DisplayName;
                        return userId;
                    }));
                DirectMessages.ChildSpaces.Add(space);
            }

            foreach (var roomId in roomIds) {
                var room = space.ChildRooms.FirstOrDefault(x => x.RoomID == roomId);
                if (room is null) {
                    room = AllRooms.TryGetValue(roomId, out var existing) ? existing : new RoomNode { Name = "Unknown: " + roomId, RoomID = roomId };
                    space.ChildRooms.Add(room);
                }
            }

            foreach (var spaceChildRoom in space.ChildRooms.ToList()) {
                if (!roomIds.Contains(spaceChildRoom.RoomID)) {
                    space.ChildRooms.Remove(spaceChildRoom);
                }
            }
        }

        await AwaitTasks(tasks, "Waiting for {0}/{1} tasks while applying DM changes...");
    }

    private async Task AwaitTasks(List<Task> tasks, string message) {
        if (tasks.Count > 0) {
            int total = tasks.Count;
            while (tasks.Any(x => !x.IsCompleted)) {
                int incomplete = tasks.Count(x => !x.IsCompleted);
                // Program.Beep((short)MathUtil.Map(incomplete, 0, total, 20, 7500), 5);
                // Program.Beep(0, 0);
                if (incomplete < 10 || incomplete % (total / 10) == 0)
                    Status = string.Format(message, incomplete, total);
                await Task.WhenAny(tasks);
                tasks.RemoveAll(x => x.IsCompleted);
            }

            Program.Beep(0, 0);
        }
    }
}

// implementation details
public class AllRoomsSpaceNode : SpaceNode {
    public AllRoomsSpaceNode(ClientViewModel vm) : base(false) {
        Name = "All rooms";
        vm.AllRooms.CollectionChanged += (_, args) => {
            switch (args.Action) {
                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Remove:
                case NotifyCollectionChangedAction.Replace: {
                    foreach (var room in args.NewItems?.Cast<KeyValuePair<string, RoomNode>>() ?? []) ChildRooms.Add(room.Value);
                    foreach (var room in args.OldItems?.Cast<KeyValuePair<string, RoomNode>>() ?? []) ChildRooms.Remove(room.Value);
                    break;
                }

                case NotifyCollectionChangedAction.Reset: {
                    ChildSpaces.Clear();
                    ChildRooms.Clear();
                    break;
                }
            }
        };
    }
}