diff options
Diffstat (limited to 'Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms')
3 files changed, 294 insertions, 0 deletions
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs new file mode 100644 index 0000000..d5f4217 --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs @@ -0,0 +1,58 @@ +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using LibMatrix.RoomTypes; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.OpenApi.Validations.Rules; + +namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; + +[ApiController] +[Route("/_matrix/client/{version}/rooms/{roomId}/")] +public class RoomMembersController(ILogger<RoomMembersController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("members")] + public async Task<List<StateEventResponse>> CreateRoom(string roomId, string? at = null, string? membership = null, string? not_membership = null) { + 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" + }; + + var members = room.State.Where(x => x.Type == "m.room.member").ToList(); + + if(membership != null) + members = members.Where(x => (x.TypedContent as RoomMemberEventContent)?.Membership == membership).ToList(); + + if(not_membership != null) + members = members.Where(x => (x.TypedContent as RoomMemberEventContent)?.Membership != not_membership).ToList(); + + if (at != null) { + var evt = room.Timeline.FirstOrDefault(x => x.EventId == at); + if (evt == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + + members = members.Where(x => x.OriginServerTs <= evt.OriginServerTs).ToList(); + } + + return members; + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs new file mode 100644 index 0000000..593f5b0 --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs @@ -0,0 +1,106 @@ +using System.Collections.Frozen; +using LibMatrix.HomeserverEmulator.Extensions; +using LibMatrix.HomeserverEmulator.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; + +[ApiController] +[Route("/_matrix/client/{version}/rooms/{roomId}/state")] +public class RoomStateController(ILogger<RoomStateController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("")] + public async Task<FrozenSet<StateEventResponse>> GetState(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" + }; + + return room.State; + } + + [HttpGet("{eventType}")] + public async Task<StateEventResponse> GetState(string roomId, string eventType) { + return await GetState(roomId, eventType, ""); + } + + [HttpGet("{eventType}/{stateKey}")] + public async Task<StateEventResponse> GetState(string roomId, string eventType, string stateKey) { + 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" + }; + + var stateEvent = room.State.FirstOrDefault(x => x.Type == eventType && x.StateKey == stateKey); + if (stateEvent == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + return stateEvent; + } + + [HttpPut("{eventType}")] + public async Task<EventIdResponse> SetState(string roomId, string eventType, [FromBody] StateEvent request) { + return await SetState(roomId, eventType, "", request); + } + + [HttpPut("{eventType}/{stateKey}")] + public async Task<EventIdResponse> SetState(string roomId, string eventType, string stateKey, [FromBody] StateEvent request) { + 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" + }; + var evt = room.SetStateInternal(request.ToStateEvent(user, room)); + evt.Type = eventType; + evt.StateKey = stateKey; + return new EventIdResponse(evt); + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs new file mode 100644 index 0000000..e9f52dc --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs @@ -0,0 +1,130 @@ +using System.Text.Json.Serialization; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.EventTypes.Spec.State.RoomInfo; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using LibMatrix.RoomTypes; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class RoomsController(ILogger<RoomsController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + //createRoom + [HttpPost("createRoom")] + public async Task<RoomIdResponse> CreateRoom([FromBody] CreateRoomRequest request) { + 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 = new RoomStore.Room($"!{Guid.NewGuid()}:{tokenService.GenerateServerName(HttpContext)}"); + var createEvent = room.SetStateInternal(new() { + Type = RoomCreateEventContent.EventId, + RawContent = new() { + ["creator"] = user.UserId + } + }); + foreach (var (key, value) in request.CreationContent) { + createEvent.RawContent[key] = value.DeepClone(); + } + + if (!string.IsNullOrWhiteSpace(request.Name)) + room.SetStateInternal(new StateEvent() { + Type = RoomNameEventContent.EventId, + TypedContent = new RoomNameEventContent() { + Name = request.Name + } + }); + + if (!string.IsNullOrWhiteSpace(request.RoomAliasName)) + room.SetStateInternal(new StateEvent() { + Type = RoomCanonicalAliasEventContent.EventId, + TypedContent = new RoomCanonicalAliasEventContent() { + Alias = $"#{request.RoomAliasName}:localhost" + } + }); + + if (request.InitialState is { Count: > 0 }) { + foreach (var stateEvent in request.InitialState) { + room.SetStateInternal(stateEvent); + } + } + + room.AddUser(user.UserId); + + // user.Rooms.Add(room.RoomId, room); + return new() { + RoomId = room.RoomId + }; + } + + [HttpPost("rooms/{roomId}/upgrade")] + public async Task<object> UpgradeRoom(string roomId, [FromBody] UpgradeRoomRequest request) { + 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 oldRoom = roomStore.GetRoomById(roomId); + if (oldRoom == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + var room = new RoomStore.Room($"!{Guid.NewGuid()}:{tokenService.GenerateServerName(HttpContext)}"); + + var eventTypesToTransfer = new[] { + RoomServerACLEventContent.EventId, + RoomEncryptionEventContent.EventId, + RoomNameEventContent.EventId, + RoomAvatarEventContent.EventId, + RoomTopicEventContent.EventId, + RoomGuestAccessEventContent.EventId, + RoomHistoryVisibilityEventContent.EventId, + RoomJoinRulesEventContent.EventId, + RoomPowerLevelEventContent.EventId, + }; + + var createEvent = room.SetStateInternal(new() { + Type = RoomCreateEventContent.EventId, + RawContent = new() { + ["creator"] = user.UserId + } + }); + + oldRoom.State.Where(x => eventTypesToTransfer.Contains(x.Type)).ToList().ForEach(x => room.SetStateInternal(x)); + + room.AddUser(user.UserId); + + // user.Rooms.Add(room.RoomId, room); + return new { + replacement_room = room.RoomId + }; + } +} + +public class UpgradeRoomRequest { + [JsonPropertyName("new_version")] + public required string NewVersion { get; set; } +} \ No newline at end of file |