diff options
author | Rory& <root@rory.gay> | 2024-01-24 02:31:56 +0100 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-01-24 17:05:25 +0100 |
commit | 03313562d21d5db9bf6a14ebbeab80e06c883d3a (patch) | |
tree | e000546a2ee8e6a886a7ed9fd01ad674178fb7cb /MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs | |
parent | Make RMU installable (diff) | |
download | MatrixUtils-03313562d21d5db9bf6a14ebbeab80e06c883d3a.tar.xz |
MRU->RMU, fixes, cleanup
Diffstat (limited to 'MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs')
-rw-r--r-- | MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs b/MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs new file mode 100644 index 0000000..1e4a127 --- /dev/null +++ b/MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs @@ -0,0 +1,74 @@ +using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Media.Imaging; +using LibMatrix; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.EventTypes.Spec.State.RoomInfo; +using LibMatrix.Helpers; +using LibMatrix.Interfaces.Services; +using LibMatrix.Services; +using MatrixUtils.Abstractions; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace MatrixUtils.Desktop.Components; + +public partial class RoomListEntry : UserControl { + public RoomInfo Room { get; set; } + + public RoomListEntry() { + InitializeComponent(); + } + + protected override void OnLoaded(RoutedEventArgs e) { + base.OnLoaded(e); + RoomName.Content = Room.Room.RoomId; + Task.WhenAll(GetRoomName(), GetRoomIcon()); + } + + private async Task GetRoomName() { + try { + var nameEvent = await Room.GetStateEvent("m.room.name"); + if (nameEvent?.TypedContent is RoomNameEventContent nameData) + RoomName.Content = nameData.Name; + } + catch (MatrixException e) { + if (e.ErrorCode != "M_NOT_FOUND") + throw; + } + } + + private async Task GetRoomIcon() { + try { + using var hc = new HttpClient(); + var avatarEvent = await Room.GetStateEvent("m.room.avatar"); + if (avatarEvent?.TypedContent is RoomAvatarEventContent avatarData) { + var mxcUrl = avatarData.Url; + var resolvedUrl = await Room.Room.GetResolvedRoomAvatarUrlAsync(); + + // await using var svc = _serviceScopeFactory.CreateAsyncScope(); + // var hs = await svc.ServiceProvider.GetService<RMUStorageWrapper>()?.GetCurrentSessionOrPrompt()!; + // var hsResolver = svc.ServiceProvider.GetService<HomeserverResolverService>(); + // var storage = svc.ServiceProvider.GetService<TieredStorageService>()?.CacheStorageProvider; + // var resolvedUrl = await hsResolver.ResolveMediaUri(hs.ServerName, mxcUrl); + var storage = new FileStorageProvider("cache"); + var storageKey = $"media/{mxcUrl.Replace("mxc://", "").Replace("/", ".")}"; + try { + if (!await storage.ObjectExistsAsync(storageKey)) + await storage.SaveStreamAsync(storageKey, await hc.GetStreamAsync(resolvedUrl)); + + RoomIcon.Source = new Bitmap(await storage.LoadStreamAsync(storageKey) ?? throw new NullReferenceException()); + } + catch (IOException) { } + catch (MatrixException e) { + if (e.ErrorCode != "M_UNKNOWN") + throw; + } + } + } + catch (MatrixException e) { + if (e.ErrorCode != "M_NOT_FOUND") + throw; + } + } +} |