about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/Rooms/Timeline.razor
blob: 68125cb141c329289ae9d1a333a5645b738ca04c (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
@page "/Rooms/{RoomId}/Timeline"
@using MatrixRoomUtils.Web.Shared.TimelineComponents
@using LibMatrix
@using LibMatrix.EventTypes.Spec
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.Homeservers
@using LibMatrix.Responses
<h3>RoomManagerTimeline</h3>
<hr/>
<p>Loaded @Events.Count events...</p>

@foreach (var evt in Events) {
    <div type="@evt.Type" key="@evt.StateKey" itemid="@evt.EventId">
        <DynamicComponent Type="@ComponentType(evt)"
                          Parameters="@(new Dictionary<string, object> { { "Event", evt }, { "Events", Events }, { "HomeServer", HomeServer!} })">
        </DynamicComponent>
    </div>
}

@code {

    [Parameter]
    public string RoomId { get; set; }

    private List<MessagesResponse> Messages { get; } = new();
    private List<StateEventResponse> Events { get; } = new();

    private AuthenticatedHomeserverGeneric? HomeServer { get; set; }

    protected override async Task OnInitializedAsync() {
        Console.WriteLine("RoomId: " + RoomId);
        HomeServer = await MRUStorage.GetCurrentSessionOrNavigate();
        if (HomeServer is null) return;
        var room = HomeServer.GetRoom(RoomId);
        MessagesResponse? msgs = null;
        do {
            msgs = await room.GetMessagesAsync(limit: 1000, from: msgs?.End, dir: "b");
            Messages.Add(msgs);
            Console.WriteLine($"Got {msgs.Chunk.Count} messages");
            msgs.Chunk.Reverse();
            Events.InsertRange(0, msgs.Chunk);
        } while (msgs.End is not null);


        await base.OnInitializedAsync();
    }

    private StateEventResponse GetProfileEventBefore(StateEventResponse Event) => Events.TakeWhile(x => x != Event).Last(e => e.Type == "m.room.member" && e.StateKey == Event.Sender);

    private Type ComponentType(StateEvent Event) => Event.TypedContent switch {
        RoomMessageEventContent => typeof(TimelineMessageItem),
        RoomMemberEventContent => typeof(TimelineMemberItem),
        RoomCreateEventContent => typeof(TimelineRoomCreateItem),
        _ => typeof(TimelineUnknownItem)
        };

}