RoomListItem.razor (5389B)
1 @using MatrixRoomUtils.Core.Authentication 2 @using System.Text.Json 3 @using MatrixRoomUtils.Core.Extensions 4 <div style="background-color: #ffffff11; border-radius: 25px; margin: 8px; width: fit-Content; @(hasDangerousRoomVersion ? "border: red 4px solid;" : hasOldRoomVersion ? "border: #FF0 1px solid;" : "")"> 5 @if (ShowOwnProfile) 6 { 7 <img style="@(ChildContent != null ? "vertical-align: baseline;":"") width: 32px; height: 32px; border-radius: 50%; @(hasCustomProfileAvatar ? "border-color: red; border-width: 3px; border-style: dashed;" : "")" src="@profileAvatar"/> 8 <span style="vertical-align: middle; margin-right: 8px; border-radius: 75px; @(hasCustomProfileName ? "background-color: red;" : "")">@profileName</span> 9 <span style="vertical-align: middle; padding-right: 8px; padding-left: 0px;">-></span> 10 } 11 <img style="@(ChildContent != null ? "vertical-align: baseline;":"") width: 32px; height: 32px; border-radius: 50%;" src="@roomIcon"/> 12 <div style="display: inline-block;"> 13 <span style="vertical-align: middle; padding-right: 8px;">@roomName</span> 14 @if (ChildContent != null) 15 { 16 @ChildContent 17 } 18 </div> 19 20 </div> 21 22 @code { 23 24 [Parameter] 25 public RenderFragment? ChildContent { get; set; } 26 27 [Parameter] 28 public Room Room { get; set; } 29 30 [Parameter] 31 public string RoomId { get; set; } 32 33 [Parameter] 34 public bool ShowOwnProfile { get; set; } = false; 35 36 private string roomName { get; set; } = "Loading..."; 37 private string roomIcon { get; set; } = "/icon-192.png"; 38 39 private string profileAvatar { get; set; } = "/icon-192.png"; 40 private string profileName { get; set; } = "Loading..."; 41 private bool hasCustomProfileAvatar { get; set; } = false; 42 private bool hasCustomProfileName { get; set; } = false; 43 44 private bool hasOldRoomVersion { get; set; } = false; 45 private bool hasDangerousRoomVersion { get; set; } = false; 46 47 48 private static SemaphoreSlim _semaphoreSlim = new(128); 49 50 protected override async Task OnInitializedAsync() 51 { 52 await base.OnInitializedAsync(); 53 await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); 54 55 await _semaphoreSlim.WaitAsync(); 56 57 var hs = await new AuthenticatedHomeServer(RuntimeCache.CurrentHomeServer.UserId, RuntimeCache.CurrentHomeServer.AccessToken, RuntimeCache.CurrentHomeServer.HomeServerDomain).Configure(); 58 59 if (Room == null) 60 { 61 if (RoomId == null) 62 { 63 throw new ArgumentNullException(nameof(RoomId)); 64 } 65 Room = await hs.GetRoom(RoomId); 66 } 67 else 68 { 69 RoomId = Room.RoomId; 70 } 71 72 roomName = await Room.GetNameAsync(); 73 if (roomName == null) 74 { 75 roomName = "Unnamed room: " + RoomId; 76 } 77 78 var ce = await Room.GetCreateEventAsync(); 79 if (ce != null) 80 { 81 if (int.TryParse(ce.RoomVersion, out int rv) && rv < 10) 82 { 83 hasOldRoomVersion = true; 84 } 85 if (new[] { "1", "8" }.Contains(ce.RoomVersion)) 86 { 87 hasDangerousRoomVersion = true; 88 roomName = "Dangerous room: " + roomName; 89 } 90 } 91 92 93 var state = await Room.GetStateAsync("m.room.avatar"); 94 if (state != null) 95 { 96 try 97 { 98 var url = state.Value.GetProperty("url").GetString(); 99 if (url != null) 100 { 101 roomIcon = hs.ResolveMediaUri(url); 102 Console.WriteLine($"Got avatar for room {RoomId}: {roomIcon} ({url})"); 103 } 104 } 105 catch (InvalidOperationException e) 106 { 107 Console.WriteLine($"Failed to get avatar for room {RoomId}: {e.Message}\n{state.Value.ToJson()}"); 108 } 109 } 110 111 if (ShowOwnProfile) 112 { 113 var profile = await hs.GetProfile(hs.UserId, debounce: true); 114 115 var memberState = await Room.GetStateAsync("m.room.member", hs.UserId); 116 if (memberState.HasValue) 117 { 118 memberState.Value.TryGetProperty("avatar_url", out var _avatar); 119 if (_avatar.ValueKind == JsonValueKind.String) 120 { 121 hasCustomProfileAvatar = _avatar.GetString() != profile.AvatarUrl; 122 profileAvatar = hs.ResolveMediaUri(_avatar.GetString()); 123 } 124 else 125 { 126 profileAvatar = "/icon-192.png"; 127 } 128 memberState.Value.TryGetProperty("displayname", out var _name); 129 if (_name.ValueKind == JsonValueKind.String) 130 { 131 hasCustomProfileName = _name.GetString() != profile.DisplayName; 132 profileName = _name.GetString(); 133 // Console.WriteLine($"{profile.DisplayName} - {_name.GetString()}: {hasCustomProfileName}"); 134 } 135 else 136 { 137 profileName = "Unnamed user"; 138 } 139 } 140 } 141 _semaphoreSlim.Release(); 142 if (Random.Shared.Next(100) == 1) 143 await LocalStorageWrapper.SaveCacheToLocalStorage(LocalStorage); 144 } 145 146 }