about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Controllers
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-02-23 11:23:27 +0000
committerRory& <root@rory.gay>2024-02-23 11:23:27 +0000
commit3dfb7b81b0fe19d37a7bf1183e248ca10c56277c (patch)
tree52c36b45c41e32cddd90dfcd110a393158f55675 /Tests/LibMatrix.HomeserverEmulator/Controllers
parentApply syntax style to LibMatrix side projects (diff)
downloadLibMatrix-3dfb7b81b0fe19d37a7bf1183e248ca10c56277c.tar.xz
HS emulator
Diffstat (limited to '')
-rw-r--r--Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs23
-rw-r--r--Tests/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs18
-rw-r--r--Tests/LibMatrix.HomeserverEmulator/Controllers/UserController.cs81
-rw-r--r--Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs48
-rw-r--r--Tests/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs32
5 files changed, 202 insertions, 0 deletions
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs
new file mode 100644
index 0000000..d0496bf
--- /dev/null
+++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs
@@ -0,0 +1,23 @@
+using System.Text.Json.Nodes;

+using LibMatrix.HomeserverEmulator.Services;

+using LibMatrix.Responses;

+using LibMatrix.Services;

+using Microsoft.AspNetCore.Mvc;

+

+namespace LibMatrix.HomeserverEmulator.Controllers;

+

+[ApiController]

+[Route("/_matrix/client/{version}/")]

+public class AuthController(ILogger<AuthController> logger, UserStore userStore) : ControllerBase {

+    [HttpPost("login")]

+    public async Task<LoginResponse> Login(LoginRequest request) {

+        var user = await userStore.CreateUser($"@{Guid.NewGuid().ToString()}:{Request.Host}", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new Dictionary<string, object>());

+        var loginResponse = new LoginResponse {

+            AccessToken = user.AccessToken,

+            DeviceId = user.DeviceId,

+            UserId = user.UserId

+        };

+

+        return loginResponse;

+    }

+}
\ No newline at end of file
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs
new file mode 100644
index 0000000..0c4d8bd
--- /dev/null
+++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs
@@ -0,0 +1,18 @@
+using LibMatrix.HomeserverEmulator.Services;

+using Microsoft.AspNetCore.Mvc;

+

+namespace LibMatrix.HomeserverEmulator.Controllers;

+

+[ApiController]

+[Route("/_hsEmulator")]

+public class HEDebugController(ILogger<HEDebugController> logger, UserStore userStore, RoomStore roomStore) : ControllerBase {

+    [HttpGet("users")]

+    public async Task<List<UserStore.User>> GetUsers() {

+        return userStore._users;

+    }

+    

+    [HttpGet("rooms")]

+    public async Task<List<RoomStore.Room>> GetRooms() {

+        return roomStore._rooms.ToList();

+    }

+}
\ No newline at end of file
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/UserController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/UserController.cs
new file mode 100644
index 0000000..d763b26
--- /dev/null
+++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/UserController.cs
@@ -0,0 +1,81 @@
+using System.Text.Json.Nodes;

+using ArcaneLibs.Extensions;

+using LibMatrix.HomeserverEmulator.Services;

+using LibMatrix.Responses;

+using Microsoft.AspNetCore.Mvc;

+

+namespace LibMatrix.HomeserverEmulator.Controllers;

+

+[ApiController]

+[Route("/_matrix/client/{version}/")]

+public class UserController(ILogger<UserController> logger, TokenService tokenService, UserStore userStore) : ControllerBase {

+    [HttpGet("account/whoami")]

+    public async Task<WhoAmIResponse> Login() {

+        var token = tokenService.GetAccessToken();

+        if (token is null)

+            throw new MatrixException() {

+                ErrorCode = "M_UNAUTHORIZED",

+                Error = "No token passed."

+            };

+

+        var user = await userStore.GetUserByToken(token, Random.Shared.Next(101) <= 10, tokenService.GenerateServerName());

+        if (user is null)

+            throw new MatrixException() {

+                ErrorCode = "M_UNKNOWN_TOKEN",

+                Error = "Invalid token."

+            };

+        var whoAmIResponse = new WhoAmIResponse {

+            UserId = user.UserId

+        };

+        return whoAmIResponse;

+    }

+    

+    [HttpGet("profile/{userId}")]

+    public async Task<Dictionary<string, object>> GetProfile(string userId) {

+        var user = await userStore.GetUserById(userId, false);

+        if (user is null)

+            throw new MatrixException() {

+                ErrorCode = "M_NOT_FOUND",

+                Error = "User not found."

+            };

+        return user.Profile;

+    }

+    

+    [HttpGet("profile/{userId}/{key}")]

+    public async Task<object> GetProfile(string userId, string key) {

+        var user = await userStore.GetUserById(userId, false);

+        if (user is null)

+            throw new MatrixException() {

+                ErrorCode = "M_NOT_FOUND",

+                Error = "User not found."

+            };

+        if (!user.Profile.TryGetValue(key, out var value))

+            throw new MatrixException() {

+                ErrorCode = "M_NOT_FOUND",

+                Error = "Key not found."

+            };

+        return value;

+    }

+    

+    [HttpGet("joined_rooms")]

+    public async Task<object> GetJoinedRooms() {

+        var token = tokenService.GetAccessToken();

+        if (token is null)

+            throw new MatrixException() {

+                ErrorCode = "M_UNAUTHORIZED",

+                Error = "No token passed."

+            };

+

+        var user = await userStore.GetUserByToken(token, false);

+        if (user is null)

+            throw new MatrixException() {

+                ErrorCode = "M_UNAUTHORIZED",

+                Error = "Invalid token."

+            };

+        // return user.JoinedRooms;

+

+        return new {

+            joined_rooms = user.JoinedRooms

+        };

+    }

+}
\ No newline at end of file
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs
new file mode 100644
index 0000000..1349fac
--- /dev/null
+++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs
@@ -0,0 +1,48 @@
+using System.Text.Json.Nodes;

+using LibMatrix.Homeservers;

+using LibMatrix.Responses;

+using LibMatrix.Services;

+using Microsoft.AspNetCore.Mvc;

+

+namespace LibMatrix.HomeserverEmulator.Controllers;

+

+[ApiController]

+[Route("/_matrix/")]

+public class VersionsController(ILogger<WellKnownController> logger) : ControllerBase {

+    [HttpGet("client/versions")]

+    public async Task<ClientVersionsResponse> GetClientVersions() {

+        var clientVersions = new ClientVersionsResponse {

+            Versions = new() {

+                "r0.0.1",

+                "r0.1.0",

+                "r0.2.0",

+                "r0.3.0",

+                "r0.4.0",

+                "r0.5.0",

+                "r0.6.0",

+                "r0.6.1",

+                "v1.1",

+                "v1.2",

+                "v1.3",

+                "v1.4",

+                "v1.5",

+                "v1.6",

+                "v1.7",

+                "v1.8",

+            },

+            UnstableFeatures = new()

+        };

+        return clientVersions;

+    }

+

+    [HttpGet("federation/v1/version")]

+    public async Task<ServerVersionResponse> GetServerVersions() {

+        var clientVersions = new ServerVersionResponse() {

+            Server = new() {

+                Name = "LibMatrix.HomeserverEmulator",

+                Version = "0.0.0"

+            }

+        };

+        return clientVersions;

+    }

+}
\ No newline at end of file
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs
new file mode 100644
index 0000000..97e460d
--- /dev/null
+++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs
@@ -0,0 +1,32 @@
+using System.Text.Json.Nodes;

+using LibMatrix.Services;

+using Microsoft.AspNetCore.Mvc;

+

+namespace LibMatrix.HomeserverEmulator.Controllers;

+

+[ApiController]

+[Route("/.well-known/matrix/")]

+public class WellKnownController(ILogger<WellKnownController> logger) : ControllerBase {

+    [HttpGet("client")]

+    public JsonObject GetClientWellKnown() {

+        var obj = new JsonObject() {

+            ["m.homeserver"] = new JsonObject() {

+                ["base_url"] = $"{Request.Scheme}://{Request.Host}"

+            }

+        };

+

+        logger.LogInformation("Serving client well-known: {}", obj);

+

+        return obj;

+    }

+    [HttpGet("server")]

+    public JsonObject GetServerWellKnown() {

+        var obj = new JsonObject() {

+            ["m.server"] = $"{Request.Scheme}://{Request.Host}"

+        };

+

+        logger.LogInformation("Serving server well-known: {}", obj);

+

+        return obj;

+    }

+}
\ No newline at end of file