From 2b566a31b68f14d51faae61cbfbe359d0691a890 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 22 Mar 2024 16:38:53 +0000 Subject: Changes --- .../Controllers/Users/UserController.cs | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'Tests/LibMatrix.HomeserverEmulator/Controllers/Users') diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs index eb2b879..e886d46 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs @@ -1,4 +1,5 @@ using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using ArcaneLibs.Extensions; using LibMatrix.EventTypes.Spec.State; using LibMatrix.Filters; @@ -55,4 +56,43 @@ public class UserController(ILogger logger, TokenService tokenSe ).Select(r => r.RoomId).ToList() }; } + + [HttpGet("devices")] + public async Task GetDevices() { + var token = tokenService.GetAccessToken(HttpContext); + 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 new() { + Devices = user.AccessTokens.Select(x=>new DevicesResponse.Device() { + DeviceId = x.Value.DeviceId, + DisplayName = x.Value.DeviceId + }).ToList() + }; + } + + public class DevicesResponse { + [JsonPropertyName("devices")] + public List Devices { get; set; } + + public class Device { + [JsonPropertyName("device_id")] + public string DeviceId { get; set; } + [JsonPropertyName("display_name")] + public string DisplayName { get; set; } + [JsonPropertyName("last_seen_ip")] + public string LastSeenIp { get; set; } + [JsonPropertyName("last_seen_ts")] + public long LastSeenTs { get; set; } + } + } } \ No newline at end of file -- cgit 1.4.1