2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-08-17 17:43:23 +05:30
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 22:08:55 +05:30
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-04-04 21:04:46 +05:30
|
|
|
import {AbortError} from "./error.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {ObservableValue} from "../observable/ObservableValue.js";
|
|
|
|
import {createEnum} from "../utils/enum.js";
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-02-11 01:55:29 +05:30
|
|
|
const INCREMENTAL_TIMEOUT = 30000;
|
|
|
|
const SYNC_EVENT_LIMIT = 10;
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2020-04-19 23:22:26 +05:30
|
|
|
export const SyncStatus = createEnum(
|
|
|
|
"InitialSync",
|
|
|
|
"CatchupSync",
|
|
|
|
"Syncing",
|
|
|
|
"Stopped"
|
|
|
|
);
|
|
|
|
|
2019-02-11 01:55:29 +05:30
|
|
|
function parseRooms(roomsSection, roomCallback) {
|
2019-06-02 04:19:47 +05:30
|
|
|
if (roomsSection) {
|
|
|
|
const allMemberships = ["join", "invite", "leave"];
|
|
|
|
for(const membership of allMemberships) {
|
|
|
|
const membershipSection = roomsSection[membership];
|
|
|
|
if (membershipSection) {
|
|
|
|
return Object.entries(membershipSection).map(([roomId, roomResponse]) => {
|
|
|
|
return roomCallback(roomId, roomResponse, membership);
|
|
|
|
});
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 04:19:47 +05:30
|
|
|
return [];
|
2019-02-05 03:56:45 +05:30
|
|
|
}
|
|
|
|
|
2020-08-17 17:43:23 +05:30
|
|
|
function timelineIsEmpty(roomResponse) {
|
|
|
|
try {
|
2020-08-19 15:06:43 +05:30
|
|
|
const events = roomResponse?.timeline?.events;
|
|
|
|
return Array.isArray(events) && events.length === 0;
|
2020-08-17 17:43:23 +05:30
|
|
|
} catch (err) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 23:17:45 +05:30
|
|
|
export class Sync {
|
2019-09-08 13:49:16 +05:30
|
|
|
constructor({hsApi, session, storage}) {
|
2019-05-12 23:56:46 +05:30
|
|
|
this._hsApi = hsApi;
|
|
|
|
this._session = session;
|
|
|
|
this._storage = storage;
|
|
|
|
this._currentRequest = null;
|
2020-04-19 23:22:26 +05:30
|
|
|
this._status = new ObservableValue(SyncStatus.Stopped);
|
|
|
|
this._error = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
return this._status;
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2019-06-16 14:24:16 +05:30
|
|
|
|
2020-04-19 23:22:26 +05:30
|
|
|
/** the error that made the sync stop */
|
|
|
|
get error() {
|
|
|
|
return this._error;
|
2019-06-16 14:24:16 +05:30
|
|
|
}
|
|
|
|
|
2020-04-19 23:22:26 +05:30
|
|
|
start() {
|
|
|
|
// not already syncing?
|
|
|
|
if (this._status.get() !== SyncStatus.Stopped) {
|
2019-05-12 23:56:46 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
let syncToken = this._session.syncToken;
|
2020-04-19 23:22:26 +05:30
|
|
|
if (syncToken) {
|
|
|
|
this._status.set(SyncStatus.CatchupSync);
|
|
|
|
} else {
|
|
|
|
this._status.set(SyncStatus.InitialSync);
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
|
|
|
this._syncLoop(syncToken);
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-05-12 23:56:46 +05:30
|
|
|
async _syncLoop(syncToken) {
|
2020-09-08 18:07:24 +05:30
|
|
|
let afterSyncCompletedPromise = Promise.resolve();
|
2019-05-12 23:56:46 +05:30
|
|
|
// if syncToken is falsy, it will first do an initial sync ...
|
2020-04-19 23:22:26 +05:30
|
|
|
while(this._status.get() !== SyncStatus.Stopped) {
|
2020-09-08 18:07:24 +05:30
|
|
|
let roomChanges;
|
2019-05-12 23:56:46 +05:30
|
|
|
try {
|
|
|
|
console.log(`starting sync request with since ${syncToken} ...`);
|
2020-04-19 23:22:26 +05:30
|
|
|
const timeout = syncToken ? INCREMENTAL_TIMEOUT : undefined;
|
2020-09-08 18:07:24 +05:30
|
|
|
const syncResult = await this._syncRequest(syncToken, timeout, afterSyncCompletedPromise);
|
|
|
|
syncToken = syncResult.syncToken;
|
|
|
|
roomChanges = syncResult.roomChanges;
|
2020-04-19 23:22:26 +05:30
|
|
|
this._status.set(SyncStatus.Syncing);
|
2019-05-12 23:56:46 +05:30
|
|
|
} catch (err) {
|
2020-04-04 21:04:46 +05:30
|
|
|
if (!(err instanceof AbortError)) {
|
2020-04-19 23:22:26 +05:30
|
|
|
this._error = err;
|
|
|
|
this._status.set(SyncStatus.Stopped);
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
|
|
|
}
|
2020-08-31 17:43:36 +05:30
|
|
|
if (!this._error) {
|
2020-09-08 18:07:24 +05:30
|
|
|
afterSyncCompletedPromise = this._runAfterSyncCompleted(roomChanges);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _runAfterSyncCompleted(roomChanges) {
|
|
|
|
const sessionPromise = (async () => {
|
|
|
|
try {
|
|
|
|
await this._session.afterSyncCompleted();
|
|
|
|
} catch (err) {
|
|
|
|
console.error("error during session afterSyncCompleted, continuing", err.stack);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
let allPromises = [sessionPromise];
|
|
|
|
|
|
|
|
const roomsNeedingAfterSyncCompleted = roomChanges.filter(rc => {
|
|
|
|
return rc.changes.needsAfterSyncCompleted;
|
|
|
|
});
|
|
|
|
if (roomsNeedingAfterSyncCompleted.length) {
|
|
|
|
allPromises = allPromises.concat(roomsNeedingAfterSyncCompleted.map(async ({room, changes}) => {
|
2020-08-31 17:43:36 +05:30
|
|
|
try {
|
2020-09-08 18:07:24 +05:30
|
|
|
await room.afterSyncCompleted(changes);
|
2020-08-31 17:43:36 +05:30
|
|
|
} catch (err) {
|
2020-09-08 18:07:24 +05:30
|
|
|
console.error(`error during room ${room.id} afterSyncCompleted, continuing`, err.stack);
|
2020-08-31 17:43:36 +05:30
|
|
|
}
|
2020-09-08 18:07:24 +05:30
|
|
|
}));
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2020-09-08 18:07:24 +05:30
|
|
|
// run everything in parallel,
|
|
|
|
// we don't want to delay the next sync too much
|
|
|
|
await Promise.all(allPromises);
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2019-02-04 02:47:24 +05:30
|
|
|
|
2020-09-08 18:07:24 +05:30
|
|
|
async _syncRequest(syncToken, timeout, prevAfterSyncCompletedPromise) {
|
2019-10-12 23:54:09 +05:30
|
|
|
let {syncFilterId} = this._session;
|
|
|
|
if (typeof syncFilterId !== "string") {
|
2020-05-07 03:34:41 +05:30
|
|
|
this._currentRequest = this._hsApi.createFilter(this._session.user.id, {room: {state: {lazy_load_members: true}}});
|
|
|
|
syncFilterId = (await this._currentRequest.response()).filter_id;
|
2019-10-12 23:54:09 +05:30
|
|
|
}
|
2020-05-07 03:20:12 +05:30
|
|
|
const totalRequestTimeout = timeout + (80 * 1000); // same as riot-web, don't get stuck on wedged long requests
|
|
|
|
this._currentRequest = this._hsApi.sync(syncToken, syncFilterId, timeout, {timeout: totalRequestTimeout});
|
2019-05-12 23:56:46 +05:30
|
|
|
const response = await this._currentRequest.response();
|
2020-09-08 18:07:24 +05:30
|
|
|
// wait here for the afterSyncCompleted step of the previous sync to complete
|
|
|
|
// before we continue processing this sync response
|
|
|
|
await prevAfterSyncCompletedPromise;
|
|
|
|
|
2020-08-17 17:43:23 +05:30
|
|
|
const isInitialSync = !syncToken;
|
2019-05-12 23:56:46 +05:30
|
|
|
syncToken = response.next_batch;
|
2020-09-08 18:07:24 +05:30
|
|
|
const syncTxn = await this._openSyncTxn();
|
|
|
|
let roomChanges = [];
|
2020-03-15 01:15:36 +05:30
|
|
|
let sessionChanges;
|
2019-02-27 23:57:45 +05:30
|
|
|
try {
|
|
|
|
// to_device
|
|
|
|
// presence
|
2019-05-12 23:56:46 +05:30
|
|
|
if (response.rooms) {
|
2020-09-08 18:07:24 +05:30
|
|
|
roomChanges = await this._writeRoomResponses(response.rooms, isInitialSync, syncTxn);
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2020-08-31 17:43:21 +05:30
|
|
|
sessionChanges = await this._session.writeSync(response, syncFilterId, roomChanges, syncTxn);
|
2019-05-12 23:56:46 +05:30
|
|
|
} catch(err) {
|
2019-06-02 18:29:30 +05:30
|
|
|
console.warn("aborting syncTxn because of error");
|
2020-06-27 02:56:24 +05:30
|
|
|
console.error(err);
|
2019-05-12 23:56:46 +05:30
|
|
|
// avoid corrupting state by only
|
|
|
|
// storing the sync up till the point
|
|
|
|
// the exception occurred
|
|
|
|
syncTxn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await syncTxn.complete();
|
|
|
|
console.info("syncTxn committed!!");
|
|
|
|
} catch (err) {
|
2019-10-13 01:48:19 +05:30
|
|
|
console.error("unable to commit sync tranaction");
|
2019-06-27 01:30:50 +05:30
|
|
|
throw err;
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2020-03-15 01:15:36 +05:30
|
|
|
this._session.afterSync(sessionChanges);
|
2019-02-27 23:57:45 +05:30
|
|
|
// emit room related events after txn has been closed
|
|
|
|
for(let {room, changes} of roomChanges) {
|
2020-03-15 01:16:49 +05:30
|
|
|
room.afterSync(changes);
|
2019-02-27 23:57:45 +05:30
|
|
|
}
|
|
|
|
|
2020-09-08 18:07:24 +05:30
|
|
|
return {syncToken, roomChanges};
|
|
|
|
}
|
|
|
|
|
|
|
|
async _writeRoomResponses(roomResponses, isInitialSync, syncTxn) {
|
|
|
|
const roomChanges = [];
|
|
|
|
const promises = parseRooms(roomResponses, async (roomId, roomResponse, membership) => {
|
|
|
|
// ignore rooms with empty timelines during initial sync,
|
|
|
|
// see https://github.com/vector-im/hydrogen-web/issues/15
|
|
|
|
if (isInitialSync && timelineIsEmpty(roomResponse)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let room = this._session.rooms.get(roomId);
|
|
|
|
if (!room) {
|
|
|
|
room = this._session.createRoom(roomId);
|
|
|
|
}
|
|
|
|
console.log(` * applying sync response to room ${roomId} ...`);
|
|
|
|
const changes = await room.writeSync(roomResponse, membership, isInitialSync, syncTxn);
|
|
|
|
roomChanges.push({room, changes});
|
|
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
return roomChanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _openSyncTxn() {
|
|
|
|
const storeNames = this._storage.storeNames;
|
|
|
|
return await this._storage.readWriteTxn([
|
|
|
|
storeNames.session,
|
|
|
|
storeNames.roomSummary,
|
|
|
|
storeNames.roomState,
|
|
|
|
storeNames.roomMembers,
|
|
|
|
storeNames.timelineEvents,
|
|
|
|
storeNames.timelineFragments,
|
|
|
|
storeNames.pendingEvents,
|
|
|
|
storeNames.userIdentities,
|
|
|
|
storeNames.inboundGroupSessions,
|
|
|
|
storeNames.groupSessionDecryptions,
|
|
|
|
storeNames.deviceIdentities,
|
2020-09-08 18:30:00 +05:30
|
|
|
// to discard outbound session when somebody leaves a room
|
|
|
|
storeNames.outboundGroupSessions
|
2020-09-08 18:07:24 +05:30
|
|
|
]);
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-05-12 23:56:46 +05:30
|
|
|
stop() {
|
2020-04-19 23:22:26 +05:30
|
|
|
if (this._status.get() === SyncStatus.Stopped) {
|
2019-05-12 23:56:46 +05:30
|
|
|
return;
|
|
|
|
}
|
2020-04-19 23:22:26 +05:30
|
|
|
this._status.set(SyncStatus.Stopped);
|
2019-05-12 23:56:46 +05:30
|
|
|
if (this._currentRequest) {
|
|
|
|
this._currentRequest.abort();
|
|
|
|
this._currentRequest = null;
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|