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 | |
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')
33 files changed, 2909 insertions, 0 deletions
diff --git a/Utilities/LibMatrix.HomeserverEmulator/.gitignore b/Utilities/LibMatrix.HomeserverEmulator/.gitignore new file mode 100644 index 0000000..8fce603 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/.gitignore @@ -0,0 +1 @@ +data/ diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs new file mode 100644 index 0000000..66548e2 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs @@ -0,0 +1,69 @@ +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, TokenService tokenService) : ControllerBase { + [HttpPost("login")] + public async Task<LoginResponse> Login(LoginRequest request) { + if (!request.Identifier.User.StartsWith('@')) + request.Identifier.User = $"@{request.Identifier.User}:{tokenService.GenerateServerName(HttpContext)}"; + if (request.Identifier.User.EndsWith("localhost")) + request.Identifier.User = request.Identifier.User.Replace("localhost", tokenService.GenerateServerName(HttpContext)); + + var user = await userStore.GetUserById(request.Identifier.User); + if (user is null) { + user = await userStore.CreateUser(request.Identifier.User); + } + + return user.Login(); + } + + [HttpGet("login")] + public async Task<LoginFlowsResponse> GetLoginFlows() { + return new LoginFlowsResponse { + Flows = ((string[]) [ + "m.login.password", + "m.login.recaptcha", + "m.login.sso", + "m.login.email.identity", + "m.login.msisdn", + "m.login.dummy", + "m.login.registration_token", + ]).Select(x => new LoginFlowsResponse.LoginFlow { Type = x }).ToList() + }; + } + + [HttpPost("logout")] + public async Task<object> Logout() { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + if (user == null) + throw new MatrixException() { + ErrorCode = "M_UNKNOWN_TOKEN", + Error = "No such user" + }; + + if (!user.AccessTokens.ContainsKey(token)) + throw new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_NOT_FOUND, + Error = "Token not found" + }; + + user.AccessTokens.Remove(token); + return new { }; + } +} + +public class LoginFlowsResponse { + public required List<LoginFlow> Flows { get; set; } + + public class LoginFlow { + public required string Type { get; set; } + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs new file mode 100644 index 0000000..52d5932 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/DirectoryController.cs @@ -0,0 +1,34 @@ +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Homeservers; +using LibMatrix.Responses; +using LibMatrix.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/")] +public class DirectoryController(ILogger<DirectoryController> logger, RoomStore roomStore) : ControllerBase { + [HttpGet("client/v3/directory/room/{alias}")] + public async Task<AliasResult> GetRoomAliasV3(string alias) { + var match = roomStore._rooms.FirstOrDefault(x => + x.State.Any(y => y.Type == RoomCanonicalAliasEventContent.EventId && y.StateKey == "" && y.RawContent?["alias"]?.ToString() == alias)); + + if (match == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + var servers = match.State.Where(x => x.Type == RoomMemberEventContent.EventId && x.RawContent?["membership"]?.ToString() == "join") + .Select(x => x.StateKey!.Split(':', 2)[1]).ToList(); + + return new() { + RoomId = match.RoomId, + Servers = servers + }; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs new file mode 100644 index 0000000..9e0c17c --- /dev/null +++ b/Utilities/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.ToList(); + } + + [HttpGet("rooms")] + public async Task<List<RoomStore.Room>> GetRooms() { + return roomStore._rooms.ToList(); + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/KeysController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/KeysController.cs new file mode 100644 index 0000000..7898a8c --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/KeysController.cs @@ -0,0 +1,103 @@ +// using System.Security.Cryptography; +// using System.Text.Json.Nodes; +// using System.Text.Json.Serialization; +// 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 KeysController(ILogger<KeysController> logger, TokenService tokenService, UserStore userStore) : ControllerBase { +// [HttpGet("room_keys/version")] +// public async Task<RoomKeysResponse> GetRoomKeys() { +// 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" +// }; +// +// if (user.RoomKeys is not { Count: > 0 }) +// throw new MatrixException() { +// ErrorCode = "M_NOT_FOUND", +// Error = "No keys found" +// }; +// +// return user.RoomKeys.Values.Last(); +// } +// +// [HttpPost("room_keys/version")] +// public async Task<RoomKeysResponse> UploadRoomKeys(RoomKeysRequest 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 roomKeys = new RoomKeysResponse { +// Version = Guid.NewGuid().ToString(), +// Etag = Guid.NewGuid().ToString(), +// Algorithm = request.Algorithm, +// AuthData = request.AuthData +// }; +// user.RoomKeys.Add(roomKeys.Version, roomKeys); +// return roomKeys; +// } +// +// [HttpPost("keys/device_signing/upload")] +// public async Task<object> UploadDeviceSigning(JsonObject 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" +// }; +// +// return new { }; +// } +// } +// +// public class DeviceSigningRequest { +// public CrossSigningKey? MasterKey { get; set; } +// public CrossSigningKey? SelfSigningKey { get; set; } +// public CrossSigningKey? UserSigningKey { get; set; } +// +// public class CrossSigningKey { +// [JsonPropertyName("keys")] +// public Dictionary<string, string> Keys { get; set; } +// +// [JsonPropertyName("signatures")] +// public Dictionary<string, Dictionary<string, string>> Signatures { get; set; } +// +// [JsonPropertyName("usage")] +// public List<string> Usage { get; set; } +// +// [JsonPropertyName("user_id")] +// public string UserId { get; set; } +// } +// } \ No newline at end of file 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 diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs new file mode 100644 index 0000000..7899ada --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs @@ -0,0 +1,99 @@ +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using ArcaneLibs.Collections; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.Media; + +[ApiController] +[Route("/_matrix/media/{version}/")] +public class MediaController( + ILogger<MediaController> logger, + TokenService tokenService, + UserStore userStore, + HSEConfiguration cfg, + HomeserverResolverService hsResolver, + MediaStore mediaStore) + : ControllerBase { + [HttpPost("upload")] + public async Task<object> UploadMedia([FromHeader(Name = "Content-Type")] string ContentType, [FromQuery] string filename, [FromBody] Stream file) { + 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 mediaId = Guid.NewGuid().ToString(); + var media = new { + content_uri = $"mxc://{tokenService.GenerateServerName(HttpContext)}/{mediaId}" + }; + return media; + } + + [HttpGet("download/{serverName}/{mediaId}")] + public async Task DownloadMedia(string serverName, string mediaId) { + var stream = await DownloadRemoteMedia(serverName, mediaId); + await stream.CopyToAsync(Response.Body); + } + + [HttpGet("thumbnail/{serverName}/{mediaId}")] + public async Task DownloadThumbnail(string serverName, string mediaId) { + await DownloadMedia(serverName, mediaId); + } + + [HttpGet("preview_url")] + public async Task<JsonObject> GetPreviewUrl([FromQuery] string url) { + JsonObject data = new(); + + using var hc = new HttpClient(); + using var response = await hc.GetAsync(url); + var doc = await response.Content.ReadAsStringAsync(); + var match = Regex.Match(doc, "<meta property=\"(.*?)\" content=\"(.*?)\""); + + while (match.Success) { + data[match.Groups[1].Value] = match.Groups[2].Value; + match = match.NextMatch(); + } + + return data; + } + + private async Task<Stream> DownloadRemoteMedia(string serverName, string mediaId) { + if (cfg.StoreData) { + var path = Path.Combine(cfg.DataStoragePath, "media", serverName, mediaId); + if (!System.IO.File.Exists(path)) { + var mediaUrl = await hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var client = new HttpClient(); + var stream = await client.GetStreamAsync(mediaUrl); + await using var fs = System.IO.File.Create(path); + await stream.CopyToAsync(fs); + } + return new FileStream(path, FileMode.Open); + } + else { + var mediaUrl = await hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var client = new HttpClient(); + return await client.GetStreamAsync(mediaUrl); + } + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs new file mode 100644 index 0000000..bac803f --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs @@ -0,0 +1,78 @@ +using System.Text.Json.Serialization; +using LibMatrix.HomeserverEmulator.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; + +[ApiController] +[Route("/_matrix/client/{version}/rooms/{roomId}")] +public class RoomAccountDataController(ILogger<RoomAccountDataController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpPost("read_markers")] + public async Task<object> SetReadMarkers(string roomId, [FromBody] ReadMarkersData data) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + 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" + }; + + if (!room.ReadMarkers.ContainsKey(user.UserId)) + room.ReadMarkers[user.UserId] = new(); + + if (data.FullyRead != null) + room.ReadMarkers[user.UserId].FullyRead = data.FullyRead; + if (data.Read != null) + room.ReadMarkers[user.UserId].Read = data.Read; + if (data.ReadPrivate != null) + room.ReadMarkers[user.UserId].ReadPrivate = data.ReadPrivate; + + if (!room.AccountData.ContainsKey(user.UserId)) + room.AccountData[user.UserId] = new(); + + room.AccountData[user.UserId].Add(new StateEventResponse() { + Type = "m.fully_read", + StateKey = user.UserId, + RawContent = new() { + ["event_id"] = data.FullyRead + } + }); + + room.AccountData[user.UserId].Add(new StateEventResponse() { + Type = "m.read", + StateKey = user.UserId, + RawContent = new() { + ["event_id"] = data.Read + } + }); + + room.AccountData[user.UserId].Add(new StateEventResponse() { + Type = "m.read.private", + StateKey = user.UserId, + RawContent = new() { + ["event_id"] = data.ReadPrivate + } + }); + + return data; + } +} + +public class ReadMarkersData { + [JsonPropertyName("m.fully_read")] + public string? FullyRead { get; set; } + + [JsonPropertyName("m.read")] + public string? Read { get; set; } + + [JsonPropertyName("m.read.private")] + public string? ReadPrivate { get; set; } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs new file mode 100644 index 0000000..7d735f7 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs @@ -0,0 +1,79 @@ +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.HomeserverEmulator.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; + +[ApiController] +[Route("/_matrix/client/{version}/rooms/{roomId}/")] +public class RoomMembersController( + ILogger<RoomMembersController> logger, + TokenService tokenService, + UserStore userStore, + RoomStore roomStore, + PaginationTokenResolverService paginationTokenResolver) : ControllerBase { + [HttpGet("members")] + public async Task<List<StateEventResponse>> GetMembers(string roomId, string? at = null, string? membership = null, string? not_membership = null) { + 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 == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + var members = room.Timeline.Where(x => x.Type == "m.room.member" && x.StateKey != null).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) { + StateEventResponse? evt = null; + if (at.StartsWith('$')) + evt = await paginationTokenResolver.ResolveTokenToEvent(at, room); + + if (evt is null) { + var time = await paginationTokenResolver.ResolveTokenToTimestamp(at); + evt = room.Timeline.LastOrDefault(x => x.OriginServerTs <= time); + if (evt is null) { + logger.LogWarning("Sent empty list of members for room {roomId} at {at}, because there were no events at this time!", roomId, at); + return []; + } + } + else if (!room.Timeline.Contains(evt)) { + evt = room.Timeline.LastOrDefault(x => x.OriginServerTs <= evt.OriginServerTs); + if (evt is null) { + logger.LogWarning("Sent empty list of members for room {roomId} at {at}, because there were no events at this time!", roomId, at); + return []; + } + } + + // 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/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs new file mode 100644 index 0000000..74c70a3 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs @@ -0,0 +1,113 @@ +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}/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.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 == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + return room.State; + } + + [HttpGet("{eventType}")] + public async Task<object?> GetState(string roomId, string eventType, [FromQuery] string format = "client") { + return await GetState(roomId, eventType, "", format); + } + + [HttpGet("{eventType}/{stateKey}")] + public async Task<object?> GetState(string roomId, string eventType, string stateKey, [FromQuery] string format = "client") { + 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 == 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) { + Console.WriteLine($"Event not found in room {roomId} matching {eventType}/{stateKey}"); + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + } + + // return stateEvent; + return format == "event" ? stateEvent : stateEvent.RawContent; + } + + [HttpPut("{eventType}")] + public async Task<EventIdResponse> SetState(string roomId, string eventType, [FromBody] JsonObject? request) { + return await SetState(roomId, eventType, "", request); + } + + [HttpPut("{eventType}/{stateKey}")] + public async Task<EventIdResponse> SetState(string roomId, string eventType, string stateKey, [FromBody] JsonObject? request) { + 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 == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + var evt = room.SetStateInternal(new StateEvent() { Type = eventType, StateKey = stateKey, RawContent = request }.ToStateEvent(user, room)); + evt.Type = eventType; + evt.StateKey = stateKey; + return new EventIdResponse() { + EventId = evt.EventId ?? throw new InvalidOperationException("EventId is null") + }; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs new file mode 100644 index 0000000..afd69d1 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs @@ -0,0 +1,432 @@ +using System.Collections.Immutable; +using System.Diagnostics; +using System.Text.Json.Nodes; +using ArcaneLibs; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Helpers; +using LibMatrix.HomeserverEmulator.Extensions; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using LibMatrix.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; + +[ApiController] +[Route("/_matrix/client/{version}/rooms/{roomId}")] +public class RoomTimelineController( + ILogger<RoomTimelineController> logger, + TokenService tokenService, + UserStore userStore, + RoomStore roomStore, + HomeserverProviderService hsProvider) : ControllerBase { + [HttpPut("send/{eventType}/{txnId}")] + public async Task<EventIdResponse> SendMessage(string roomId, string eventType, string txnId, [FromBody] JsonObject content) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + 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); + if (evt.Type == RoomMessageEventContent.EventId && (evt.TypedContent as RoomMessageEventContent).Body.StartsWith("!hse")) + await HandleHseCommand(evt, room, user); + // else + + return new() { + EventId = evt.EventId + }; + } + + [HttpGet("messages")] + public async Task<MessagesResponse> GetMessages(string roomId, [FromQuery] string? from = null, [FromQuery] string? to = null, [FromQuery] int limit = 100, + [FromQuery] string? dir = "b") { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + 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" + }; + + if (dir == "b") { + var timeline = room.Timeline.TakeLast(limit).ToList(); + return new() { + Start = timeline.First().EventId, + End = timeline.Last().EventId, + Chunk = timeline.AsEnumerable().Reverse().ToList(), + State = timeline.GetCalculatedState() + }; + } + else if (dir == "f") { + var timeline = room.Timeline.Take(limit).ToList(); + return new() { + Start = timeline.First().EventId, + End = room.Timeline.Last() == timeline.Last() ? null : timeline.Last().EventId, + Chunk = timeline + }; + } + else + throw new MatrixException() { + ErrorCode = "M_BAD_REQUEST", + Error = $"Invalid direction '{dir}'" + }; + } + + [HttpGet("event/{eventId}")] + public async Task<StateEventResponse> GetEvent(string roomId, string eventId) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + 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 = room.Timeline.SingleOrDefault(x => x.EventId == eventId); + if (evt == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + + return evt; + } + + [HttpGet("relations/{eventId}")] + public async Task<RecursedBatchedChunkedStateEventResponse> GetRelations(string roomId, string eventId, [FromQuery] string? dir = "b", [FromQuery] string? from = null, [FromQuery] int? limit = 100, [FromQuery] bool? recurse = false, [FromQuery] string? to = null) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + 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 = room.Timeline.SingleOrDefault(x => x.EventId == eventId); + if (evt == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + + var matchingEvents = await GetRelationsInternal(roomId, eventId, dir, from, limit, recurse, to); + + return new() { + Chunk = matchingEvents.ToList() + }; + } + + [HttpGet("relations/{eventId}/{relationType}")] + public async Task<RecursedBatchedChunkedStateEventResponse> GetRelations(string roomId, string eventId, string relationType, [FromQuery] string? dir = "b", [FromQuery] string? from = null, [FromQuery] int? limit = 100, [FromQuery] bool? recurse = false, [FromQuery] string? to = null) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + 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 = room.Timeline.SingleOrDefault(x => x.EventId == eventId); + if (evt == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + + var matchingEvents = await GetRelationsInternal(roomId, eventId, dir, from, limit, recurse, to); + + return new() { + Chunk = matchingEvents.ToList() + }; + } + + [HttpGet("relations/{eventId}/{relationType}/{eventType}")] + public async Task<RecursedBatchedChunkedStateEventResponse> GetRelations(string roomId, string eventId, string relationType, string eventType, [FromQuery] string? dir = "b", [FromQuery] string? from = null, [FromQuery] int? limit = 100, [FromQuery] bool? recurse = false, [FromQuery] string? to = null) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + 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 = room.Timeline.SingleOrDefault(x => x.EventId == eventId); + if (evt == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + + var matchingEvents = await GetRelationsInternal(roomId, eventId, dir, from, limit, recurse, to); + + return new() { + Chunk = matchingEvents.ToList() + }; + } + + private async Task<IEnumerable<StateEventResponse>> GetRelationsInternal(string roomId, string eventId, string dir, string? from, int? limit, bool? recurse, string? to) { + var room = roomStore.GetRoomById(roomId); + var evt = room.Timeline.SingleOrDefault(x => x.EventId == eventId); + if (evt == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + + var relatedEvents = room.Timeline.Where(x => x.RawContent?["m.relates_to"]?["event_id"]?.GetValue<string>() == eventId); + if (dir == "b") { + relatedEvents = relatedEvents.TakeLast(limit ?? 100); + } + else if (dir == "f") { + relatedEvents = relatedEvents.Take(limit ?? 100); + } + + return relatedEvents; + } + +#region Commands + + private void InternalSendMessage(RoomStore.Room room, string content) { + InternalSendMessage(room, new MessageBuilder().WithBody(content).Build()); + } + + private void InternalSendMessage(RoomStore.Room room, RoomMessageEventContent content) { + logger.LogInformation("Sending internal message: {content}", content.Body); + room.Timeline.Add(new StateEventResponse() { + Type = RoomMessageEventContent.EventId, + TypedContent = content, + Sender = $"@hse:{tokenService.GenerateServerName(HttpContext)}", + RoomId = room.RoomId, + EventId = "$" + string.Join("", Random.Shared.GetItems("abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789".ToCharArray(), 100)), + OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds() + }); + } + + private async Task HandleHseCommand(StateEventResponse evt, RoomStore.Room room, UserStore.User user) { + try { + var msgContent = evt.TypedContent as RoomMessageEventContent; + var parts = msgContent.Body.Split('\n')[0].Split(" "); + if (parts.Length < 2) return; + + var command = parts[1]; + switch (command) { + case "import": + await HandleImportCommand(parts[2..], evt, room, user); + break; + case "import-nheko-profiles": + await HandleImportNhekoProfilesCommand(parts[2..], evt, room, user); + break; + case "clear-sync-states": + foreach (var (token, session) in user.AccessTokens) { + session.SyncStates.Clear(); + InternalSendMessage(room, $"Cleared sync states for {token}."); + } + + break; + case "rsp": { + await Task.Delay(1000); + var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789"; + for (int i = 0; i < 10000; i++) { + // await Task.Delay(100); + // InternalSendMessage(room, $"https://music.youtube.com/watch?v=90oZtyvavSk&i={i}"); + var url = $"https://music.youtube.com/watch?v="; + for (int j = 0; j < 11; j++) { + url += chars[Random.Shared.Next(chars.Length)]; + } + + InternalSendMessage(room, url + "&i=" + i); + if (i % 5000 == 0 || i == 9999) { + Thread.Sleep(5000); + + do { + InternalSendMessage(room, + $"Current GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}"); + GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true); + GC.WaitForPendingFinalizers(); + InternalSendMessage(room, + $"GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}"); + await Task.Delay(5000); + } while (Process.GetCurrentProcess().WorkingSet64 >= 1_024_000_000); + } + } + break; + } + case "genrooms": { + var sw = Stopwatch.StartNew(); + var count = 1000; + for (int i = 0; i < count; i++) { + var crq = new CreateRoomRequest() { + Name = "Test room", + CreationContent = new() { + ["version"] = "11" + }, + InitialState = [] + }; + + if (Random.Shared.Next(100) > 75) { + crq.CreationContent["type"] = "m.space"; + foreach (var item in Random.Shared.GetItems(roomStore._rooms.ToArray(), 50)) { + crq.InitialState!.Add(new StateEvent() { + Type = "m.space.child", + StateKey = item.RoomId, + TypedContent = new SpaceChildEventContent() { + Suggested = true, + AutoJoin = true, + Via = new List<string>() + } + }.ToStateEvent(user, room)); + } + } + var newRoom = roomStore.CreateRoom(crq); + newRoom.AddUser(user.UserId); + } + InternalSendMessage(room, $"Generated {count} new rooms in {sw.Elapsed}!"); + break; + } + case "gc": + InternalSendMessage(room, + $"Current GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}"); + GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true); + GC.WaitForPendingFinalizers(); + InternalSendMessage(room, + $"GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}"); + break; + default: + InternalSendMessage(room, $"Command {command} not found!"); + break; + } + } + catch (Exception ex) { + InternalSendMessage(room, $"An error occurred: {ex.Message}"); + } + } + + private async Task HandleImportNhekoProfilesCommand(string[] args, StateEventResponse evt, RoomStore.Room room, UserStore.User user) { + var msgContent = evt.TypedContent as RoomMessageEventContent; + var parts = msgContent.Body.Split('\n'); + + var data = parts.Where(x => x.Contains(@"\auth\access_token") || x.Contains(@"\auth\home_server")).ToList(); + if (data.Count < 2) { + InternalSendMessage(room, "Invalid data."); + return; + } + + foreach (var line in data) { + var processedLine = line.Replace("\\\\", "\\").Replace("\\_", "_"); + + if (!processedLine.Contains(@"\auth\")) continue; + var profile = processedLine.Split(@"\auth\")[0]; + if (!user.AuthorizedSessions.ContainsKey(profile)) + user.AuthorizedSessions.Add(profile, new()); + if (processedLine.Contains(@"home_server")) { + var server = processedLine.Split('=')[1]; + user.AuthorizedSessions[profile].Homeserver = server; + } + else if (processedLine.Contains(@"access_token")) { + var token = processedLine.Split('=')[1]; + user.AuthorizedSessions[profile].AccessToken = token; + } + } + + foreach (var (key, session) in user.AuthorizedSessions.ToList()) { + if (string.IsNullOrWhiteSpace(session.Homeserver) || string.IsNullOrWhiteSpace(session.AccessToken)) { + InternalSendMessage(room, $"Invalid profile {key}"); + user.AuthorizedSessions.Remove(key); + continue; + } + + InternalSendMessage(room, $"Got profile {key} with server {session.AccessToken}"); + } + } + + private async Task HandleImportCommand(string[] args, StateEventResponse evt, RoomStore.Room room, UserStore.User user) { + var roomId = args[0]; + var profile = args[1]; + + InternalSendMessage(room, $"Importing room {roomId} through profile {profile}..."); + if (!user.AuthorizedSessions.ContainsKey(profile)) { + InternalSendMessage(room, $"Profile {profile} not found."); + return; + } + + var userProfile = user.AuthorizedSessions[profile]; + + InternalSendMessage(room, $"Authenticating with {userProfile.Homeserver}..."); + var hs = await hsProvider.GetAuthenticatedWithToken(userProfile.Homeserver, userProfile.AccessToken); + InternalSendMessage(room, $"Authenticated with {userProfile.Homeserver}."); + var hsRoom = hs.GetRoom(roomId); + + InternalSendMessage(room, $"Starting import..."); + var internalRoom = new RoomStore.Room(roomId); + + var timeline = hsRoom.GetManyMessagesAsync(limit: int.MaxValue, dir: "b", chunkSize: 100000); + await foreach (var resp in timeline) { + internalRoom.Timeline = new(resp.Chunk.AsEnumerable().Reverse().Concat(internalRoom.Timeline)); + InternalSendMessage(room, $"Imported {resp.Chunk.Count} events. Now up to a total of {internalRoom.Timeline.Count} events."); + } + + InternalSendMessage(room, $"Import complete. Saving and inserting user"); + roomStore.AddRoom(internalRoom); + internalRoom.AddUser(user.UserId); + InternalSendMessage(room, $"Import complete. Room is now available."); + } + +#endregion +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs new file mode 100644 index 0000000..c24e6e9 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs @@ -0,0 +1,139 @@ +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.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 = new RoomStore.Room($"!{Guid.NewGuid()}:{tokenService.GenerateServerName(HttpContext)}"); + var room = roomStore.CreateRoom(request, user); + + return new() { + RoomId = room.RoomId + }; + } + + [HttpPost("rooms/{roomId}/upgrade")] + public async Task<object> UpgradeRoom(string roomId, [FromBody] UpgradeRoomRequest request) { + 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 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 ReasonBody { + [JsonPropertyName("reason")] + public string? Reason { get; set; } + } + [HttpPost("rooms/{roomId}/leave")] // TODO: implement + public async Task<object> LeaveRoom(string roomId, [FromBody] ReasonBody body) { + 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 == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + room.SetStateInternal(new() { + Type = RoomMemberEventContent.EventId, + TypedContent = new RoomMemberEventContent() { + Membership = "leave", + Reason = body.Reason + }, + StateKey = user.UserId + }); + + logger.LogTrace($"User {user.UserId} left room {room.RoomId}"); + return new { + room_id = room.RoomId + }; + } +} + +public class UpgradeRoomRequest { + [JsonPropertyName("new_version")] + public required string NewVersion { get; set; } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs new file mode 100644 index 0000000..024d071 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs @@ -0,0 +1,272 @@ +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.HomeserverEmulator.Extensions; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class SyncController(ILogger<SyncController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore, HSEConfiguration cfg) : ControllerBase { + [HttpGet("sync")] + [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<SyncResponse> Sync([FromQuery] string? since = null, [FromQuery] int? timeout = 5000) { + var sw = Stopwatch.StartNew(); + var token = tokenService.GetAccessToken(HttpContext); + + var user = await userStore.GetUserByToken(token); + if (user == null) + throw new MatrixException() { + ErrorCode = "M_UNKNOWN_TOKEN", + Error = "No such user" + }; + var session = user.AccessTokens[token]; + UserStore.User.SessionInfo.UserSyncState newSyncState = new(); + + SyncResponse syncResp; + if (string.IsNullOrWhiteSpace(since) || !session.SyncStates.ContainsKey(since)) + syncResp = InitialSync(user, session); + else { + var syncState = session.SyncStates[since]; + newSyncState = syncState.Clone(); + + var newSyncToken = Guid.NewGuid().ToString(); + do { + syncResp = IncrementalSync(user, session, syncState); + syncResp.NextBatch = newSyncToken; + } while (!await HasDataOrStall(syncResp) && sw.ElapsedMilliseconds < timeout); + + if (sw.ElapsedMilliseconds > timeout) { + logger.LogTrace("Sync timed out after {Elapsed}", sw.Elapsed); + return new() { + NextBatch = since + }; + } + } + + session.SyncStates[syncResp.NextBatch] = RecalculateSyncStates(newSyncState, syncResp); + logger.LogTrace("Responding to sync after {totalElapsed}", sw.Elapsed); + return syncResp; + } + + private UserStore.User.SessionInfo.UserSyncState RecalculateSyncStates(UserStore.User.SessionInfo.UserSyncState newSyncState, SyncResponse sync) { + logger.LogTrace("Updating sync state"); + var syncStateRecalcSw = Stopwatch.StartNew(); + foreach (var (roomId, roomData) in sync.Rooms?.Join ?? []) { + if (!newSyncState.RoomPositions.ContainsKey(roomId)) + newSyncState.RoomPositions[roomId] = new(); + var state = newSyncState.RoomPositions[roomId]; + + state.TimelinePosition += roomData.Timeline?.Events?.Count ?? 0; + state.LastTimelineEventId = roomData.Timeline?.Events?.LastOrDefault()?.EventId ?? state.LastTimelineEventId; + state.AccountDataPosition += roomData.AccountData?.Events?.Count ?? 0; + state.Joined = true; + } + + foreach (var (roomId, _) in sync.Rooms?.Invite ?? []) { + if (!newSyncState.RoomPositions.ContainsKey(roomId)) + newSyncState.RoomPositions[roomId] = new() { + Joined = false + }; + } + + foreach (var (roomId, _) in sync.Rooms?.Leave ?? []) { + if (newSyncState.RoomPositions.ContainsKey(roomId)) + newSyncState.RoomPositions.Remove(roomId); + } + + logger.LogTrace("Updated sync state in {Elapsed}", syncStateRecalcSw.Elapsed); + + return newSyncState; + } + +#region Initial Sync parts + + private SyncResponse InitialSync(UserStore.User user, UserStore.User.SessionInfo session) { + var response = new SyncResponse() { + NextBatch = Guid.NewGuid().ToString(), + DeviceOneTimeKeysCount = new(), + AccountData = new(events: user.AccountData.ToList()) + }; + + session.SyncStates.Add(response.NextBatch, new()); + + var rooms = roomStore._rooms.Where(x => x.State.Any(y => y.Type == "m.room.member" && y.StateKey == user.UserId)).ToList(); + foreach (var room in rooms) { + response.Rooms ??= new(); + response.Rooms.Join ??= new(); + + response.Rooms.Join[room.RoomId] = new() { + State = new(room.State.ToList()), + Timeline = new(events: room.Timeline.ToList(), limited: false), + AccountData = new(room.AccountData.GetOrCreate(user.UserId, _ => []).ToList()) + }; + } + + return response; + } + + private SyncResponse.RoomsDataStructure.JoinedRoomDataStructure GetInitialSyncRoomData(RoomStore.Room room, UserStore.User user) { + return new() { + State = new(room.State.ToList()), + Timeline = new(room.Timeline.ToList(), false), + AccountData = new(room.AccountData.GetOrCreate(user.UserId, _ => []).ToList()) + }; + } + +#endregion + + private SyncResponse IncrementalSync(UserStore.User user, UserStore.User.SessionInfo session, UserStore.User.SessionInfo.UserSyncState syncState) { + return new SyncResponse { + Rooms = GetIncrementalSyncRooms(user, session, syncState) + }; + } + +#region Incremental Sync parts + + private SyncResponse.RoomsDataStructure GetIncrementalSyncRooms(UserStore.User user, UserStore.User.SessionInfo session, UserStore.User.SessionInfo.UserSyncState syncState) { + SyncResponse.RoomsDataStructure data = new() { + Join = [], + Invite = [], + Leave = [] + }; + + // step 1: check previously synced rooms + foreach (var (roomId, roomPosition) in syncState.RoomPositions) { + var room = roomStore.GetRoomById(roomId); + if (room == null) { + // room no longer exists + data.Leave[roomId] = new(); + continue; + } + + if (roomPosition.Joined) { + var newTimelineEvents = room.Timeline.Skip(roomPosition.TimelinePosition).ToList(); + var newAccountDataEvents = room.AccountData[user.UserId].Skip(roomPosition.AccountDataPosition).ToList(); + if (newTimelineEvents.Count == 0 && newAccountDataEvents.Count == 0) continue; + data.Join[room.RoomId] = new() { + State = new(newTimelineEvents.GetCalculatedState()), + Timeline = new(newTimelineEvents, false) + }; + } + } + + if (data.Join.Count > 0) return data; + + // step 2: check newly joined rooms + var untrackedRooms = roomStore._rooms.Where(r => !syncState.RoomPositions.ContainsKey(r.RoomId)).ToList(); + + var allJoinedRooms = roomStore.GetRoomsByMember(user.UserId).ToArray(); + if (allJoinedRooms.Length == 0) return data; + var rooms = Random.Shared.GetItems(allJoinedRooms, Math.Min(allJoinedRooms.Length, 50)); + foreach (var membership in rooms) { + var membershipContent = membership.TypedContent as RoomMemberEventContent ?? + throw new InvalidOperationException("Membership event content is not RoomMemberEventContent"); + var room = roomStore.GetRoomById(membership.RoomId!); + //handle leave + if (syncState.RoomPositions.TryGetValue(membership.RoomId!, out var syncPosition)) { + // logger.LogTrace("Found sync position {roomId} {value}", room.RoomId, syncPosition.ToJson(indent: false, ignoreNull: false)); + + if (membershipContent.Membership == "join") { + var newTimelineEvents = room.Timeline.Skip(syncPosition.TimelinePosition).ToList(); + var newAccountDataEvents = room.AccountData[user.UserId].Skip(syncPosition.AccountDataPosition).ToList(); + if (newTimelineEvents.Count == 0 && newAccountDataEvents.Count == 0) continue; + data.Join[room.RoomId] = new() { + State = new(newTimelineEvents.GetCalculatedState()), + Timeline = new(newTimelineEvents, false) + }; + } + } + else { + //syncPosisition = null + if (membershipContent.Membership == "join") { + var joinData = data.Join[membership.RoomId!] = new() { + State = new(room.State.ToList()), + Timeline = new(events: room.Timeline.ToList(), limited: false), + AccountData = new(room.AccountData.GetOrCreate(user.UserId, _ => []).ToList()) + }; + } + } + } + + //handle nonexistant rooms + foreach (var roomId in syncState.RoomPositions.Keys) { + if (!roomStore._rooms.Any(x => x.RoomId == roomId)) { + data.Leave[roomId] = new(); + session.SyncStates[session.SyncStates.Last().Key].RoomPositions.Remove(roomId); + } + } + + return data; + } + +#endregion + + private async Task<bool> HasDataOrStall(SyncResponse resp) { + // logger.LogTrace("Checking if sync response has data: {resp}", resp.ToJson(indent: false, ignoreNull: true)); + // if (resp.AccountData?.Events?.Count > 0) return true; + // if (resp.Rooms?.Invite?.Count > 0) return true; + // if (resp.Rooms?.Join?.Count > 0) return true; + // if (resp.Rooms?.Leave?.Count > 0) return true; + // if (resp.Presence?.Events?.Count > 0) return true; + // if (resp.DeviceLists?.Changed?.Count > 0) return true; + // if (resp.DeviceLists?.Left?.Count > 0) return true; + // if (resp.ToDevice?.Events?.Count > 0) return true; + // + // var hasData = + // resp is not { + // AccountData: null or { + // Events: null or { Count: 0 } + // }, + // Rooms: null or { + // Invite: null or { Count: 0 }, + // Join: null or { Count: 0 }, + // Leave: null or { Count: 0 } + // }, + // Presence: null or { + // Events: null or { Count: 0 } + // }, + // DeviceLists: null or { + // Changed: null or { Count: 0 }, + // Left: null or { Count: 0 } + // }, + // ToDevice: null or { + // Events: null or { Count: 0 } + // } + // }; + + var hasData = resp is { + AccountData: { + Events: { Count: > 0 } + } + } or { + Presence: { + Events: { Count: > 0 } + } + } or { + DeviceLists: { + Changed: { Count: > 0 }, + Left: { Count: > 0 } + } + } or { + ToDevice: { + Events: { Count: > 0 } + } + }; + + if (!hasData) { + // hasData = + } + + if (!hasData) { + // logger.LogDebug($"Sync response has no data, stalling for 1000ms: {resp.ToJson(indent: false, ignoreNull: true)}"); + await Task.Delay(10); + } + + return hasData; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/ThirdParty/ThirdPartyController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/ThirdParty/ThirdPartyController.cs new file mode 100644 index 0000000..ff006df --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/ThirdParty/ThirdPartyController.cs @@ -0,0 +1,25 @@ +using LibMatrix.HomeserverEmulator.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.ThirdParty; + +[ApiController] +[Route("/_matrix/client/{version}/thirdparty/")] +public class ThirdPartyController(ILogger<ThirdPartyController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("protocols")] + public async Task<object> GetProtocols() { + return new { }; + } + + [HttpGet("location")] + public async Task<List<object>> GetLocations([FromQuery] string alias) { + // TODO: implement + return []; + } + + [HttpGet("location/{protocol}")] + public async Task<List<object>> GetLocation([FromRoute] string protocol) { + // TODO: implement + return []; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/AccountDataController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/AccountDataController.cs new file mode 100644 index 0000000..a32d283 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/AccountDataController.cs @@ -0,0 +1,68 @@ +using System.Text.Json.Nodes; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Filters; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class AccountDataController(ILogger<AccountDataController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("user/{mxid}/account_data/{type}")] + public async Task<object> GetAccountData(string type) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + + var value = user.AccountData.FirstOrDefault(x => x.Type == type); + if (value is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Key not found." + }; + return value; + } + + [HttpPut("user/{mxid}/account_data/{type}")] + public async Task<object> SetAccountData(string type, [FromBody] JsonObject data) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + + user.AccountData.Add(new() { + Type = type, + RawContent = data + }); + return data; + } + + // specialised account data... + [HttpGet("pushrules")] + public async Task<object> GetPushRules() { + 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 { }; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs new file mode 100644 index 0000000..bdee7bc --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs @@ -0,0 +1,46 @@ +using System.Text.Json.Nodes; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Filters; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class FilterController(ILogger<FilterController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpPost("user/{mxid}/filter")] + public async Task<object> CreateFilter(string mxid, [FromBody] SyncFilter filter) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + var filterId = Guid.NewGuid().ToString(); + user.Filters[filterId] = filter; + return new { + filter_id = filterId + }; + } + + [HttpGet("user/{mxid}/filter/{filterId}")] + public async Task<SyncFilter> GetFilter(string mxid, string filterId) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + if (!user.Filters.ContainsKey(filterId)) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Filter not found." + }; + return user.Filters[filterId]; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/ProfileController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/ProfileController.cs new file mode 100644 index 0000000..98c41da --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/ProfileController.cs @@ -0,0 +1,52 @@ +using System.Text.Json.Nodes; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Filters; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class ProfileController(ILogger<ProfileController> logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("profile/{userId}")] + public async Task<IDictionary<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; + } + + [HttpPut("profile/{userId}/{key}")] + public async Task<object> SetProfile(string userId, string key, [FromBody] JsonNode value) { + var user = await userStore.GetUserById(userId, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "User not found." + }; + user.Profile[key] = value[key]?.AsObject(); + return value; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs new file mode 100644 index 0000000..2be3896 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs @@ -0,0 +1,80 @@ +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Filters; +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, RoomStore roomStore) : ControllerBase { + [HttpGet("account/whoami")] + public async Task<WhoAmIResponse> Login() { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token, Random.Shared.Next(101) <= 10, tokenService.GenerateServerName(HttpContext)); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNKNOWN_TOKEN", + Error = "Invalid token." + }; + var whoAmIResponse = new WhoAmIResponse { + UserId = user.UserId + }; + return whoAmIResponse; + } + + [HttpGet("joined_rooms")] + public async Task<object> GetJoinedRooms() { + var token = tokenService.GetAccessToken(HttpContext); + 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 = roomStore._rooms.Where(r => + r.State.Any(s => s.StateKey == user.UserId && s.Type == RoomMemberEventContent.EventId && (s.TypedContent as RoomMemberEventContent).Membership == "join") + ).Select(r => r.RoomId).ToList() + }; + } + + [HttpGet("devices")] + public async Task<DevicesResponse> GetDevices() { + var token = tokenService.GetAccessToken(HttpContext); + 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<Device> 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 diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs new file mode 100644 index 0000000..0c3bde6 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs @@ -0,0 +1,96 @@ +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +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; + } + + [HttpGet("client/{version}/capabilities")] + public async Task<CapabilitiesResponse> GetCapabilities() { + var caps = new CapabilitiesResponse() { + Capabilities = new() { + ChangePassword = new() { + Enabled = false + }, + RoomVersions = new() { + Default = "11", + Available = [] + } + } + }; + + for (int i = 0; i < 15; i++) { + caps.Capabilities.RoomVersions.Available.Add(i.ToString(), "stable"); + } + + return caps; + } +} + +public class CapabilitiesResponse { + [JsonPropertyName("capabilities")] + public CapabilitiesContent Capabilities { get; set; } + + public class CapabilitiesContent { + [JsonPropertyName("m.room_versions")] + public RoomVersionsContent RoomVersions { get; set; } + + [JsonPropertyName("m.change_password")] + public ChangePasswordContent ChangePassword { get; set; } + + public class ChangePasswordContent { + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } + } + + public class RoomVersionsContent { + [JsonPropertyName("default")] + public string Default { get; set; } + + [JsonPropertyName("available")] + public Dictionary<string, string> Available { get; set; } + } + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs b/Utilities/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs new file mode 100644 index 0000000..97e460d --- /dev/null +++ b/Utilities/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 diff --git a/Utilities/LibMatrix.HomeserverEmulator/Extensions/EventExtensions.cs b/Utilities/LibMatrix.HomeserverEmulator/Extensions/EventExtensions.cs new file mode 100644 index 0000000..d938b1b --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Extensions/EventExtensions.cs @@ -0,0 +1,24 @@ +using LibMatrix.HomeserverEmulator.Services; + +namespace LibMatrix.HomeserverEmulator.Extensions; + +public static class EventExtensions { + public static StateEventResponse ToStateEvent(this StateEvent stateEvent, UserStore.User user, RoomStore.Room room) { + return new StateEventResponse { + RawContent = stateEvent.RawContent, + EventId = "$" + string.Join("", Random.Shared.GetItems("abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789".ToCharArray(), 100)), + RoomId = room.RoomId, + Sender = user.UserId, + StateKey = stateEvent.StateKey, + Type = stateEvent.Type, + OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds() + }; + } + + public static List<StateEventResponse> GetCalculatedState(this IEnumerable<StateEventResponse> events) { + return events.Where(s => s.StateKey != null) + .OrderByDescending(s => s.OriginServerTs) + .DistinctBy(x => (x.Type, x.StateKey)) + .ToList(); + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj b/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj new file mode 100644 index 0000000..0a43299 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj @@ -0,0 +1,23 @@ +<Project Sdk="Microsoft.NET.Sdk.Web"> + + <PropertyGroup> + <TargetFramework>net8.0</TargetFramework> + <Nullable>enable</Nullable> + <ImplicitUsings>enable</ImplicitUsings> + <InvariantGlobalization>true</InvariantGlobalization> + <LangVersion>preview</LangVersion> + <GenerateDocumentationFile>true</GenerateDocumentationFile> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="EasyCompressor.LZMA" Version="1.4.0"/> + <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0"/> + <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\LibMatrix\LibMatrix.csproj"/> +<!-- <ProjectReference Include="..\..\Utilities\LibMatrix.Utilities.Bot\LibMatrix.Utilities.Bot.csproj"/>--> + </ItemGroup> + +</Project> diff --git a/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.http b/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.http new file mode 100644 index 0000000..01d5fcc --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.http @@ -0,0 +1,6 @@ +@LibMatrix.HomeserverEmulator_HostAddress = http://localhost:5298 + +GET {{LibMatrix.HomeserverEmulator_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Utilities/LibMatrix.HomeserverEmulator/Program.cs b/Utilities/LibMatrix.HomeserverEmulator/Program.cs new file mode 100644 index 0000000..9ea6fce --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Program.cs @@ -0,0 +1,117 @@ +using System.Net.Mime; +using System.Text.Json.Serialization; +using LibMatrix; +using LibMatrix.HomeserverEmulator.Extensions; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Services; +using Microsoft.AspNetCore.Diagnostics; +using Microsoft.AspNetCore.Http.Timeouts; +using Microsoft.AspNetCore.Mvc; +using Microsoft.OpenApi.Models; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.Configure<ApiBehaviorOptions>(options => +{ + options.SuppressModelStateInvalidFilter = true; +}); + +builder.Services.AddControllers(options => { + options.MaxValidationDepth = null; + options.MaxIAsyncEnumerableBufferLimit = 100; +}).AddJsonOptions(options => { options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; }); + +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(c => { + c.SwaggerDoc("v1", new OpenApiInfo() { + Version = "v1", + Title = "Rory&::LibMatrix.HomeserverEmulator", + Description = "Partial Matrix homeserver implementation" + }); + c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "LibMatrix.HomeserverEmulator.xml")); +}); + +builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); +builder.Services.AddSingleton<HSEConfiguration>(); +builder.Services.AddSingleton<UserStore>(); +builder.Services.AddSingleton<RoomStore>(); +builder.Services.AddSingleton<MediaStore>(); + +builder.Services.AddScoped<TokenService>(); + +builder.Services.AddSingleton<HomeserverProviderService>(); +builder.Services.AddSingleton<HomeserverResolverService>(); +builder.Services.AddSingleton<PaginationTokenResolverService>(); + +builder.Services.AddRequestTimeouts(x => { + x.DefaultPolicy = new RequestTimeoutPolicy { + Timeout = TimeSpan.FromMinutes(10), + WriteTimeoutResponse = async context => { + context.Response.StatusCode = 504; + context.Response.ContentType = "application/json"; + await context.Response.StartAsync(); + await context.Response.WriteAsJsonAsync(new MatrixException() { + ErrorCode = "M_TIMEOUT", + Error = "Request timed out" + }.GetAsJson()); + await context.Response.CompleteAsync(); + } + }; +}); +builder.Services.AddCors(options => { + options.AddPolicy( + "Open", + policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); +}); +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment() || true) { + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseCors("Open"); +app.UseExceptionHandler(exceptionHandlerApp => { + exceptionHandlerApp.Run(async context => { + var exceptionHandlerPathFeature = + context.Features.Get<IExceptionHandlerPathFeature>(); + if (exceptionHandlerPathFeature?.Error is not null) + Console.WriteLine(exceptionHandlerPathFeature.Error.ToString()!); + + if (exceptionHandlerPathFeature?.Error is MatrixException mxe) { + context.Response.StatusCode = mxe.ErrorCode switch { + "M_NOT_FOUND" => StatusCodes.Status404NotFound, + "M_UNAUTHORIZED" => StatusCodes.Status401Unauthorized, + _ => StatusCodes.Status500InternalServerError + }; + context.Response.ContentType = MediaTypeNames.Application.Json; + await context.Response.WriteAsync(mxe.GetAsJson()!); + } + else { + context.Response.StatusCode = StatusCodes.Status500InternalServerError; + context.Response.ContentType = MediaTypeNames.Application.Json; + await context.Response.WriteAsync(new MatrixException() { + ErrorCode = "M_UNKNOWN", + Error = exceptionHandlerPathFeature?.Error.ToString() + }.GetAsJson()); + } + }); +}); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Map("/_matrix/{*_}", (HttpContext ctx, ILogger<Program> logger) => { + logger.LogWarning("Client hit non-existing route: {Method} {Path}{Query}", ctx.Request.Method, ctx.Request.Path, ctx.Request.Query); + // Console.WriteLine($"Client hit non-existing route: {ctx.Request.Method} {ctx.Request.Path}"); + ctx.Response.StatusCode = StatusCodes.Status404NotFound; + ctx.Response.ContentType = MediaTypeNames.Application.Json; + return ctx.Response.WriteAsJsonAsync(new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_UNRECOGNISED, + Error = "Endpoint not implemented" + }); +}); + +app.Run(); \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Properties/launchSettings.json b/Utilities/LibMatrix.HomeserverEmulator/Properties/launchSettings.json new file mode 100644 index 0000000..4ddf341 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Properties/launchSettings.json @@ -0,0 +1,32 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:9169", + "sslPort": 44321 + } + }, + "profiles": { + "Development": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5298", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Local": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5298", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Local" + } + } + } +} diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs new file mode 100644 index 0000000..73b0d23 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs @@ -0,0 +1,57 @@ +using System.Collections; +using System.Diagnostics.CodeAnalysis; +using ArcaneLibs.Extensions; + +namespace LibMatrix.HomeserverEmulator.Services; + +public class HSEConfiguration { + private static ILogger<HSEConfiguration> _logger; + public static HSEConfiguration Current { get; set; } + + [RequiresUnreferencedCode("Uses reflection binding")] + public HSEConfiguration(ILogger<HSEConfiguration> logger, IConfiguration config, HostBuilderContext host) { + Current = this; + _logger = logger; + logger.LogInformation("Loading configuration for environment: {}...", host.HostingEnvironment.EnvironmentName); + config.GetSection("HomeserverEmulator").Bind(this); + if (StoreData) { + DataStoragePath = ExpandPath(DataStoragePath ?? throw new NullReferenceException("DataStoragePath is not set")); + CacheStoragePath = ExpandPath(CacheStoragePath ?? throw new NullReferenceException("CacheStoragePath is not set")); + } + + _logger.LogInformation("Configuration loaded: {}", this.ToJson()); + } + + public string CacheStoragePath { get; set; } + + public string DataStoragePath { get; set; } + + public bool StoreData { get; set; } = true; + + public bool UnknownSyncTokenIsInitialSync { get; set; } = true; + + private static string ExpandPath(string path, bool retry = true) { + _logger.LogInformation("Expanding path `{}`", path); + + if (path.StartsWith('~')) { + path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), path[1..]); + } + + Environment.GetEnvironmentVariables().Cast<DictionaryEntry>().OrderByDescending(x => x.Key.ToString()!.Length).ToList().ForEach(x => { + path = path.Replace($"${x.Key}", x.Value.ToString()); + }); + + _logger.LogInformation("Expanded path to `{}`", path); + var tries = 0; + while (retry && path.ContainsAnyOf("~$".Split())) { + if (tries++ > 100) + throw new Exception($"Path `{path}` contains unrecognised environment variables"); + path = ExpandPath(path, false); + } + + if(path.StartsWith("./")) + path = Path.Join(Directory.GetCurrentDirectory(), path[2..].Replace("/", Path.DirectorySeparatorChar.ToString())); + + return path; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs new file mode 100644 index 0000000..00f2a42 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs @@ -0,0 +1,64 @@ +using System.Text.Json; +using LibMatrix.Services; + +namespace LibMatrix.HomeserverEmulator.Services; + +public class MediaStore { + private readonly HSEConfiguration _config; + private readonly HomeserverResolverService _hsResolver; + private List<MediaInfo> index = new(); + + public MediaStore(HSEConfiguration config, HomeserverResolverService hsResolver) { + _config = config; + _hsResolver = hsResolver; + if (config.StoreData) { + var path = Path.Combine(config.DataStoragePath, "media"); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + if (File.Exists(Path.Combine(path, "index.json"))) + index = JsonSerializer.Deserialize<List<MediaInfo>>(File.ReadAllText(Path.Combine(path, "index.json"))); + } + else + Console.WriteLine("Data storage is disabled, not loading rooms from disk"); + } + + // public async Task<object> UploadMedia(string userId, string mimeType, Stream stream, string? filename = null) { + // var mediaId = $"mxc://{Guid.NewGuid().ToString()}"; + // var path = Path.Combine(_config.DataStoragePath, "media", mediaId); + // if (!Directory.Exists(path)) Directory.CreateDirectory(path); + // var file = Path.Combine(path, filename ?? "file"); + // await using var fs = File.Create(file); + // await stream.CopyToAsync(fs); + // index.Add(new() { }); + // return media; + // } + + public async Task<Stream> GetRemoteMedia(string serverName, string mediaId) { + if (_config.StoreData) { + var path = Path.Combine(_config.DataStoragePath, "media", serverName, mediaId); + if (!File.Exists(path)) { + var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var client = new HttpClient(); + var stream = await client.GetStreamAsync(mediaUrl); + await using var fs = File.Create(path); + await stream.CopyToAsync(fs); + } + return new FileStream(path, FileMode.Open); + } + else { + var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var client = new HttpClient(); + return await client.GetStreamAsync(mediaUrl); + } + } + public class MediaInfo { } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs new file mode 100644 index 0000000..0128ba6 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs @@ -0,0 +1,54 @@ +namespace LibMatrix.HomeserverEmulator.Services; + +public class PaginationTokenResolverService(ILogger<PaginationTokenResolverService> logger, RoomStore roomStore, UserStore userStore) { + public async Task<long?> ResolveTokenToTimestamp(string token) { + logger.LogTrace("ResolveTokenToTimestamp({token})", token); + if (token.StartsWith('$')) { + //we have an event ID + foreach (var room in roomStore._rooms) { + var evt = await ResolveTokenToEvent(token, room); + if (evt is not null) return evt.OriginServerTs; + } + + // event not found + throw new NotImplementedException(); + } + else { + // we have a sync token + foreach (var user in userStore._users) { + foreach (var (_, session) in user.AccessTokens) { + if (!session.SyncStates.TryGetValue(token, out var syncState)) continue; + long? maxTs = 0; + foreach (var room in syncState.RoomPositions) { + var roomObj = roomStore.GetRoomById(room.Key); + if (roomObj is null) + continue; + var ts = roomObj.Timeline.Last().OriginServerTs; + if (ts > maxTs) maxTs = ts; + } + + return maxTs; + } + } + + throw new NotImplementedException(); + } + } + + public async Task<StateEventResponse?> ResolveTokenToEvent(string token, RoomStore.Room room) { + if (token.StartsWith('$')) { + //we have an event ID + logger.LogTrace("ResolveTokenToEvent(EventId({token}), Room({room})): searching for event...", token, room.RoomId); + + var evt = room.Timeline.SingleOrDefault(x => x.EventId == token); + if (evt is not null) return evt; + logger.LogTrace("ResolveTokenToEvent({token}, Room({room})): event not in requested room...", token, room.RoomId); + return null; + } + else { + // we have a sync token + logger.LogTrace("ResolveTokenToEvent(SyncToken({token}), Room({room}))", token, room.RoomId); + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/RoomStore.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/RoomStore.cs new file mode 100644 index 0000000..5cdc3ab --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/RoomStore.cs @@ -0,0 +1,308 @@ +using System.Collections.Concurrent; +using System.Collections.Frozen; +using System.Collections.Immutable; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.Text.Json; +using System.Text.Json.Nodes; +using ArcaneLibs; +using ArcaneLibs.Collections; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.HomeserverEmulator.Controllers.Rooms; +using LibMatrix.Responses; + +namespace LibMatrix.HomeserverEmulator.Services; + +public class RoomStore { + private readonly ILogger<RoomStore> _logger; + public ConcurrentBag<Room> _rooms = new(); + private FrozenDictionary<string, Room> _roomsById = FrozenDictionary<string, Room>.Empty; + + public RoomStore(ILogger<RoomStore> logger, HSEConfiguration config) { + _logger = logger; + if (config.StoreData) { + var path = Path.Combine(config.DataStoragePath, "rooms"); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + foreach (var file in Directory.GetFiles(path)) { + var room = JsonSerializer.Deserialize<Room>(File.ReadAllText(file)); + if (room is not null) _rooms.Add(room); + } + } + else + Console.WriteLine("Data storage is disabled, not loading rooms from disk"); + + RebuildIndexes(); + } + + private SemaphoreSlim a = new(1, 1); + private void RebuildIndexes() { + // a.Wait(); + // lock (_roomsById) + // _roomsById = new ConcurrentDictionary<string, Room>(_rooms.ToDictionary(u => u.RoomId)); + // foreach (var room in _rooms) { + // _roomsById.AddOrUpdate(room.RoomId, room, (key, old) => room); + // } + // + // var roomsArr = _rooms.ToArray(); + // foreach (var (id, room) in _roomsById) { + // if (!roomsArr.Any(x => x.RoomId == id)) + // _roomsById.TryRemove(id, out _); + // } + + // _roomsById = new ConcurrentDictionary<string, Room>(_rooms.ToDictionary(u => u.RoomId)); + _roomsById = _rooms.ToFrozenDictionary(u => u.RoomId); + + // a.Release(); + } + + public Room? GetRoomById(string roomId, bool createIfNotExists = false) { + if (_roomsById.TryGetValue(roomId, out var room)) { + return room; + } + + if (!createIfNotExists) + return null; + + return CreateRoom(new() { }); + } + + public Room CreateRoom(CreateRoomRequest request, UserStore.User? user = null) { + var room = new Room(roomId: $"!{Guid.NewGuid().ToString()}"); + var newCreateEvent = new StateEvent() { + Type = RoomCreateEventContent.EventId, + RawContent = new() { } + }; + + foreach (var (key, value) in request.CreationContent) { + newCreateEvent.RawContent[key] = value.DeepClone(); + } + + if (user != null) { + newCreateEvent.RawContent["creator"] = user.UserId; + var createEvent = room.SetStateInternal(newCreateEvent, user: user); + createEvent.Sender = user.UserId; + + room.SetStateInternal(new() { + Type = RoomMemberEventContent.EventId, + StateKey = user.UserId, + TypedContent = new RoomMemberEventContent() { + Membership = "join", + AvatarUrl = (user.Profile.GetOrNull("avatar_url") as JsonObject)?.GetOrNull("avatar_url")?.GetValue<string>(), + DisplayName = (user.Profile.GetOrNull("displayname") as string) + } + }, user: user); + } + + 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" + } + }); + + foreach (var stateEvent in request.InitialState ?? []) { + room.SetStateInternal(stateEvent); + } + + _rooms.Add(room); + // _roomsById.TryAdd(room.RoomId, room); + RebuildIndexes(); + return room; + } + + public Room AddRoom(Room room) { + _rooms.Add(room); + RebuildIndexes(); + + return room; + } + + public class Room : NotifyPropertyChanged { + private CancellationTokenSource _debounceCts = new(); + private ObservableCollection<StateEventResponse> _timeline; + private ObservableDictionary<string, List<StateEventResponse>> _accountData; + private ObservableDictionary<string, ReadMarkersData> _readMarkers; + private FrozenSet<StateEventResponse> _stateCache; + private int _timelineHash; + + public Room(string roomId) { + if (string.IsNullOrWhiteSpace(roomId)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(roomId)); + if (roomId[0] != '!') throw new ArgumentException("Room ID must start with !", nameof(roomId)); + RoomId = roomId; + Timeline = new(); + AccountData = new(); + ReadMarkers = new(); + } + + public string RoomId { get; set; } + + public FrozenSet<StateEventResponse> State => _timelineHash == _timeline.GetHashCode() ? _stateCache : RebuildState(); + + public ObservableCollection<StateEventResponse> Timeline { + get => _timeline; + set { + if (Equals(value, _timeline)) return; + _timeline = new(value); + _timeline.CollectionChanged += (sender, args) => { + // we dont want to do this as it's rebuilt when the state is accessed + + // if (args.Action == NotifyCollectionChangedAction.Add) { + // foreach (StateEventResponse state in args.NewItems) { + // if (state.StateKey is not null) + // // we want state to be deduplicated by type and key, and we want the latest state to be the one that is returned + // RebuildState(); + // } + // } + + SaveDebounced(); + }; + // RebuildState(); + OnPropertyChanged(); + } + } + + public ObservableDictionary<string, List<StateEventResponse>> AccountData { + get => _accountData; + set { + if (Equals(value, _accountData)) return; + _accountData = new(value); + _accountData.CollectionChanged += (sender, args) => SaveDebounced(); + OnPropertyChanged(); + } + } + + public ImmutableList<StateEventResponse> JoinedMembers => + State.Where(s => s is { Type: RoomMemberEventContent.EventId, TypedContent: RoomMemberEventContent { Membership: "join" } }).ToImmutableList(); + + public ObservableDictionary<string, ReadMarkersData> ReadMarkers { + get => _readMarkers; + set { + if (Equals(value, _readMarkers)) return; + _readMarkers = new(value); + _readMarkers.CollectionChanged += (sender, args) => SaveDebounced(); + OnPropertyChanged(); + } + } + + internal StateEventResponse SetStateInternal(StateEvent request, string? senderId = null, UserStore.User? user = null) { + var state = request as StateEventResponse ?? new StateEventResponse() { + Type = request.Type, + StateKey = request.StateKey ?? "", + EventId = "$" + Guid.NewGuid().ToString(), + RoomId = RoomId, + OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds(), + Sender = user?.UserId ?? senderId ?? "", + RawContent = request.RawContent ?? (request.TypedContent is not null + ? new JsonObject() + : JsonSerializer.Deserialize<JsonObject>(JsonSerializer.Serialize(request.TypedContent))) + }; + Timeline.Add(state); + if(state.StateKey != null) + RebuildState(); + return state; + } + + public StateEventResponse AddUser(string userId) { + var state = SetStateInternal(new() { + Type = RoomMemberEventContent.EventId, + StateKey = userId, + TypedContent = new RoomMemberEventContent() { + Membership = "join" + }, + }); + + state.Sender = userId; + return state; + } + + // public async Task SaveDebounced() { + // if (!HSEConfiguration.Current.StoreData) return; + // await _debounceCts.CancelAsync(); + // _debounceCts = new CancellationTokenSource(); + // try { + // await Task.Delay(250, _debounceCts.Token); + // // Ensure all state events are in the timeline + // State.Where(s => !Timeline.Contains(s)).ToList().ForEach(s => Timeline.Add(s)); + // var path = Path.Combine(HSEConfiguration.Current.DataStoragePath, "rooms", $"{RoomId}.json"); + // Console.WriteLine($"Saving room {RoomId} to {path}!"); + // await File.WriteAllTextAsync(path, this.ToJson(ignoreNull: true)); + // } + // catch (TaskCanceledException) { } + // } + + private SemaphoreSlim saveSemaphore = new(1, 1); + + public async Task SaveDebounced() { + Task.Run(async () => { + await saveSemaphore.WaitAsync(); + try { + var path = Path.Combine(HSEConfiguration.Current.DataStoragePath, "rooms", $"{RoomId}.json"); + Console.WriteLine($"Saving room {RoomId} to {path}!"); + await File.WriteAllTextAsync(path, this.ToJson(ignoreNull: true)); + } + finally { + saveSemaphore.Release(); + } + }); + } + + private SemaphoreSlim stateRebuildSemaphore = new(1, 1); + + private FrozenSet<StateEventResponse> RebuildState() { + stateRebuildSemaphore.Wait(); + while (true) + try { + Console.WriteLine($"Rebuilding state for room {RoomId}"); + // ReSharper disable once RedundantEnumerableCastCall - This sometimes happens when the collection is modified during enumeration + List<StateEventResponse>? timeline = null; + lock (_timeline) { + timeline = Timeline.OfType<StateEventResponse>().ToList(); + } + + foreach (var evt in timeline) { + if (evt == null) { + throw new InvalidOperationException("Event is null"); + } + + if (evt.EventId == null) { + evt.EventId = "$" + Guid.NewGuid(); + } + else if (!evt.EventId.StartsWith('$')) { + evt.EventId = "$" + evt.EventId; + Console.WriteLine($"Sanitised invalid event ID {evt.EventId}"); + } + } + + _stateCache = timeline //.Where(s => s.Type == state.Type && s.StateKey == state.StateKey) + .Where(x => x.StateKey != null) + .OrderByDescending(s => s.OriginServerTs) + .DistinctBy(x => (x.Type, x.StateKey)) + .ToFrozenSet(); + + _timelineHash = _timeline.GetHashCode(); + stateRebuildSemaphore.Release(); + return _stateCache; + } + finally { } + } + } + + public List<StateEventResponse> GetRoomsByMember(string userId) { + // return _rooms + // // .Where(r => r.State.Any(s => s.Type == RoomMemberEventContent.EventId && s.StateKey == userId)) + // .Select(r => (Room: r, MemberEvent: r.State.SingleOrDefault(s => s.Type == RoomMemberEventContent.EventId && s.StateKey == userId))) + // .Where(r => r.MemberEvent != null) + // .ToDictionary(x => x.Room, x => x.MemberEvent!); + return _rooms.SelectMany(r => r.State.Where(s => s.Type == RoomMemberEventContent.EventId && s.StateKey == userId)).ToList(); + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs new file mode 100644 index 0000000..cf79aae --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/TokenService.cs @@ -0,0 +1,29 @@ +namespace LibMatrix.HomeserverEmulator.Services; + +public class TokenService{ + public string? GetAccessTokenOrNull(HttpContext ctx) { + //qry + if (ctx.Request.Query.TryGetValue("access_token", out var token)) { + return token; + } + //header + if (ctx.Request.Headers.TryGetValue("Authorization", out var auth)) { + var parts = auth.ToString().Split(' '); + if (parts is ["Bearer", _]) { + return parts[1]; + } + } + return null; + } + + public string GetAccessToken(HttpContext ctx) { + return GetAccessTokenOrNull(ctx) ?? throw new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_UNKNOWN_TOKEN, + Error = "Missing token" + }; + } + + public string? GenerateServerName(HttpContext ctx) { + return ctx.Request.Host.ToString(); + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/UserStore.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/UserStore.cs new file mode 100644 index 0000000..4ce9f92 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/UserStore.cs @@ -0,0 +1,250 @@ +using System.Collections.Concurrent; +using System.Collections.ObjectModel; +using System.Text.Json; +using System.Text.Json.Nodes; +using ArcaneLibs; +using ArcaneLibs.Collections; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Filters; +using LibMatrix.Responses; + +namespace LibMatrix.HomeserverEmulator.Services; + +public class UserStore { + public ConcurrentBag<User> _users = new(); + private readonly RoomStore _roomStore; + + public UserStore(HSEConfiguration config, RoomStore roomStore) { + _roomStore = roomStore; + if (config.StoreData) { + var dataDir = Path.Combine(HSEConfiguration.Current.DataStoragePath, "users"); + if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir); + foreach (var userId in Directory.GetDirectories(dataDir)) { + var tokensDir = Path.Combine(dataDir, userId, "tokens.json"); + var path = Path.Combine(dataDir, userId, $"user.json"); + + var user = JsonSerializer.Deserialize<User>(File.ReadAllText(path)); + user!.AccessTokens = JsonSerializer.Deserialize<ObservableDictionary<string, User.SessionInfo>>(File.ReadAllText(tokensDir))!; + _users.Add(user); + } + + Console.WriteLine($"Loaded {_users.Count} users from disk"); + } + else { + Console.WriteLine("Data storage is disabled, not loading users from disk"); + } + } + + public async Task<User?> GetUserById(string userId, bool createIfNotExists = false) { + if (_users.Any(x => x.UserId == userId)) + return _users.First(x => x.UserId == userId); + + if (!createIfNotExists) + return null; + + return await CreateUser(userId); + } + + public async Task<User?> GetUserByTokenOrNull(string token, bool createIfNotExists = false, string? serverName = null) { + if (_users.Any(x => x.AccessTokens.ContainsKey(token))) + return _users.First(x => x.AccessTokens.ContainsKey(token)); + + if (!createIfNotExists) + return null; + if (string.IsNullOrWhiteSpace(serverName)) throw new NullReferenceException("Server name was not passed"); + var uid = $"@{Guid.NewGuid().ToString()}:{serverName}"; + return await CreateUser(uid); + } + + public async Task<User> GetUserByToken(string token, bool createIfNotExists = false, string? serverName = null) { + return await GetUserByTokenOrNull(token, createIfNotExists, serverName) ?? throw new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_UNKNOWN_TOKEN, + Error = "Invalid token." + }; + } + + public async Task<User> CreateUser(string userId, Dictionary<string, object>? profile = null) { + profile ??= new(); + if (!profile.ContainsKey("displayname")) profile.Add("displayname", userId.Split(":")[0]); + if (!profile.ContainsKey("avatar_url")) profile.Add("avatar_url", null); + var user = new User() { + UserId = userId, + AccountData = new() { + new StateEventResponse() { + Type = "im.vector.analytics", + RawContent = new JsonObject() { + ["pseudonymousAnalyticsOptIn"] = false + }, + }, + new StateEventResponse() { + Type = "im.vector.web.settings", + RawContent = new JsonObject() { + ["developerMode"] = true + } + }, + } + }; + user.Profile.AddRange(profile); + _users.Add(user); + if (!_roomStore._rooms.IsEmpty) + foreach (var item in Random.Shared.GetItems(_roomStore._rooms.ToArray(), Math.Min(_roomStore._rooms.Count, 400))) { + item.AddUser(userId); + } + + int random = Random.Shared.Next(10); + for (int i = 0; i < random; i++) { + var room = _roomStore.CreateRoom(new()); + room.AddUser(userId); + } + + return user; + } + + public class User : NotifyPropertyChanged { + public User() { + AccessTokens = new(); + Filters = new(); + Profile = new(); + AccountData = new(); + RoomKeys = new(); + AuthorizedSessions = new(); + } + + private CancellationTokenSource _debounceCts = new(); + private string _userId; + private ObservableDictionary<string, SessionInfo> _accessTokens; + private ObservableDictionary<string, SyncFilter> _filters; + private ObservableDictionary<string, object> _profile; + private ObservableCollection<StateEventResponse> _accountData; + private ObservableDictionary<string, RoomKeysResponse> _roomKeys; + private ObservableDictionary<string, AuthorizedSession> _authorizedSessions; + + public string UserId { + get => _userId; + set => SetField(ref _userId, value); + } + + public ObservableDictionary<string, SessionInfo> AccessTokens { + get => _accessTokens; + set { + if (value == _accessTokens) return; + _accessTokens = new(value); + _accessTokens.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableDictionary<string, SyncFilter> Filters { + get => _filters; + set { + if (value == _filters) return; + _filters = new(value); + _filters.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableDictionary<string, object> Profile { + get => _profile; + set { + if (value == _profile) return; + _profile = new(value); + _profile.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableCollection<StateEventResponse> AccountData { + get => _accountData; + set { + if (value == _accountData) return; + _accountData = new(value); + _accountData.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableDictionary<string, RoomKeysResponse> RoomKeys { + get => _roomKeys; + set { + if (value == _roomKeys) return; + _roomKeys = new(value); + _roomKeys.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableDictionary<string, AuthorizedSession> AuthorizedSessions { + get => _authorizedSessions; + set { + if (value == _authorizedSessions) return; + _authorizedSessions = new(value); + _authorizedSessions.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public async Task SaveDebounced() { + if (!HSEConfiguration.Current.StoreData) return; + await _debounceCts.CancelAsync(); + _debounceCts = new CancellationTokenSource(); + try { + await Task.Delay(250, _debounceCts.Token); + var dataDir = Path.Combine(HSEConfiguration.Current.DataStoragePath, "users", _userId); + if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir); + var tokensDir = Path.Combine(dataDir, "tokens.json"); + var path = Path.Combine(dataDir, $"user.json"); + Console.WriteLine($"Saving user {_userId} to {path}!"); + await File.WriteAllTextAsync(path, this.ToJson(ignoreNull: true)); + await File.WriteAllTextAsync(tokensDir, AccessTokens.ToJson(ignoreNull: true)); + } + catch (TaskCanceledException) { } + catch (InvalidOperationException) { } // We don't care about 100% data safety, this usually happens when something is updated while serialising + } + + public class SessionInfo { + public string DeviceId { get; set; } = Guid.NewGuid().ToString(); + public Dictionary<string, UserSyncState> SyncStates { get; set; } = new(); + + public class UserSyncState { + public Dictionary<string, SyncRoomPosition> RoomPositions { get; set; } = new(); + public string FilterId { get; set; } + public DateTime SyncStateCreated { get; set; } = DateTime.Now; + + public class SyncRoomPosition { + public int TimelinePosition { get; set; } + public string LastTimelineEventId { get; set; } + public int AccountDataPosition { get; set; } + public bool Joined { get; set; } + } + + public UserSyncState Clone() { + return new() { + FilterId = FilterId, + RoomPositions = RoomPositions.ToDictionary(x => x.Key, x => new SyncRoomPosition() { + TimelinePosition = x.Value.TimelinePosition, + AccountDataPosition = x.Value.AccountDataPosition + }) + }; + } + } + } + + public LoginResponse Login() { + var session = new SessionInfo(); + AccessTokens.Add(Guid.NewGuid().ToString(), session); + SaveDebounced(); + return new LoginResponse() { + AccessToken = AccessTokens.Keys.Last(), + DeviceId = session.DeviceId, + UserId = UserId + }; + } + + public class AuthorizedSession { + public string Homeserver { get; set; } + public string AccessToken { get; set; } + } + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.HomeserverEmulator/appsettings.Development.json b/Utilities/LibMatrix.HomeserverEmulator/appsettings.Development.json new file mode 100644 index 0000000..f14522d --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/appsettings.Development.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Information", + "Microsoft.AspNetCore.Routing": "Warning", + "Microsoft.AspNetCore.Mvc": "Warning" + } + }, + "HomeserverEmulator": { + } +} diff --git a/Utilities/LibMatrix.HomeserverEmulator/appsettings.json b/Utilities/LibMatrix.HomeserverEmulator/appsettings.json new file mode 100644 index 0000000..b16968a --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/appsettings.json @@ -0,0 +1,41 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + // Configuration for the proxy + "MxApiExtensions": { + // WARNING: this exposes user tokens to servers listed here, which could be a security risk + // Only list servers you trust! + // Keep in mind that token conflicts can occur between servers! + "AuthHomeservers": [ + "rory.gay", + "conduit.rory.gay" + ], + // List of administrator MXIDs for the proxy, this allows them to use administrative and debug endpoints + "Admins": [ + "@emma:rory.gay", + "@emma:conduit.rory.gay" + ], + "FastInitialSync": { + "Enabled": true, + "UseRoomInfoCache": true + }, + "Cache": { + "RoomInfo": { + "BaseTtl": "00:01:00", + "ExtraTtlPerState": "00:00:00.1000000" + } + }, + "DefaultUserConfiguration": { + "ProtocolChanges": { + "DisableThreads": false, + "DisableVoip": false, + "AutoFollowTombstones": false + } + } + } +} |