about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-04-05 18:58:32 +0200
committerEmma [it/its]@Rory& <root@rory.gay>2024-04-05 18:58:32 +0200
commit37b97d65c0a5262539a5de560e911048166b8bba (patch)
treef704a9c703b0ec47122a460576e151e0cb06fdc6 /Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs
parentFix merge conficts between machines (diff)
downloadLibMatrix-37b97d65c0a5262539a5de560e911048166b8bba.tar.xz
Fix homeserver resolution, rewrite homeserver initialisation, HSE work
Diffstat (limited to 'Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs')
-rw-r--r--Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs53
1 files changed, 37 insertions, 16 deletions
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs
index d5f4217..7d735f7 100644
--- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs
+++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs
@@ -1,19 +1,20 @@
 using LibMatrix.EventTypes.Spec.State;
 using LibMatrix.HomeserverEmulator.Services;
-using LibMatrix.Responses;
-using LibMatrix.RoomTypes;
-using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
-using Microsoft.OpenApi.Validations.Rules;
 
 namespace LibMatrix.HomeserverEmulator.Controllers.Rooms;
 
 [ApiController]
 [Route("/_matrix/client/{version}/rooms/{roomId}/")]
-public class RoomMembersController(ILogger<RoomMembersController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase {
+public class RoomMembersController(
+    ILogger<RoomMembersController> logger,
+    TokenService tokenService,
+    UserStore userStore,
+    RoomStore roomStore,
+    PaginationTokenResolverService paginationTokenResolver) : ControllerBase {
     [HttpGet("members")]
-    public async Task<List<StateEventResponse>> CreateRoom(string roomId, string? at = null, string? membership = null, string? not_membership = null) {
-        var token = tokenService.GetAccessToken(HttpContext);
+    public async Task<List<StateEventResponse>> GetMembers(string roomId, string? at = null, string? membership = null, string? not_membership = null) {
+        var token = tokenService.GetAccessTokenOrNull(HttpContext);
         if (token == null)
             throw new MatrixException() {
                 ErrorCode = "M_MISSING_TOKEN",
@@ -26,30 +27,50 @@ public class RoomMembersController(ILogger<RoomMembersController> logger, TokenS
                 ErrorCode = "M_UNKNOWN_TOKEN",
                 Error = "No such user"
             };
-        
+
         var room = roomStore.GetRoomById(roomId);
         if (room == null)
             throw new MatrixException() {
                 ErrorCode = "M_NOT_FOUND",
                 Error = "Room not found"
             };
-        
-        var members = room.State.Where(x => x.Type == "m.room.member").ToList();
-        
-        if(membership != null)
+
+        var members = room.Timeline.Where(x => x.Type == "m.room.member" && x.StateKey != null).ToList();
+
+        if (membership != null)
             members = members.Where(x => (x.TypedContent as RoomMemberEventContent)?.Membership == membership).ToList();
-        
-        if(not_membership != null)
+
+        if (not_membership != null)
             members = members.Where(x => (x.TypedContent as RoomMemberEventContent)?.Membership != not_membership).ToList();
 
         if (at != null) {
-            var evt = room.Timeline.FirstOrDefault(x => x.EventId == at);
+            StateEventResponse? evt = null;
+            if (at.StartsWith('$'))
+                evt = await paginationTokenResolver.ResolveTokenToEvent(at, room);
+
+            if (evt is null) {
+                var time = await paginationTokenResolver.ResolveTokenToTimestamp(at);
+                evt = room.Timeline.LastOrDefault(x => x.OriginServerTs <= time);
+                if (evt is null) {
+                    logger.LogWarning("Sent empty list of members for room {roomId} at {at}, because there were no events at this time!", roomId, at);
+                    return [];
+                }
+            }
+            else if (!room.Timeline.Contains(evt)) {
+                evt = room.Timeline.LastOrDefault(x => x.OriginServerTs <= evt.OriginServerTs);
+                if (evt is null) {
+                    logger.LogWarning("Sent empty list of members for room {roomId} at {at}, because there were no events at this time!", roomId, at);
+                    return [];
+                }
+            }
+
+            // evt = room.Timeline.FirstOrDefault(x => x.EventId == at);
             if (evt == null)
                 throw new MatrixException() {
                     ErrorCode = "M_NOT_FOUND",
                     Error = "Event not found"
                 };
-            
+
             members = members.Where(x => x.OriginServerTs <= evt.OriginServerTs).ToList();
         }