about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/User/Profile.razor
blob: 79b83aec29128c0df2f4c495f701e68e3e2dce50 (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
@page "/User/Profile"
@using LibMatrix.EventTypes.Spec.State
@using ArcaneLibs.Extensions
@using LibMatrix
@using LibMatrix.Responses
@using MatrixUtils.Abstractions
<h3>Manage Profile - @Homeserver?.WhoAmI?.UserId</h3>
<hr/>

@if (NewProfile is not null) {
    <h4>Profile</h4>
    <hr/>
    <div>
        <img src="@Homeserver.ResolveMediaUri(NewProfile.AvatarUrl)" style="width: 96px; height: 96px; border-radius: 50%; object-fit: cover;"/>
        <div style="display: inline-block; vertical-align: middle;">
            <span>Display name: </span><FancyTextBox @bind-Value="@NewProfile.DisplayName"></FancyTextBox><br/>
            <span>Avatar URL: </span><FancyTextBox @bind-Value="@NewProfile.AvatarUrl"></FancyTextBox>
            <InputFile OnChange="@AvatarChanged"></InputFile><br/>
            <LinkButton OnClick="@(() => UpdateProfile())">Update profile</LinkButton>
            <LinkButton OnClick="@(() => UpdateProfile(true))">Update profile (restore room overrides)</LinkButton>
        </div>
    </div>
    @if (!string.IsNullOrWhiteSpace(Status)) {
        <p>@Status</p>
    }

    <br/>

    @* <details> *@
    <h4>Room profiles<hr></h4>

    @foreach (var room in Rooms) {
        <details class="details-compact">
            <summary style="@(room.OwnMembership?.DisplayName == OldProfile.DisplayName && room.OwnMembership?.AvatarUrl == OldProfile.AvatarUrl ? "" : "#ffff0033")">
                <div style="display: inline-block; width: calc(100% - 50px); vertical-align: middle; margin-top: -8px; margin-bottom: -8px;">
                    <CascadingValue Value="OldProfile">
                        <RoomListItem ShowOwnProfile="true" RoomInfo="@room" OwnMemberState="@room.OwnMembership"></RoomListItem>
                    </CascadingValue>
                </div>
            </summary>
            @if (room.OwnMembership is not null) {
                <img src="@Homeserver.ResolveMediaUri(room.OwnMembership.AvatarUrl)" style="width: 96px; height: 96px; border-radius: 50%; object-fit: cover;"/>
                <div style="display: inline-block; vertical-align: middle;">
                    <span>Display name: </span><FancyTextBox BackgroundColor="@(room.OwnMembership.DisplayName == OldProfile.DisplayName ? "" : "#ffff0033")" @bind-Value="@room.OwnMembership.DisplayName"></FancyTextBox><br/>
                    <span>Avatar URL: </span><FancyTextBox BackgroundColor="@(room.OwnMembership.AvatarUrl == OldProfile.AvatarUrl ? "" : "#ffff0033")" @bind-Value="@room.OwnMembership.AvatarUrl"></FancyTextBox>
                    <InputFile OnChange="@(ifcea => RoomAvatarChanged(ifcea, room.Room.RoomId))"></InputFile><br/>
                    <LinkButton OnClick="@(() => UpdateRoomProfile(room.Room.RoomId))">Update profile</LinkButton>
                </div>
                <br/>
                @if (!string.IsNullOrWhiteSpace(Status)) {
                    <p>@Status</p>
                }
            }
            else {
                <p>Something went wrong, own membership is missing...</p>
            }
        </details>
        <br/>
    }

    @foreach (var (roomId, roomProfile) in RoomProfiles.OrderBy(x => RoomNames.TryGetValue(x.Key, out var _name) ? _name : x.Key)) {
        <details class="details-compact">
            <summary style="@(roomProfile.DisplayName == OldProfile.DisplayName && roomProfile.AvatarUrl == OldProfile.AvatarUrl ? "" : "#ffff0033")">@(RoomNames.TryGetValue(roomId, out var name) ? name : roomId)</summary>
            <img src="@Homeserver.ResolveMediaUri(roomProfile.AvatarUrl)" style="width: 96px; height: 96px; border-radius: 50%; object-fit: cover;"/>
            <div style="display: inline-block; vertical-align: middle;">
                <span>Display name: </span><FancyTextBox BackgroundColor="@(roomProfile.DisplayName == OldProfile.DisplayName ? "" : "#ffff0033")" @bind-Value="@roomProfile.DisplayName"></FancyTextBox><br/>
                <span>Avatar URL: </span><FancyTextBox BackgroundColor="@(roomProfile.AvatarUrl == OldProfile.AvatarUrl ? "" : "#ffff0033")" @bind-Value="@roomProfile.AvatarUrl"></FancyTextBox>
                <InputFile OnChange="@(ifcea => RoomAvatarChanged(ifcea, roomId))"></InputFile><br/>
                <LinkButton OnClick="@(() => UpdateRoomProfile(roomId))">Update profile</LinkButton>
            </div>
            <br/>
            @if (!string.IsNullOrWhiteSpace(Status)) {
                <p>@Status</p>
            }
        </details>
        <br/>
    }
    // </details>
}

@code {
    private string? _status = null;

    private AuthenticatedHomeserverGeneric? Homeserver { get; set; }
    private UserProfileResponse? NewProfile { get; set; }
    private UserProfileResponse? OldProfile { get; set; }

    private string? Status {
        get => _status;
        set {
            _status = value;
            StateHasChanged();
        }
    }

    private List<RoomInfo> Rooms { get; set; } = new();
    private Dictionary<string, RoomMemberEventContent> RoomProfiles { get; set; } = new();
    private Dictionary<string, string> RoomNames { get; set; } = new();

    protected override async Task OnInitializedAsync() {
        Homeserver = await RMUStorage.GetCurrentSessionOrNavigate();
        if (Homeserver is null) return;
        Status = "Loading global profile...";
        if (Homeserver.WhoAmI?.UserId is null) return;
        NewProfile = (await Homeserver.GetProfileAsync(Homeserver.WhoAmI.UserId)); //.DeepClone();
        OldProfile = (await Homeserver.GetProfileAsync(Homeserver.WhoAmI.UserId)); //.DeepClone();
        Status = "Loading room profiles...";
        var roomProfiles = Homeserver.GetRoomProfilesAsync();
        await foreach (var (roomId, roomProfile) in roomProfiles) {
            var room = Homeserver.GetRoom(roomId);
            var roomNameTask = room.GetNameOrFallbackAsync();
            var roomIconTask = room.GetAvatarUrlAsync();
            var roomInfo = new RoomInfo() {
                Room = room,
                OwnMembership = roomProfile
            };
            try {
                roomInfo.RoomIcon = (await roomIconTask).Url;
            }
            catch (MatrixException e) {
                if (e is not { ErrorCode: "M_NOT_FOUND" }) throw;
            }

            try {
                roomInfo.RoomName = await roomNameTask;
            }
            catch (MatrixException e) {
                if (e is not { ErrorCode: "M_NOT_FOUND" }) throw;
            }

            Rooms.Add(roomInfo);
            // Status = $"Got profile for {roomId}...";
            RoomProfiles[roomId] = roomProfile; //.DeepClone();
        }

        StateHasChanged();
        Status = "Room profiles loaded, loading room names...";

        var roomNameTasks = RoomProfiles.Keys.Select(x => Homeserver.GetRoom(x)).Select(async x => {
            var name = await x.GetNameOrFallbackAsync();
            return new KeyValuePair<string, string?>(x.RoomId, name);
        }).ToAsyncEnumerable();

        await foreach (var (roomId, roomName) in roomNameTasks) {
            // Status = $"Got room name for {roomId}: {roomName}";
            RoomNames[roomId] = roomName;
        }

        StateHasChanged();
        Status = null;

        await base.OnInitializedAsync();
    }

    private async Task AvatarChanged(InputFileChangeEventArgs arg) {
        var res = await Homeserver.UploadFile(arg.File.Name, arg.File.OpenReadStream(Int64.MaxValue), arg.File.ContentType);
        Console.WriteLine(res);
        NewProfile.AvatarUrl = res;
        StateHasChanged();
    }

    private async Task UpdateProfile(bool restoreRoomProfiles = false) {
        Status = "Busy processing global profile update, please do not leave this page...";
        StateHasChanged();
        await Homeserver.UpdateProfileAsync(NewProfile, restoreRoomProfiles);
        Status = null;
        StateHasChanged();
        await OnInitializedAsync();
    }

    private async Task RoomAvatarChanged(InputFileChangeEventArgs arg, string roomId) {
        var res = await Homeserver.UploadFile(arg.File.Name, arg.File.OpenReadStream(Int64.MaxValue), arg.File.ContentType);
        Console.WriteLine(res);
        RoomProfiles[roomId].AvatarUrl = res;
        StateHasChanged();
    }

    private async Task UpdateRoomProfile(string roomId) {
        Status = "Busy processing room profile update, please do not leave this page...";
        StateHasChanged();
        var room = Homeserver.GetRoom(roomId);
        await room.SendStateEventAsync("m.room.member", Homeserver.WhoAmI.UserId, RoomProfiles[roomId]);
        Status = null;
        StateHasChanged();
    }

}