diff options
Diffstat (limited to 'MatrixRoomUtils.Desktop')
-rw-r--r-- | MatrixRoomUtils.Desktop/App.axaml.cs | 4 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/Components/NavigationStack.axaml | 2 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs | 7 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml | 2 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs | 7 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/FileStorageProvider.cs | 14 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs | 10 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/MRUStorageWrapper.cs | 40 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/MainWindow.axaml | 3 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/MainWindow.axaml.cs | 1 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/Program.cs | 4 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/RoomInfo.cs | 6 | ||||
-rw-r--r-- | MatrixRoomUtils.Desktop/SentryService.cs | 4 |
13 files changed, 46 insertions, 58 deletions
diff --git a/MatrixRoomUtils.Desktop/App.axaml.cs b/MatrixRoomUtils.Desktop/App.axaml.cs index 20f2a3c..e0b50a5 100644 --- a/MatrixRoomUtils.Desktop/App.axaml.cs +++ b/MatrixRoomUtils.Desktop/App.axaml.cs @@ -20,12 +20,12 @@ public partial class App : Application { services.AddSingleton<MRUDesktopConfiguration>(); services.AddSingleton<SentryService>(); services.AddSingleton<TieredStorageService>(x => - new( + new TieredStorageService( cacheStorageProvider: new FileStorageProvider(x.GetService<MRUDesktopConfiguration>().CacheStoragePath), dataStorageProvider: new FileStorageProvider(x.GetService<MRUDesktopConfiguration>().DataStoragePath) ) ); - services.AddSingleton(new RoryLibMatrixConfiguration() { + services.AddSingleton(new RoryLibMatrixConfiguration { AppName = "MatrixRoomUtils.Desktop" }); services.AddRoryLibMatrixServices(); diff --git a/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml b/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml index e0812ec..c773b8d 100644 --- a/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml +++ b/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml @@ -3,7 +3,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" - x:Class="MatrixRoomUtils.Desktop.NavigationStack"> + x:Class="MatrixRoomUtils.Desktop.Components.NavigationStack"> <DockPanel x:Name="dock"> <StackPanel x:Name="navPanel"></StackPanel> <UserControl x:Name="content"></UserControl> diff --git a/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs b/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs index f4e0fed..d6343e2 100644 --- a/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs +++ b/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs @@ -1,8 +1,7 @@ -using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; -namespace MatrixRoomUtils.Desktop; +namespace MatrixRoomUtils.Desktop.Components; public partial class NavigationStack : UserControl { public NavigationStack() { @@ -20,7 +19,7 @@ public partial class NavigationStack : UserControl { Button btn = new() { Content = item.Name }; - btn.Click += (sender, args) => { + btn.Click += (_, _) => { PopTo(_stack.IndexOf(item)); buildView(); }; @@ -41,7 +40,7 @@ public partial class NavigationStack : UserControl { public NavigationStackItem? Current => _stack.LastOrDefault(); public void Push(string name, UserControl view) { - _stack.Add(new NavigationStackItem() { + _stack.Add(new NavigationStackItem { Name = name, View = view }); diff --git a/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml b/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml index c80ef2f..09fe52b 100644 --- a/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml +++ b/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml @@ -3,7 +3,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="250" d:DesignHeight="32" - x:Class="MatrixRoomUtils.Desktop.RoomListEntry"> + x:Class="MatrixRoomUtils.Desktop.Components.RoomListEntry"> <StackPanel Orientation="Horizontal"> <Image MaxWidth="64" x:Name="RoomIcon"></Image> <Label x:Name="RoomName"></Label> diff --git a/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs b/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs index f29db63..359deb3 100644 --- a/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs +++ b/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs @@ -5,10 +5,9 @@ using LibMatrix; using LibMatrix.Helpers; using LibMatrix.Services; using LibMatrix.StateEventTypes.Spec; -using MatrixRoomUtils.Web.Classes; using Microsoft.Extensions.DependencyInjection; -namespace MatrixRoomUtils.Desktop; +namespace MatrixRoomUtils.Desktop.Components; public partial class RoomListEntry : UserControl { private readonly IServiceScopeFactory _serviceScopeFactory; @@ -29,7 +28,7 @@ public partial class RoomListEntry : UserControl { private async Task GetRoomName() { try { var nameEvent = await _roomInfo.GetStateEvent("m.room.name"); - if (nameEvent is not null && nameEvent.TypedContent is RoomNameEventData nameData) + if (nameEvent?.TypedContent is RoomNameEventData nameData) RoomName.Content = nameData.Name; } catch (MatrixException e) { @@ -41,7 +40,7 @@ public partial class RoomListEntry : UserControl { private async Task GetRoomIcon() { try { var avatarEvent = await _roomInfo.GetStateEvent("m.room.avatar"); - if (avatarEvent is not null && avatarEvent.TypedContent is RoomAvatarEventData avatarData) { + if (avatarEvent?.TypedContent is RoomAvatarEventData avatarData) { var mxcUrl = avatarData.Url; await using var svc = _serviceScopeFactory.CreateAsyncScope(); var hs = await svc.ServiceProvider.GetService<MRUStorageWrapper>().GetCurrentSessionOrPrompt(); diff --git a/MatrixRoomUtils.Desktop/FileStorageProvider.cs b/MatrixRoomUtils.Desktop/FileStorageProvider.cs index 36a3c7e..b3850b0 100644 --- a/MatrixRoomUtils.Desktop/FileStorageProvider.cs +++ b/MatrixRoomUtils.Desktop/FileStorageProvider.cs @@ -23,20 +23,24 @@ public class FileStorageProvider : IStorageProvider { } } - public async Task SaveObjectAsync<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), ObjectExtensions.ToJson(value)); + public async Task SaveObjectAsync<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); public async Task<T?> LoadObjectAsync<T>(string key) => JsonSerializer.Deserialize<T>(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); - public async Task<bool> ObjectExistsAsync(string key) => File.Exists(Path.Join(TargetPath, key)); + public Task<bool> ObjectExistsAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key))); - public async Task<List<string>> GetAllKeysAsync() => Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList(); + public Task<List<string>> GetAllKeysAsync() => Task.FromResult(Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList()); + + public Task DeleteObjectAsync(string key) { + File.Delete(Path.Join(TargetPath, key)); + return Task.CompletedTask; + } - public async Task DeleteObjectAsync(string key) => File.Delete(Path.Join(TargetPath, key)); public async Task SaveStreamAsync(string key, Stream stream) { Directory.CreateDirectory(Path.GetDirectoryName(Path.Join(TargetPath, key)) ?? throw new InvalidOperationException()); await using var fileStream = File.Create(Path.Join(TargetPath, key)); await stream.CopyToAsync(fileStream); } - public async Task<Stream?> LoadStreamAsync(string key) => File.Exists(Path.Join(TargetPath, key)) ? File.OpenRead(Path.Join(TargetPath, key)) : null; + public Task<Stream?> LoadStreamAsync(string key) => Task.FromResult<Stream?>(File.Exists(Path.Join(TargetPath, key)) ? File.OpenRead(Path.Join(TargetPath, key)) : null); } diff --git a/MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs b/MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs index 39c42cf..4f265ee 100644 --- a/MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs +++ b/MatrixRoomUtils.Desktop/MRUDesktopConfiguration.cs @@ -11,7 +11,7 @@ public class MRUDesktopConfiguration { public MRUDesktopConfiguration(ILogger<MRUDesktopConfiguration> logger, IConfiguration config, HostBuilderContext host) { _logger = logger; - logger.LogInformation($"Loading configuration for environment: {host.HostingEnvironment.EnvironmentName}..."); + logger.LogInformation("Loading configuration for environment: {}...", host.HostingEnvironment.EnvironmentName); config.GetSection("MRUDesktop").Bind(this); DataStoragePath = ExpandPath(DataStoragePath); CacheStoragePath = ExpandPath(CacheStoragePath); @@ -22,18 +22,18 @@ public class MRUDesktopConfiguration { public string? SentryDsn { get; set; } private static string ExpandPath(string path, bool retry = true) { - _logger.LogInformation($"Expanding path `{path}`"); + _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 => { + 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}`"); - int tries = 0; + _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"); diff --git a/MatrixRoomUtils.Desktop/MRUStorageWrapper.cs b/MatrixRoomUtils.Desktop/MRUStorageWrapper.cs index 5444f24..2243092 100644 --- a/MatrixRoomUtils.Desktop/MRUStorageWrapper.cs +++ b/MatrixRoomUtils.Desktop/MRUStorageWrapper.cs @@ -4,31 +4,20 @@ using LibMatrix.Services; namespace MatrixRoomUtils.Desktop; -public class MRUStorageWrapper { - private readonly TieredStorageService _storageService; - private readonly HomeserverProviderService _homeserverProviderService; - - public MRUStorageWrapper( - TieredStorageService storageService, - HomeserverProviderService homeserverProviderService - ) { - _storageService = storageService; - _homeserverProviderService = homeserverProviderService; - } - +public class MRUStorageWrapper(TieredStorageService storageService, HomeserverProviderService homeserverProviderService) { public async Task<List<LoginResponse>?> GetAllTokens() { - if(!await _storageService.DataStorageProvider.ObjectExistsAsync("mru.tokens")) { + if(!await storageService.DataStorageProvider.ObjectExistsAsync("mru.tokens")) { return null; } - return await _storageService.DataStorageProvider.LoadObjectAsync<List<LoginResponse>>("mru.tokens") ?? + return await storageService.DataStorageProvider.LoadObjectAsync<List<LoginResponse>>("mru.tokens") ?? new List<LoginResponse>(); } public async Task<LoginResponse?> GetCurrentToken() { - if(!await _storageService.DataStorageProvider.ObjectExistsAsync("token")) { + if(!await storageService.DataStorageProvider.ObjectExistsAsync("token")) { return null; } - var currentToken = await _storageService.DataStorageProvider.LoadObjectAsync<LoginResponse>("token"); + var currentToken = await storageService.DataStorageProvider.LoadObjectAsync<LoginResponse>("token"); var allTokens = await GetAllTokens(); if (allTokens is null or { Count: 0 }) { await SetCurrentToken(null); @@ -47,13 +36,10 @@ public class MRUStorageWrapper { } public async Task AddToken(LoginResponse loginResponse) { - var tokens = await GetAllTokens(); - if (tokens == null) { - tokens = new List<LoginResponse>(); - } + var tokens = await GetAllTokens() ?? new List<LoginResponse>(); tokens.Add(loginResponse); - await _storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens); + await storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens); if(await GetCurrentToken() is null) await SetCurrentToken(loginResponse); } @@ -64,7 +50,7 @@ public class MRUStorageWrapper { return null; } - return await _homeserverProviderService.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken); + return await homeserverProviderService.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken); } public async Task<AuthenticatedHomeServer?> GetCurrentSessionOrPrompt() { @@ -87,7 +73,7 @@ public class MRUStorageWrapper { if (session is null) { // _navigationManager.NavigateTo("/Login"); var wnd = new LoginWindow(this); - wnd.ShowDialog(MainWindow.Instance); + await wnd.ShowDialog(MainWindow.Instance); while (wnd.IsVisible) await Task.Delay(100); session = await GetCurrentSession(); } @@ -112,16 +98,14 @@ public class MRUStorageWrapper { } tokens.RemoveAll(x => x.AccessToken == auth.AccessToken); - await _storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens); + await storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens); } - public async Task SetCurrentToken(LoginResponse? auth) { - _storageService.DataStorageProvider.SaveObjectAsync("token", auth); - } + public async Task SetCurrentToken(LoginResponse? auth) => await storageService.DataStorageProvider.SaveObjectAsync("token", auth); public async Task<LoginResponse?> Login(string homeserver, string username, string password) { try { - return await _homeserverProviderService.Login(homeserver, username, password); + return await homeserverProviderService.Login(homeserver, username, password); } catch (MatrixException e) { if (e.ErrorCode == "M_FORBIDDEN") { diff --git a/MatrixRoomUtils.Desktop/MainWindow.axaml b/MatrixRoomUtils.Desktop/MainWindow.axaml index 464fc77..dd807b5 100644 --- a/MatrixRoomUtils.Desktop/MainWindow.axaml +++ b/MatrixRoomUtils.Desktop/MainWindow.axaml @@ -3,6 +3,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:desktop="clr-namespace:MatrixRoomUtils.Desktop" + xmlns:components="clr-namespace:MatrixRoomUtils.Desktop.Components" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="MatrixRoomUtils.Desktop.MainWindow" Title="Rory&::MatrixRoomUtils"> @@ -11,5 +12,5 @@ <!-- <InvokeCommandAction Command="{Binding LoadedCommand}"></InvokeCommandAction> --> <!-- </EventTriggerBehavior> --> <!-- </Interaction.Behaviors> --> - <desktop:NavigationStack x:Name="windowContent"/> + <components:NavigationStack x:Name="windowContent"/> </Window> diff --git a/MatrixRoomUtils.Desktop/MainWindow.axaml.cs b/MatrixRoomUtils.Desktop/MainWindow.axaml.cs index 89f9d52..135542b 100644 --- a/MatrixRoomUtils.Desktop/MainWindow.axaml.cs +++ b/MatrixRoomUtils.Desktop/MainWindow.axaml.cs @@ -1,7 +1,6 @@ using Avalonia.Controls; using Avalonia.Data; using Avalonia.Interactivity; -using MatrixRoomUtils.Web.Classes; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/MatrixRoomUtils.Desktop/Program.cs b/MatrixRoomUtils.Desktop/Program.cs index f692e45..6e299eb 100644 --- a/MatrixRoomUtils.Desktop/Program.cs +++ b/MatrixRoomUtils.Desktop/Program.cs @@ -10,7 +10,7 @@ internal class Program { // SynchronizationContext-reliant code before AppMain is called: things aren't initialized // yet and stuff might break. // [STAThread] - public static async Task Main(string[] args) { + public static Task Main(string[] args) { try { BuildAvaloniaApp() .StartWithClassicDesktopLifetime(args); @@ -20,6 +20,8 @@ internal class Program { Console.WriteLine(e); throw; } + + return Task.CompletedTask; } // Avalonia configuration, don't remove; also used by visual designer. diff --git a/MatrixRoomUtils.Desktop/RoomInfo.cs b/MatrixRoomUtils.Desktop/RoomInfo.cs index fdd7d03..4e76247 100644 --- a/MatrixRoomUtils.Desktop/RoomInfo.cs +++ b/MatrixRoomUtils.Desktop/RoomInfo.cs @@ -2,7 +2,7 @@ using LibMatrix; using LibMatrix.Responses; using LibMatrix.RoomTypes; -namespace MatrixRoomUtils.Web.Classes; +namespace MatrixRoomUtils.Desktop; public class RoomInfo { public RoomInfo() { } @@ -17,10 +17,10 @@ public class RoomInfo { public async Task<StateEventResponse?> GetStateEvent(string type, string stateKey = "") { var @event = StateEvents.FirstOrDefault(x => x.Type == type && x.StateKey == stateKey); if (@event is not null) return @event; - @event = new StateEventResponse() { + @event = new StateEventResponse { RoomId = Room.RoomId, Type = type, - StateKey = stateKey, + StateKey = stateKey }; try { @event.TypedContent = await Room.GetStateAsync<object>(type, stateKey); diff --git a/MatrixRoomUtils.Desktop/SentryService.cs b/MatrixRoomUtils.Desktop/SentryService.cs index ed96697..648946c 100644 --- a/MatrixRoomUtils.Desktop/SentryService.cs +++ b/MatrixRoomUtils.Desktop/SentryService.cs @@ -6,8 +6,8 @@ namespace MatrixRoomUtils.Desktop; public class SentryService : IDisposable { private IDisposable? _sentrySdkDisposable; - public SentryService(IServiceScopeFactory scopeFactory, ILogger<SentryService> logger) { - MRUDesktopConfiguration config = scopeFactory.CreateScope().ServiceProvider.GetRequiredService<MRUDesktopConfiguration>(); + public SentryService(IServiceScopeFactory scopeFactory, ILogger logger) { + var config = scopeFactory.CreateScope().ServiceProvider.GetRequiredService<MRUDesktopConfiguration>(); if (config.SentryDsn is null) { logger.LogWarning("Sentry DSN is not set, skipping Sentry initialisation"); return; |