about summary refs log tree commit diff
path: root/LibMatrix/Helpers/SyncProcessors/Msc4222EmulationSyncProcessor.cs
blob: 6cb42cad855d0db3c8e22dd2261a6e786e6e87eb (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
using System.Diagnostics;
using ArcaneLibs.Extensions;
using LibMatrix.Homeservers;
using LibMatrix.Responses;

namespace LibMatrix.Helpers.SyncProcessors;

public class Msc4222EmulationSyncProcessor(AuthenticatedHomeserverGeneric homeserver) {
    private static bool StateEventsMatch(StateEventResponse a, StateEventResponse b) {
        return a.Type == b.Type && a.StateKey == b.StateKey;
    }

    private static bool StateEventIsNewer(StateEventResponse a, StateEventResponse b) {
        return StateEventsMatch(a, b) && a.OriginServerTs < b.OriginServerTs;
    }

    public async Task<SyncResponse?> EmulateMsc4222(SyncResponse? resp) {
        var sw = Stopwatch.StartNew();
        if (resp is null or { Rooms: null }) return resp;

        if (
            resp.Rooms.Join?.Any(x => x.Value.StateAfter is { Events.Count: > 0 }) == true
            || resp.Rooms.Leave?.Any(x => x.Value.StateAfter is { Events.Count: > 0 }) == true
        ) {
            Console.WriteLine($"Msc4222EmulationSyncProcessor.EmulateMsc4222 determined that no emulation is needed in {sw.Elapsed}");
            return resp;
        }

        resp = await EmulateMsc4222Internal(resp, sw);
        
        return SimpleSyncProcessors.FillRoomIds(resp);
    }

    private async Task<SyncResponse?> EmulateMsc4222Internal(SyncResponse? resp, Stopwatch sw) {
        var modified = false;
        List<Task<bool>> tasks = [];
        if (resp.Rooms is { Join.Count: > 0 }) {
            tasks.AddRange(resp.Rooms.Join.Select(ProcessJoinedRooms).ToList());
        }

        if (resp.Rooms is { Leave.Count: > 0 }) {
            tasks.AddRange(resp.Rooms.Leave.Select(ProcessLeftRooms).ToList());
        }

        var tasksEnum = tasks.ToAsyncEnumerable();
        await foreach (var wasModified in tasksEnum) {
            if (wasModified) {
                modified = true;
            }
        }

        Console.WriteLine($"Msc4222EmulationSyncProcessor.EmulateMsc4222 processed {resp.Rooms?.Join?.Count}/{resp.Rooms?.Leave?.Count} rooms in {sw.Elapsed} (modified: {modified})");
        if (modified)
            resp.Msc4222Method = SyncResponse.Msc4222SyncType.Emulated;

        return resp;
    }

    private async Task<bool> ProcessJoinedRooms(KeyValuePair<string, SyncResponse.RoomsDataStructure.JoinedRoomDataStructure> roomData) {
        var (roomId, data) = roomData;
        var room = homeserver.GetRoom(roomId);

        if (data.StateAfter is { Events.Count: > 0 }) {
            return false;
        }

        data.StateAfter = new() { Events = [] };

        data.StateAfter = new() {
            Events = []
        };

        var oldState = new List<StateEventResponse>();
        if (data.State is { Events.Count: > 0 }) {
            oldState.ReplaceBy(data.State.Events, StateEventIsNewer);
        }

        if (data.Timeline is { Limited: true }) {
            if (data.Timeline.Events != null)
                oldState.ReplaceBy(data.Timeline.Events, StateEventIsNewer);

            try {
                var timeline = await homeserver.GetRoom(roomId).GetMessagesAsync(limit: 250);
                if (timeline is { State.Count: > 0 }) {
                    oldState.ReplaceBy(timeline.State, StateEventIsNewer);
                }

                if (timeline is { Chunk.Count: > 0 }) {
                    oldState.ReplaceBy(timeline.Chunk.Where(x => x.StateKey != null), StateEventIsNewer);
                }
            }
            catch (Exception e) {
                Console.WriteLine($"Msc4222Emulation: Failed to get timeline for room {roomId}, state may be incomplete!\n{e}");
            }
        }

        oldState = oldState.DistinctBy(x => (x.Type, x.StateKey)).ToList();

        // Different order: we need oldState here to reduce the set
        try {
            data.StateAfter.Events = (await room.GetFullStateAsListAsync())
                // .Where(x=> oldState.Any(y => StateEventsMatch(x, y)))
                // .Join(oldState, x => (x.Type, x.StateKey), y => (y.Type, y.StateKey), (x, y) => x)
                .IntersectBy(oldState.Select(s => (s.Type, s.StateKey)), s => (s.Type, s.StateKey))
                .ToList();
            
            data.State = null;
            return true;
        }
        catch (Exception e) {
            Console.WriteLine($"Msc4222Emulation: Failed to get full state for room {roomId}, state may be incomplete!\n{e}");
        }

        var tasks = oldState
            .Select(async oldEvt => {
                try {
                    return await room.GetStateEventAsync(oldEvt.Type, oldEvt.StateKey!);
                }
                catch (Exception e) {
                    Console.WriteLine($"Msc4222Emulation: Failed to get state event {oldEvt.Type}/{oldEvt.StateKey} for room {roomId}, state may be incomplete!\n{e}");
                    return oldEvt;
                }
            });

        var tasksEnum = tasks.ToAsyncEnumerable();
        await foreach (var evt in tasksEnum) {
            data.StateAfter.Events.Add(evt);
        }

        data.State = null;

        return true;
    }

    private async Task<bool> ProcessLeftRooms(KeyValuePair<string, SyncResponse.RoomsDataStructure.LeftRoomDataStructure> roomData) {
        var (roomId, data) = roomData;
        var room = homeserver.GetRoom(roomId);

        if (data.StateAfter is { Events.Count: > 0 }) {
            return false;
        }

        data.StateAfter = new() {
            Events = []
        };

        try {
            data.StateAfter.Events = await room.GetFullStateAsListAsync();
            data.State = null;
            return true;
        }
        catch (Exception e) {
            Console.WriteLine($"Msc4222Emulation: Failed to get full state for room {roomId}, state may be incomplete!\n{e}");
        }

        var oldState = new List<StateEventResponse>();
        if (data.State is { Events.Count: > 0 }) {
            oldState.ReplaceBy(data.State.Events, StateEventIsNewer);
        }

        if (data.Timeline is { Limited: true }) {
            if (data.Timeline.Events != null)
                oldState.ReplaceBy(data.Timeline.Events, StateEventIsNewer);

            try {
                var timeline = await homeserver.GetRoom(roomId).GetMessagesAsync(limit: 250);
                if (timeline is { State.Count: > 0 }) {
                    oldState.ReplaceBy(timeline.State, StateEventIsNewer);
                }

                if (timeline is { Chunk.Count: > 0 }) {
                    oldState.ReplaceBy(timeline.Chunk.Where(x => x.StateKey != null), StateEventIsNewer);
                }
            }
            catch (Exception e) {
                Console.WriteLine($"Msc4222Emulation: Failed to get timeline for room {roomId}, state may be incomplete!\n{e}");
            }
        }

        oldState = oldState.DistinctBy(x => (x.Type, x.StateKey)).ToList();

        var tasks = oldState
            .Select(async oldEvt => {
                try {
                    return await room.GetStateEventAsync(oldEvt.Type, oldEvt.StateKey!);
                }
                catch (Exception e) {
                    Console.WriteLine($"Msc4222Emulation: Failed to get state event {oldEvt.Type}/{oldEvt.StateKey} for room {roomId}, state may be incomplete!\n{e}");
                    return oldEvt;
                }
            });

        var tasksEnum = tasks.ToAsyncEnumerable();
        await foreach (var evt in tasksEnum) {
            data.StateAfter.Events.Add(evt);
        }

        data.State = null;

        return true;
    }
}