From ec925d7c4955f29d903faac9aede25b8ec14af11 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 8 Mar 2019 20:03:18 +0100 Subject: [PATCH] draft of how to implement filling a timeline gap --- src/matrix/room/persister.js | 6 +++--- src/matrix/room/room.js | 9 ++++++--- src/matrix/room/timeline.js | 24 ++++++++++++++++++++++++ src/matrix/session.js | 11 +++++++++-- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/matrix/room/persister.js b/src/matrix/room/persister.js index c08fbc92..6ed72ece 100644 --- a/src/matrix/room/persister.js +++ b/src/matrix/room/persister.js @@ -17,9 +17,9 @@ export default class RoomPersister { } } - // async persistGapFill(...) { - - // } + async persistGapFill(gapEntry, response) { + throw new Error("not yet implemented"); + } persistSync(roomResponse, txn) { let nextKey = this._lastSortKey; diff --git a/src/matrix/room/room.js b/src/matrix/room/room.js index a581fbcb..014ea529 100644 --- a/src/matrix/room/room.js +++ b/src/matrix/room/room.js @@ -4,10 +4,11 @@ import RoomPersister from "./persister.js"; import Timeline from "./timeline.js"; export default class Room extends EventEmitter { - constructor(roomId, storage, emitCollectionChange) { + constructor({roomId, storage, hsApi, emitCollectionChange}) { super(); - this._roomId = roomId; - this._storage = storage; + this._roomId = roomId; + this._storage = storage; + this._hsApi = hsApi; this._summary = new RoomSummary(roomId); this._persister = new RoomPersister(roomId); this._emitCollectionChange = emitCollectionChange; @@ -50,6 +51,8 @@ export default class Room extends EventEmitter { this._timeline = new Timeline({ roomId: this.id, storage: this._storage, + persister: this._persister, + hsApi: this._hsApi, closeCallback: () => this._timeline = null, }); await this._timeline.load(); diff --git a/src/matrix/room/timeline.js b/src/matrix/room/timeline.js index 44f5f83d..27abee37 100644 --- a/src/matrix/room/timeline.js +++ b/src/matrix/room/timeline.js @@ -24,6 +24,30 @@ export default class Timeline { } } + /** @public */ + async fillGap(gapEntry, amount) { + const gap = gapEntry.gap; + let direction; + if (gap.prev_batch) { + direction = "b"; + } else if (gap.next_batch) { + direction = "f"; + } else { + throw new Error("Invalid gap, no prev_batch or next_batch field: " + JSON.stringify(gapEntry.gap)); + } + const token = gap.prev_batch || gap.next_batch; + + const response = await this._hsApi.messages({ + roomId: this._roomId, + from: token, + dir: direction, + limit: amount + }); + const newEntries = await this._persister.persistGapFill(gapEntry, response); + // find where to replace existing gap with newEntries by doing binary search + + } + /** @public */ get entries() { return this._entriesList; diff --git a/src/matrix/session.js b/src/matrix/session.js index 1f9a3d7e..9dbeb010 100644 --- a/src/matrix/session.js +++ b/src/matrix/session.js @@ -2,8 +2,10 @@ import Room from "./room/room.js"; import { ObservableMap } from "../observable/index.js"; export default class Session { - constructor({storage, sessionInfo}) { + // sessionInfo contains deviceId, userId and homeServer + constructor({storage, hsApi, sessionInfo}) { this._storage = storage; + this._hsApi = hsApi; this._session = null; this._sessionInfo = sessionInfo; this._rooms = new ObservableMap(); @@ -36,7 +38,12 @@ export default class Session { } createRoom(roomId) { - const room = new Room(roomId, this._storage, this._roomUpdateCallback); + const room = new Room({ + roomId, + storage: this._storage, + emitCollectionChange: this._roomUpdateCallback, + hsApi: this._hsApi, + }); this._rooms.add(roomId, room); return room; }