summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/matrix/lib/matrix-sdk/timeline-window.js
blob: 2c7365bdffb28ea166e44951ac53582883e7933b (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TimelineWindow = exports.TimelineIndex = void 0;
var _eventTimeline = require("./models/event-timeline");
var _logger = require("./logger");
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /*
                                                                                                                                                                                                                                                                                                                                                                                          Copyright 2016 - 2021 The Matrix.org Foundation C.I.C.
                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                          Licensed under the Apache License, Version 2.0 (the "License");
                                                                                                                                                                                                                                                                                                                                                                                          you may not use this file except in compliance with the License.
                                                                                                                                                                                                                                                                                                                                                                                          You may obtain a copy of the License at
                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                              http://www.apache.org/licenses/LICENSE-2.0
                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                          Unless required by applicable law or agreed to in writing, software
                                                                                                                                                                                                                                                                                                                                                                                          distributed under the License is distributed on an "AS IS" BASIS,
                                                                                                                                                                                                                                                                                                                                                                                          WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
                                                                                                                                                                                                                                                                                                                                                                                          See the License for the specific language governing permissions and
                                                                                                                                                                                                                                                                                                                                                                                          limitations under the License.
                                                                                                                                                                                                                                                                                                                                                                                          */
/**
 * @internal
 */
const DEBUG = false;

/**
 * @internal
 */
/* istanbul ignore next */
const debuglog = DEBUG ? _logger.logger.log.bind(_logger.logger) : function () {};

/**
 * the number of times we ask the server for more events before giving up
 *
 * @internal
 */
const DEFAULT_PAGINATE_LOOP_LIMIT = 5;
class TimelineWindow {
  /**
   * Construct a TimelineWindow.
   *
   * <p>This abstracts the separate timelines in a Matrix {@link Room} into a single iterable thing.
   * It keeps track of the start and endpoints of the window, which can be advanced with the help
   * of pagination requests.
   *
   * <p>Before the window is useful, it must be initialised by calling {@link TimelineWindow#load}.
   *
   * <p>Note that the window will not automatically extend itself when new events
   * are received from /sync; you should arrange to call {@link TimelineWindow#paginate}
   * on {@link RoomEvent.Timeline} events.
   *
   * @param client -   MatrixClient to be used for context/pagination
   *   requests.
   *
   * @param timelineSet -  The timelineSet to track
   *
   * @param opts - Configuration options for this window
   */
  constructor(client, timelineSet, opts = {}) {
    this.client = client;
    this.timelineSet = timelineSet;
    _defineProperty(this, "windowLimit", void 0);
    // these will be TimelineIndex objects; they delineate the 'start' and
    // 'end' of the window.
    //
    // start.index is inclusive; end.index is exclusive.
    _defineProperty(this, "start", void 0);
    _defineProperty(this, "end", void 0);
    _defineProperty(this, "eventCount", 0);
    this.windowLimit = opts.windowLimit || 1000;
  }

  /**
   * Initialise the window to point at a given event, or the live timeline
   *
   * @param initialEventId -   If given, the window will contain the
   *    given event
   * @param initialWindowSize -   Size of the initial window
   */
  load(initialEventId, initialWindowSize = 20) {
    // given an EventTimeline, find the event we were looking for, and initialise our
    // fields so that the event in question is in the middle of the window.
    const initFields = timeline => {
      if (!timeline) {
        throw new Error("No timeline given to initFields");
      }
      let eventIndex;
      const events = timeline.getEvents();
      if (!initialEventId) {
        // we were looking for the live timeline: initialise to the end
        eventIndex = events.length;
      } else {
        eventIndex = events.findIndex(e => e.getId() === initialEventId);
        if (eventIndex < 0) {
          throw new Error("getEventTimeline result didn't include requested event");
        }
      }
      const endIndex = Math.min(events.length, eventIndex + Math.ceil(initialWindowSize / 2));
      const startIndex = Math.max(0, endIndex - initialWindowSize);
      this.start = new TimelineIndex(timeline, startIndex - timeline.getBaseIndex());
      this.end = new TimelineIndex(timeline, endIndex - timeline.getBaseIndex());
      this.eventCount = endIndex - startIndex;
    };

    // We avoid delaying the resolution of the promise by a reactor tick if we already have the data we need,
    // which is important to keep room-switching feeling snappy.
    if (this.timelineSet.getTimelineForEvent(initialEventId)) {
      initFields(this.timelineSet.getTimelineForEvent(initialEventId));
      return Promise.resolve();
    } else if (initialEventId) {
      return this.client.getEventTimeline(this.timelineSet, initialEventId).then(initFields);
    } else {
      initFields(this.timelineSet.getLiveTimeline());
      return Promise.resolve();
    }
  }

  /**
   * Get the TimelineIndex of the window in the given direction.
   *
   * @param direction -   EventTimeline.BACKWARDS to get the TimelineIndex
   * at the start of the window; EventTimeline.FORWARDS to get the TimelineIndex at
   * the end.
   *
   * @returns The requested timeline index if one exists, null
   * otherwise.
   */
  getTimelineIndex(direction) {
    if (direction == _eventTimeline.EventTimeline.BACKWARDS) {
      return this.start ?? null;
    } else if (direction == _eventTimeline.EventTimeline.FORWARDS) {
      return this.end ?? null;
    } else {
      throw new Error("Invalid direction '" + direction + "'");
    }
  }

  /**
   * Try to extend the window using events that are already in the underlying
   * TimelineIndex.
   *
   * @param direction -   EventTimeline.BACKWARDS to try extending it
   *   backwards; EventTimeline.FORWARDS to try extending it forwards.
   * @param size -   number of events to try to extend by.
   *
   * @returns true if the window was extended, false otherwise.
   */
  extend(direction, size) {
    const tl = this.getTimelineIndex(direction);
    if (!tl) {
      debuglog("TimelineWindow: no timeline yet");
      return false;
    }
    const count = direction == _eventTimeline.EventTimeline.BACKWARDS ? tl.retreat(size) : tl.advance(size);
    if (count) {
      this.eventCount += count;
      debuglog("TimelineWindow: increased cap by " + count + " (now " + this.eventCount + ")");
      // remove some events from the other end, if necessary
      const excess = this.eventCount - this.windowLimit;
      if (excess > 0) {
        this.unpaginate(excess, direction != _eventTimeline.EventTimeline.BACKWARDS);
      }
      return true;
    }
    return false;
  }

  /**
   * Check if this window can be extended
   *
   * <p>This returns true if we either have more events, or if we have a
   * pagination token which means we can paginate in that direction. It does not
   * necessarily mean that there are more events available in that direction at
   * this time.
   *
   * @param direction -   EventTimeline.BACKWARDS to check if we can
   *   paginate backwards; EventTimeline.FORWARDS to check if we can go forwards
   *
   * @returns true if we can paginate in the given direction
   */
  canPaginate(direction) {
    const tl = this.getTimelineIndex(direction);
    if (!tl) {
      debuglog("TimelineWindow: no timeline yet");
      return false;
    }
    if (direction == _eventTimeline.EventTimeline.BACKWARDS) {
      if (tl.index > tl.minIndex()) {
        return true;
      }
    } else {
      if (tl.index < tl.maxIndex()) {
        return true;
      }
    }
    const hasNeighbouringTimeline = tl.timeline.getNeighbouringTimeline(direction);
    const paginationToken = tl.timeline.getPaginationToken(direction);
    return Boolean(hasNeighbouringTimeline) || Boolean(paginationToken);
  }

  /**
   * Attempt to extend the window
   *
   * @param direction -   EventTimeline.BACKWARDS to extend the window
   *    backwards (towards older events); EventTimeline.FORWARDS to go forwards.
   *
   * @param size -   number of events to try to extend by. If fewer than this
   *    number are immediately available, then we return immediately rather than
   *    making an API call.
   *
   * @param makeRequest - whether we should make API calls to
   *    fetch further events if we don't have any at all. (This has no effect if
   *    the room already knows about additional events in the relevant direction,
   *    even if there are fewer than 'size' of them, as we will just return those
   *    we already know about.)
   *
   * @param requestLimit - limit for the number of API requests we
   *    should make.
   *
   * @returns Promise which resolves to a boolean which is true if more events
   *    were successfully retrieved.
   */
  async paginate(direction, size, makeRequest = true, requestLimit = DEFAULT_PAGINATE_LOOP_LIMIT) {
    // Either wind back the message cap (if there are enough events in the
    // timeline to do so), or fire off a pagination request.
    const tl = this.getTimelineIndex(direction);
    if (!tl) {
      debuglog("TimelineWindow: no timeline yet");
      return false;
    }
    if (tl.pendingPaginate) {
      return tl.pendingPaginate;
    }

    // try moving the cap
    if (this.extend(direction, size)) {
      return true;
    }
    if (!makeRequest || requestLimit === 0) {
      // todo: should we return something different to indicate that there
      // might be more events out there, but we haven't found them yet?
      return false;
    }

    // try making a pagination request
    const token = tl.timeline.getPaginationToken(direction);
    if (!token) {
      debuglog("TimelineWindow: no token");
      return false;
    }
    debuglog("TimelineWindow: starting request");
    const prom = this.client.paginateEventTimeline(tl.timeline, {
      backwards: direction == _eventTimeline.EventTimeline.BACKWARDS,
      limit: size
    }).finally(function () {
      tl.pendingPaginate = undefined;
    }).then(r => {
      debuglog("TimelineWindow: request completed with result " + r);
      if (!r) {
        return this.paginate(direction, size, false, 0);
      }

      // recurse to advance the index into the results.
      //
      // If we don't get any new events, we want to make sure we keep asking
      // the server for events for as long as we have a valid pagination
      // token. In particular, we want to know if we've actually hit the
      // start of the timeline, or if we just happened to know about all of
      // the events thanks to https://matrix.org/jira/browse/SYN-645.
      //
      // On the other hand, we necessarily want to wait forever for the
      // server to make its mind up about whether there are other events,
      // because it gives a bad user experience
      // (https://github.com/vector-im/vector-web/issues/1204).
      return this.paginate(direction, size, true, requestLimit - 1);
    });
    tl.pendingPaginate = prom;
    return prom;
  }

  /**
   * Remove `delta` events from the start or end of the timeline.
   *
   * @param delta - number of events to remove from the timeline
   * @param startOfTimeline - if events should be removed from the start
   *     of the timeline.
   */
  unpaginate(delta, startOfTimeline) {
    const tl = startOfTimeline ? this.start : this.end;
    if (!tl) {
      throw new Error(`Attempting to unpaginate startOfTimeline=${startOfTimeline} but don't have this direction`);
    }

    // sanity-check the delta
    if (delta > this.eventCount || delta < 0) {
      throw new Error(`Attemting to unpaginate ${delta} events, but only have ${this.eventCount} in the timeline`);
    }
    while (delta > 0) {
      const count = startOfTimeline ? tl.advance(delta) : tl.retreat(delta);
      if (count <= 0) {
        // sadness. This shouldn't be possible.
        throw new Error("Unable to unpaginate any further, but still have " + this.eventCount + " events");
      }
      delta -= count;
      this.eventCount -= count;
      debuglog("TimelineWindow.unpaginate: dropped " + count + " (now " + this.eventCount + ")");
    }
  }

  /**
   * Get a list of the events currently in the window
   *
   * @returns the events in the window
   */
  getEvents() {
    if (!this.start) {
      // not yet loaded
      return [];
    }
    const result = [];

    // iterate through each timeline between this.start and this.end
    // (inclusive).
    let timeline = this.start.timeline;
    // eslint-disable-next-line no-constant-condition
    while (timeline) {
      const events = timeline.getEvents();

      // For the first timeline in the chain, we want to start at
      // this.start.index. For the last timeline in the chain, we want to
      // stop before this.end.index. Otherwise, we want to copy all of the
      // events in the timeline.
      //
      // (Note that both this.start.index and this.end.index are relative
      // to their respective timelines' BaseIndex).
      //
      let startIndex = 0;
      let endIndex = events.length;
      if (timeline === this.start.timeline) {
        startIndex = this.start.index + timeline.getBaseIndex();
      }
      if (timeline === this.end?.timeline) {
        endIndex = this.end.index + timeline.getBaseIndex();
      }
      for (let i = startIndex; i < endIndex; i++) {
        result.push(events[i]);
      }

      // if we're not done, iterate to the next timeline.
      if (timeline === this.end?.timeline) {
        break;
      } else {
        timeline = timeline.getNeighbouringTimeline(_eventTimeline.EventTimeline.FORWARDS);
      }
    }
    return result;
  }
}

/**
 * A thing which contains a timeline reference, and an index into it.
 * @internal
 */
exports.TimelineWindow = TimelineWindow;
class TimelineIndex {
  // index: the indexes are relative to BaseIndex, so could well be negative.
  constructor(timeline, index) {
    this.timeline = timeline;
    this.index = index;
    _defineProperty(this, "pendingPaginate", void 0);
  }

  /**
   * @returns the minimum possible value for the index in the current
   *    timeline
   */
  minIndex() {
    return this.timeline.getBaseIndex() * -1;
  }

  /**
   * @returns the maximum possible value for the index in the current
   *    timeline (exclusive - ie, it actually returns one more than the index
   *    of the last element).
   */
  maxIndex() {
    return this.timeline.getEvents().length - this.timeline.getBaseIndex();
  }

  /**
   * Try move the index forward, or into the neighbouring timeline
   *
   * @param delta -  number of events to advance by
   * @returns number of events successfully advanced by
   */
  advance(delta) {
    if (!delta) {
      return 0;
    }

    // first try moving the index in the current timeline. See if there is room
    // to do so.
    let cappedDelta;
    if (delta < 0) {
      // we want to wind the index backwards.
      //
      // (this.minIndex() - this.index) is a negative number whose magnitude
      // is the amount of room we have to wind back the index in the current
      // timeline. We cap delta to this quantity.
      cappedDelta = Math.max(delta, this.minIndex() - this.index);
      if (cappedDelta < 0) {
        this.index += cappedDelta;
        return cappedDelta;
      }
    } else {
      // we want to wind the index forwards.
      //
      // (this.maxIndex() - this.index) is a (positive) number whose magnitude
      // is the amount of room we have to wind forward the index in the current
      // timeline. We cap delta to this quantity.
      cappedDelta = Math.min(delta, this.maxIndex() - this.index);
      if (cappedDelta > 0) {
        this.index += cappedDelta;
        return cappedDelta;
      }
    }

    // the index is already at the start/end of the current timeline.
    //
    // next see if there is a neighbouring timeline to switch to.
    const neighbour = this.timeline.getNeighbouringTimeline(delta < 0 ? _eventTimeline.EventTimeline.BACKWARDS : _eventTimeline.EventTimeline.FORWARDS);
    if (neighbour) {
      this.timeline = neighbour;
      if (delta < 0) {
        this.index = this.maxIndex();
      } else {
        this.index = this.minIndex();
      }
      debuglog("paginate: switched to new neighbour");

      // recurse, using the next timeline
      return this.advance(delta);
    }
    return 0;
  }

  /**
   * Try move the index backwards, or into the neighbouring timeline
   *
   * @param delta -  number of events to retreat by
   * @returns number of events successfully retreated by
   */
  retreat(delta) {
    return this.advance(delta * -1) * -1;
  }
}
exports.TimelineIndex = TimelineIndex;