From 2b566a31b68f14d51faae61cbfbe359d0691a890 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 22 Mar 2024 16:38:53 +0000 Subject: Changes --- .../Controllers/Rooms/RoomStateController.cs | 4 +- .../Controllers/Rooms/RoomTimelineController.cs | 52 ++++++++++++++++++++++ .../Controllers/Rooms/RoomsController.cs | 34 +++++++++++++- 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs (limited to 'Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms') diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs index 593f5b0..46c3062 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs @@ -101,6 +101,8 @@ public class RoomStateController(ILogger logger, TokenServi var evt = room.SetStateInternal(request.ToStateEvent(user, room)); evt.Type = eventType; evt.StateKey = stateKey; - return new EventIdResponse(evt); + return new EventIdResponse(){ + EventId = evt.EventId + }; } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs new file mode 100644 index 0000000..c9bdb57 --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs @@ -0,0 +1,52 @@ +using System.Collections.Frozen; +using System.Text.Json.Nodes; +using LibMatrix.HomeserverEmulator.Extensions; +using LibMatrix.HomeserverEmulator.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; + +[ApiController] +[Route("/_matrix/client/{version}/rooms/{roomId}")] +public class RoomTimelineController(ILogger logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpPut("send/{eventType}/{txnId}")] + public async Task SendMessage(string roomId, string eventType, string txnId, [FromBody] JsonObject content) { + var token = tokenService.GetAccessToken(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 == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + if (!room.JoinedMembers.Any(x=>x.StateKey == user.UserId)) + throw new MatrixException() { + ErrorCode = "M_FORBIDDEN", + Error = "User is not in the room" + }; + + var evt = new StateEvent() { + RawContent = content, + Type = eventType + }.ToStateEvent(user, room); + + room.Timeline.Add(evt); + + return new() { + EventId = evt.EventId + }; + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs index e9f52dc..b0f5014 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs @@ -112,7 +112,7 @@ public class RoomsController(ILogger logger, TokenService token ["creator"] = user.UserId } }); - + oldRoom.State.Where(x => eventTypesToTransfer.Contains(x.Type)).ToList().ForEach(x => room.SetStateInternal(x)); room.AddUser(user.UserId); @@ -122,6 +122,38 @@ public class RoomsController(ILogger logger, TokenService token replacement_room = room.RoomId }; } + + [HttpPost("rooms/{roomId}/leave")] // TODO: implement + public async Task LeaveRoom(string roomId) { + var token = tokenService.GetAccessToken(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 == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + // room.RemoveUser(user.UserId); + + // room.SetStateInternal(new StateEventResponse() { }); + + return new { + room_id = room.RoomId + }; + } } public class UpgradeRoomRequest { -- cgit 1.4.1