about summary refs log tree commit diff
path: root/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs')
-rw-r--r--Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs33
1 files changed, 33 insertions, 0 deletions
diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs
index 52d5932..b29edf5 100644
--- a/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs
+++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs
@@ -1,5 +1,6 @@
 using System.Text.Json.Nodes;
 using System.Text.Json.Serialization;
+using ArcaneLibs.Extensions;
 using LibMatrix.EventTypes.Spec.State;
 using LibMatrix.HomeserverEmulator.Services;
 using LibMatrix.Homeservers;
@@ -12,6 +13,8 @@ namespace LibMatrix.HomeserverEmulator.Controllers;
 [ApiController]
 [Route("/_matrix/")]
 public class DirectoryController(ILogger<DirectoryController> logger, RoomStore roomStore) : ControllerBase {
+#region Room directory
+
     [HttpGet("client/v3/directory/room/{alias}")]
     public async Task<AliasResult> GetRoomAliasV3(string alias) {
         var match = roomStore._rooms.FirstOrDefault(x =>
@@ -31,4 +34,34 @@ public class DirectoryController(ILogger<DirectoryController> logger, RoomStore
             Servers = servers
         };
     }
+
+#endregion
+
+#region User directory
+
+    [HttpPost("client/v3/user_directory/search")]
+    public async Task<UserDirectoryResponse> SearchUserDirectory([FromBody] UserDirectoryRequest request) {
+        var users = roomStore._rooms
+            .SelectMany(x => x.State.Where(y =>
+                    y.Type == RoomMemberEventContent.EventId
+                    && y.RawContent?["membership"]?.ToString() == "join"
+                    && (y.StateKey!.ContainsAnyCase(request.SearchTerm) || y.RawContent?["displayname"]?.ToString()?.ContainsAnyCase(request.SearchTerm) == true)
+                )
+            )
+            .DistinctBy(x => x.StateKey)
+            .ToList();
+
+        request.Limit ??= 10;
+
+        return new() {
+            Results = users.Select(x => new UserDirectoryResponse.UserDirectoryResult {
+                UserId = x.StateKey!,
+                DisplayName = x.RawContent?["displayname"]?.ToString(),
+                AvatarUrl = x.RawContent?["avatar_url"]?.ToString()
+            }).ToList(),
+            Limited = users.Count > request.Limit
+        };
+    }
+
+#endregion
 }
\ No newline at end of file