about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Shared/RoomListItem.razor
blob: 07f0756ef29ecc0dc3aa079cd950c0bae9909a3f (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
193
194
195
196
@using System.Text.Json
@using LibMatrix
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.Helpers
@using LibMatrix.Homeservers
@using LibMatrix.Responses
@using LibMatrix.RoomTypes
@using MatrixRoomUtils.Abstractions
@using MatrixRoomUtils.Web.Classes.Constants
@if (RoomInfo is not null) {
    <div class="roomListItem @(HasDangerousRoomVersion ? "dangerousRoomVersion" : HasOldRoomVersion ? "oldRoomVersion" : "")" id="@RoomInfo.Room.RoomId">
        @if (OwnMemberState != null) {
            <MxcImage Class="@("avatar32" + (OwnMemberState?.AvatarUrl != GlobalProfile?.AvatarUrl ? " highlightChange" : "") + (ChildContent is not null ? " vcenter" : ""))"
                      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 Class="avatar32" MxcUri="@RoomInfo.RoomIcon" Style="@(ChildContent is not null ? "vertical-align: middle;" : "")"/>
        <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 {
            _roomInfo = value;
            OnParametersSetAsync();
        }
    }

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

    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 static AuthenticatedHomeserverGeneric? hs { get; set; }

    private bool _hooked;
    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();

        await _semaphoreSlim.WaitAsync();

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

        try {
            await CheckRoomVersion();
    // await GetRoomInfo();
    // await LoadOwnProfile();
        }
        catch (MatrixException e) {
            if (e is not { ErrorCode: "M_FORBIDDEN" }) {
                throw;
            }
    // RoomName = "Error: " + e.Message;
    // RoomIcon = "/blobfox_outage.gif";
        }
        catch (Exception e) {
            Console.WriteLine($"Failed to load room info for {RoomInfo.Room.RoomId}: {e.Message}");
        }
        _semaphoreSlim.Release();
    }

    private async Task LoadOwnProfile() {
        if (!ShowOwnProfile) return;
        try {
    // OwnMemberState ??= (await RoomInfo.GetStateEvent("m.room.member", hs.UserId)).TypedContent as RoomMemberEventContent;
            GlobalProfile ??= await hs.GetProfileAsync(hs.UserId);
        }
        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() {
        while (RoomInfo?.CreationEventContent is null) {
            Console.WriteLine($"Room creation event content for {RoomInfo.Room.RoomId} is null...");
            await Task.Delay(Random.Shared.Next(1000, 2500));
        }
        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;
    //         }
    //     }
    // }

}