about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Shared/RoomListItem.razor
blob: 15ca5c0793db0d764460e946cd426f3e2bdc7f41 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
@using MatrixRoomUtils.Core.Authentication
@using System.Text.Json
<div style="background-color: #ffffff11; border-radius: 25px; margin: 8px; width: fit-content;">
    @if (ShowOwnProfile)
    {
        <img style="width: 32px; height:  32px; border-radius: 50%; @(hasCustomProfileAvatar ? "border-color: red; border-width: 3px; border-style: dashed;" : "")" src="@profileAvatar"/>
        <span style="vertical-align: middle; margin-right: 8px; border-radius: 75px; @(hasCustomProfileName ? "background-color: red;" : "")">@profileName</span>
        <span style="vertical-align: middle; padding-right: 8px; padding-left: 0px;">-></span>
    }
    <img style="width: 32px; height:  32px; border-radius: 50%;" src="@roomIcon"/>
    <span style="vertical-align: middle; padding-right: 8px;">@roomName</span>
</div>

@code {

    [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;

    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);
        }

        roomName = await Room.GetNameAsync();
        if (roomName == null)
        {
            roomName = "Unnamed room: " + RoomId;
        }

        var state = await Room.GetStateAsync("m.room.avatar");
        if (state != null)
        {
            var url = state.Value.GetProperty("url").GetString();
            if (url != null)
            {
                roomIcon = await RuntimeCache.CurrentHomeServer.ResolveMediaUri(url);
            }
        }

        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 = await 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);
    }

}