From 03313562d21d5db9bf6a14ebbeab80e06c883d3a Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 24 Jan 2024 02:31:56 +0100 Subject: MRU->RMU, fixes, cleanup --- .../FileStorageProvider.cs | 49 ---------- .../MatrixRoomUtils.Abstractions.csproj | 12 --- MatrixRoomUtils.Abstractions/RoomInfo.cs | 100 --------------------- 3 files changed, 161 deletions(-) delete mode 100644 MatrixRoomUtils.Abstractions/FileStorageProvider.cs delete mode 100644 MatrixRoomUtils.Abstractions/MatrixRoomUtils.Abstractions.csproj delete mode 100644 MatrixRoomUtils.Abstractions/RoomInfo.cs (limited to 'MatrixRoomUtils.Abstractions') diff --git a/MatrixRoomUtils.Abstractions/FileStorageProvider.cs b/MatrixRoomUtils.Abstractions/FileStorageProvider.cs deleted file mode 100644 index 73d5604..0000000 --- a/MatrixRoomUtils.Abstractions/FileStorageProvider.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using System.Text.Json; -using ArcaneLibs.Extensions; -using LibMatrix.Extensions; -using LibMatrix.Interfaces.Services; -using Microsoft.Extensions.Logging; - -namespace MatrixRoomUtils.Abstractions; - -public class FileStorageProvider : IStorageProvider { - private readonly ILogger _logger; - - public string TargetPath { get; } - - /// - /// Creates a new instance of . - /// - /// - public FileStorageProvider(string targetPath) { - // new Logger(new LoggerFactory()).LogInformation("test"); - Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); - TargetPath = targetPath; - if (!Directory.Exists(targetPath)) { - Directory.CreateDirectory(targetPath); - } - } - - public async Task SaveObjectAsync(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); - - [RequiresUnreferencedCode("This API uses reflection to deserialize JSON")] - public async Task LoadObjectAsync(string key) => JsonSerializer.Deserialize(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); - - public Task ObjectExistsAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key))); - - public Task> 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 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 Task LoadStreamAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key)) ? File.OpenRead(Path.Join(TargetPath, key)) : null); -} diff --git a/MatrixRoomUtils.Abstractions/MatrixRoomUtils.Abstractions.csproj b/MatrixRoomUtils.Abstractions/MatrixRoomUtils.Abstractions.csproj deleted file mode 100644 index 1665ff0..0000000 --- a/MatrixRoomUtils.Abstractions/MatrixRoomUtils.Abstractions.csproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - net8.0 - enable - enable - - - - - - diff --git a/MatrixRoomUtils.Abstractions/RoomInfo.cs b/MatrixRoomUtils.Abstractions/RoomInfo.cs deleted file mode 100644 index 6db3447..0000000 --- a/MatrixRoomUtils.Abstractions/RoomInfo.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System.Collections.ObjectModel; -using System.Text.Json.Nodes; -using ArcaneLibs; -using LibMatrix; -using LibMatrix.EventTypes.Spec.State; -using LibMatrix.EventTypes.Spec.State.RoomInfo; -using LibMatrix.RoomTypes; - -namespace MatrixRoomUtils.Abstractions; - -public class RoomInfo : NotifyPropertyChanged { - public required GenericRoom Room { get; set; } - public ObservableCollection StateEvents { get; } = new(); - - public async Task 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 { - RoomId = Room.RoomId, - Type = type, - StateKey = stateKey, - Sender = null, //TODO implement - EventId = null - }; - // if (Room is null) return null; - try { - @event.RawContent = await Room.GetStateAsync(type, stateKey); - } - catch (MatrixException e) { - if (e is { ErrorCode: "M_NOT_FOUND" }) { - if (type == "m.room.name") - @event = new() { - Type = type, - StateKey = stateKey, - TypedContent = new RoomNameEventContent() { - Name = await Room.GetNameOrFallbackAsync() - }, - //TODO implement - RoomId = null, - Sender = null, - EventId = null - }; - else - @event.RawContent = default!; - } - else { - throw; - } - } - - StateEvents.Add(@event); - return @event; - } - - public string? RoomIcon { - get => _roomIcon ?? "https://api.dicebear.com/6.x/identicon/svg?seed=" + Room.RoomId; - set => SetField(ref _roomIcon, value); - } - - public string? RoomName { - get => _roomName ?? DefaultRoomName ?? Room.RoomId; - set => SetField(ref _roomName, value); - } - - public RoomCreateEventContent? CreationEventContent { - get => _creationEventContent; - set => SetField(ref _creationEventContent, value); - } - - public string? RoomCreator { - get => _roomCreator; - set => SetField(ref _roomCreator, value); - } - - // public string? GetRoomIcon() => (StateEvents.FirstOrDefault(x => x?.Type == RoomAvatarEventContent.EventId)?.TypedContent as RoomAvatarEventContent)?.Url ?? - // "mxc://rory.gay/dgP0YPjJEWaBwzhnbyLLwGGv"; - - private string? _roomIcon; - private string? _roomName; - private RoomCreateEventContent? _creationEventContent; - private string? _roomCreator; - - public string? DefaultRoomName { get; set; } - - public RoomInfo() { - StateEvents.CollectionChanged += (_, args) => { - if (args.NewItems is { Count: > 0 }) - foreach (StateEventResponse newState in args.NewItems) { // TODO: switch statement benchmark? - if (newState.Type == RoomNameEventContent.EventId && newState.TypedContent is RoomNameEventContent roomNameContent) - RoomName = roomNameContent.Name; - else if (newState is { Type: RoomAvatarEventContent.EventId, TypedContent: RoomAvatarEventContent roomAvatarContent }) - RoomIcon = roomAvatarContent.Url; - else if (newState is { Type: RoomCreateEventContent.EventId, TypedContent: RoomCreateEventContent roomCreateContent }) { - CreationEventContent = roomCreateContent; - RoomCreator = newState.Sender; - } - } - }; - } -} -- cgit 1.5.1