blob: aaa1bee20e1d3ecc83537f66ad4d3392e1da37eb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media.Imaging;
using LibMatrix;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Helpers;
using LibMatrix.Interfaces.Services;
using LibMatrix.Services;
using Microsoft.Extensions.DependencyInjection;
namespace MatrixRoomUtils.Desktop.Components;
public partial class RoomListEntry : UserControl {
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly RoomInfo _roomInfo;
public RoomListEntry(IServiceScopeFactory serviceScopeFactory, RoomInfo roomInfo) {
_serviceScopeFactory = serviceScopeFactory;
_roomInfo = roomInfo;
InitializeComponent();
}
protected override void OnLoaded(RoutedEventArgs e) {
base.OnLoaded(e);
RoomName.Content = _roomInfo.Room.RoomId;
Task.WhenAll(GetRoomName(), GetRoomIcon());
}
private async Task GetRoomName() {
try {
var nameEvent = await _roomInfo.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 {
var avatarEvent = await _roomInfo.GetStateEvent("m.room.avatar");
if (avatarEvent?.TypedContent is RoomAvatarEventContent avatarData) {
var mxcUrl = avatarData.Url;
await using var svc = _serviceScopeFactory.CreateAsyncScope();
var hs = await svc.ServiceProvider.GetService<MRUStorageWrapper>()?.GetCurrentSessionOrPrompt()!;
var hsResolver = svc.ServiceProvider.GetService<HomeserverResolverService>();
var storage = svc.ServiceProvider.GetService<TieredStorageService>()?.CacheStorageProvider;
var resolvedUrl = await hsResolver.ResolveMediaUri(hs.ServerName, mxcUrl);
var storageKey = $"media/{mxcUrl.Replace("mxc://", "").Replace("/", ".")}";
try {
if (!await storage.ObjectExistsAsync(storageKey))
await storage.SaveStreamAsync(storageKey, await hs.ClientHttpClient.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;
}
}
}
|