about summary refs log tree commit diff
path: root/LibMatrix/Responses/SyncResponse.cs
blob: f2b901d9778d470fc83224f1dc7ce03e9b0ae90a (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
using System.Text.Json.Serialization;
using LibMatrix.EventTypes.Spec.Ephemeral;
using LibMatrix.EventTypes.Spec.State;

namespace LibMatrix.Responses;

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(SyncResponse))]
internal partial class SyncResponseSerializerContext : JsonSerializerContext { }

public class SyncResponse {
    [JsonPropertyName("next_batch")]
    public string NextBatch { get; set; } = null!;

    [JsonPropertyName("account_data")]
    public EventList? AccountData { get; set; }

    [JsonPropertyName("presence")]
    public EventList? Presence { get; set; }

    [JsonPropertyName("device_one_time_keys_count")]
    public Dictionary<string, int>? DeviceOneTimeKeysCount { get; set; } = null!;

    [JsonPropertyName("rooms")]
    public RoomsDataStructure? Rooms { get; set; }

    [JsonPropertyName("to_device")]
    public EventList? ToDevice { get; set; }

    [JsonPropertyName("device_lists")]
    public DeviceListsDataStructure? DeviceLists { get; set; }

    public class DeviceListsDataStructure {
        [JsonPropertyName("changed")]
        public List<string>? Changed { get; set; }

        [JsonPropertyName("left")]
        public List<string>? Left { get; set; }
    }

    // supporting classes

    public class RoomsDataStructure {
        [JsonPropertyName("join")]
        public Dictionary<string, JoinedRoomDataStructure>? Join { get; set; }

        [JsonPropertyName("invite")]
        public Dictionary<string, InvitedRoomDataStructure>? Invite { get; set; }

        [JsonPropertyName("leave")]
        public Dictionary<string, LeftRoomDataStructure>? Leave { get; set; }

        public class LeftRoomDataStructure {
            [JsonPropertyName("account_data")]
            public EventList? AccountData { get; set; }

            [JsonPropertyName("timeline")]
            public JoinedRoomDataStructure.TimelineDataStructure? Timeline { get; set; }

            [JsonPropertyName("state")]
            public EventList? State { get; set; }

            public override string ToString() {
                var lastEvent = Timeline?.Events?.LastOrDefault(x => x.Type == "m.room.member");
                var membership = (lastEvent?.TypedContent as RoomMemberEventContent);
                return $"LeftRoomDataStructure: {lastEvent?.Sender} {membership?.Membership} ({membership?.Reason})";
            }
        }

        public class JoinedRoomDataStructure {
            [JsonPropertyName("timeline")]
            public TimelineDataStructure? Timeline { get; set; }

            [JsonPropertyName("state")]
            public EventList? State { get; set; }

            [JsonPropertyName("account_data")]
            public EventList? AccountData { get; set; }

            [JsonPropertyName("ephemeral")]
            public EventList? Ephemeral { get; set; }

            [JsonPropertyName("unread_notifications")]
            public UnreadNotificationsDataStructure? UnreadNotifications { get; set; }

            [JsonPropertyName("summary")]
            public SummaryDataStructure? Summary { get; set; }

            public class TimelineDataStructure : EventList {
                public TimelineDataStructure() { }

                public TimelineDataStructure(List<StateEventResponse>? events, bool? limited) {
                    Events = events;
                    Limited = limited;
                }

                // [JsonPropertyName("events")]
                // public List<StateEventResponse>? Events { get; set; }

                [JsonPropertyName("prev_batch")]
                public string? PrevBatch { get; set; }

                [JsonPropertyName("limited")]
                public bool? Limited { get; set; }
            }

            public class UnreadNotificationsDataStructure {
                [JsonPropertyName("notification_count")]
                public int NotificationCount { get; set; }

                [JsonPropertyName("highlight_count")]
                public int HighlightCount { get; set; }
            }

            public class SummaryDataStructure {
                [JsonPropertyName("m.heroes")]
                public List<string>? Heroes { get; set; }

                [JsonPropertyName("m.invited_member_count")]
                public int? InvitedMemberCount { get; set; }

                [JsonPropertyName("m.joined_member_count")]
                public int? JoinedMemberCount { get; set; }
            }
        }

        public class InvitedRoomDataStructure {
            [JsonPropertyName("invite_state")]
            public EventList? InviteState { get; set; }
        }
    }

    public long GetDerivedSyncTime() {
        return ((long[]) [
            AccountData?.Events?.Max(x => x.OriginServerTs) ?? 0,
            Presence?.Events?.Max(x => x.OriginServerTs) ?? 0,
            ToDevice?.Events?.Max(x => x.OriginServerTs) ?? 0,
            Rooms?.Join?.Values?.Max(x => x.Timeline?.Events?.Max(y => y.OriginServerTs)) ?? 0,
            Rooms?.Invite?.Values?.Max(x => x.InviteState?.Events?.Max(y => y.OriginServerTs)) ?? 0,
            Rooms?.Leave?.Values?.Max(x => x.Timeline?.Events?.Max(y => y.OriginServerTs)) ?? 0
        ]).Max();
    }
}