diff options
Diffstat (limited to 'MatrixRoomUtils.Web/Classes')
3 files changed, 97 insertions, 129 deletions
diff --git a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs index f70572b..4e7117d 100644 --- a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs +++ b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs @@ -3,23 +3,20 @@ using MatrixRoomUtils.Core; namespace MatrixRoomUtils.Web.Classes; -public partial class LocalStorageWrapper -{ - private static SemaphoreSlim _semaphoreSlim = new(1); +public class LocalStorageWrapper { + private static readonly SemaphoreSlim _semaphoreSlim = new(1); public static Settings Settings { get; set; } = new(); //some basic logic - public static async Task InitialiseRuntimeVariables(ILocalStorageService localStorage) - { + public static async Task InitialiseRuntimeVariables(ILocalStorageService localStorage) { //RuntimeCache stuff async void Save() => await SaveToLocalStorage(localStorage); RuntimeCache.Save = Save; RuntimeCache.SaveObject = async (key, obj) => await localStorage.SetItemAsync(key, obj); - RuntimeCache.RemoveObject = async (key) => await localStorage.RemoveItemAsync(key); - if (RuntimeCache.LastUsedToken != null) - { - Console.WriteLine($"Access token is not null, creating authenticated home server"); + RuntimeCache.RemoveObject = async key => await localStorage.RemoveItemAsync(key); + if (RuntimeCache.LastUsedToken != null) { + Console.WriteLine("Access token is not null, creating authenticated home server"); Console.WriteLine($"Homeserver cache: {RuntimeCache.HomeserverResolutionCache.Count} entries"); // Console.WriteLine(RuntimeCache.HomeserverResolutionCache.ToJson()); RuntimeCache.CurrentHomeServer = await new AuthenticatedHomeServer(RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.UserId, RuntimeCache.LastUsedToken, @@ -28,26 +25,24 @@ public partial class LocalStorageWrapper } } - public static async Task LoadFromLocalStorage(ILocalStorageService localStorage) - { + public static async Task LoadFromLocalStorage(ILocalStorageService localStorage) { await _semaphoreSlim.WaitAsync(); - if (RuntimeCache.WasLoaded) - { + if (RuntimeCache.WasLoaded) { _semaphoreSlim.Release(); return; } + Console.WriteLine("Loading from local storage..."); - Settings = await localStorage.GetItemAsync<Settings>("rory.matrixroomutils.settings") ?? new(); + Settings = await localStorage.GetItemAsync<Settings>("rory.matrixroomutils.settings") ?? new Settings(); RuntimeCache.LastUsedToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.last_used_token"); - RuntimeCache.LoginSessions = await localStorage.GetItemAsync<Dictionary<string, UserInfo>>("rory.matrixroomutils.login_sessions") ?? new(); - RuntimeCache.HomeserverResolutionCache = await localStorage.GetItemAsync<Dictionary<string, HomeServerResolutionResult>>("rory.matrixroomutils.homeserver_resolution_cache") ?? new(); + RuntimeCache.LoginSessions = await localStorage.GetItemAsync<Dictionary<string, UserInfo>>("rory.matrixroomutils.login_sessions") ?? new Dictionary<string, UserInfo>(); + RuntimeCache.HomeserverResolutionCache = await localStorage.GetItemAsync<Dictionary<string, HomeServerResolutionResult>>("rory.matrixroomutils.homeserver_resolution_cache") ?? new Dictionary<string, HomeServerResolutionResult>(); Console.WriteLine($"[LocalStorageWrapper] Loaded {RuntimeCache.LoginSessions.Count} login sessions, {RuntimeCache.HomeserverResolutionCache.Count} homeserver resolution cache entries"); //RuntimeCache.GenericResponseCache = await localStorage.GetItemAsync<Dictionary<string, ObjectCache<object>>>("rory.matrixroomutils.generic_cache") ?? new(); - foreach (var s in (await localStorage.KeysAsync()).Where(x => x.StartsWith("rory.matrixroomutils.generic_cache:")).ToList()) - { + foreach (var s in (await localStorage.KeysAsync()).Where(x => x.StartsWith("rory.matrixroomutils.generic_cache:")).ToList()) { Console.WriteLine($"Loading generic cache entry {s}"); RuntimeCache.GenericResponseCache[s.Replace("rory.matrixroomutils.generic_cache:", "")] = await localStorage.GetItemAsync<ObjectCache<object>>(s); } @@ -57,36 +52,31 @@ public partial class LocalStorageWrapper _semaphoreSlim.Release(); } - public static async Task SaveToLocalStorage(ILocalStorageService localStorage) - { + public static async Task SaveToLocalStorage(ILocalStorageService localStorage) { Console.WriteLine("Saving to local storage..."); await localStorage.SetItemAsync("rory.matrixroomutils.settings", Settings); if (RuntimeCache.LoginSessions != null) await localStorage.SetItemAsync("rory.matrixroomutils.login_sessions", RuntimeCache.LoginSessions); if (RuntimeCache.LastUsedToken != null) await localStorage.SetItemAsync("rory.matrixroomutils.last_used_token", RuntimeCache.LastUsedToken); } - public static async Task SaveCacheToLocalStorage(ILocalStorageService localStorage, bool awaitSave = true, bool saveGenericCache = true) - { + public static async Task SaveCacheToLocalStorage(ILocalStorageService localStorage, bool awaitSave = true, bool saveGenericCache = true) { await localStorage.SetItemAsync("rory.matrixroomutils.homeserver_resolution_cache", RuntimeCache.HomeserverResolutionCache.DistinctBy(x => x.Key) .ToDictionary(x => x.Key, x => x.Value)); //await localStorage.SetItemAsync("rory.matrixroomutils.generic_cache", RuntimeCache.GenericResponseCache); - if(saveGenericCache) - foreach (var s in RuntimeCache.GenericResponseCache.Keys) - { + if (saveGenericCache) + foreach (var s in RuntimeCache.GenericResponseCache.Keys) { var t = localStorage.SetItemAsync($"rory.matrixroomutils.generic_cache:{s}", RuntimeCache.GenericResponseCache[s]); if (awaitSave) await t; } } } -public class Settings -{ +public class Settings { public DeveloperSettings DeveloperSettings { get; set; } = new(); } -public class DeveloperSettings -{ +public class DeveloperSettings { public bool EnableLogViewers { get; set; } = false; public bool EnableConsoleLogging { get; set; } = true; public bool EnablePortableDevtools { get; set; } = false; diff --git a/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs b/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs index c43bd3c..77c8281 100644 --- a/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs +++ b/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs @@ -1,110 +1,89 @@ +using System.Text.Json.Nodes; using MatrixRoomUtils.Core; using MatrixRoomUtils.Core.Responses; namespace MatrixRoomUtils.Web.Classes.RoomCreationTemplates; -public class DefaultRoomCreationTemplate : IRoomCreationTemplate -{ +public class DefaultRoomCreationTemplate : IRoomCreationTemplate { public string Name => "Default"; - public CreateRoomRequest CreateRoomRequest - { - get - { - return new() - { - Name = "My new room", - RoomAliasName = "myroom", - InitialState = new() - { - new() - { - Type = "m.room.history_visibility", - Content = new - { - history_visibility = "world_readable" - } - }, - new StateEvent<Pages.RoomManager.RoomManagerCreateRoom.GuestAccessContent> - { - Type = "m.room.guest_access", - Content = new() - { - GuestAccess = "can_join" - } - }, - new() - { - Type = "m.room.join_rules", - Content = new - { - join_rule = "public" - } - }, - new() - { - Type = "m.room.server_acl", - Content = new - { - allow = new[] { "*" }, - deny = Array.Empty<string>(), - allow_ip_literals = false - } - }, - new() - { - Type = "m.room.avatar", - Content = new - { - url = "mxc://feline.support/UKNhEyrVsrAbYteVvZloZcFj" - } + + public CreateRoomRequest CreateRoomRequest => + new CreateRoomRequest { + Name = "My new room", + RoomAliasName = "myroom", + InitialState = new List<StateEvent> { + new() { + Type = "m.room.history_visibility", + Content = new { + history_visibility = "world_readable" } }, - Visibility = "public", - PowerLevelContentOverride = new() - { - UsersDefault = 0, - EventsDefault = 100, - StateDefault = 50, - Invite = 0, - Redact = 50, - Kick = 50, - Ban = 50, - NotificationsPl = new() - { - Room = 50 - }, - Events = new() - { - { "im.vector.modular.widgets", 50 }, - { "io.element.voice_broadcast_info", 50 }, - { "m.reaction", 100 }, - { "m.room.avatar", 50 }, - { "m.room.canonical_alias", 50 }, - { "m.room.encryption", 100 }, - { "m.room.history_visibility", 100 }, - { "m.room.name", 50 }, - { "m.room.pinned_events", 50 }, - { "m.room.power_levels", 100 }, - { "m.room.redaction", 100 }, - { "m.room.server_acl", 100 }, - { "m.room.tombstone", 100 }, - { "m.room.topic", 50 }, - { "m.space.child", 50 }, - { "org.matrix.msc3401.call", 50 }, - { "org.matrix.msc3401.call.member", 50 } - }, - Users = new() - { - { RuntimeCache.CurrentHomeServer.UserId, 100 }, - }, + new StateEvent<Pages.RoomManager.RoomManagerCreateRoom.GuestAccessContent> { + Type = "m.room.guest_access", + Content = new Pages.RoomManager.RoomManagerCreateRoom.GuestAccessContent { + GuestAccess = "can_join" + } }, - CreationContent = new() - { - { - "type", null + new() { + Type = "m.room.join_rules", + Content = new { + join_rule = "public" + } + }, + new() { + Type = "m.room.server_acl", + Content = new { + allow = new[] { "*" }, + deny = Array.Empty<string>(), + allow_ip_literals = false } + }, + new() { + Type = "m.room.avatar", + Content = new { + url = "mxc://feline.support/UKNhEyrVsrAbYteVvZloZcFj" + } + } + }, + Visibility = "public", + PowerLevelContentOverride = new PowerLevelEvent { + UsersDefault = 0, + EventsDefault = 100, + StateDefault = 50, + Invite = 0, + Redact = 50, + Kick = 50, + Ban = 50, + NotificationsPl = new NotificationsPL { + Room = 50 + }, + Events = new Dictionary<string, int> { + { "im.vector.modular.widgets", 50 }, + { "io.element.voice_broadcast_info", 50 }, + { "m.reaction", 100 }, + { "m.room.avatar", 50 }, + { "m.room.canonical_alias", 50 }, + { "m.room.encryption", 100 }, + { "m.room.history_visibility", 100 }, + { "m.room.name", 50 }, + { "m.room.pinned_events", 50 }, + { "m.room.power_levels", 100 }, + { "m.room.redaction", 100 }, + { "m.room.server_acl", 100 }, + { "m.room.tombstone", 100 }, + { "m.room.topic", 50 }, + { "m.space.child", 50 }, + { "org.matrix.msc3401.call", 50 }, + { "org.matrix.msc3401.call.member", 50 } + }, + Users = new Dictionary<string, int> { + { RuntimeCache.CurrentHomeServer.UserId, 100 } + } + }, + CreationContent = new JsonObject { + { + "type", null } - }; - } - } + } + }; } \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/IRoomCreationTemplate.cs b/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/IRoomCreationTemplate.cs index 7f84dc4..bbb09b7 100644 --- a/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/IRoomCreationTemplate.cs +++ b/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/IRoomCreationTemplate.cs @@ -2,8 +2,7 @@ using MatrixRoomUtils.Core.Responses; namespace MatrixRoomUtils.Web.Classes.RoomCreationTemplates; -public interface IRoomCreationTemplate -{ +public interface IRoomCreationTemplate { public CreateRoomRequest CreateRoomRequest { get; } public string Name { get; } } \ No newline at end of file |