about summary refs log tree commit diff
path: root/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs
blob: afd69d11d347ba0a97b3f1e795a0383228b52390 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using System.Collections.Immutable;
using System.Diagnostics;
using System.Text.Json.Nodes;
using ArcaneLibs;
using LibMatrix.EventTypes.Spec;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Helpers;
using LibMatrix.HomeserverEmulator.Extensions;
using LibMatrix.HomeserverEmulator.Services;
using LibMatrix.Responses;
using LibMatrix.Services;
using Microsoft.AspNetCore.Mvc;

namespace LibMatrix.HomeserverEmulator.Controllers.Rooms;

[ApiController]
[Route("/_matrix/client/{version}/rooms/{roomId}")]
public class RoomTimelineController(
    ILogger<RoomTimelineController> logger,
    TokenService tokenService,
    UserStore userStore,
    RoomStore roomStore,
    HomeserverProviderService hsProvider) : ControllerBase {
    [HttpPut("send/{eventType}/{txnId}")]
    public async Task<EventIdResponse> SendMessage(string roomId, string eventType, string txnId, [FromBody] JsonObject content) {
        var token = tokenService.GetAccessToken(HttpContext);
        var user = await userStore.GetUserByToken(token);

        var room = roomStore.GetRoomById(roomId);
        if (room == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Room not found"
            };

        if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId))
            throw new MatrixException() {
                ErrorCode = "M_FORBIDDEN",
                Error = "User is not in the room"
            };

        var evt = new StateEvent() {
            RawContent = content,
            Type = eventType
        }.ToStateEvent(user, room);

        room.Timeline.Add(evt);
        if (evt.Type == RoomMessageEventContent.EventId && (evt.TypedContent as RoomMessageEventContent).Body.StartsWith("!hse"))
            await HandleHseCommand(evt, room, user);
        // else

        return new() {
            EventId = evt.EventId
        };
    }

    [HttpGet("messages")]
    public async Task<MessagesResponse> GetMessages(string roomId, [FromQuery] string? from = null, [FromQuery] string? to = null, [FromQuery] int limit = 100,
        [FromQuery] string? dir = "b") {
        var token = tokenService.GetAccessToken(HttpContext);
        var user = await userStore.GetUserByToken(token);

        var room = roomStore.GetRoomById(roomId);
        if (room == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Room not found"
            };

        if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId))
            throw new MatrixException() {
                ErrorCode = "M_FORBIDDEN",
                Error = "User is not in the room"
            };

        if (dir == "b") {
            var timeline = room.Timeline.TakeLast(limit).ToList();
            return new() {
                Start = timeline.First().EventId,
                End = timeline.Last().EventId,
                Chunk = timeline.AsEnumerable().Reverse().ToList(),
                State = timeline.GetCalculatedState()
            };
        }
        else if (dir == "f") {
            var timeline = room.Timeline.Take(limit).ToList();
            return new() {
                Start = timeline.First().EventId,
                End = room.Timeline.Last() == timeline.Last() ? null : timeline.Last().EventId,
                Chunk = timeline
            };
        }
        else
            throw new MatrixException() {
                ErrorCode = "M_BAD_REQUEST",
                Error = $"Invalid direction '{dir}'"
            };
    }

    [HttpGet("event/{eventId}")]
    public async Task<StateEventResponse> GetEvent(string roomId, string eventId) {
        var token = tokenService.GetAccessToken(HttpContext);
        var user = await userStore.GetUserByToken(token);

        var room = roomStore.GetRoomById(roomId);
        if (room == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Room not found"
            };

        if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId))
            throw new MatrixException() {
                ErrorCode = "M_FORBIDDEN",
                Error = "User is not in the room"
            };

        var evt = room.Timeline.SingleOrDefault(x => x.EventId == eventId);
        if (evt == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Event not found"
            };

        return evt;
    }
    
    [HttpGet("relations/{eventId}")]
    public async Task<RecursedBatchedChunkedStateEventResponse> GetRelations(string roomId, string eventId, [FromQuery] string? dir = "b", [FromQuery] string? from = null, [FromQuery] int? limit = 100, [FromQuery] bool? recurse = false, [FromQuery] string? to = null) {
        var token = tokenService.GetAccessToken(HttpContext);
        var user = await userStore.GetUserByToken(token);

        var room = roomStore.GetRoomById(roomId);
        if (room == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Room not found"
            };

        if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId))
            throw new MatrixException() {
                ErrorCode = "M_FORBIDDEN",
                Error = "User is not in the room"
            };

        var evt = room.Timeline.SingleOrDefault(x => x.EventId == eventId);
        if (evt == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Event not found"
            };

        var matchingEvents = await GetRelationsInternal(roomId, eventId, dir, from, limit, recurse, to);

        return new() {
            Chunk = matchingEvents.ToList()
        };
    }
    
    [HttpGet("relations/{eventId}/{relationType}")]
    public async Task<RecursedBatchedChunkedStateEventResponse> GetRelations(string roomId, string eventId, string relationType, [FromQuery] string? dir = "b", [FromQuery] string? from = null, [FromQuery] int? limit = 100, [FromQuery] bool? recurse = false, [FromQuery] string? to = null) {
        var token = tokenService.GetAccessToken(HttpContext);
        var user = await userStore.GetUserByToken(token);

        var room = roomStore.GetRoomById(roomId);
        if (room == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Room not found"
            };

        if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId))
            throw new MatrixException() {
                ErrorCode = "M_FORBIDDEN",
                Error = "User is not in the room"
            };

        var evt = room.Timeline.SingleOrDefault(x => x.EventId == eventId);
        if (evt == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Event not found"
            };

        var matchingEvents = await GetRelationsInternal(roomId, eventId, dir, from, limit, recurse, to);

        return new() {
            Chunk = matchingEvents.ToList()
        };
    }
    
    [HttpGet("relations/{eventId}/{relationType}/{eventType}")]
    public async Task<RecursedBatchedChunkedStateEventResponse> GetRelations(string roomId, string eventId, string relationType, string eventType, [FromQuery] string? dir = "b", [FromQuery] string? from = null, [FromQuery] int? limit = 100, [FromQuery] bool? recurse = false, [FromQuery] string? to = null) {
        var token = tokenService.GetAccessToken(HttpContext);
        var user = await userStore.GetUserByToken(token);

        var room = roomStore.GetRoomById(roomId);
        if (room == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Room not found"
            };

        if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId))
            throw new MatrixException() {
                ErrorCode = "M_FORBIDDEN",
                Error = "User is not in the room"
            };

        var evt = room.Timeline.SingleOrDefault(x => x.EventId == eventId);
        if (evt == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Event not found"
            };

        var matchingEvents = await GetRelationsInternal(roomId, eventId, dir, from, limit, recurse, to);

        return new() {
            Chunk = matchingEvents.ToList()
        };
    }
    
    private async Task<IEnumerable<StateEventResponse>> GetRelationsInternal(string roomId, string eventId, string dir, string? from, int? limit, bool? recurse, string? to) {
        var room = roomStore.GetRoomById(roomId);
        var evt = room.Timeline.SingleOrDefault(x => x.EventId == eventId);
        if (evt == null)
            throw new MatrixException() {
                ErrorCode = "M_NOT_FOUND",
                Error = "Event not found"
            };

        var relatedEvents = room.Timeline.Where(x => x.RawContent?["m.relates_to"]?["event_id"]?.GetValue<string>() == eventId);
        if (dir == "b") {
            relatedEvents = relatedEvents.TakeLast(limit ?? 100);
        }
        else if (dir == "f") {
            relatedEvents = relatedEvents.Take(limit ?? 100);
        }
        
        return relatedEvents;
    }

#region Commands

    private void InternalSendMessage(RoomStore.Room room, string content) {
        InternalSendMessage(room, new MessageBuilder().WithBody(content).Build());
    }

    private void InternalSendMessage(RoomStore.Room room, RoomMessageEventContent content) {
        logger.LogInformation("Sending internal message: {content}", content.Body);
        room.Timeline.Add(new StateEventResponse() {
            Type = RoomMessageEventContent.EventId,
            TypedContent = content,
            Sender = $"@hse:{tokenService.GenerateServerName(HttpContext)}",
            RoomId = room.RoomId,
            EventId = "$" + string.Join("", Random.Shared.GetItems("abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789".ToCharArray(), 100)),
            OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds()
        });
    }

    private async Task HandleHseCommand(StateEventResponse evt, RoomStore.Room room, UserStore.User user) {
        try {
            var msgContent = evt.TypedContent as RoomMessageEventContent;
            var parts = msgContent.Body.Split('\n')[0].Split(" ");
            if (parts.Length < 2) return;

            var command = parts[1];
            switch (command) {
                case "import":
                    await HandleImportCommand(parts[2..], evt, room, user);
                    break;
                case "import-nheko-profiles":
                    await HandleImportNhekoProfilesCommand(parts[2..], evt, room, user);
                    break;
                case "clear-sync-states":
                    foreach (var (token, session) in user.AccessTokens) {
                        session.SyncStates.Clear();
                        InternalSendMessage(room, $"Cleared sync states for {token}.");
                    }

                    break;
                case "rsp": {
                    await Task.Delay(1000);
                    var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789";
                    for (int i = 0; i < 10000; i++) {
                        // await Task.Delay(100);
                        // InternalSendMessage(room, $"https://music.youtube.com/watch?v=90oZtyvavSk&i={i}");
                        var url = $"https://music.youtube.com/watch?v=";
                        for (int j = 0; j < 11; j++) {
                            url += chars[Random.Shared.Next(chars.Length)];
                        }

                        InternalSendMessage(room, url + "&i=" + i);
                        if (i % 5000 == 0 || i == 9999) {
                            Thread.Sleep(5000);

                            do {
                                InternalSendMessage(room,
                                    $"Current GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}");
                                GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true);
                                GC.WaitForPendingFinalizers();
                                InternalSendMessage(room,
                                    $"GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}");
                                await Task.Delay(5000);
                            } while (Process.GetCurrentProcess().WorkingSet64 >= 1_024_000_000);
                        }
                    }
                    break;
                }
                case "genrooms": {
                    var sw = Stopwatch.StartNew();
                    var count = 1000;
                    for (int i = 0; i < count; i++) {
                        var crq = new CreateRoomRequest() {
                            Name = "Test room",
                            CreationContent = new() {
                                ["version"] = "11"
                            },
                            InitialState = []
                        };

                        if (Random.Shared.Next(100) > 75) {
                            crq.CreationContent["type"] = "m.space";
                            foreach (var item in Random.Shared.GetItems(roomStore._rooms.ToArray(), 50)) {
                                crq.InitialState!.Add(new StateEvent() {
                                    Type = "m.space.child",
                                    StateKey = item.RoomId,
                                    TypedContent = new SpaceChildEventContent() {
                                        Suggested = true,
                                        AutoJoin = true,
                                        Via = new List<string>()
                                    }
                                }.ToStateEvent(user, room));
                            }
                        }
                        var newRoom = roomStore.CreateRoom(crq);
                        newRoom.AddUser(user.UserId);
                    }
                    InternalSendMessage(room, $"Generated {count} new rooms in {sw.Elapsed}!");
                    break;
                }
                case "gc":
                    InternalSendMessage(room,
                        $"Current GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}");
                    GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true);
                    GC.WaitForPendingFinalizers();
                    InternalSendMessage(room,
                        $"GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}");
                    break;
                default:
                    InternalSendMessage(room, $"Command {command} not found!");
                    break;
            }
        }
        catch (Exception ex) {
            InternalSendMessage(room, $"An error occurred: {ex.Message}");
        }
    }

    private async Task HandleImportNhekoProfilesCommand(string[] args, StateEventResponse evt, RoomStore.Room room, UserStore.User user) {
        var msgContent = evt.TypedContent as RoomMessageEventContent;
        var parts = msgContent.Body.Split('\n');

        var data = parts.Where(x => x.Contains(@"\auth\access_token") || x.Contains(@"\auth\home_server")).ToList();
        if (data.Count < 2) {
            InternalSendMessage(room, "Invalid data.");
            return;
        }

        foreach (var line in data) {
            var processedLine = line.Replace("\\\\", "\\").Replace("\\_", "_");

            if (!processedLine.Contains(@"\auth\")) continue;
            var profile = processedLine.Split(@"\auth\")[0];
            if (!user.AuthorizedSessions.ContainsKey(profile))
                user.AuthorizedSessions.Add(profile, new());
            if (processedLine.Contains(@"home_server")) {
                var server = processedLine.Split('=')[1];
                user.AuthorizedSessions[profile].Homeserver = server;
            }
            else if (processedLine.Contains(@"access_token")) {
                var token = processedLine.Split('=')[1];
                user.AuthorizedSessions[profile].AccessToken = token;
            }
        }

        foreach (var (key, session) in user.AuthorizedSessions.ToList()) {
            if (string.IsNullOrWhiteSpace(session.Homeserver) || string.IsNullOrWhiteSpace(session.AccessToken)) {
                InternalSendMessage(room, $"Invalid profile {key}");
                user.AuthorizedSessions.Remove(key);
                continue;
            }

            InternalSendMessage(room, $"Got profile {key} with server {session.AccessToken}");
        }
    }

    private async Task HandleImportCommand(string[] args, StateEventResponse evt, RoomStore.Room room, UserStore.User user) {
        var roomId = args[0];
        var profile = args[1];

        InternalSendMessage(room, $"Importing room {roomId} through profile {profile}...");
        if (!user.AuthorizedSessions.ContainsKey(profile)) {
            InternalSendMessage(room, $"Profile {profile} not found.");
            return;
        }

        var userProfile = user.AuthorizedSessions[profile];

        InternalSendMessage(room, $"Authenticating with {userProfile.Homeserver}...");
        var hs = await hsProvider.GetAuthenticatedWithToken(userProfile.Homeserver, userProfile.AccessToken);
        InternalSendMessage(room, $"Authenticated with {userProfile.Homeserver}.");
        var hsRoom = hs.GetRoom(roomId);

        InternalSendMessage(room, $"Starting import...");
        var internalRoom = new RoomStore.Room(roomId);

        var timeline = hsRoom.GetManyMessagesAsync(limit: int.MaxValue, dir: "b", chunkSize: 100000);
        await foreach (var resp in timeline) {
            internalRoom.Timeline = new(resp.Chunk.AsEnumerable().Reverse().Concat(internalRoom.Timeline));
            InternalSendMessage(room, $"Imported {resp.Chunk.Count} events. Now up to a total of {internalRoom.Timeline.Count} events.");
        }

        InternalSendMessage(room, $"Import complete. Saving and inserting user");
        roomStore.AddRoom(internalRoom);
        internalRoom.AddUser(user.UserId);
        InternalSendMessage(room, $"Import complete. Room is now available.");
    }

#endregion
}