@using MatrixRoomUtils.Core.Extensions
@using System.Text.Json
@using MatrixRoomUtils.Core.Helpers
@using MatrixRoomUtils.Core.StateEventTypes
@if (OwnMemberState != null) {
@(OwnMemberState?.Displayname ?? GlobalProfile?.DisplayName ?? "Loading...")
->
}
@roomName
@if (ChildContent is not null) {
@ChildContent
}
@code {
[Parameter]
public RenderFragment? ChildContent { get; set; }
[Parameter]
public GenericRoom? Room { get; set; }
[Parameter]
public string? RoomId { get; set; }
[Parameter]
public bool ShowOwnProfile { get; set; } = false;
[Parameter]
public RoomMemberEventData? OwnMemberState { get; set; }
[Parameter]
public ProfileResponse? GlobalProfile { get; set; }
private string? roomName { get; set; }
private string? roomIcon { get; set; } = "/icon-192.png";
private bool hasOldRoomVersion { get; set; } = false;
private bool hasDangerousRoomVersion { get; set; } = false;
private static SemaphoreSlim _semaphoreSlim = new(8);
private static AuthenticatedHomeServer? hs { get; set; }
private static readonly string[] DangerousRoomVersions = { "1", "8" };
protected override async Task OnInitializedAsync() {
await base.OnInitializedAsync();
await _semaphoreSlim.WaitAsync();
hs ??= await MRUStorage.GetCurrentSessionOrNavigate();
if (hs is null) return;
if (Room is null && RoomId is null) {
throw new ArgumentNullException(nameof(RoomId));
}
Room ??= await hs.GetRoom(RoomId);
RoomId = Room.RoomId;
await CheckRoomVersion();
await GetRoomInfo();
await LoadOwnProfile();
_semaphoreSlim.Release();
}
private async Task LoadOwnProfile() {
if (!ShowOwnProfile) return;
try {
OwnMemberState ??= await Room.GetStateAsync("m.room.member", hs.UserId);
GlobalProfile ??= await hs.GetProfile(hs.UserId, true);
}
catch (MatrixException e) {
if (e is { ErrorCode: "M_FORBIDDEN" }) {
Console.WriteLine($"Failed to get profile for {hs.UserId}: {e.Message}");
ShowOwnProfile = false;
}
else {
throw;
}
}
}
private async Task CheckRoomVersion() {
try {
var ce = await Room.GetCreateEventAsync();
if (int.TryParse(ce.RoomVersion, out var rv)) {
if (rv < 10)
hasOldRoomVersion = true;
}
else // treat unstable room versions as dangerous
hasDangerousRoomVersion = true;
if (DangerousRoomVersions.Contains(ce.RoomVersion)) {
hasDangerousRoomVersion = true;
roomName = "Dangerous room: " + roomName;
}
}
catch (MatrixException e) {
if (e is not { ErrorCode: "M_FORBIDDEN" }) {
throw;
}
}
}
private async Task GetRoomInfo() {
try {
roomName ??= await Room.GetNameAsync();
var state = await Room.GetStateAsync("m.room.avatar");
if (state?.Url is { } url) {
roomIcon = MediaResolver.ResolveMediaUri(hs.FullHomeServerDomain, url);
Console.WriteLine($"Got avatar for room {RoomId}: {roomIcon} ({url})");
}
}
catch (MatrixException e) {
if (e is not { ErrorCode: "M_FORBIDDEN" }) {
throw;
}
}
}
}