diff options
author | Rory& <root@rory.gay> | 2024-05-30 08:22:50 +0000 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-05-30 08:22:50 +0000 |
commit | 0fa768556aca00f4346ccd71917fad048def6323 (patch) | |
tree | e6835af94759eac7814aa6d1c718d98d37dfc4a9 /Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs | |
parent | Log warning if registering a duplicate type (diff) | |
download | LibMatrix-0fa768556aca00f4346ccd71917fad048def6323.tar.xz |
Move around some projects, further cleanup pending github/dev/project-cleanup dev/project-cleanup
Diffstat (limited to 'Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs')
-rw-r--r-- | Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs new file mode 100644 index 0000000..1fb427e --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs @@ -0,0 +1,56 @@ +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Security.Cryptography; +using System.Text.Json.Nodes; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +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 LegacyController(ILogger<LegacyController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("rooms/{roomId}/initialSync")] + [SuppressMessage("ReSharper.DPA", "DPA0011: High execution time of MVC action", Justification = "Endpoint is expected to wait until data is available or timeout.")] + public async Task<object> Sync([FromRoute] string roomId, [FromQuery] int limit = 20) { + var sw = Stopwatch.StartNew(); + var token = tokenService.GetAccessTokenOrNull(HttpContext); + if (token == null) + throw new MatrixException() { + ErrorCode = "M_MISSING_TOKEN", + Error = "Missing token" + }; + + var user = await userStore.GetUserByToken(token); + if (user == null) + throw new MatrixException() { + ErrorCode = "M_UNKNOWN_TOKEN", + Error = "No such user" + }; + var room = roomStore.GetRoomById(roomId); + if (room is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found." + }; + var accountData = room.AccountData.GetOrCreate(user.UserId, _ => []); + var membership = room.State.FirstOrDefault(x => x.Type == "m.room.member" && x.StateKey == user.UserId); + var timelineChunk = room.Timeline.TakeLast(limit).ToList(); + return new { + account_data = accountData, + membership = (membership?.TypedContent as RoomMemberEventContent)?.Membership ?? "leave", + room_id = room.RoomId, + state = room.State.ToList(), + visibility = "public", + messages = new PaginatedChunkedStateEventResponse() { + Chunk = timelineChunk, + End = timelineChunk.Last().EventId, + Start = timelineChunk.Count >= limit ? timelineChunk.First().EventId : null + } + }; + } +} \ No newline at end of file |