about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/User/Profile.razor
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixUtils.Web/Pages/User/Profile.razor')
-rw-r--r--MatrixUtils.Web/Pages/User/Profile.razor134
1 files changed, 134 insertions, 0 deletions
diff --git a/MatrixUtils.Web/Pages/User/Profile.razor b/MatrixUtils.Web/Pages/User/Profile.razor
new file mode 100644
index 0000000..8cffaab
--- /dev/null
+++ b/MatrixUtils.Web/Pages/User/Profile.razor
@@ -0,0 +1,134 @@
+@page "/User/Profile"
+@using LibMatrix.Homeservers
+@using LibMatrix.EventTypes.Spec.State
+@using ArcaneLibs.Extensions
+@using LibMatrix.Responses
+<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 (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 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) {
+            // 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();
+    }
+
+}
\ No newline at end of file