about summary refs log tree commit diff
path: root/MatrixUtils.Web/Shared/RoomListItem.razor
blob: 248cb59f28950b9934c7628b793aecbc053e8e58 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
@using LibMatrix
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.Responses
@using MatrixUtils.Abstractions
@using MatrixUtils.Web.Classes.Constants
@if (RoomInfo is not null) {
    <div class="roomListItem @(HasDangerousRoomVersion ? "dangerousRoomVersion" : HasOldRoomVersion ? "oldRoomVersion" : "")" id="@RoomInfo.Room.RoomId">
        @if (OwnMemberState != null) {
            @* Class="@("avatar32" + (OwnMemberState?.AvatarUrl != GlobalProfile?.AvatarUrl ? " highlightChange" : "") + (ChildContent is not null ? " vcenter" : ""))" *@
            @* <MxcImage Homeserver="hs" Circular="true" Height="32" Width="32" MxcUri="@(OwnMemberState.AvatarUrl ?? GlobalProfile.AvatarUrl)"/> *@
            <MxcAvatar Homeserver="Homeserver" Circular="true" Size="32" MxcUri="@(OwnMemberState.AvatarUrl ?? GlobalProfile.AvatarUrl)"/>
            <span class="centerVertical border75 @(OwnMemberState?.AvatarUrl != GlobalProfile?.AvatarUrl ? "highlightChange" : "")">
                @(OwnMemberState?.DisplayName ?? GlobalProfile?.DisplayName ?? "Loading...")
            </span>
            <span class="centerVertical noLeftPadding">-></span>
        }
        @* <MxcImage Circular="true" Height="32" Width="32" MxcUri="@RoomInfo.RoomIcon" Style="@(ChildContent is not null ? "vertical-align: middle;" : "")"/> *@
        <MxcAvatar Homeserver="Homeserver" Circular="true" Size="32" MxcUri="@RoomInfo.RoomIcon"/>
        <div class="inlineBlock">
            <span class="centerVertical">@RoomInfo.RoomName</span>
            @if (ChildContent is not null) {
                @ChildContent
            }
        </div>

    </div>
}
else {
    <p>Warning: RoomInfo is null!</p>
}

@code {

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    [Parameter]
    public RoomInfo? RoomInfo {
        get => _roomInfo;
        set {
            if (RoomInfo != value)
                RoomInfoChanged();
            _roomInfo = value;
        }
    }

    [Parameter]
    public bool ShowOwnProfile { get; set; } = false;

    [Parameter]
    public RoomMemberEventContent? OwnMemberState { get; set; }

    [CascadingParameter]
    public UserProfileResponse? GlobalProfile { get; set; }

    [Parameter]
    public bool LoadData {
        get => _loadData;
        set {
            _loadData = value;
            OnParametersSetAsync();
        }
    }
    
    [Parameter]
    public AuthenticatedHomeserverGeneric? Homeserver { get; set; }

    private bool HasOldRoomVersion { get; set; } = false;
    private bool HasDangerousRoomVersion { get; set; } = false;

    private static SemaphoreSlim _semaphoreSlim = new(8);
    private RoomInfo? _roomInfo;
    private bool _loadData = false;

    private bool _hooked;

    private async Task RoomInfoChanged() {
        RoomInfo.PropertyChanged += async (_, a) => {
            if (a.PropertyName == nameof(RoomInfo.CreationEventContent)) {
                await CheckRoomVersion();
            }

            StateHasChanged();
        };
    }

    // protected override async Task OnParametersSetAsync() {
    //     if (RoomInfo != null) {
    //         if (!_hooked) {
    //             _hooked = true;
    //             RoomInfo.PropertyChanged += (_, a) => {
    //                 Console.WriteLine(a.PropertyName);
    //                 StateHasChanged();
    //             };
    //         }
    //
    //         if (LoadData) {
    //             try {
    //                 await RoomInfo.GetStateEvent("m.room.create");
    //                 if (ShowOwnProfile)
    //                     OwnMemberState ??= (await RoomInfo.GetStateEvent("m.room.member", hs.WhoAmI.UserId)).TypedContent as RoomMemberEventContent;
    //
    //                 await RoomInfo.GetStateEvent("m.room.name");
    //                 await RoomInfo.GetStateEvent("m.room.avatar");
    //             }
    //             catch (MatrixException e) {
    //                 if (e.ErrorCode == "M_FORBIDDEN") {
    //                     LoadData = false;
    //                     RoomInfo.StateEvents.Add(new() {
    //                         Type = "m.room.create",
    //                         TypedContent = new RoomCreateEventContent() { RoomVersion = "0" }, 
    //                         RoomId = null, Sender = null, EventId = null //TODO: implement
    //                     });
    //                     RoomInfo.StateEvents.Add(new() {
    //                         Type = "m.room.name",
    //                         TypedContent = new RoomNameEventContent() {
    //                             Name = "M_FORBIDDEN: Are you a member of this room? " + RoomInfo.Room.RoomId
    //                         },
    //                         RoomId = null, Sender = null, EventId = null //TODO: implement
    //                     });
    //                 }
    //             }
    //         }
    //     }
    //
    //     await base.OnParametersSetAsync();
    // }

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

        // hs ??= await RMUStorage.GetCurrentSessionOrNavigate();
        // if (hs is null) return;

        if (Homeserver is null) {
            Console.WriteLine($"RoomListItem called without homeserver");
        }
        await CheckRoomVersion();
    }

    private async Task LoadOwnProfile() {
        if (!ShowOwnProfile) return;
        try {
            // OwnMemberState ??= (await RoomInfo.GetStateEvent("m.room.member", hs.UserId)).TypedContent as RoomMemberEventContent;
            GlobalProfile ??= await Homeserver.GetProfileAsync(Homeserver.UserId);
        }
        catch (MatrixException e) {
            if (e is { ErrorCode: "M_FORBIDDEN" }) {
                Console.WriteLine($"Failed to get profile for {Homeserver.UserId}: {e.Message}");
                ShowOwnProfile = false;
            }
            else {
                throw;
            }
        }
    }

    private async Task CheckRoomVersion() {
        if (RoomInfo?.CreationEventContent is null) return;

        var ce = RoomInfo.CreationEventContent;
        if (int.TryParse(ce.RoomVersion, out var rv)) {
            if (rv < 10)
                HasOldRoomVersion = true;
        }
        else // treat unstable room versions as dangerous
            HasDangerousRoomVersion = true;

        if (RoomConstants.DangerousRoomVersions.Contains(ce.RoomVersion)) {
            HasDangerousRoomVersion = true;
            // RoomName = "Dangerous room: " + RoomName;
        }
    }

    // private async Task GetRoomInfo() {
    //     try {
    //         RoomName ??= ((await RoomInfo.GetStateEvent("m.room.name"))?.TypedContent as RoomNameEventContent)?.Name ?? RoomId;
    //
    //         var state = (await RoomInfo.GetStateEvent("m.room.avatar")).TypedContent as RoomAvatarEventContent;
    //         if (state?.Url is { } url) {
    //             RoomIcon = await hsResolver.ResolveMediaUri(hs.ServerName, url);
    // // Console.WriteLine($"Got avatar for room {RoomId}: {roomIcon} ({url})");
    //         }
    //     }
    //     catch (MatrixException e) {
    //         if (e is not { ErrorCode: "M_FORBIDDEN" }) {
    //             throw;
    //         }
    //     }
    // }

}