2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2021-04-20 17:39:46 +02:00
|
|
|
Copyright 2020, 2021 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +02:00
|
|
|
|
|
|
|
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-20 21:26:39 +02:00
|
|
|
import {ObservableValue} from "../observable/ObservableValue.js";
|
|
|
|
import {createEnum} from "../utils/enum.js";
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-02-10 21:25:29 +01:00
|
|
|
const INCREMENTAL_TIMEOUT = 30000;
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2020-04-19 19:52:26 +02:00
|
|
|
export const SyncStatus = createEnum(
|
|
|
|
"InitialSync",
|
|
|
|
"CatchupSync",
|
|
|
|
"Syncing",
|
|
|
|
"Stopped"
|
|
|
|
);
|
|
|
|
|
2020-08-17 14:13:23 +02:00
|
|
|
function timelineIsEmpty(roomResponse) {
|
|
|
|
try {
|
2020-08-19 11:36:43 +02:00
|
|
|
const events = roomResponse?.timeline?.events;
|
|
|
|
return Array.isArray(events) && events.length === 0;
|
2020-08-17 14:13:23 +02:00
|
|
|
} catch (err) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 12:11:43 +02:00
|
|
|
/**
|
|
|
|
* Sync steps in js-pseudocode:
|
|
|
|
* ```js
|
2020-09-23 14:26:14 +02:00
|
|
|
* // can only read some stores
|
2021-03-01 15:04:45 +01:00
|
|
|
* const preparation = await room.prepareSync(roomResponse, membership, newRoomKeys, prepareTxn);
|
2020-09-23 14:26:14 +02:00
|
|
|
* // can do async work that is not related to storage (such as decryption)
|
|
|
|
* await room.afterPrepareSync(preparation);
|
2020-09-10 12:11:43 +02:00
|
|
|
* // writes and calculates changes
|
2020-09-23 14:26:14 +02:00
|
|
|
* const changes = await room.writeSync(roomResponse, isInitialSync, preparation, syncTxn);
|
2020-09-10 12:11:43 +02:00
|
|
|
* // applies and emits changes once syncTxn is committed
|
2020-09-23 18:06:16 +02:00
|
|
|
* room.afterSync(changes);
|
2020-09-10 12:11:43 +02:00
|
|
|
* if (room.needsAfterSyncCompleted(changes)) {
|
|
|
|
* // can do network requests
|
|
|
|
* await room.afterSyncCompleted(changes);
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
2020-04-20 19:47:45 +02:00
|
|
|
export class Sync {
|
2021-02-12 18:56:26 +01:00
|
|
|
constructor({hsApi, session, storage, logger}) {
|
2019-05-12 20:26:46 +02:00
|
|
|
this._hsApi = hsApi;
|
2021-02-12 18:56:26 +01:00
|
|
|
this._logger = logger;
|
2019-05-12 20:26:46 +02:00
|
|
|
this._session = session;
|
|
|
|
this._storage = storage;
|
|
|
|
this._currentRequest = null;
|
2020-04-19 19:52:26 +02:00
|
|
|
this._status = new ObservableValue(SyncStatus.Stopped);
|
|
|
|
this._error = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
return this._status;
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2019-06-16 10:54:16 +02:00
|
|
|
|
2020-04-19 19:52:26 +02:00
|
|
|
/** the error that made the sync stop */
|
|
|
|
get error() {
|
|
|
|
return this._error;
|
2019-06-16 10:54:16 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 19:52:26 +02:00
|
|
|
start() {
|
|
|
|
// not already syncing?
|
|
|
|
if (this._status.get() !== SyncStatus.Stopped) {
|
2019-05-12 20:26:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-09-21 13:55:35 +02:00
|
|
|
this._error = null;
|
2019-05-12 20:26:46 +02:00
|
|
|
let syncToken = this._session.syncToken;
|
2020-04-19 19:52:26 +02:00
|
|
|
if (syncToken) {
|
|
|
|
this._status.set(SyncStatus.CatchupSync);
|
|
|
|
} else {
|
|
|
|
this._status.set(SyncStatus.InitialSync);
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
|
|
|
this._syncLoop(syncToken);
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-05-12 20:26:46 +02:00
|
|
|
async _syncLoop(syncToken) {
|
|
|
|
// if syncToken is falsy, it will first do an initial sync ...
|
2020-04-19 19:52:26 +02:00
|
|
|
while(this._status.get() !== SyncStatus.Stopped) {
|
2020-09-10 12:11:43 +02:00
|
|
|
let roomStates;
|
2020-09-24 10:52:56 +02:00
|
|
|
let sessionChanges;
|
2021-02-18 19:48:19 +01:00
|
|
|
let wasCatchupOrInitial = this._status.get() === SyncStatus.CatchupSync || this._status.get() === SyncStatus.InitialSync;
|
2021-02-17 18:45:04 +01:00
|
|
|
await this._logger.run("sync", async log => {
|
|
|
|
log.set("token", syncToken);
|
|
|
|
log.set("status", this._status.get());
|
|
|
|
try {
|
|
|
|
// unless we are happily syncing already, we want the server to return
|
|
|
|
// as quickly as possible, even if there are no events queued. This
|
|
|
|
// serves two purposes:
|
|
|
|
//
|
|
|
|
// * When the connection dies, we want to know asap when it comes back,
|
|
|
|
// so that we can hide the error from the user. (We don't want to
|
|
|
|
// have to wait for an event or a timeout).
|
|
|
|
//
|
|
|
|
// * We want to know if the server has any to_device messages queued up
|
|
|
|
// for us. We do that by calling it with a zero timeout until it
|
|
|
|
// doesn't give us any more to_device messages.
|
|
|
|
const timeout = this._status.get() === SyncStatus.Syncing ? INCREMENTAL_TIMEOUT : 0;
|
|
|
|
const syncResult = await this._syncRequest(syncToken, timeout, log);
|
|
|
|
syncToken = syncResult.syncToken;
|
|
|
|
roomStates = syncResult.roomStates;
|
|
|
|
sessionChanges = syncResult.sessionChanges;
|
|
|
|
// initial sync or catchup sync
|
|
|
|
if (this._status.get() !== SyncStatus.Syncing && syncResult.hadToDeviceMessages) {
|
|
|
|
this._status.set(SyncStatus.CatchupSync);
|
|
|
|
} else {
|
|
|
|
this._status.set(SyncStatus.Syncing);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// retry same request on timeout
|
|
|
|
if (err.name === "ConnectionError" && err.isTimeout) {
|
|
|
|
// don't run afterSyncCompleted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._error = err;
|
|
|
|
if (err.name !== "AbortError") {
|
|
|
|
// sync wasn't asked to stop, but is stopping
|
|
|
|
// because of the error.
|
|
|
|
log.error = err;
|
|
|
|
log.logLevel = log.level.Fatal;
|
|
|
|
}
|
|
|
|
log.set("stopping", true);
|
|
|
|
this._status.set(SyncStatus.Stopped);
|
2020-09-21 17:57:01 +02:00
|
|
|
}
|
2021-02-17 18:45:04 +01:00
|
|
|
if (this._status.get() !== SyncStatus.Stopped) {
|
|
|
|
// TODO: if we're not going to run this phase in parallel with the next
|
|
|
|
// sync request (because this causes OTKs to be uploaded twice)
|
|
|
|
// should we move this inside _syncRequest?
|
|
|
|
// Alternatively, we can try to fix the OTK upload issue while still
|
|
|
|
// running in parallel.
|
|
|
|
await log.wrap("afterSyncCompleted", log => this._runAfterSyncCompleted(sessionChanges, roomStates, log));
|
2020-09-25 10:44:29 +02:00
|
|
|
}
|
2021-02-17 18:45:04 +01:00
|
|
|
},
|
|
|
|
this._logger.level.Info,
|
|
|
|
(filter, log) => {
|
2021-02-18 19:48:19 +01:00
|
|
|
if (log.durationWithoutType("network") >= 2000 || log.error || wasCatchupOrInitial) {
|
2021-02-17 18:45:04 +01:00
|
|
|
return filter.minLevel(log.level.Detail);
|
|
|
|
} else {
|
|
|
|
return filter.minLevel(log.level.Info);
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2021-02-17 18:45:04 +01:00
|
|
|
});
|
2020-09-08 14:37:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:45:04 +01:00
|
|
|
async _runAfterSyncCompleted(sessionChanges, roomStates, log) {
|
2020-09-21 17:57:01 +02:00
|
|
|
const isCatchupSync = this._status.get() === SyncStatus.CatchupSync;
|
2020-09-08 14:37:24 +02:00
|
|
|
const sessionPromise = (async () => {
|
|
|
|
try {
|
2021-02-18 13:07:52 +01:00
|
|
|
await log.wrap("session", log => this._session.afterSyncCompleted(sessionChanges, isCatchupSync, log), log.level.Detail);
|
2021-02-17 18:45:04 +01:00
|
|
|
} catch (err) {} // error is logged, but don't fail sessionPromise
|
2020-09-08 14:37:24 +02:00
|
|
|
})();
|
|
|
|
|
2020-09-10 12:11:43 +02:00
|
|
|
const roomsNeedingAfterSyncCompleted = roomStates.filter(rs => {
|
|
|
|
return rs.room.needsAfterSyncCompleted(rs.changes);
|
|
|
|
});
|
|
|
|
const roomsPromises = roomsNeedingAfterSyncCompleted.map(async rs => {
|
|
|
|
try {
|
2021-02-18 12:39:29 +01:00
|
|
|
await log.wrap("room", log => rs.room.afterSyncCompleted(rs.changes, log), log.level.Detail);
|
2021-02-17 18:45:04 +01:00
|
|
|
} catch (err) {} // error is logged, but don't fail roomsPromises
|
2020-09-08 14:37:24 +02:00
|
|
|
});
|
|
|
|
// run everything in parallel,
|
|
|
|
// we don't want to delay the next sync too much
|
2020-09-10 12:11:43 +02:00
|
|
|
// Also, since all promises won't reject (as they have a try/catch)
|
|
|
|
// it's fine to use Promise.all
|
|
|
|
await Promise.all(roomsPromises.concat(sessionPromise));
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2019-02-03 22:17:24 +01:00
|
|
|
|
2021-02-12 18:56:26 +01:00
|
|
|
async _syncRequest(syncToken, timeout, log) {
|
2019-10-12 20:24:09 +02:00
|
|
|
let {syncFilterId} = this._session;
|
|
|
|
if (typeof syncFilterId !== "string") {
|
2021-02-17 18:45:04 +01:00
|
|
|
this._currentRequest = this._hsApi.createFilter(this._session.user.id, {room: {state: {lazy_load_members: true}}}, {log});
|
2020-05-07 00:04:41 +02:00
|
|
|
syncFilterId = (await this._currentRequest.response()).filter_id;
|
2019-10-12 20:24:09 +02:00
|
|
|
}
|
2020-05-06 23:50:12 +02:00
|
|
|
const totalRequestTimeout = timeout + (80 * 1000); // same as riot-web, don't get stuck on wedged long requests
|
2021-02-12 18:56:26 +01:00
|
|
|
this._currentRequest = this._hsApi.sync(syncToken, syncFilterId, timeout, {timeout: totalRequestTimeout, log});
|
2019-05-12 20:26:46 +02:00
|
|
|
const response = await this._currentRequest.response();
|
2020-09-08 14:37:24 +02:00
|
|
|
|
2020-08-17 14:13:23 +02:00
|
|
|
const isInitialSync = !syncToken;
|
2021-03-01 15:04:45 +01:00
|
|
|
const sessionState = new SessionSyncProcessState();
|
2021-04-20 17:39:46 +02:00
|
|
|
const inviteStates = this._parseInvites(response.rooms);
|
|
|
|
const roomStates = this._parseRoomsResponse(response.rooms, inviteStates, isInitialSync);
|
2021-02-17 18:45:04 +01:00
|
|
|
|
2021-03-01 15:04:45 +01:00
|
|
|
try {
|
|
|
|
// take a lock on olm sessions used in this sync so sending a message doesn't change them while syncing
|
|
|
|
sessionState.lock = await log.wrap("obtainSyncLock", () => this._session.obtainSyncLock(response));
|
2021-04-20 17:57:17 +02:00
|
|
|
await log.wrap("prepare", log => this._prepareSync(sessionState, roomStates, response, log));
|
2021-03-01 15:04:45 +01:00
|
|
|
await log.wrap("afterPrepareSync", log => Promise.all(roomStates.map(rs => {
|
|
|
|
return rs.room.afterPrepareSync(rs.preparation, log);
|
|
|
|
})));
|
2021-04-20 17:57:17 +02:00
|
|
|
await log.wrap("write", async log => this._writeSync(
|
|
|
|
sessionState, inviteStates, roomStates, response, syncFilterId, isInitialSync, log));
|
2021-03-01 15:04:45 +01:00
|
|
|
} finally {
|
|
|
|
sessionState.dispose();
|
|
|
|
}
|
2021-04-20 17:57:17 +02:00
|
|
|
// sync txn comitted, emit updates and apply changes to in-memory state
|
|
|
|
log.wrap("after", log => this._afterSync(sessionState, inviteStates, roomStates, log));
|
2019-02-27 19:27:45 +01:00
|
|
|
|
2020-09-21 17:57:01 +02:00
|
|
|
const toDeviceEvents = response.to_device?.events;
|
|
|
|
return {
|
2021-02-17 18:45:04 +01:00
|
|
|
syncToken: response.next_batch,
|
2020-09-21 17:57:01 +02:00
|
|
|
roomStates,
|
2021-03-01 15:04:45 +01:00
|
|
|
sessionChanges: sessionState.changes,
|
2020-09-21 17:57:01 +02:00
|
|
|
hadToDeviceMessages: Array.isArray(toDeviceEvents) && toDeviceEvents.length > 0,
|
|
|
|
};
|
2020-09-08 14:37:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 16:42:41 +02:00
|
|
|
_openPrepareSyncTxn() {
|
2020-09-10 12:11:43 +02:00
|
|
|
const storeNames = this._storage.storeNames;
|
2020-09-25 16:42:41 +02:00
|
|
|
return this._storage.readTxn([
|
2021-03-01 15:04:45 +01:00
|
|
|
storeNames.olmSessions,
|
2020-09-10 12:11:43 +02:00
|
|
|
storeNames.inboundGroupSessions,
|
2021-03-02 15:29:35 +01:00
|
|
|
storeNames.timelineEvents // to read events that can now be decrypted
|
2020-09-10 12:11:43 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-04-20 17:57:17 +02:00
|
|
|
async _prepareSync(sessionState, roomStates, response, log) {
|
2021-03-04 19:47:02 +01:00
|
|
|
const prepareTxn = await this._openPrepareSyncTxn();
|
2021-03-01 15:04:45 +01:00
|
|
|
sessionState.preparation = await log.wrap("session", log => this._session.prepareSync(
|
|
|
|
response, sessionState.lock, prepareTxn, log));
|
|
|
|
|
|
|
|
const newKeysByRoom = sessionState.preparation?.newKeysByRoom;
|
2021-03-01 22:30:45 +01:00
|
|
|
|
|
|
|
// add any rooms with new keys but no sync response to the list of rooms to be synced
|
|
|
|
if (newKeysByRoom) {
|
|
|
|
const {hasOwnProperty} = Object.prototype;
|
|
|
|
for (const roomId of newKeysByRoom.keys()) {
|
|
|
|
const isRoomInResponse = response.rooms?.join && hasOwnProperty.call(response.rooms.join, roomId);
|
|
|
|
if (!isRoomInResponse) {
|
|
|
|
let room = this._session.rooms.get(roomId);
|
|
|
|
if (room) {
|
2021-04-20 17:39:46 +02:00
|
|
|
roomStates.push(new RoomSyncProcessState(room, false, null, {}, room.membership));
|
2021-03-01 22:30:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 15:04:45 +01:00
|
|
|
|
2020-09-23 14:26:14 +02:00
|
|
|
await Promise.all(roomStates.map(async rs => {
|
2021-03-01 15:04:45 +01:00
|
|
|
const newKeys = newKeysByRoom?.get(rs.room.id);
|
|
|
|
rs.preparation = await log.wrap("room", log => rs.room.prepareSync(
|
2021-04-20 17:39:46 +02:00
|
|
|
rs.roomResponse, rs.membership, rs.invite, newKeys, prepareTxn, log), log.level.Detail);
|
2020-09-23 14:26:14 +02:00
|
|
|
}));
|
2021-03-01 15:04:45 +01:00
|
|
|
|
2020-10-01 16:14:06 +02:00
|
|
|
// This is needed for safari to not throw TransactionInactiveErrors on the syncTxn. See docs/INDEXEDDB.md
|
2020-10-01 14:31:38 +02:00
|
|
|
await prepareTxn.complete();
|
2020-09-08 14:37:24 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 17:57:17 +02:00
|
|
|
async _writeSync(sessionState, inviteStates, roomStates, response, syncFilterId, isInitialSync, log) {
|
|
|
|
const syncTxn = await this._openSyncTxn();
|
|
|
|
try {
|
|
|
|
sessionState.changes = await log.wrap("session", log => this._session.writeSync(
|
|
|
|
response, syncFilterId, sessionState.preparation, syncTxn, log));
|
|
|
|
await Promise.all(inviteStates.map(async is => {
|
|
|
|
is.changes = await log.wrap("invite", log => is.invite.writeSync(
|
|
|
|
is.membership, is.roomResponse, syncTxn, log));
|
|
|
|
}));
|
|
|
|
await Promise.all(roomStates.map(async rs => {
|
|
|
|
rs.changes = await log.wrap("room", log => rs.room.writeSync(
|
|
|
|
rs.roomResponse, isInitialSync, rs.preparation, syncTxn, log));
|
|
|
|
}));
|
|
|
|
} catch(err) {
|
|
|
|
// avoid corrupting state by only
|
|
|
|
// storing the sync up till the point
|
|
|
|
// the exception occurred
|
|
|
|
try {
|
|
|
|
syncTxn.abort();
|
|
|
|
} catch (abortErr) {
|
|
|
|
log.set("couldNotAbortTxn", true);
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await syncTxn.complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
_afterSync(sessionState, inviteStates, roomStates, log) {
|
|
|
|
log.wrap("session", log => this._session.afterSync(sessionState.changes, log), log.level.Detail);
|
2021-04-23 18:04:10 +02:00
|
|
|
// emit room related events after txn has been closed
|
|
|
|
for(let rs of roomStates) {
|
|
|
|
log.wrap("room", log => rs.room.afterSync(rs.changes, log), log.level.Detail);
|
|
|
|
if (rs.isNewRoom) {
|
2021-04-28 10:05:34 +02:00
|
|
|
// important to add the room before removing the invite,
|
|
|
|
// so the room will be found if looking for it when the invite
|
|
|
|
// is removed
|
2021-04-23 18:04:10 +02:00
|
|
|
this._session.addRoomAfterSync(rs.room);
|
2021-05-05 16:54:38 +02:00
|
|
|
} else if (rs.membership === "leave") {
|
|
|
|
this._session.archiveRoomAfterSync(rs.room);
|
2021-04-23 18:04:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-20 17:57:17 +02:00
|
|
|
// emit invite related events after txn has been closed
|
|
|
|
for(let is of inviteStates) {
|
2021-05-05 16:50:45 +02:00
|
|
|
log.wrap("invite", () => {
|
|
|
|
// important to remove before emitting change in afterSync
|
|
|
|
// so code checking session.invites.get(id) won't
|
|
|
|
// find the invite anymore on update
|
|
|
|
if (is.membership !== "invite") {
|
|
|
|
this._session.removeInviteAfterSync(is.invite);
|
|
|
|
}
|
|
|
|
is.invite.afterSync(is.changes);
|
|
|
|
}, log.level.Detail);
|
2021-04-20 17:57:17 +02:00
|
|
|
if (is.isNewInvite) {
|
|
|
|
this._session.addInviteAfterSync(is.invite);
|
|
|
|
}
|
2021-04-23 18:05:14 +02:00
|
|
|
// if we haven't archived or forgotten the (left) room yet,
|
|
|
|
// notify there is an invite now, so we can update the UI
|
|
|
|
if (is.room) {
|
|
|
|
is.room.setInvite(is.invite);
|
2021-04-20 17:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 16:42:41 +02:00
|
|
|
_openSyncTxn() {
|
2020-09-08 14:37:24 +02:00
|
|
|
const storeNames = this._storage.storeNames;
|
2020-09-25 16:42:41 +02:00
|
|
|
return this._storage.readWriteTxn([
|
2020-09-08 14:37:24 +02:00
|
|
|
storeNames.session,
|
|
|
|
storeNames.roomSummary,
|
2021-05-04 13:34:42 +02:00
|
|
|
storeNames.archivedRoomSummary,
|
2021-04-20 17:39:46 +02:00
|
|
|
storeNames.invites,
|
2020-09-08 14:37:24 +02:00
|
|
|
storeNames.roomState,
|
|
|
|
storeNames.roomMembers,
|
|
|
|
storeNames.timelineEvents,
|
|
|
|
storeNames.timelineFragments,
|
|
|
|
storeNames.pendingEvents,
|
|
|
|
storeNames.userIdentities,
|
|
|
|
storeNames.groupSessionDecryptions,
|
|
|
|
storeNames.deviceIdentities,
|
2020-09-08 15:00:00 +02:00
|
|
|
// to discard outbound session when somebody leaves a room
|
2020-10-01 14:39:23 +02:00
|
|
|
// and to create room key messages when somebody joins
|
2020-09-11 14:41:12 +02:00
|
|
|
storeNames.outboundGroupSessions,
|
2020-09-17 10:39:51 +02:00
|
|
|
storeNames.operations,
|
|
|
|
storeNames.accountData,
|
2021-03-01 15:04:45 +01:00
|
|
|
// to decrypt and store new room keys
|
|
|
|
storeNames.olmSessions,
|
|
|
|
storeNames.inboundGroupSessions,
|
2020-09-08 14:37:24 +02:00
|
|
|
]);
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2020-09-10 12:11:43 +02:00
|
|
|
|
2021-04-20 17:39:46 +02:00
|
|
|
_parseRoomsResponse(roomsSection, inviteStates, isInitialSync) {
|
2020-09-10 12:11:43 +02:00
|
|
|
const roomStates = [];
|
|
|
|
if (roomsSection) {
|
2021-04-22 17:23:29 +02:00
|
|
|
const allMemberships = ["join", "leave"];
|
2020-09-10 12:11:43 +02:00
|
|
|
for(const membership of allMemberships) {
|
|
|
|
const membershipSection = roomsSection[membership];
|
|
|
|
if (membershipSection) {
|
|
|
|
for (const [roomId, roomResponse] of Object.entries(membershipSection)) {
|
|
|
|
// ignore rooms with empty timelines during initial sync,
|
|
|
|
// see https://github.com/vector-im/hydrogen-web/issues/15
|
|
|
|
if (isInitialSync && timelineIsEmpty(roomResponse)) {
|
2020-09-21 14:11:28 +02:00
|
|
|
continue;
|
2020-09-10 12:11:43 +02:00
|
|
|
}
|
2021-04-20 17:39:46 +02:00
|
|
|
let isNewRoom = false;
|
2020-09-10 12:11:43 +02:00
|
|
|
let room = this._session.rooms.get(roomId);
|
2021-04-26 10:41:21 +02:00
|
|
|
// don't create a room for a rejected invite
|
|
|
|
if (!room && membership === "join") {
|
2020-09-10 12:11:43 +02:00
|
|
|
room = this._session.createRoom(roomId);
|
2021-04-20 17:39:46 +02:00
|
|
|
isNewRoom = true;
|
2020-09-10 12:11:43 +02:00
|
|
|
}
|
2021-04-20 17:39:46 +02:00
|
|
|
const invite = this._session.invites.get(roomId);
|
|
|
|
// if there is an existing invite, add a process state for it
|
|
|
|
// so its writeSync and afterSync will run and remove the invite
|
|
|
|
if (invite) {
|
2021-04-23 18:05:14 +02:00
|
|
|
inviteStates.push(new InviteSyncProcessState(invite, false, null, membership, null));
|
2021-04-20 17:39:46 +02:00
|
|
|
}
|
2021-04-26 10:41:21 +02:00
|
|
|
if (room) {
|
|
|
|
roomStates.push(new RoomSyncProcessState(
|
|
|
|
room, isNewRoom, invite, roomResponse, membership));
|
|
|
|
}
|
2020-09-10 12:11:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return roomStates;
|
|
|
|
}
|
|
|
|
|
2021-04-21 17:26:10 +02:00
|
|
|
_parseInvites(roomsSection) {
|
2021-04-20 17:39:46 +02:00
|
|
|
const inviteStates = [];
|
2021-05-06 12:10:10 +02:00
|
|
|
if (roomsSection?.invite) {
|
2021-04-20 17:39:46 +02:00
|
|
|
for (const [roomId, roomResponse] of Object.entries(roomsSection.invite)) {
|
|
|
|
let invite = this._session.invites.get(roomId);
|
|
|
|
let isNewInvite = false;
|
|
|
|
if (!invite) {
|
|
|
|
invite = this._session.createInvite(roomId);
|
|
|
|
isNewInvite = true;
|
|
|
|
}
|
2021-04-23 18:05:14 +02:00
|
|
|
const room = this._session.rooms.get(roomId);
|
|
|
|
inviteStates.push(new InviteSyncProcessState(invite, isNewInvite, room, "invite", roomResponse));
|
2021-04-20 17:39:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return inviteStates;
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-05-12 20:26:46 +02:00
|
|
|
stop() {
|
2020-04-19 19:52:26 +02:00
|
|
|
if (this._status.get() === SyncStatus.Stopped) {
|
2019-05-12 20:26:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-19 19:52:26 +02:00
|
|
|
this._status.set(SyncStatus.Stopped);
|
2019-05-12 20:26:46 +02:00
|
|
|
if (this._currentRequest) {
|
|
|
|
this._currentRequest.abort();
|
|
|
|
this._currentRequest = null;
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 23:48:16 +01:00
|
|
|
}
|
2020-09-10 12:11:43 +02:00
|
|
|
|
2021-03-01 15:04:45 +01:00
|
|
|
class SessionSyncProcessState {
|
|
|
|
constructor() {
|
|
|
|
this.lock = null;
|
|
|
|
this.preparation = null;
|
|
|
|
this.changes = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this.lock?.release();
|
|
|
|
this.preparation?.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 12:11:43 +02:00
|
|
|
class RoomSyncProcessState {
|
2021-04-20 17:39:46 +02:00
|
|
|
constructor(room, isNewRoom, invite, roomResponse, membership) {
|
2020-09-10 12:11:43 +02:00
|
|
|
this.room = room;
|
2021-04-20 17:39:46 +02:00
|
|
|
this.isNewRoom = isNewRoom;
|
|
|
|
this.invite = invite;
|
2020-09-10 12:11:43 +02:00
|
|
|
this.roomResponse = roomResponse;
|
|
|
|
this.membership = membership;
|
|
|
|
this.preparation = null;
|
|
|
|
this.changes = null;
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 17:39:46 +02:00
|
|
|
|
|
|
|
class InviteSyncProcessState {
|
2021-04-23 18:05:14 +02:00
|
|
|
constructor(invite, isNewInvite, room, membership, roomResponse) {
|
2021-04-20 17:39:46 +02:00
|
|
|
this.invite = invite;
|
|
|
|
this.isNewInvite = isNewInvite;
|
2021-04-23 18:05:14 +02:00
|
|
|
this.room = room;
|
2021-04-20 17:39:46 +02:00
|
|
|
this.membership = membership;
|
|
|
|
this.roomResponse = roomResponse;
|
|
|
|
this.changes = null;
|
|
|
|
}
|
|
|
|
}
|