From f41b6e5ec431c88bc1d94e4832d8ba49ddc42004 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Tue, 5 Mar 2024 11:19:52 +0100 Subject: HomeserverEmulator work --- .../Services/HSEConfiguration.cs | 57 ++++++ .../Services/MediaStore.cs | 47 +++++ .../Services/RoomStore.cs | 103 +++++++++-- .../Services/TokenService.cs | 12 +- .../Services/UserStore.cs | 197 +++++++++++++++++---- 5 files changed, 366 insertions(+), 50 deletions(-) create mode 100644 Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs create mode 100644 Tests/LibMatrix.HomeserverEmulator/Services/MediaStore.cs (limited to 'Tests/LibMatrix.HomeserverEmulator/Services') diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs b/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs new file mode 100644 index 0000000..73b0d23 --- /dev/null +++ b/Tests/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 _logger; + public static HSEConfiguration Current { get; set; } + + [RequiresUnreferencedCode("Uses reflection binding")] + public HSEConfiguration(ILogger 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().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/Tests/LibMatrix.HomeserverEmulator/Services/MediaStore.cs b/Tests/LibMatrix.HomeserverEmulator/Services/MediaStore.cs new file mode 100644 index 0000000..e34a731 --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Services/MediaStore.cs @@ -0,0 +1,47 @@ +using System.Collections.Concurrent; +using System.Collections.Frozen; +using System.Collections.ObjectModel; +using System.Security.Cryptography; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using ArcaneLibs; +using ArcaneLibs.Collections; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Responses; + +namespace LibMatrix.HomeserverEmulator.Services; + +public class MediaStore { + private readonly HSEConfiguration _config; + private List index = new(); + + public MediaStore(HSEConfiguration config) { + _config = config; + 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>(File.ReadAllText(Path.Combine(path, "index.json"))); + } + else + Console.WriteLine("Data storage is disabled, not loading rooms from disk"); + } + + public async Task 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 class MediaInfo { } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/RoomStore.cs b/Tests/LibMatrix.HomeserverEmulator/Services/RoomStore.cs index b4624ab..37d9c7d 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Services/RoomStore.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Services/RoomStore.cs @@ -1,5 +1,13 @@ using System.Collections.Concurrent; +using System.Collections.Frozen; +using System.Collections.ObjectModel; using System.Security.Cryptography; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using ArcaneLibs; +using ArcaneLibs.Collections; +using ArcaneLibs.Extensions; using LibMatrix.EventTypes; using LibMatrix.EventTypes.Spec.State; using LibMatrix.Responses; @@ -9,6 +17,21 @@ namespace LibMatrix.HomeserverEmulator.Services; public class RoomStore { public ConcurrentBag _rooms = new(); private Dictionary _roomsById = new(); + + public RoomStore(HSEConfiguration config) { + 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(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 void RebuildIndexes() { _roomsById = _rooms.ToDictionary(u => u.RoomId); @@ -26,9 +49,7 @@ public class RoomStore { } public Room CreateRoom(CreateRoomRequest request) { - var room = new Room { - RoomId = $"!{Guid.NewGuid().ToString()}" - }; + var room = new Room(roomId: $"!{Guid.NewGuid().ToString()}"); if (!string.IsNullOrWhiteSpace(request.Name)) room.SetStateInternal(new StateEvent() { Type = RoomNameEventContent.EventId, @@ -54,30 +75,90 @@ public class RoomStore { return room; } - public class Room { + public class Room : NotifyPropertyChanged { + private CancellationTokenSource _debounceCts = new(); + private ObservableCollection _timeline; + private ObservableDictionary> _accountData; + + 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; + State = FrozenSet.Empty; + Timeline = new(); + AccountData = new(); + } + public string RoomId { get; set; } - public List State { get; set; } = new(); - public Dictionary Timeline { get; set; } = new(); + + public FrozenSet State { get; private set; } + + public ObservableCollection Timeline { + get => _timeline; + set { + if (Equals(value, _timeline)) return; + _timeline = new(value); + _timeline.CollectionChanged += (sender, args) => SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableDictionary> AccountData { + get => _accountData; + set { + if (Equals(value, _accountData)) return; + _accountData = new(value); + _accountData.CollectionChanged += (sender, args) => SaveDebounced(); + OnPropertyChanged(); + } + } internal StateEventResponse SetStateInternal(StateEvent request) { var state = new StateEventResponse() { Type = request.Type, StateKey = request.StateKey, - RawContent = request.RawContent, - EventId = Guid.NewGuid().ToString() + EventId = Guid.NewGuid().ToString(), + RoomId = RoomId, + OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds(), + Sender = "", + RawContent = request.RawContent ?? (request.TypedContent is not null ? new JsonObject() : JsonSerializer.Deserialize(JsonSerializer.Serialize(request.TypedContent))) }; - State.Add(state); + Timeline.Add(state); + 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 + State = Timeline.Where(s => s.Type == state.Type && s.StateKey == state.StateKey) + .OrderByDescending(s => s.OriginServerTs) + .DistinctBy(x=>(x.Type, x.StateKey)) + .ToFrozenSet(); return state; } public StateEventResponse AddUser(string userId) { - return SetStateInternal(new() { + 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) { } } } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs b/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs index 8115bee..1f59342 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs @@ -1,9 +1,7 @@ namespace LibMatrix.HomeserverEmulator.Services; -public class TokenService(IHttpContextAccessor accessor) { - public string? GetAccessToken() { - var ctx = accessor.HttpContext; - if (ctx is null) return null; +public class TokenService{ + public string? GetAccessToken(HttpContext ctx) { //qry if (ctx.Request.Query.TryGetValue("access_token", out var token)) { return token; @@ -11,16 +9,14 @@ public class TokenService(IHttpContextAccessor accessor) { //header if (ctx.Request.Headers.TryGetValue("Authorization", out var auth)) { var parts = auth.ToString().Split(' '); - if (parts.Length == 2 && parts[0] == "Bearer") { + if (parts is ["Bearer", _]) { return parts[1]; } } return null; } - public string? GenerateServerName() { - var ctx = accessor.HttpContext; - if (ctx is null) return null; + public string? GenerateServerName(HttpContext ctx) { return ctx.Request.Host.ToString(); } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/UserStore.cs b/Tests/LibMatrix.HomeserverEmulator/Services/UserStore.cs index ca1c577..faf0046 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Services/UserStore.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Services/UserStore.cs @@ -1,72 +1,207 @@ +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(RoomStore roomStore) { - public List _users = new(); - private Dictionary _usersById = new(); - private Dictionary _usersByToken = new(); +public class UserStore { + public ConcurrentBag _users = new(); + private readonly RoomStore _roomStore; - private void RebuildIndexes() { - _usersById = _users.ToDictionary(u => u.UserId); - _usersByToken = _users.ToDictionary(u => u.AccessToken); + public UserStore(HSEConfiguration config, RoomStore roomStore) { + _roomStore = roomStore; + if (config.StoreData) { + var path = Path.Combine(config.DataStoragePath, "users"); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + foreach (var file in Directory.GetFiles(path)) { + var user = JsonSerializer.Deserialize(File.ReadAllText(file)); + if (user is not null) _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 GetUserById(string userId, bool createIfNotExists = false) { - if (_usersById.TryGetValue(userId, out var user)) { - return user; - } + if (_users.Any(x => x.UserId == userId)) + return _users.First(x => x.UserId == userId); if (!createIfNotExists) return null; - return await CreateUser(userId, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new Dictionary()); + return await CreateUser(userId); } public async Task GetUserByToken(string token, bool createIfNotExists = false, string? serverName = null) { - if (_usersByToken.TryGetValue(token, out var user)) { - return user; - } + 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, Guid.NewGuid().ToString(), token, new Dictionary()); + return await CreateUser(uid); } - public async Task CreateUser(string userId, string deviceId, string accessToken, Dictionary profile) { + public async Task CreateUser(string userId, Dictionary? 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 { + var user = new User() { UserId = userId, - DeviceId = deviceId, - AccessToken = accessToken, - Profile = profile + 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); - RebuildIndexes(); - - if (roomStore._rooms.Count > 0) - foreach (var item in Random.Shared.GetItems(roomStore._rooms.ToArray(), Math.Min(roomStore._rooms.Count, 400))) { + 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()); + var room = _roomStore.CreateRoom(new()); room.AddUser(userId); } return user; } - public class User { - public string UserId { get; set; } - public string AccessToken { get; set; } - public string DeviceId { get; set; } - public Dictionary Profile { get; set; } + public class User : NotifyPropertyChanged { + public User() { + AccessTokens = new(); + Filters = new(); + Profile = new(); + AccountData = new(); + RoomKeys = new(); + } + + private CancellationTokenSource _debounceCts = new(); + private string _userId; + private ObservableDictionary _accessTokens; + private ObservableDictionary _filters; + private ObservableDictionary _profile; + private ObservableCollection _accountData; + private ObservableDictionary _roomKeys; + + public string UserId { + get => _userId; + set => SetField(ref _userId, value); + } + + public ObservableDictionary AccessTokens { + get => _accessTokens; + set { + if (value == _accessTokens) return; + _accessTokens = new(value); + _accessTokens.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableDictionary Filters { + get => _filters; + set { + if (value == _filters) return; + _filters = new(value); + _filters.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableDictionary Profile { + get => _profile; + set { + if (value == _profile) return; + _profile = new(value); + _profile.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableCollection AccountData { + get => _accountData; + set { + if (value == _accountData) return; + _accountData = new(value); + _accountData.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public ObservableDictionary RoomKeys { + get => _roomKeys; + set { + if (value == _roomKeys) return; + _roomKeys = new(value); + _roomKeys.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + + public async Task SaveDebounced() { + if (!HSEConfiguration.Current.StoreData) return; + _debounceCts.Cancel(); + _debounceCts = new CancellationTokenSource(); + try { + await Task.Delay(250, _debounceCts.Token); + var path = Path.Combine(HSEConfiguration.Current.DataStoragePath, "users", $"{_userId}.json"); + Console.WriteLine($"Saving user {_userId} to {path}!"); + await File.WriteAllTextAsync(path, this.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 SyncStates { get; set; } = new(); + + public class UserSyncState { + public Dictionary RoomPositions { get; set; } = new(); + public string FilterId { get; set; } + public DateTime SyncStateCreated { get; set; } = DateTime.Now; - public List JoinedRooms { get; set; } = new(); + public class SyncRoomPosition { + public int TimelinePosition { get; set; } + public int StatePosition { get; set; } + public int AccountDataPosition { get; set; } + } + } + } + + 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 + }; + } } } \ No newline at end of file -- cgit 1.4.1