very basic support for lazy loading

This commit is contained in:
Bruno Windels 2019-10-12 20:24:09 +02:00
parent 3c57ebf8a0
commit d367037332
4 changed files with 26 additions and 7 deletions

View file

@ -48,7 +48,12 @@ export default class HomeServerApi {
_request(method, csPath, queryParams = {}, body) { _request(method, csPath, queryParams = {}, body) {
const queryString = Object.entries(queryParams) const queryString = Object.entries(queryParams)
.filter(([, value]) => value !== undefined) .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("&"); .join("&");
const url = this._url(`${csPath}?${queryString}`); const url = this._url(`${csPath}?${queryString}`);
let bodyString; let bodyString;
@ -117,11 +122,11 @@ export default class HomeServerApi {
// params is from, dir and optionally to, limit, filter. // params is from, dir and optionally to, limit, filter.
messages(roomId, params) { messages(roomId, params) {
return this._get(`/rooms/${roomId}/messages`, params); return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params);
} }
send(roomId, eventType, txnId, content) { 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) { passwordLogin(username, password) {
@ -134,4 +139,8 @@ export default class HomeServerApi {
"password": password "password": password
}); });
} }
createFilter(userId, filter) {
return this._post(`/user/${encodeURIComponent(userId)}/filter`, undefined, filter);
}
} }

View file

@ -41,7 +41,8 @@ export default class Timeline {
const response = await this._hsApi.messages(this._roomId, { const response = await this._hsApi.messages(this._roomId, {
from: fragmentEntry.token, from: fragmentEntry.token,
dir: fragmentEntry.direction.asApiString(), dir: fragmentEntry.direction.asApiString(),
limit: amount limit: amount,
filter: {lazy_load_members: true}
}).response(); }).response();
const gapWriter = new GapWriter({ const gapWriter = new GapWriter({
roomId: this._roomId, roomId: this._roomId,

View file

@ -77,9 +77,10 @@ export default class Session {
return room; return room;
} }
persistSync(syncToken, accountData, txn) { persistSync(syncToken, syncFilterId, accountData, txn) {
if (syncToken !== this._session.syncToken) { if (syncToken !== this._session.syncToken) {
this._session.syncToken = syncToken; this._session.syncToken = syncToken;
this._session.syncFilterId = syncFilterId;
txn.session.set(this._session); txn.session.set(this._session);
} }
} }
@ -88,6 +89,10 @@ export default class Session {
return this._session.syncToken; return this._session.syncToken;
} }
get syncFilterId() {
return this._session.syncFilterId;
}
get user() { get user() {
return this._user; return this._user;
} }

View file

@ -67,7 +67,11 @@ export default class Sync extends EventEmitter {
} }
async _syncRequest(syncToken, timeout) { 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(); const response = await this._currentRequest.response();
syncToken = response.next_batch; syncToken = response.next_batch;
const storeNames = this._storage.storeNames; const storeNames = this._storage.storeNames;
@ -81,7 +85,7 @@ export default class Sync extends EventEmitter {
]); ]);
const roomChanges = []; const roomChanges = [];
try { try {
this._session.persistSync(syncToken, response.account_data, syncTxn); this._session.persistSync(syncToken, syncFilterId, response.account_data, syncTxn);
// to_device // to_device
// presence // presence
if (response.rooms) { if (response.rooms) {