about summary refs log tree commit diff
path: root/ModerationClient/ViewModels
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-03-12 04:44:19 +0100
committerRory& <root@rory.gay>2025-03-12 04:44:19 +0100
commit1a224441228d07440f279ce4a15c9a043b8cda6d (patch)
tree3ea4f4e2b694bf4a64079b084ea41d3c363937bb /ModerationClient/ViewModels
parentVarious work (diff)
downloadModerationClient-1a224441228d07440f279ce4a15c9a043b8cda6d.tar.xz
Part of event rendering
Diffstat (limited to 'ModerationClient/ViewModels')
-rw-r--r--ModerationClient/ViewModels/ClientViewModel.cs20
-rw-r--r--ModerationClient/ViewModels/RoomViewModel.cs82
2 files changed, 57 insertions, 45 deletions
diff --git a/ModerationClient/ViewModels/ClientViewModel.cs b/ModerationClient/ViewModels/ClientViewModel.cs

index ab4f2da..9403123 100644 --- a/ModerationClient/ViewModels/ClientViewModel.cs +++ b/ModerationClient/ViewModels/ClientViewModel.cs
@@ -175,7 +175,14 @@ public partial class ClientViewModel : ViewModelBase { if (string.IsNullOrWhiteSpace(AllRooms[room.Key].Name)) { AllRooms[room.Key].Name = "Loading..."; - tasks.Add(_authService.Homeserver!.GetRoom(room.Key).GetNameOrFallbackAsync().ContinueWith(r => AllRooms[room.Key].Name = r.Result)); + tasks.Add(_authService.Homeserver!.GetRoom(room.Key).GetNameOrFallbackAsync().ContinueWith(r => { + if (r.IsFaulted) { + _logger.LogError(r.Exception, "Error getting room name for {RoomKey}", room.Key); + return AllRooms[room.Key].Name = "Error loading room name"; + } + + return AllRooms[room.Key].Name = r.Result; + })); // Status = $"Getting room name for {room.Key}..."; // AllRooms[room.Key].Name = await _authService.Homeserver!.GetRoom(room.Key).GetNameOrFallbackAsync(); } @@ -196,7 +203,8 @@ public partial class ClientViewModel : ViewModelBase { await AwaitTasks(tasks, "Waiting for {0}/{1} tasks while applying room changes..."); } - private ExpiringSemaphoreCache<UserProfileResponse> _profileCache = new(); + private ExpiringSemaphoreCache<UserProfileResponse> _profileCache = new(); + private async Task ApplyDirectMessagesChanges(StateEventResponse evt) { _logger.LogCritical("Direct messages updated!"); var dms = evt.RawContent.Deserialize<Dictionary<string, string[]?>>(); @@ -207,9 +215,13 @@ public partial class ClientViewModel : ViewModelBase { if (space is null) { space = new SpaceNode { Name = userId, RoomID = userId }; // tasks.Add(_authService.Homeserver!.GetProfileAsync(userId) - // .ContinueWith(r => space.Name = string.IsNullOrWhiteSpace(r.Result.DisplayName) ? userId : r.Result.DisplayName)); + // .ContinueWith(r => space.Name = string.IsNullOrWhiteSpace(r.Result.DisplayName) ? userId : r.Result.DisplayName)); tasks.Add(_profileCache.GetOrAdd(userId, async () => await _authService.Homeserver!.GetProfileAsync(userId), TimeSpan.FromMinutes(5)) - .ContinueWith(r => string.IsNullOrWhiteSpace(r.Result.DisplayName) ? userId : r.Result.DisplayName)); + .ContinueWith(r => { + if (!r.IsFaulted) + return string.IsNullOrWhiteSpace(r.Result.DisplayName) ? userId : r.Result.DisplayName; + return userId; + })); DirectMessages.ChildSpaces.Add(space); } diff --git a/ModerationClient/ViewModels/RoomViewModel.cs b/ModerationClient/ViewModels/RoomViewModel.cs
index c18b842..2ce0363 100644 --- a/ModerationClient/ViewModels/RoomViewModel.cs +++ b/ModerationClient/ViewModels/RoomViewModel.cs
@@ -6,6 +6,8 @@ using System.Collections.Specialized; using System.ComponentModel; using System.IO; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using ArcaneLibs; using Avalonia; using Avalonia.Media.Imaging; @@ -63,50 +65,48 @@ public class RoomMember(AuthenticatedHomeserverGeneric homeserver) : NotifyPrope get; set { field = value; - - if(!Directory.Exists(MediaDir)) - Directory.CreateDirectory(MediaDir); - - var fsPath = Path.Combine(MediaDir, AvatarUrl.Replace("/", "_")); - Console.WriteLine($"Avatar path for {UserId}: {fsPath}"); - if (File.Exists(fsPath)) { - UserAvatarBitmap = new Bitmap(fsPath); - Console.WriteLine($"Avatar bitmap for {UserId} loaded: {UserAvatarBitmap.GetHashCode()}, Path: {fsPath}"); - return; - } - - homeserver.GetMediaStreamAsync(field).ContinueWith(async streamTask => { - try { - await using var stream = await streamTask; - Console.WriteLine($"Avatar stream for {UserId} received: {stream.GetHashCode()}"); - // await using var ms = new MemoryStream(); - // await stream.CopyToAsync(ms); - // var fs = new FileStream("avatar.png", FileMode.Create); - // ms.Seek(0, SeekOrigin.Begin); - // Console.WriteLine($"Avatar stream for {UserId} copied: {ms.GetHashCode()}"); - // var bm = new Bitmap(ms); - // Console.WriteLine($"Avatar bitmap for {UserId} loaded: {bm.GetHashCode()}"); - // var sbm = bm.CreateScaledBitmap(new(32, 32)); - // Console.WriteLine($"Avatar bitmap for {UserId} loaded: {sbm.GetHashCode()}"); - // UserAvatarBitmap = sbm; - // Console.WriteLine($"Bitmap for {UserId} set to {UserAvatarBitmap.GetHashCode()}"); - - var fs = new FileStream(fsPath, FileMode.Create); - await stream.CopyToAsync(fs); - fs.Close(); - UserAvatarBitmap = new Bitmap(fsPath); - Console.WriteLine($"Avatar bitmap for {UserId} loaded: {UserAvatarBitmap.GetHashCode()}, Path: {fsPath}"); - } - catch (Exception e) { - Console.WriteLine($"Failed to load avatar for {UserId}: {e}"); - UserAvatarBitmap = new Bitmap(PixelFormat.Rgba8888, AlphaFormat.Unpremul, 1, PixelSize.Empty, Vector.Zero, 0); - } - }); - // UserAvatarBitmap = new Bitmap(); + _ = UpdateAvatar(); + } + } + + private static readonly SemaphoreSlim MediaFetchLock = new(16, 16); + private async Task UpdateAvatar() { + if (string.IsNullOrEmpty(AvatarUrl)) { + UserAvatarBitmap = new WriteableBitmap(new PixelSize(1, 1), Vector.One); + return; + } + + if (!Directory.Exists(MediaDir)) + Directory.CreateDirectory(MediaDir); + + var fsPath = Path.Combine(MediaDir, AvatarUrl.Replace("/", "_")); + Console.WriteLine($"Avatar path for {UserId}: {fsPath}"); + if (File.Exists(fsPath)) { + UserAvatarBitmap = new(fsPath); + Console.WriteLine($"Avatar bitmap for {UserId} loaded: {UserAvatarBitmap.GetHashCode()}, Path: {fsPath}"); + return; + } + + await MediaFetchLock.WaitAsync(); + try { + var stream = await homeserver.GetMediaStreamAsync(AvatarUrl); + Console.WriteLine($"Avatar stream for {UserId} received: {stream.GetHashCode()}"); + var fs = new FileStream(fsPath, FileMode.Create); + await stream.CopyToAsync(fs); + fs.Close(); + UserAvatarBitmap = new Bitmap(fsPath); + Console.WriteLine($"Avatar bitmap for {UserId} loaded: {UserAvatarBitmap.GetHashCode()}, Path: {fsPath}"); + } + catch (Exception e) { + Console.WriteLine($"Failed to load avatar for {UserId}: {e}"); + UserAvatarBitmap = new WriteableBitmap(new PixelSize(1, 1), Vector.One); + } + finally { + MediaFetchLock.Release(); } } - public Bitmap UserAvatarBitmap { + public Bitmap? UserAvatarBitmap { get; private set => SetField(ref field, value); }