2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
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:41:10 +02:00
|
|
|
import {Room} from "./room/Room.js";
|
2019-02-26 20:49:45 +01:00
|
|
|
import { ObservableMap } from "../observable/index.js";
|
2020-09-22 13:49:01 +02:00
|
|
|
import {RequestScheduler} from "./net/RequestScheduler.js";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {User} from "./User.js";
|
2020-09-02 14:30:18 +02:00
|
|
|
import {DeviceMessageHandler} from "./DeviceMessageHandler.js";
|
2020-09-03 17:51:11 +02:00
|
|
|
import {Account as E2EEAccount} from "./e2ee/Account.js";
|
2020-09-02 14:30:18 +02:00
|
|
|
import {Decryption as OlmDecryption} from "./e2ee/olm/Decryption.js";
|
2020-09-03 15:32:08 +02:00
|
|
|
import {Encryption as OlmEncryption} from "./e2ee/olm/Encryption.js";
|
2020-09-02 14:30:18 +02:00
|
|
|
import {Decryption as MegOlmDecryption} from "./e2ee/megolm/Decryption.js";
|
2020-09-17 15:58:46 +02:00
|
|
|
import {SessionBackup} from "./e2ee/megolm/SessionBackup.js";
|
2020-09-03 17:50:28 +02:00
|
|
|
import {Encryption as MegOlmEncryption} from "./e2ee/megolm/Encryption.js";
|
2020-09-03 17:51:00 +02:00
|
|
|
import {MEGOLM_ALGORITHM} from "./e2ee/common.js";
|
2020-09-03 15:36:17 +02:00
|
|
|
import {RoomEncryption} from "./e2ee/RoomEncryption.js";
|
2020-08-31 14:13:21 +02:00
|
|
|
import {DeviceTracker} from "./e2ee/DeviceTracker.js";
|
2020-09-03 12:12:33 +02:00
|
|
|
import {LockMap} from "../utils/LockMap.js";
|
2020-09-11 14:41:40 +02:00
|
|
|
import {groupBy} from "../utils/groupBy.js";
|
2020-09-17 15:58:46 +02:00
|
|
|
import {
|
|
|
|
keyFromCredential as ssssKeyFromCredential,
|
|
|
|
readKey as ssssReadKey,
|
|
|
|
writeKey as ssssWriteKey,
|
2020-09-17 17:57:12 +02:00
|
|
|
} from "./ssss/index.js";
|
|
|
|
import {SecretStorage} from "./ssss/SecretStorage.js";
|
2020-09-17 18:00:00 +02:00
|
|
|
import {ObservableValue} from "../observable/ObservableValue.js";
|
2020-09-03 12:12:33 +02:00
|
|
|
|
2020-08-27 19:12:06 +02:00
|
|
|
const PICKLE_KEY = "DEFAULT_KEY";
|
2019-02-10 21:25:29 +01:00
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
export class Session {
|
2019-03-08 20:03:18 +01:00
|
|
|
// sessionInfo contains deviceId, userId and homeServer
|
2020-09-22 13:43:18 +02:00
|
|
|
constructor({clock, storage, unwrappedHsApi, sessionInfo, olm, olmWorker, cryptoDriver, mediaRepository}) {
|
2020-09-02 14:59:17 +02:00
|
|
|
this._clock = clock;
|
2019-05-12 20:26:46 +02:00
|
|
|
this._storage = storage;
|
2020-09-22 13:49:01 +02:00
|
|
|
this._requestScheduler = new RequestScheduler({hsApi: unwrappedHsApi, clock});
|
|
|
|
this._hsApi = this._requestScheduler.createHomeServerApiWrapper();
|
2020-09-22 13:40:38 +02:00
|
|
|
this._mediaRepository = mediaRepository;
|
2020-08-27 14:36:50 +02:00
|
|
|
this._syncInfo = null;
|
2019-03-08 20:00:37 +01:00
|
|
|
this._sessionInfo = sessionInfo;
|
2019-05-12 20:26:46 +02:00
|
|
|
this._rooms = new ObservableMap();
|
2019-02-24 19:25:06 +01:00
|
|
|
this._roomUpdateCallback = (room, params) => this._rooms.update(room.id, params);
|
2019-07-29 10:23:15 +02:00
|
|
|
this._user = new User(sessionInfo.userId);
|
2020-09-02 14:59:17 +02:00
|
|
|
this._deviceMessageHandler = new DeviceMessageHandler({storage});
|
2020-08-27 13:24:55 +02:00
|
|
|
this._olm = olm;
|
2020-09-02 14:30:18 +02:00
|
|
|
this._olmUtil = null;
|
2020-08-27 19:12:06 +02:00
|
|
|
this._e2eeAccount = null;
|
2020-09-02 14:30:18 +02:00
|
|
|
this._deviceTracker = null;
|
2020-09-03 15:32:08 +02:00
|
|
|
this._olmEncryption = null;
|
2020-09-04 15:31:27 +02:00
|
|
|
this._megolmEncryption = null;
|
|
|
|
this._megolmDecryption = null;
|
2020-09-09 09:50:03 +02:00
|
|
|
this._getSyncToken = () => this.syncToken;
|
2020-09-11 10:43:17 +02:00
|
|
|
this._olmWorker = olmWorker;
|
2020-09-17 15:58:46 +02:00
|
|
|
this._cryptoDriver = cryptoDriver;
|
2020-09-04 15:31:27 +02:00
|
|
|
|
2020-09-02 14:30:18 +02:00
|
|
|
if (olm) {
|
|
|
|
this._olmUtil = new olm.Utility();
|
|
|
|
this._deviceTracker = new DeviceTracker({
|
|
|
|
storage,
|
2020-09-09 09:50:03 +02:00
|
|
|
getSyncToken: this._getSyncToken,
|
2020-09-02 14:30:18 +02:00
|
|
|
olmUtil: this._olmUtil,
|
2020-09-03 15:28:03 +02:00
|
|
|
ownUserId: sessionInfo.userId,
|
|
|
|
ownDeviceId: sessionInfo.deviceId,
|
2020-09-02 14:30:18 +02:00
|
|
|
});
|
|
|
|
}
|
2020-09-03 15:36:17 +02:00
|
|
|
this._createRoomEncryption = this._createRoomEncryption.bind(this);
|
2020-09-17 18:00:00 +02:00
|
|
|
this.needsSessionBackup = new ObservableValue(false);
|
2020-09-02 14:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// called once this._e2eeAccount is assigned
|
|
|
|
_setupEncryption() {
|
2020-09-03 15:32:33 +02:00
|
|
|
console.log("loaded e2ee account with keys", this._e2eeAccount.identityKeys);
|
2020-09-03 12:12:33 +02:00
|
|
|
const senderKeyLock = new LockMap();
|
2020-09-02 14:30:18 +02:00
|
|
|
const olmDecryption = new OlmDecryption({
|
|
|
|
account: this._e2eeAccount,
|
|
|
|
pickleKey: PICKLE_KEY,
|
2020-09-03 17:51:11 +02:00
|
|
|
olm: this._olm,
|
|
|
|
storage: this._storage,
|
2020-09-02 14:30:18 +02:00
|
|
|
now: this._clock.now,
|
|
|
|
ownUserId: this._user.id,
|
2020-09-03 12:12:33 +02:00
|
|
|
senderKeyLock
|
2020-09-02 14:30:18 +02:00
|
|
|
});
|
2020-09-03 15:32:08 +02:00
|
|
|
this._olmEncryption = new OlmEncryption({
|
|
|
|
account: this._e2eeAccount,
|
|
|
|
pickleKey: PICKLE_KEY,
|
2020-09-03 17:51:11 +02:00
|
|
|
olm: this._olm,
|
|
|
|
storage: this._storage,
|
2020-09-03 15:32:08 +02:00
|
|
|
now: this._clock.now,
|
|
|
|
ownUserId: this._user.id,
|
|
|
|
olmUtil: this._olmUtil,
|
|
|
|
senderKeyLock
|
|
|
|
});
|
2020-09-03 17:50:28 +02:00
|
|
|
this._megolmEncryption = new MegOlmEncryption({
|
|
|
|
account: this._e2eeAccount,
|
|
|
|
pickleKey: PICKLE_KEY,
|
|
|
|
olm: this._olm,
|
|
|
|
storage: this._storage,
|
|
|
|
now: this._clock.now,
|
|
|
|
ownDeviceId: this._sessionInfo.deviceId,
|
2020-09-04 15:31:27 +02:00
|
|
|
});
|
|
|
|
this._megolmDecryption = new MegOlmDecryption({
|
|
|
|
pickleKey: PICKLE_KEY,
|
|
|
|
olm: this._olm,
|
2020-09-11 10:43:17 +02:00
|
|
|
olmWorker: this._olmWorker,
|
2020-09-04 15:31:27 +02:00
|
|
|
});
|
|
|
|
this._deviceMessageHandler.enableEncryption({olmDecryption, megolmDecryption: this._megolmDecryption});
|
2020-08-27 19:12:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 17:50:28 +02:00
|
|
|
_createRoomEncryption(room, encryptionParams) {
|
2020-09-03 15:36:17 +02:00
|
|
|
// TODO: this will actually happen when users start using the e2ee version for the first time
|
|
|
|
|
|
|
|
// this should never happen because either a session was already synced once
|
|
|
|
// and thus an e2ee account was created as well and _setupEncryption is called from load
|
|
|
|
// OR
|
|
|
|
// this is a new session and loading it will load zero rooms, thus not calling this method.
|
|
|
|
// in this case _setupEncryption is called from beforeFirstSync, right after load,
|
|
|
|
// so any incoming synced rooms won't be there yet
|
|
|
|
if (!this._olmEncryption) {
|
|
|
|
throw new Error("creating room encryption before encryption got globally enabled");
|
|
|
|
}
|
2020-09-03 17:51:00 +02:00
|
|
|
// only support megolm
|
|
|
|
if (encryptionParams.algorithm !== MEGOLM_ALGORITHM) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-03 15:36:17 +02:00
|
|
|
return new RoomEncryption({
|
|
|
|
room,
|
|
|
|
deviceTracker: this._deviceTracker,
|
|
|
|
olmEncryption: this._olmEncryption,
|
2020-09-03 17:50:28 +02:00
|
|
|
megolmEncryption: this._megolmEncryption,
|
2020-09-04 15:31:27 +02:00
|
|
|
megolmDecryption: this._megolmDecryption,
|
2020-09-08 14:38:27 +02:00
|
|
|
storage: this._storage,
|
2020-09-17 15:58:46 +02:00
|
|
|
sessionBackup: this._sessionBackup,
|
|
|
|
encryptionParams,
|
2020-09-17 18:00:00 +02:00
|
|
|
notifyMissingMegolmSession: () => {
|
|
|
|
if (!this._sessionBackup) {
|
|
|
|
this.needsSessionBackup.set(true)
|
|
|
|
}
|
|
|
|
},
|
2020-09-18 13:10:41 +02:00
|
|
|
clock: this._clock
|
2020-09-03 15:36:17 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-17 15:58:46 +02:00
|
|
|
/**
|
|
|
|
* Enable secret storage by providing the secret storage credential.
|
|
|
|
* This will also see if there is a megolm session backup and try to enable that if so.
|
|
|
|
*
|
|
|
|
* @param {string} type either "passphrase" or "recoverykey"
|
|
|
|
* @param {string} credential either the passphrase or the recovery key, depending on the type
|
|
|
|
* @return {Promise} resolves or rejects after having tried to enable secret storage
|
|
|
|
*/
|
|
|
|
async enableSecretStorage(type, credential) {
|
|
|
|
if (!this._olm) {
|
|
|
|
throw new Error("olm required");
|
|
|
|
}
|
2020-09-17 18:55:39 +02:00
|
|
|
const key = await ssssKeyFromCredential(type, credential, this._storage, this._cryptoDriver, this._olm);
|
2020-09-17 17:57:12 +02:00
|
|
|
// and create session backup, which needs to read from accountData
|
2020-09-17 18:55:39 +02:00
|
|
|
const readTxn = await this._storage.readTxn([
|
2020-09-17 17:57:12 +02:00
|
|
|
this._storage.storeNames.accountData,
|
|
|
|
]);
|
2020-09-17 18:55:39 +02:00
|
|
|
await this._createSessionBackup(key, readTxn);
|
2020-09-17 17:57:12 +02:00
|
|
|
// only after having read a secret, write the key
|
|
|
|
// as we only find out if it was good if the MAC verification succeeds
|
2020-09-17 18:55:39 +02:00
|
|
|
const writeTxn = await this._storage.readWriteTxn([
|
2020-09-17 15:58:46 +02:00
|
|
|
this._storage.storeNames.session,
|
|
|
|
]);
|
|
|
|
try {
|
2020-09-17 18:55:39 +02:00
|
|
|
ssssWriteKey(key, writeTxn);
|
2020-09-17 15:58:46 +02:00
|
|
|
} catch (err) {
|
2020-09-17 18:55:39 +02:00
|
|
|
writeTxn.abort();
|
2020-09-17 15:58:46 +02:00
|
|
|
throw err;
|
|
|
|
}
|
2020-09-17 18:55:39 +02:00
|
|
|
await writeTxn.complete();
|
2020-09-17 15:58:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async _createSessionBackup(ssssKey, txn) {
|
|
|
|
const secretStorage = new SecretStorage({key: ssssKey, cryptoDriver: this._cryptoDriver});
|
|
|
|
this._sessionBackup = await SessionBackup.fromSecretStorage({olm: this._olm, secretStorage, hsApi: this._hsApi, txn});
|
|
|
|
if (this._sessionBackup) {
|
|
|
|
for (const room of this._rooms.values()) {
|
|
|
|
if (room.isEncrypted) {
|
|
|
|
room.enableSessionBackup(this._sessionBackup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 18:00:00 +02:00
|
|
|
this.needsSessionBackup.set(false);
|
2020-09-17 15:58:46 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 14:30:18 +02:00
|
|
|
// called after load
|
2020-08-27 19:12:06 +02:00
|
|
|
async beforeFirstSync(isNewLogin) {
|
|
|
|
if (this._olm) {
|
|
|
|
if (isNewLogin && this._e2eeAccount) {
|
|
|
|
throw new Error("there should not be an e2ee account already on a fresh login");
|
|
|
|
}
|
|
|
|
if (!this._e2eeAccount) {
|
2020-09-11 10:43:17 +02:00
|
|
|
this._e2eeAccount = await E2EEAccount.create({
|
|
|
|
hsApi: this._hsApi,
|
|
|
|
olm: this._olm,
|
|
|
|
pickleKey: PICKLE_KEY,
|
|
|
|
userId: this._sessionInfo.userId,
|
|
|
|
deviceId: this._sessionInfo.deviceId,
|
|
|
|
olmWorker: this._olmWorker,
|
|
|
|
storage: this._storage,
|
|
|
|
});
|
2020-09-02 14:30:18 +02:00
|
|
|
this._setupEncryption();
|
2020-08-27 19:12:06 +02:00
|
|
|
}
|
2020-08-28 13:58:17 +02:00
|
|
|
await this._e2eeAccount.generateOTKsIfNeeded(this._storage);
|
2020-08-27 19:13:24 +02:00
|
|
|
await this._e2eeAccount.uploadKeys(this._storage);
|
2020-09-04 12:09:19 +02:00
|
|
|
await this._deviceMessageHandler.decryptPending(this.rooms);
|
2020-09-17 15:58:46 +02:00
|
|
|
|
|
|
|
const txn = await this._storage.readTxn([
|
|
|
|
this._storage.storeNames.session,
|
|
|
|
this._storage.storeNames.accountData,
|
|
|
|
]);
|
|
|
|
// try set up session backup if we stored the ssss key
|
|
|
|
const ssssKey = await ssssReadKey(txn);
|
|
|
|
if (ssssKey) {
|
|
|
|
// txn will end here as this does a network request
|
|
|
|
await this._createSessionBackup(ssssKey, txn);
|
|
|
|
}
|
2020-08-27 19:12:06 +02:00
|
|
|
}
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-05-12 20:26:46 +02:00
|
|
|
async load() {
|
|
|
|
const txn = await this._storage.readTxn([
|
|
|
|
this._storage.storeNames.session,
|
|
|
|
this._storage.storeNames.roomSummary,
|
2020-08-21 18:14:07 +02:00
|
|
|
this._storage.storeNames.roomMembers,
|
2019-05-12 20:26:46 +02:00
|
|
|
this._storage.storeNames.timelineEvents,
|
2019-05-19 20:49:46 +02:00
|
|
|
this._storage.storeNames.timelineFragments,
|
2019-07-26 22:33:33 +02:00
|
|
|
this._storage.storeNames.pendingEvents,
|
2019-05-12 20:26:46 +02:00
|
|
|
]);
|
|
|
|
// restore session object
|
2020-08-27 14:36:50 +02:00
|
|
|
this._syncInfo = await txn.session.get("sync");
|
2020-08-27 19:12:06 +02:00
|
|
|
// restore e2ee account, if any
|
|
|
|
if (this._olm) {
|
|
|
|
this._e2eeAccount = await E2EEAccount.load({
|
|
|
|
hsApi: this._hsApi,
|
|
|
|
olm: this._olm,
|
|
|
|
pickleKey: PICKLE_KEY,
|
|
|
|
userId: this._sessionInfo.userId,
|
|
|
|
deviceId: this._sessionInfo.deviceId,
|
2020-09-11 10:43:17 +02:00
|
|
|
olmWorker: this._olmWorker,
|
2020-08-27 19:12:06 +02:00
|
|
|
txn
|
|
|
|
});
|
2020-09-02 14:30:18 +02:00
|
|
|
if (this._e2eeAccount) {
|
|
|
|
this._setupEncryption();
|
|
|
|
}
|
2020-08-27 19:12:06 +02:00
|
|
|
}
|
2019-07-26 22:33:33 +02:00
|
|
|
const pendingEventsByRoomId = await this._getPendingEventsByRoom(txn);
|
2019-05-12 20:26:46 +02:00
|
|
|
// load rooms
|
|
|
|
const rooms = await txn.roomSummary.getAll();
|
|
|
|
await Promise.all(rooms.map(summary => {
|
2019-07-27 10:40:56 +02:00
|
|
|
const room = this.createRoom(summary.roomId, pendingEventsByRoomId.get(summary.roomId));
|
2019-05-12 20:26:46 +02:00
|
|
|
return room.load(summary, txn);
|
|
|
|
}));
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2020-04-20 19:48:21 +02:00
|
|
|
get isStarted() {
|
2020-09-22 13:49:01 +02:00
|
|
|
return this._requestScheduler.isStarted;
|
2020-04-20 19:48:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:08:18 +02:00
|
|
|
dispose() {
|
2020-09-11 10:43:17 +02:00
|
|
|
this._olmWorker?.dispose();
|
2020-09-22 13:49:01 +02:00
|
|
|
this._requestScheduler.stop();
|
2020-09-17 15:58:46 +02:00
|
|
|
this._sessionBackup?.dispose();
|
2020-09-18 13:08:18 +02:00
|
|
|
for (const room of this._rooms.values()) {
|
|
|
|
room.dispose();
|
|
|
|
}
|
2020-04-18 19:16:16 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 19:48:21 +02:00
|
|
|
async start(lastVersionResponse) {
|
|
|
|
if (lastVersionResponse) {
|
|
|
|
// store /versions response
|
|
|
|
const txn = await this._storage.readWriteTxn([
|
|
|
|
this._storage.storeNames.session
|
|
|
|
]);
|
2020-08-27 14:28:40 +02:00
|
|
|
txn.session.set("serverVersions", lastVersionResponse);
|
2020-04-20 19:48:21 +02:00
|
|
|
// TODO: what can we do if this throws?
|
|
|
|
await txn.complete();
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:41:40 +02:00
|
|
|
const opsTxn = await this._storage.readWriteTxn([
|
|
|
|
this._storage.storeNames.operations
|
|
|
|
]);
|
|
|
|
const operations = await opsTxn.operations.getAll();
|
|
|
|
const operationsByScope = groupBy(operations, o => o.scope);
|
|
|
|
|
2020-09-22 13:49:01 +02:00
|
|
|
this._requestScheduler.start();
|
2019-07-27 10:40:56 +02:00
|
|
|
for (const [, room] of this._rooms) {
|
2020-09-11 14:41:40 +02:00
|
|
|
let roomOperationsByType;
|
|
|
|
const roomOperations = operationsByScope.get(room.id);
|
|
|
|
if (roomOperations) {
|
|
|
|
roomOperationsByType = groupBy(roomOperations, r => r.type);
|
|
|
|
}
|
|
|
|
room.start(roomOperationsByType);
|
2019-07-26 22:40:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:33:33 +02:00
|
|
|
async _getPendingEventsByRoom(txn) {
|
|
|
|
const pendingEvents = await txn.pendingEvents.getAll();
|
|
|
|
return pendingEvents.reduce((groups, pe) => {
|
|
|
|
const group = groups.get(pe.roomId);
|
|
|
|
if (group) {
|
|
|
|
group.push(pe);
|
|
|
|
} else {
|
|
|
|
groups.set(pe.roomId, [pe]);
|
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
}, new Map());
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:48:16 +01:00
|
|
|
get rooms() {
|
|
|
|
return this._rooms;
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:33:33 +02:00
|
|
|
createRoom(roomId, pendingEvents) {
|
2019-05-12 20:26:46 +02:00
|
|
|
const room = new Room({
|
2019-03-08 20:03:18 +01:00
|
|
|
roomId,
|
2020-09-09 09:50:03 +02:00
|
|
|
getSyncToken: this._getSyncToken,
|
2019-03-08 20:03:18 +01:00
|
|
|
storage: this._storage,
|
|
|
|
emitCollectionChange: this._roomUpdateCallback,
|
|
|
|
hsApi: this._hsApi,
|
2020-09-22 13:43:18 +02:00
|
|
|
mediaRepository: this._mediaRepository,
|
|
|
|
pendingEvents,
|
2019-07-29 10:23:15 +02:00
|
|
|
user: this._user,
|
2020-09-11 11:43:40 +02:00
|
|
|
createRoomEncryption: this._createRoomEncryption,
|
|
|
|
clock: this._clock
|
2019-03-08 20:03:18 +01:00
|
|
|
});
|
2019-05-12 20:26:46 +02:00
|
|
|
this._rooms.add(roomId, room);
|
|
|
|
return room;
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2020-09-10 12:11:43 +02:00
|
|
|
async writeSync(syncResponse, syncFilterId, txn) {
|
2020-08-28 13:56:44 +02:00
|
|
|
const changes = {};
|
|
|
|
const syncToken = syncResponse.next_batch;
|
|
|
|
const deviceOneTimeKeysCount = syncResponse.device_one_time_keys_count;
|
|
|
|
|
|
|
|
if (this._e2eeAccount && deviceOneTimeKeysCount) {
|
|
|
|
changes.e2eeAccountChanges = this._e2eeAccount.writeSync(deviceOneTimeKeysCount, txn);
|
|
|
|
}
|
2020-08-27 14:36:50 +02:00
|
|
|
if (syncToken !== this.syncToken) {
|
|
|
|
const syncInfo = {token: syncToken, filterId: syncFilterId};
|
2020-08-27 14:28:40 +02:00
|
|
|
// don't modify `this` because transaction might still fail
|
2020-08-27 14:36:50 +02:00
|
|
|
txn.session.set("sync", syncInfo);
|
2020-08-28 13:56:44 +02:00
|
|
|
changes.syncInfo = syncInfo;
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
2020-08-31 14:13:21 +02:00
|
|
|
if (this._deviceTracker) {
|
2020-08-31 16:08:47 +02:00
|
|
|
const deviceLists = syncResponse.device_lists;
|
|
|
|
if (deviceLists) {
|
|
|
|
await this._deviceTracker.writeDeviceChanges(deviceLists, txn);
|
|
|
|
}
|
2020-08-31 14:13:21 +02:00
|
|
|
}
|
2020-09-02 14:30:18 +02:00
|
|
|
|
|
|
|
const toDeviceEvents = syncResponse.to_device?.events;
|
|
|
|
if (Array.isArray(toDeviceEvents)) {
|
|
|
|
this._deviceMessageHandler.writeSync(toDeviceEvents, txn);
|
|
|
|
}
|
2020-09-17 10:39:51 +02:00
|
|
|
|
|
|
|
// store account data
|
|
|
|
const accountData = syncResponse["account_data"];
|
|
|
|
if (Array.isArray(accountData?.events)) {
|
|
|
|
for (const event of accountData.events) {
|
|
|
|
if (typeof event.type === "string") {
|
|
|
|
txn.accountData.set(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 13:56:44 +02:00
|
|
|
return changes;
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
|
|
|
|
2020-08-28 13:56:44 +02:00
|
|
|
afterSync({syncInfo, e2eeAccountChanges}) {
|
2020-08-27 14:36:50 +02:00
|
|
|
if (syncInfo) {
|
2020-03-14 20:45:36 +01:00
|
|
|
// sync transaction succeeded, modify object state now
|
2020-08-27 14:36:50 +02:00
|
|
|
this._syncInfo = syncInfo;
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2020-09-21 17:56:23 +02:00
|
|
|
if (this._e2eeAccount) {
|
2020-08-28 13:56:44 +02:00
|
|
|
this._e2eeAccount.afterSync(e2eeAccountChanges);
|
|
|
|
}
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
|
2020-09-21 17:57:01 +02:00
|
|
|
async afterSyncCompleted(isCatchupSync) {
|
2020-09-04 12:09:19 +02:00
|
|
|
const promises = [this._deviceMessageHandler.decryptPending(this.rooms)];
|
2020-09-21 17:57:01 +02:00
|
|
|
// we don't start uploading one-time keys until we've caught up with
|
|
|
|
// to-device messages, to help us avoid throwing away one-time-keys that we
|
|
|
|
// are about to receive messages for
|
|
|
|
// (https://github.com/vector-im/riot-web/issues/2782).
|
|
|
|
if (!isCatchupSync) {
|
|
|
|
const needsToUploadOTKs = await this._e2eeAccount.generateOTKsIfNeeded(this._storage);
|
|
|
|
if (needsToUploadOTKs) {
|
|
|
|
promises.push(this._e2eeAccount.uploadKeys(this._storage));
|
|
|
|
}
|
2020-08-28 13:58:17 +02:00
|
|
|
}
|
2020-09-02 14:30:18 +02:00
|
|
|
// run key upload and decryption in parallel
|
|
|
|
await Promise.all(promises);
|
2020-08-28 13:58:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 20:26:46 +02:00
|
|
|
get syncToken() {
|
2020-08-27 14:36:50 +02:00
|
|
|
return this._syncInfo?.token;
|
2019-05-12 20:26:46 +02:00
|
|
|
}
|
2019-06-16 10:53:23 +02:00
|
|
|
|
2019-10-12 20:24:09 +02:00
|
|
|
get syncFilterId() {
|
2020-08-27 14:36:50 +02:00
|
|
|
return this._syncInfo?.filterId;
|
2019-10-12 20:24:09 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 10:23:15 +02:00
|
|
|
get user() {
|
|
|
|
return this._user;
|
2019-06-16 10:53:23 +02:00
|
|
|
}
|
2019-02-20 23:48:16 +01:00
|
|
|
}
|
2020-03-14 20:45:36 +01:00
|
|
|
|
|
|
|
export function tests() {
|
2020-03-14 21:38:37 +01:00
|
|
|
function createStorageMock(session, pendingEvents = []) {
|
2020-03-14 20:45:36 +01:00
|
|
|
return {
|
|
|
|
readTxn() {
|
|
|
|
return Promise.resolve({
|
|
|
|
session: {
|
2020-08-27 14:28:40 +02:00
|
|
|
get(key) {
|
|
|
|
return Promise.resolve(session[key]);
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
pendingEvents: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve(pendingEvents);
|
|
|
|
}
|
2020-03-14 21:38:37 +01:00
|
|
|
},
|
|
|
|
roomSummary: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
|
|
|
});
|
2020-03-14 21:38:37 +01:00
|
|
|
},
|
|
|
|
storeNames: {}
|
2020-03-14 20:45:36 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"session data is not modified until after sync": async (assert) => {
|
|
|
|
const session = new Session({storage: createStorageMock({
|
2020-08-27 14:36:50 +02:00
|
|
|
sync: {token: "a", filterId: 5}
|
2020-03-14 21:38:37 +01:00
|
|
|
}), sessionInfo: {userId: ""}});
|
2020-03-14 20:45:36 +01:00
|
|
|
await session.load();
|
2020-08-27 14:36:50 +02:00
|
|
|
let syncSet = false;
|
2020-03-14 20:45:36 +01:00
|
|
|
const syncTxn = {
|
|
|
|
session: {
|
2020-08-27 14:28:40 +02:00
|
|
|
set(key, value) {
|
2020-08-27 14:36:50 +02:00
|
|
|
if (key === "sync") {
|
|
|
|
assert.equal(value.token, "b");
|
|
|
|
assert.equal(value.filterId, 6);
|
|
|
|
syncSet = true;
|
2020-08-27 14:28:40 +02:00
|
|
|
}
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-09-10 12:11:43 +02:00
|
|
|
const newSessionData = await session.writeSync({next_batch: "b"}, 6, syncTxn);
|
2020-08-27 14:36:50 +02:00
|
|
|
assert(syncSet);
|
2020-03-14 21:38:37 +01:00
|
|
|
assert.equal(session.syncToken, "a");
|
|
|
|
assert.equal(session.syncFilterId, 5);
|
2020-03-14 20:45:36 +01:00
|
|
|
session.afterSync(newSessionData);
|
2020-03-14 21:38:37 +01:00
|
|
|
assert.equal(session.syncToken, "b");
|
|
|
|
assert.equal(session.syncFilterId, 6);
|
2020-03-14 20:45:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|