about summary refs log tree commit diff
path: root/MatrixRoomUtils.Desktop/RoomInfo.cs
blob: a562086fb8b6282861a92b7b0b7e7e6ee67ba0e1 (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
using LibMatrix;
using LibMatrix.EventTypes;
using LibMatrix.Interfaces;
using LibMatrix.Responses;
using LibMatrix.RoomTypes;

namespace MatrixRoomUtils.Desktop;

public class RoomInfo {
    public RoomInfo() { }

    public RoomInfo(GenericRoom room) {
        Room = room;
    }

    public GenericRoom Room { get; set; }
    public List<StateEventResponse?> StateEvents { get; init; } = new();

    public async Task<StateEventResponse?> GetStateEvent(string type, string stateKey = "") {
        var @event = StateEvents.FirstOrDefault(x => x.Type == type && x.StateKey == stateKey);
        if (@event is not null) return @event;
        @event = new StateEventResponse {
            RoomId = Room.RoomId,
            Type = type,
            StateKey = stateKey,
            Sender = null, //TODO: implement
            EventId = null
        };
        try {
            @event.TypedContent = await Room.GetStateAsync<EventContent>(type, stateKey);
        }
        catch (MatrixException e) {
            if (e is { ErrorCode: "M_NOT_FOUND" }) @event.TypedContent = default!;
            else throw;
        }

        StateEvents.Add(@event);
        return @event;
    }
}