From 508c694c3d551cddb3b15c1b0d4787dae3c00530 Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 2 May 2024 07:20:13 +0200 Subject: HomeserverEmulator work --- .../Controllers/Rooms/RoomTimelineController.cs | 116 +++++++++++++++++++++ 1 file changed, 116 insertions(+) (limited to 'Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs') diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs index 3d23660..afd69d1 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs @@ -124,6 +124,122 @@ public class RoomTimelineController( return evt; } + + [HttpGet("relations/{eventId}")] + public async Task 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 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 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> 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() == eventId); + if (dir == "b") { + relatedEvents = relatedEvents.TakeLast(limit ?? 100); + } + else if (dir == "f") { + relatedEvents = relatedEvents.Take(limit ?? 100); + } + + return relatedEvents; + } #region Commands -- cgit 1.4.1