From d3670373328f53a7a4f14e886cd821fd3f645696 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sat, 12 Oct 2019 20:24:09 +0200 Subject: [PATCH] very basic support for lazy loading --- src/matrix/hs-api.js | 15 ++++++++++++--- src/matrix/room/timeline/Timeline.js | 3 ++- src/matrix/session.js | 7 ++++++- src/matrix/sync.js | 8 ++++++-- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/matrix/hs-api.js b/src/matrix/hs-api.js index 903b2849..2c9b5548 100644 --- a/src/matrix/hs-api.js +++ b/src/matrix/hs-api.js @@ -48,7 +48,12 @@ export default class HomeServerApi { _request(method, csPath, queryParams = {}, body) { const queryString = Object.entries(queryParams) .filter(([, value]) => value !== undefined) - .map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`) + .map(([name, value]) => { + if (typeof value === "object") { + value = JSON.stringify(value); + } + return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`; + }) .join("&"); const url = this._url(`${csPath}?${queryString}`); let bodyString; @@ -117,11 +122,11 @@ export default class HomeServerApi { // params is from, dir and optionally to, limit, filter. messages(roomId, params) { - return this._get(`/rooms/${roomId}/messages`, params); + return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params); } send(roomId, eventType, txnId, content) { - return this._put(`/rooms/${roomId}/send/${eventType}/${txnId}`, {}, content); + return this._put(`/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {}, content); } passwordLogin(username, password) { @@ -134,4 +139,8 @@ export default class HomeServerApi { "password": password }); } + + createFilter(userId, filter) { + return this._post(`/user/${encodeURIComponent(userId)}/filter`, undefined, filter); + } } diff --git a/src/matrix/room/timeline/Timeline.js b/src/matrix/room/timeline/Timeline.js index b2afef57..99828479 100644 --- a/src/matrix/room/timeline/Timeline.js +++ b/src/matrix/room/timeline/Timeline.js @@ -41,7 +41,8 @@ export default class Timeline { const response = await this._hsApi.messages(this._roomId, { from: fragmentEntry.token, dir: fragmentEntry.direction.asApiString(), - limit: amount + limit: amount, + filter: {lazy_load_members: true} }).response(); const gapWriter = new GapWriter({ roomId: this._roomId, diff --git a/src/matrix/session.js b/src/matrix/session.js index 91c0983b..b86a8c69 100644 --- a/src/matrix/session.js +++ b/src/matrix/session.js @@ -77,9 +77,10 @@ export default class Session { return room; } - persistSync(syncToken, accountData, txn) { + persistSync(syncToken, syncFilterId, accountData, txn) { if (syncToken !== this._session.syncToken) { this._session.syncToken = syncToken; + this._session.syncFilterId = syncFilterId; txn.session.set(this._session); } } @@ -88,6 +89,10 @@ export default class Session { return this._session.syncToken; } + get syncFilterId() { + return this._session.syncFilterId; + } + get user() { return this._user; } diff --git a/src/matrix/sync.js b/src/matrix/sync.js index 33711c91..623684de 100644 --- a/src/matrix/sync.js +++ b/src/matrix/sync.js @@ -67,7 +67,11 @@ export default class Sync extends EventEmitter { } async _syncRequest(syncToken, timeout) { - this._currentRequest = this._hsApi.sync(syncToken, undefined, timeout); + let {syncFilterId} = this._session; + if (typeof syncFilterId !== "string") { + syncFilterId = (await this._hsApi.createFilter(this._session.user.id, {room: {state: {lazy_load_members: true}}}).response()).filter_id; + } + this._currentRequest = this._hsApi.sync(syncToken, syncFilterId, timeout); const response = await this._currentRequest.response(); syncToken = response.next_batch; const storeNames = this._storage.storeNames; @@ -81,7 +85,7 @@ export default class Sync extends EventEmitter { ]); const roomChanges = []; try { - this._session.persistSync(syncToken, response.account_data, syncTxn); + this._session.persistSync(syncToken, syncFilterId, response.account_data, syncTxn); // to_device // presence if (response.rooms) {