From e10fa389ce3c4d42deadfec8bf08c2fbb1a88d79 Mon Sep 17 00:00:00 2001 From: "Emma@Rory&" Date: Fri, 15 Sep 2023 09:55:36 +0200 Subject: Refactors --- MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs | 27 +++++++++++----------- .../DefaultRoomCreationTemplate.cs | 27 +++++++++++----------- MatrixRoomUtils.Web/Classes/RoomInfo.cs | 3 ++- MatrixRoomUtils.Web/Classes/UserAuth.cs | 15 ++++++++++++ 4 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 MatrixRoomUtils.Web/Classes/UserAuth.cs (limited to 'MatrixRoomUtils.Web/Classes') diff --git a/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs index 14625fd..8ea85e9 100644 --- a/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs +++ b/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs @@ -1,4 +1,5 @@ using LibMatrix; +using LibMatrix.Homeservers; using LibMatrix.Responses; using LibMatrix.Services; using Microsoft.AspNetCore.Components; @@ -20,13 +21,13 @@ public class MRUStorageWrapper { _navigationManager = navigationManager; } - public async Task?> GetAllTokens() { - return await _storageService.DataStorageProvider.LoadObjectAsync>("mru.tokens") ?? - new List(); + public async Task?> GetAllTokens() { + return await _storageService.DataStorageProvider.LoadObjectAsync>("mru.tokens") ?? + new List(); } - public async Task GetCurrentToken() { - var currentToken = await _storageService.DataStorageProvider.LoadObjectAsync("token"); + public async Task GetCurrentToken() { + var currentToken = await _storageService.DataStorageProvider.LoadObjectAsync("token"); var allTokens = await GetAllTokens(); if (allTokens is null or { Count: 0 }) { await SetCurrentToken(null); @@ -44,14 +45,14 @@ public class MRUStorageWrapper { return currentToken; } - public async Task AddToken(LoginResponse loginResponse) { - var tokens = await GetAllTokens() ?? new List(); + public async Task AddToken(UserAuth UserAuth) { + var tokens = await GetAllTokens() ?? new List(); - tokens.Add(loginResponse); + tokens.Add(UserAuth); await _storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens); } - private async Task GetCurrentSession() { + private async Task GetCurrentSession() { var token = await GetCurrentToken(); if (token == null) { return null; @@ -60,8 +61,8 @@ public class MRUStorageWrapper { return await _homeserverProviderService.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken); } - public async Task GetCurrentSessionOrNavigate() { - AuthenticatedHomeServer? session = null; + public async Task GetCurrentSessionOrNavigate() { + AuthenticatedHomeserverGeneric? session = null; try { //catch if the token is invalid @@ -94,7 +95,7 @@ public class MRUStorageWrapper { public bool EnablePortableDevtools { get; set; } } - public async Task RemoveToken(LoginResponse auth) { + public async Task RemoveToken(UserAuth auth) { var tokens = await GetAllTokens(); if (tokens == null) { return; @@ -104,5 +105,5 @@ public class MRUStorageWrapper { await _storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens); } - public async Task SetCurrentToken(LoginResponse? auth) => await _storageService.DataStorageProvider.SaveObjectAsync("token", auth); + public async Task SetCurrentToken(UserAuth? auth) => await _storageService.DataStorageProvider.SaveObjectAsync("token", auth); } diff --git a/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs b/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs index bb2eab9..3f67f33 100644 --- a/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs +++ b/MatrixRoomUtils.Web/Classes/RoomCreationTemplates/DefaultRoomCreationTemplate.cs @@ -2,7 +2,6 @@ using System.Text.Json.Nodes; using LibMatrix; using LibMatrix.Responses; using LibMatrix.StateEventTypes.Spec; -using LibMatrix.StateEventTypes; namespace MatrixRoomUtils.Web.Classes.RoomCreationTemplates; @@ -16,39 +15,39 @@ public class DefaultRoomCreationTemplate : IRoomCreationTemplate { InitialState = new List { new() { Type = "m.room.history_visibility", - TypedContent = new { - history_visibility = "world_readable" + TypedContent = new HistoryVisibilityEventContent() { + HistoryVisibility = "world_readable" } }, new() { Type = "m.room.guest_access", - TypedContent = new GuestAccessEventData { + TypedContent = new GuestAccessEventContent { GuestAccess = "can_join" } }, new() { Type = "m.room.join_rules", - TypedContent = new JoinRulesEventData { + TypedContent = new JoinRulesEventContent { JoinRule = "public" } }, new() { Type = "m.room.server_acl", - TypedContent = new { - allow = new[] { "*" }, - deny = Array.Empty(), - allow_ip_literals = false + TypedContent = new ServerACLEventContent() { + Allow = new List() { "*" }, + Deny = new List(), + AllowIpLiterals = false } }, new() { Type = "m.room.avatar", - TypedContent = new RoomAvatarEventData { + TypedContent = new RoomAvatarEventContent { Url = "mxc://feline.support/UKNhEyrVsrAbYteVvZloZcFj" } } }, Visibility = "public", - PowerLevelContentOverride = new RoomPowerLevelEventData { + PowerLevelContentOverride = new RoomPowerLevelEventContent { UsersDefault = 0, EventsDefault = 100, StateDefault = 50, @@ -56,10 +55,10 @@ public class DefaultRoomCreationTemplate : IRoomCreationTemplate { Redact = 50, Kick = 50, Ban = 50, - NotificationsPl = new RoomPowerLevelEventData.NotificationsPL { + NotificationsPl = new RoomPowerLevelEventContent.NotificationsPL { Room = 50 }, - Events = new Dictionary { + Events = new() { { "im.vector.modular.widgets", 50 }, { "io.element.voice_broadcast_info", 50 }, { "m.reaction", 100 }, @@ -78,7 +77,7 @@ public class DefaultRoomCreationTemplate : IRoomCreationTemplate { { "org.matrix.msc3401.call", 50 }, { "org.matrix.msc3401.call.member", 50 } }, - Users = new Dictionary { + Users = new() { // { RuntimeCache.CurrentHomeServer.UserId, 100 } //TODO: re-implement this } diff --git a/MatrixRoomUtils.Web/Classes/RoomInfo.cs b/MatrixRoomUtils.Web/Classes/RoomInfo.cs index 111bfe0..0e21871 100644 --- a/MatrixRoomUtils.Web/Classes/RoomInfo.cs +++ b/MatrixRoomUtils.Web/Classes/RoomInfo.cs @@ -1,4 +1,5 @@ using LibMatrix; +using LibMatrix.Interfaces; using LibMatrix.Responses; using LibMatrix.RoomTypes; @@ -17,7 +18,7 @@ public class RoomInfo { StateKey = stateKey }; try { - @event.TypedContent = await Room.GetStateAsync(type, stateKey); + @event.TypedContent = await Room.GetStateAsync(type, stateKey); } catch (MatrixException e) { if (e is { ErrorCode: "M_NOT_FOUND" }) @event.TypedContent = default!; diff --git a/MatrixRoomUtils.Web/Classes/UserAuth.cs b/MatrixRoomUtils.Web/Classes/UserAuth.cs new file mode 100644 index 0000000..e6f0954 --- /dev/null +++ b/MatrixRoomUtils.Web/Classes/UserAuth.cs @@ -0,0 +1,15 @@ +using LibMatrix.Responses; + +namespace MatrixRoomUtils.Web.Classes; + +public class UserAuth : LoginResponse { + public UserAuth() { } + public UserAuth(LoginResponse login) { + Homeserver = login.Homeserver; + UserId = login.UserId; + AccessToken = login.AccessToken; + DeviceId = login.DeviceId; + } + + public string? Proxy { get; set; } +} -- cgit 1.5.1