about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Shared/RoomListItem.razor
blob: 53219d6cf2a963e3195ae58c65f7d447847f2b74 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
@using MatrixRoomUtils.Core.Extensions
@using System.Text.Json
@using MatrixRoomUtils.Core.Helpers
@using MatrixRoomUtils.Core.StateEventTypes
<div class="roomListItem" style="background-color: #ffffff11; border-radius: 25px; margin: 8px; width: fit-Content; @(hasDangerousRoomVersion ? "border: red 4px solid;" : hasOldRoomVersion ? "border: #FF0 1px solid;" : "")">
    @if (ShowOwnProfile) {
        <img class="imageUnloaded @(string.IsNullOrWhiteSpace(profileAvatar) ? "" : "imageLoaded")" style="@(ChildContent is not null ? "vertical-align: baseline;" : "") width: 32px; height: 32px; border-radius: 50%; @(hasCustomProfileAvatar ? "border-color: red; border-width: 3px; border-style: dashed;" : "")" src="@(profileAvatar ?? "/icon-192.png")"/>
        <span style="vertical-align: middle; margin-right: 8px; border-radius: 75px; @(hasCustomProfileName ? "background-color: red;" : "")">@(profileName ?? "Loading...")</span>
        <span style="vertical-align: middle; padding-right: 8px; padding-left: 0px;">-></span>
    }
    <img style="@(ChildContent is not null ? "vertical-align: baseline;" : "") width: 32px; height:  32px; border-radius: 50%;" src="@roomIcon"/>
    <div style="display: inline-block;">
        <span style="vertical-align: middle; padding-right: 8px;">@RoomName</span>
        @if (ChildContent is not null) {
            @ChildContent
        }
    </div>

</div>

@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 string? RoomName { get; set; }

    private string? roomIcon { get; set; } = "/icon-192.png";

    private string? profileAvatar { get; set; }
    private string? profileName { get; set; }
    private bool hasCustomProfileAvatar { get; set; } = false;
    private bool hasCustomProfileName { get; set; } = false;

    private bool hasOldRoomVersion { get; set; } = false;
    private bool hasDangerousRoomVersion { get; set; } = false;

    private static SemaphoreSlim _semaphoreSlim = new(128);

    protected override async Task OnInitializedAsync() {
        await base.OnInitializedAsync();

        await _semaphoreSlim.WaitAsync();

        var hs = await MRUStorage.GetCurrentSessionOrNavigate();
        if (hs is null) return;

        if (Room == null) {
            if (RoomId == null) {
                throw new ArgumentNullException(nameof(RoomId));
            }
            Room = await hs.GetRoom(RoomId);
        }
        else {
            RoomId = Room.RoomId;
        }

        RoomName ??= await Room.GetNameAsync() ?? "Unnamed room: " + RoomId;

        var ce = await Room.GetCreateEventAsync();
        if (ce is not null) {
            if (int.TryParse(ce.RoomVersion, out var rv) && rv < 10) {
                hasOldRoomVersion = true;
            }
            if (new[] { "1", "8" }.Contains(ce.RoomVersion)) {
                hasDangerousRoomVersion = true;
                RoomName = "Dangerous room: " + RoomName;
            }
        }

        var state = await Room.GetStateAsync<RoomAvatarEventData>("m.room.avatar");
        if (state is not null) {
            try {
                var url = state.Url;
                if (url is not null) {
                    roomIcon = MediaResolver.ResolveMediaUri(hs.FullHomeServerDomain, url);
                    Console.WriteLine($"Got avatar for room {RoomId}: {roomIcon} ({url})");
                }
            }
            catch (InvalidOperationException e) {
                Console.WriteLine($"Failed to get avatar for room {RoomId}: {e.Message}\n{state.ToJson()}");
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }
        }

        if (ShowOwnProfile) {
            var profile = await hs.GetProfile(hs.UserId, true);

            var memberState = await Room.GetStateAsync<RoomMemberEventData>("m.room.member", hs.UserId);
            if (memberState is not null) {
                
                hasCustomProfileAvatar = memberState.AvatarUrl != profile.AvatarUrl;
                profileAvatar = MediaResolver.ResolveMediaUri(hs.FullHomeServerDomain, memberState.AvatarUrl ?? profile.AvatarUrl ?? "/icon-192.png");
                
                hasCustomProfileName = memberState.Displayname != profile.DisplayName;
                profileName = memberState.Displayname;
            }
        }
        _semaphoreSlim.Release();
    }

}