about summary refs log tree commit diff
path: root/LibMatrix/Helpers/SyncHelper.cs
blob: de4f3d49ea1f1853a7c35dbf1cb61b305537d851 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using ArcaneLibs.Extensions;
using LibMatrix.Extensions;
using LibMatrix.Filters;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using LibMatrix.Services;

namespace LibMatrix.Helpers;

public class SyncHelper {
    private readonly AuthenticatedHomeserverGeneric _homeserver;
    private readonly TieredStorageService _storageService;

    public SyncHelper(AuthenticatedHomeserverGeneric homeserver, TieredStorageService storageService) {
        _homeserver = homeserver;
        _storageService = storageService;
    }

    public async Task<SyncResult?> Sync(
        string? since = null,
        int? timeout = 30000,
        string? setPresence = "online",
        SyncFilter? filter = null,
        CancellationToken? cancellationToken = null) {
        var outFileName = "sync-" +
                          (await _storageService.CacheStorageProvider.GetAllKeysAsync()).Count(
                              x => x.StartsWith("sync")) +
                          ".json";
        var url = $"/_matrix/client/v3/sync?timeout={timeout}&set_presence={setPresence}";
        if (!string.IsNullOrWhiteSpace(since)) url += $"&since={since}";
        if (filter is not null) url += $"&filter={filter.ToJson(ignoreNull: true, indent: false)}";
        // else url += "&full_state=true";
        Console.WriteLine("Calling: " + url);
        try {
            var req = await _homeserver._httpClient.GetAsync(url, cancellationToken: cancellationToken ?? CancellationToken.None);

            // var res = await JsonSerializer.DeserializeAsync<SyncResult>(await req.Content.ReadAsStreamAsync());

#if DEBUG && false
            var jsonObj = await req.Content.ReadFromJsonAsync<JsonElement>();
            try {
                await _homeServer._httpClient.PostAsJsonAsync(
                    "http://localhost:5116/validate/" + typeof(SyncResult).AssemblyQualifiedName, jsonObj);
            }
            catch (Exception e) {
                Console.WriteLine("[!!] Checking sync response failed: " + e);
            }

            var res = jsonObj.Deserialize<SyncResult>();
            return res;
#else
            return await req.Content.ReadFromJsonAsync<SyncResult>();
#endif
        }
        catch (TaskCanceledException) {
            Console.WriteLine("Sync cancelled!");
        }
        catch (Exception e) {
            Console.WriteLine(e);
        }

        return null;
    }

    [SuppressMessage("ReSharper", "FunctionNeverReturns")]
    public async Task RunSyncLoop(
        bool skipInitialSyncEvents = true,
        string? since = null,
        int? timeout = 30000,
        string? setPresence = "online",
        SyncFilter? filter = null,
        CancellationToken? cancellationToken = null
    ) {
        await Task.WhenAll((await _storageService.CacheStorageProvider.GetAllKeysAsync())
            .Where(x => x.StartsWith("sync"))
            .ToList()
            .Select(x => _storageService.CacheStorageProvider.DeleteObjectAsync(x)));
        var nextBatch = since;
        while (cancellationToken is null || !cancellationToken.Value.IsCancellationRequested) {
            var sync = await Sync(since: nextBatch, timeout: timeout, setPresence: setPresence, filter: filter,
                cancellationToken: cancellationToken);
            nextBatch = sync?.NextBatch ?? nextBatch;
            if (sync is null) continue;
            Console.WriteLine($"Got sync, next batch: {nextBatch}!");

            if (sync.Rooms is { Invite.Count: > 0 }) {
                foreach (var roomInvite in sync.Rooms.Invite) {
                    var tasks = InviteReceivedHandlers.Select(x => x(roomInvite)).ToList();
                    await Task.WhenAll(tasks);
                }
            }

            if (sync.AccountData is { Events: { Count: > 0 } }) {
                foreach (var accountDataEvent in sync.AccountData.Events) {
                    var tasks = AccountDataReceivedHandlers.Select(x => x(accountDataEvent)).ToList();
                    await Task.WhenAll(tasks);
                }
            }

            // Things that are skipped on the first sync
            if (skipInitialSyncEvents) {
                skipInitialSyncEvents = false;
                continue;
            }

            if (sync.Rooms is { Join.Count: > 0 }) {
                foreach (var updatedRoom in sync.Rooms.Join) {
                    foreach (var stateEventResponse in updatedRoom.Value.Timeline.Events) {
                        stateEventResponse.RoomId = updatedRoom.Key;
                        var tasks = TimelineEventHandlers.Select(x => x(stateEventResponse)).ToList();
                        await Task.WhenAll(tasks);
                    }
                }
            }
        }
    }

    /// <summary>
    /// Event fired when a room invite is received
    /// </summary>
    public List<Func<KeyValuePair<string, SyncResult.RoomsDataStructure.InvitedRoomDataStructure>, Task>>
        InviteReceivedHandlers { get; } = new();

    public List<Func<StateEventResponse, Task>> TimelineEventHandlers { get; } = new();
    public List<Func<StateEventResponse, Task>> AccountDataReceivedHandlers { get; } = new();
}

public class SyncResult {
    [JsonPropertyName("next_batch")]
    public string NextBatch { get; set; }

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

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

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

    [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 PresenceDataStructure {
        [JsonPropertyName("events")]
        public List<StateEventResponse> Events { get; set; }
    }

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

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

        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 {
                [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 class EventList {
    [JsonPropertyName("events")]
    public List<StateEventResponse> Events { get; set; }
}