@using MatrixRoomUtils.Core.Authentication
@using System.Text.Json
@using MatrixRoomUtils.Core.Extensions
@if (ShowOwnProfile)
{
@profileName
->
}
@roomName
@if (ChildContent != null)
{
@ChildContent
}
@code {
[Parameter]
public RenderFragment? ChildContent { get; set; }
[Parameter]
public Room Room { get; set; }
[Parameter]
public string RoomId { get; set; }
[Parameter]
public bool ShowOwnProfile { get; set; } = false;
private string roomName { get; set; } = "Loading...";
private string roomIcon { get; set; } = "/icon-192.png";
private string profileAvatar { get; set; } = "/icon-192.png";
private string profileName { get; set; } = "Loading...";
private bool hasCustomProfileAvatar { get; set; } = false;
private bool hasCustomProfileName { get; set; } = false;
private bool hasOldRoomVersion { get; set; } = false;
private bool hasDangerousRoomVersion { get; set; } = false;
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
if (!RuntimeCache.WasLoaded)
{
Console.WriteLine("Loading from local storage");
await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
}
if (Room == null)
{
if (RoomId == null)
{
throw new ArgumentNullException(nameof(RoomId));
}
Room = await RuntimeCache.CurrentHomeServer.GetRoom(RoomId);
}
else
{
RoomId = Room.RoomId;
}
roomName = await Room.GetNameAsync();
if (roomName == null)
{
roomName = "Unnamed room: " + RoomId;
}
var ce = await Room.GetCreateEventAsync();
if (ce != null)
{
if (int.TryParse(ce.RoomVersion, out int rv) && rv < 10)
{
hasOldRoomVersion = true;
}
if (new[] { "1", "8" }.Contains(ce.RoomVersion))
{
hasDangerousRoomVersion = true;
roomName = "Dangerous room: " + roomName;
}
}
var state = await Room.GetStateAsync("m.room.avatar");
if (state != null)
{
try
{
var url = state.Value.GetProperty("url").GetString();
if (url != null)
{
roomIcon = RuntimeCache.CurrentHomeServer.ResolveMediaUri(url);
}
}
catch (InvalidOperationException e)
{
Console.WriteLine($"Failed to get avatar for room {RoomId}: {e.Message}\n{state.Value.ToJson()}");
}
}
if (ShowOwnProfile)
{
var profile = await RuntimeCache.CurrentHomeServer.GetProfile(RuntimeCache.CurrentHomeServer.UserId, debounce: true);
var memberState = await Room.GetStateAsync("m.room.member", RuntimeCache.CurrentHomeServer.UserId);
if (memberState.HasValue)
{
memberState.Value.TryGetProperty("avatar_url", out var _avatar);
if (_avatar.ValueKind == JsonValueKind.String)
{
hasCustomProfileAvatar = _avatar.GetString() != profile.AvatarUrl;
profileAvatar = RuntimeCache.CurrentHomeServer.ResolveMediaUri(_avatar.GetString());
}
else
{
profileAvatar = "/icon-192.png";
}
memberState.Value.TryGetProperty("displayname", out var _name);
if (_name.ValueKind == JsonValueKind.String)
{
hasCustomProfileName = _name.GetString() != profile.DisplayName;
profileName = _name.GetString();
// Console.WriteLine($"{profile.DisplayName} - {_name.GetString()}: {hasCustomProfileName}");
}
else
{
profileName = "Unnamed user";
}
}
}
if (Random.Shared.Next(100) == 1)
await LocalStorageWrapper.SaveToLocalStorage(LocalStorage);
}
}