@page "/User/DirectMessages"
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.Responses
@using MatrixUtils.Abstractions
@using LibMatrix
Direct Messages
@foreach (var (targetUser, rooms) in DMRooms) {
@foreach (var room in rooms) {
}
}
@code {
private string? _status;
private AuthenticatedHomeserverGeneric? Homeserver { get; set; }
private Dictionary> DMRooms { get; set; } = new();
public string? Status {
get => _status;
set {
_status = value;
StateHasChanged();
}
}
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;
Status = "Loading DM list from account data...";
var dms = await Homeserver.GetAccountDataAsync>>("m.direct");
DMRooms.Clear();
var userTasks = dms.Select(async kv => {
var (userId, rooms) = kv;
var roomList = new List();
UserProfileResponse? profile = null;
try {
profile = await Homeserver.GetProfileAsync(userId);
}
catch (MatrixException e) {
if (e is { ErrorCode: "M_UNKNOWN" }) profile = new UserProfileResponse() { DisplayName = $"{userId}: {e.Error}" };
}
foreach (var room in rooms) {
var roomInfo = new RoomInfo(Homeserver.GetRoom(room));
roomList.Add(roomInfo);
roomInfo.StateEvents.Add(new() {
Type = RoomNameEventContent.EventId,
TypedContent = new RoomNameEventContent() {
Name = await Homeserver.GetRoom(room).GetNameOrFallbackAsync(4)
},
RoomId = room, Sender = null, EventId = null
});
}
DMRooms.Add(profile ?? new() { DisplayName = userId }, roomList);
StateHasChanged();
}).ToList();
await Task.WhenAll(userTasks);
await Task.Delay(500);
StateHasChanged();
Status = null;
await base.OnInitializedAsync();
}
}