2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2021-04-20 21:09:46 +05:30
|
|
|
Copyright 2020, 2021 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-21 01:11:10 +05:30
|
|
|
import {Room} from "./room/Room.js";
|
2021-05-10 22:12:30 +05:30
|
|
|
import {ArchivedRoom} from "./room/ArchivedRoom.js";
|
2022-02-03 22:27:35 +05:30
|
|
|
import {RoomStatus} from "./room/RoomStatus";
|
2022-02-02 14:49:49 +05:30
|
|
|
import {RoomBeingCreated} from "./room/create";
|
2021-04-20 19:41:21 +05:30
|
|
|
import {Invite} from "./room/Invite.js";
|
2021-11-25 13:23:05 +05:30
|
|
|
import {Pusher} from "./push/Pusher";
|
2019-02-27 01:19:45 +05:30
|
|
|
import { ObservableMap } from "../observable/index.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {User} from "./User.js";
|
2020-09-02 18:00:18 +05:30
|
|
|
import {DeviceMessageHandler} from "./DeviceMessageHandler.js";
|
2020-09-03 21:21:11 +05:30
|
|
|
import {Account as E2EEAccount} from "./e2ee/Account.js";
|
2021-10-27 13:56:36 +05:30
|
|
|
import {uploadAccountAsDehydratedDevice} from "./e2ee/Dehydration.js";
|
2020-09-02 18:00:18 +05:30
|
|
|
import {Decryption as OlmDecryption} from "./e2ee/olm/Decryption.js";
|
2020-09-03 19:02:08 +05:30
|
|
|
import {Encryption as OlmEncryption} from "./e2ee/olm/Encryption.js";
|
2021-10-22 21:18:53 +05:30
|
|
|
import {Decryption as MegOlmDecryption} from "./e2ee/megolm/Decryption";
|
2021-10-22 21:31:17 +05:30
|
|
|
import {KeyLoader as MegOlmKeyLoader} from "./e2ee/megolm/decryption/KeyLoader";
|
2022-01-26 14:21:48 +05:30
|
|
|
import {KeyBackup} from "./e2ee/megolm/keybackup/KeyBackup";
|
2020-09-03 21:20:28 +05:30
|
|
|
import {Encryption as MegOlmEncryption} from "./e2ee/megolm/Encryption.js";
|
2020-09-03 21:21:00 +05:30
|
|
|
import {MEGOLM_ALGORITHM} from "./e2ee/common.js";
|
2020-09-03 19:06:17 +05:30
|
|
|
import {RoomEncryption} from "./e2ee/RoomEncryption.js";
|
2020-08-31 17:43:21 +05:30
|
|
|
import {DeviceTracker} from "./e2ee/DeviceTracker.js";
|
2021-11-16 15:26:40 +05:30
|
|
|
import {LockMap} from "../utils/LockMap";
|
2021-10-21 18:10:51 +05:30
|
|
|
import {groupBy} from "../utils/groupBy";
|
2020-09-17 19:28:46 +05:30
|
|
|
import {
|
|
|
|
keyFromCredential as ssssKeyFromCredential,
|
|
|
|
readKey as ssssReadKey,
|
|
|
|
writeKey as ssssWriteKey,
|
2021-10-29 22:47:31 +05:30
|
|
|
removeKey as ssssRemoveKey,
|
|
|
|
keyFromDehydratedDeviceKey as createSSSSKeyFromDehydratedDeviceKey
|
2021-11-29 13:54:44 +05:30
|
|
|
} from "./ssss/index";
|
2021-11-29 17:05:15 +05:30
|
|
|
import {SecretStorage} from "./ssss/SecretStorage";
|
2021-09-30 06:00:21 +05:30
|
|
|
import {ObservableValue, RetainedObservableValue} from "../observable/ObservableValue";
|
2020-09-03 15:42:33 +05:30
|
|
|
|
2020-08-27 22:42:06 +05:30
|
|
|
const PICKLE_KEY = "DEFAULT_KEY";
|
2021-03-19 01:14:16 +05:30
|
|
|
const PUSHER_KEY = "pusher";
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class Session {
|
2021-08-23 22:56:39 +05:30
|
|
|
// sessionInfo contains deviceId, userId and homeserver
|
2020-10-26 20:14:11 +05:30
|
|
|
constructor({storage, hsApi, sessionInfo, olm, olmWorker, platform, mediaRepository}) {
|
|
|
|
this._platform = platform;
|
2019-05-12 23:56:46 +05:30
|
|
|
this._storage = storage;
|
2020-09-22 20:09:41 +05:30
|
|
|
this._hsApi = hsApi;
|
2020-09-22 17:10:38 +05:30
|
|
|
this._mediaRepository = mediaRepository;
|
2020-08-27 18:06:50 +05:30
|
|
|
this._syncInfo = null;
|
2019-03-09 00:30:37 +05:30
|
|
|
this._sessionInfo = sessionInfo;
|
2019-05-12 23:56:46 +05:30
|
|
|
this._rooms = new ObservableMap();
|
2019-02-24 23:55:06 +05:30
|
|
|
this._roomUpdateCallback = (room, params) => this._rooms.update(room.id, params);
|
2021-05-11 19:39:58 +05:30
|
|
|
this._activeArchivedRooms = new Map();
|
2021-04-20 21:09:46 +05:30
|
|
|
this._invites = new ObservableMap();
|
2021-04-20 22:31:40 +05:30
|
|
|
this._inviteUpdateCallback = (invite, params) => this._invites.update(invite.id, params);
|
2022-02-03 22:27:35 +05:30
|
|
|
this._roomsBeingCreatedUpdateCallback = (rbc, params) => {
|
|
|
|
this._roomsBeingCreated.update(rbc.localId, params);
|
|
|
|
if (rbc.roomId && !!this.rooms.get(rbc.roomId)) {
|
|
|
|
this._tryReplaceRoomBeingCreated(rbc.roomId);
|
|
|
|
}
|
|
|
|
};
|
2022-02-02 14:49:49 +05:30
|
|
|
this._roomsBeingCreated = new ObservableMap();
|
2019-07-29 13:53:15 +05:30
|
|
|
this._user = new User(sessionInfo.userId);
|
2020-09-02 18:29:17 +05:30
|
|
|
this._deviceMessageHandler = new DeviceMessageHandler({storage});
|
2020-08-27 16:54:55 +05:30
|
|
|
this._olm = olm;
|
2020-09-02 18:00:18 +05:30
|
|
|
this._olmUtil = null;
|
2020-08-27 22:42:06 +05:30
|
|
|
this._e2eeAccount = null;
|
2020-09-02 18:00:18 +05:30
|
|
|
this._deviceTracker = null;
|
2020-09-03 19:02:08 +05:30
|
|
|
this._olmEncryption = null;
|
2022-01-20 15:46:08 +05:30
|
|
|
this._keyLoader = null;
|
2020-09-04 19:01:27 +05:30
|
|
|
this._megolmEncryption = null;
|
|
|
|
this._megolmDecryption = null;
|
2020-09-09 13:20:03 +05:30
|
|
|
this._getSyncToken = () => this.syncToken;
|
2020-09-11 14:13:17 +05:30
|
|
|
this._olmWorker = olmWorker;
|
2022-01-28 19:43:58 +05:30
|
|
|
this._keyBackup = new ObservableValue(undefined);
|
2021-05-07 16:39:38 +05:30
|
|
|
this._observedRoomStatus = new Map();
|
2020-09-04 19:01:27 +05:30
|
|
|
|
2020-09-02 18:00:18 +05:30
|
|
|
if (olm) {
|
|
|
|
this._olmUtil = new olm.Utility();
|
|
|
|
this._deviceTracker = new DeviceTracker({
|
|
|
|
storage,
|
2020-09-09 13:20:03 +05:30
|
|
|
getSyncToken: this._getSyncToken,
|
2020-09-02 18:00:18 +05:30
|
|
|
olmUtil: this._olmUtil,
|
2020-09-03 18:58:03 +05:30
|
|
|
ownUserId: sessionInfo.userId,
|
|
|
|
ownDeviceId: sessionInfo.deviceId,
|
2020-09-02 18:00:18 +05:30
|
|
|
});
|
|
|
|
}
|
2020-09-03 19:06:17 +05:30
|
|
|
this._createRoomEncryption = this._createRoomEncryption.bind(this);
|
2021-05-12 19:08:54 +05:30
|
|
|
this._forgetArchivedRoom = this._forgetArchivedRoom.bind(this);
|
2022-01-26 14:21:48 +05:30
|
|
|
this.needsKeyBackup = new ObservableValue(false);
|
2020-09-02 18:00:18 +05:30
|
|
|
}
|
|
|
|
|
2020-10-16 21:36:20 +05:30
|
|
|
get fingerprintKey() {
|
|
|
|
return this._e2eeAccount?.identityKeys.ed25519;
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:27:47 +05:30
|
|
|
get hasSecretStorageKey() {
|
|
|
|
return this._hasSecretStorageKey;
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:36:20 +05:30
|
|
|
get deviceId() {
|
|
|
|
return this._sessionInfo.deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId() {
|
|
|
|
return this._sessionInfo.userId;
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:00:18 +05:30
|
|
|
// called once this._e2eeAccount is assigned
|
|
|
|
_setupEncryption() {
|
2020-10-23 16:29:40 +05:30
|
|
|
// TODO: this should all go in a wrapper in e2ee/ that is bootstrapped by passing in the account
|
|
|
|
// and can create RoomEncryption objects and handle encrypted to_device messages and device list changes.
|
2020-09-03 15:42:33 +05:30
|
|
|
const senderKeyLock = new LockMap();
|
2020-09-02 18:00:18 +05:30
|
|
|
const olmDecryption = new OlmDecryption({
|
|
|
|
account: this._e2eeAccount,
|
|
|
|
pickleKey: PICKLE_KEY,
|
2020-09-03 21:21:11 +05:30
|
|
|
olm: this._olm,
|
|
|
|
storage: this._storage,
|
2020-10-26 20:14:11 +05:30
|
|
|
now: this._platform.clock.now,
|
2020-09-02 18:00:18 +05:30
|
|
|
ownUserId: this._user.id,
|
2020-09-03 15:42:33 +05:30
|
|
|
senderKeyLock
|
2020-09-02 18:00:18 +05:30
|
|
|
});
|
2020-09-03 19:02:08 +05:30
|
|
|
this._olmEncryption = new OlmEncryption({
|
|
|
|
account: this._e2eeAccount,
|
|
|
|
pickleKey: PICKLE_KEY,
|
2020-09-03 21:21:11 +05:30
|
|
|
olm: this._olm,
|
|
|
|
storage: this._storage,
|
2020-10-26 20:14:11 +05:30
|
|
|
now: this._platform.clock.now,
|
2020-09-03 19:02:08 +05:30
|
|
|
ownUserId: this._user.id,
|
|
|
|
olmUtil: this._olmUtil,
|
|
|
|
senderKeyLock
|
|
|
|
});
|
2022-01-28 17:43:23 +05:30
|
|
|
this._keyLoader = new MegOlmKeyLoader(this._olm, PICKLE_KEY, 20);
|
2020-09-03 21:20:28 +05:30
|
|
|
this._megolmEncryption = new MegOlmEncryption({
|
|
|
|
account: this._e2eeAccount,
|
|
|
|
pickleKey: PICKLE_KEY,
|
|
|
|
olm: this._olm,
|
|
|
|
storage: this._storage,
|
2022-01-28 17:43:23 +05:30
|
|
|
keyLoader: this._keyLoader,
|
2020-10-26 20:14:11 +05:30
|
|
|
now: this._platform.clock.now,
|
2020-09-03 21:20:28 +05:30
|
|
|
ownDeviceId: this._sessionInfo.deviceId,
|
2020-09-04 19:01:27 +05:30
|
|
|
});
|
2022-01-20 15:46:08 +05:30
|
|
|
this._megolmDecryption = new MegOlmDecryption(this._keyLoader, this._olmWorker);
|
2020-09-04 19:01:27 +05:30
|
|
|
this._deviceMessageHandler.enableEncryption({olmDecryption, megolmDecryption: this._megolmDecryption});
|
2020-08-27 22:42:06 +05:30
|
|
|
}
|
|
|
|
|
2020-09-03 21:20:28 +05:30
|
|
|
_createRoomEncryption(room, encryptionParams) {
|
2020-09-03 19:06:17 +05:30
|
|
|
// 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 21:21:00 +05:30
|
|
|
// only support megolm
|
|
|
|
if (encryptionParams.algorithm !== MEGOLM_ALGORITHM) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-03 19:06:17 +05:30
|
|
|
return new RoomEncryption({
|
|
|
|
room,
|
|
|
|
deviceTracker: this._deviceTracker,
|
|
|
|
olmEncryption: this._olmEncryption,
|
2020-09-03 21:20:28 +05:30
|
|
|
megolmEncryption: this._megolmEncryption,
|
2020-09-04 19:01:27 +05:30
|
|
|
megolmDecryption: this._megolmDecryption,
|
2020-09-08 18:08:27 +05:30
|
|
|
storage: this._storage,
|
2022-01-28 19:43:58 +05:30
|
|
|
keyBackup: this._keyBackup?.get(),
|
2020-09-17 19:28:46 +05:30
|
|
|
encryptionParams,
|
2020-09-17 21:30:00 +05:30
|
|
|
notifyMissingMegolmSession: () => {
|
2022-01-28 19:43:58 +05:30
|
|
|
if (!this._keyBackup.get()) {
|
2022-01-26 14:21:48 +05:30
|
|
|
this.needsKeyBackup.set(true)
|
2020-09-17 21:30:00 +05:30
|
|
|
}
|
|
|
|
},
|
2020-10-26 20:14:11 +05:30
|
|
|
clock: this._platform.clock
|
2020-09-03 19:06:17 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:28:46 +05:30
|
|
|
/**
|
|
|
|
* Enable secret storage by providing the secret storage credential.
|
2022-01-26 14:21:48 +05:30
|
|
|
* This will also see if there is a megolm key backup and try to enable that if so.
|
2020-09-17 19:28:46 +05:30
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2022-01-27 18:49:37 +05:30
|
|
|
enableSecretStorage(type, credential, log = undefined) {
|
|
|
|
return this._platform.logger.wrapOrRun(log, "enable secret storage", async log => {
|
|
|
|
if (!this._olm) {
|
|
|
|
throw new Error("olm required");
|
|
|
|
}
|
2022-01-28 19:43:58 +05:30
|
|
|
if (this._keyBackup.get()) {
|
2022-02-01 15:56:00 +05:30
|
|
|
this._keyBackup.get().dispose();
|
2022-02-01 16:02:53 +05:30
|
|
|
this._keyBackup.set(null);
|
2022-01-27 18:49:37 +05:30
|
|
|
}
|
|
|
|
const key = await ssssKeyFromCredential(type, credential, this._storage, this._platform, this._olm);
|
|
|
|
// and create key backup, which needs to read from accountData
|
|
|
|
const readTxn = await this._storage.readTxn([
|
|
|
|
this._storage.storeNames.accountData,
|
|
|
|
]);
|
|
|
|
if (await this._createKeyBackup(key, readTxn, log)) {
|
2022-01-31 19:06:04 +05:30
|
|
|
// only after having read a secret, write the key
|
|
|
|
// as we only find out if it was good if the MAC verification succeeds
|
|
|
|
await this._writeSSSSKey(key, log);
|
2022-01-28 21:06:42 +05:30
|
|
|
this._keyBackup.get().flush(log);
|
2022-01-27 18:49:37 +05:30
|
|
|
return key;
|
2022-01-31 20:56:14 +05:30
|
|
|
} else {
|
|
|
|
throw new Error("Could not read key backup with the given key");
|
2022-01-27 18:49:37 +05:30
|
|
|
}
|
|
|
|
});
|
2021-10-29 22:47:31 +05:30
|
|
|
}
|
|
|
|
|
2022-01-31 19:06:04 +05:30
|
|
|
async _writeSSSSKey(key, log) {
|
|
|
|
// we're going to write the 4S key, and also the backup version.
|
|
|
|
// this way, we can detect when we enter a key for a new backup version
|
|
|
|
// and mark all inbound sessions to be backed up again
|
2022-01-31 20:56:14 +05:30
|
|
|
const keyBackup = this._keyBackup.get();
|
|
|
|
if (!keyBackup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const backupVersion = keyBackup.version;
|
2021-03-05 00:17:02 +05:30
|
|
|
const writeTxn = await this._storage.readWriteTxn([
|
2020-09-17 19:28:46 +05:30
|
|
|
this._storage.storeNames.session,
|
2022-01-31 19:06:04 +05:30
|
|
|
this._storage.storeNames.inboundGroupSessions,
|
2020-09-17 19:28:46 +05:30
|
|
|
]);
|
|
|
|
try {
|
2022-01-31 19:06:04 +05:30
|
|
|
const previousBackupVersion = await ssssWriteKey(key, backupVersion, writeTxn);
|
|
|
|
log.set("previousBackupVersion", previousBackupVersion);
|
|
|
|
log.set("backupVersion", backupVersion);
|
2022-01-31 20:56:14 +05:30
|
|
|
if (!!previousBackupVersion && previousBackupVersion !== backupVersion) {
|
|
|
|
const amountMarked = await keyBackup.markAllForBackup(writeTxn);
|
2022-01-31 19:06:04 +05:30
|
|
|
log.set("amountMarkedForBackup", amountMarked);
|
|
|
|
}
|
2020-09-17 19:28:46 +05:30
|
|
|
} catch (err) {
|
2020-09-17 22:25:39 +05:30
|
|
|
writeTxn.abort();
|
2020-09-17 19:28:46 +05:30
|
|
|
throw err;
|
|
|
|
}
|
2020-09-17 22:25:39 +05:30
|
|
|
await writeTxn.complete();
|
2021-10-29 19:18:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async disableSecretStorage() {
|
|
|
|
const writeTxn = await this._storage.readWriteTxn([
|
|
|
|
this._storage.storeNames.session,
|
|
|
|
]);
|
|
|
|
try {
|
|
|
|
ssssRemoveKey(writeTxn);
|
|
|
|
} catch (err) {
|
|
|
|
writeTxn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await writeTxn.complete();
|
2022-01-28 19:43:58 +05:30
|
|
|
if (this._keyBackup.get()) {
|
2021-10-29 19:18:28 +05:30
|
|
|
for (const room of this._rooms.values()) {
|
|
|
|
if (room.isEncrypted) {
|
2022-01-26 14:21:48 +05:30
|
|
|
room.enableKeyBackup(undefined);
|
2021-10-29 19:18:28 +05:30
|
|
|
}
|
|
|
|
}
|
2022-01-28 19:43:58 +05:30
|
|
|
this._keyBackup.get().dispose();
|
2022-02-01 15:57:42 +05:30
|
|
|
this._keyBackup.set(null);
|
2021-10-29 19:18:28 +05:30
|
|
|
}
|
2020-09-17 19:28:46 +05:30
|
|
|
}
|
|
|
|
|
2022-01-27 18:49:37 +05:30
|
|
|
_createKeyBackup(ssssKey, txn, log) {
|
|
|
|
return log.wrap("enable key backup", async log => {
|
|
|
|
try {
|
|
|
|
const secretStorage = new SecretStorage({key: ssssKey, platform: this._platform});
|
2022-01-28 19:43:58 +05:30
|
|
|
const keyBackup = await KeyBackup.fromSecretStorage(
|
2022-01-27 18:49:37 +05:30
|
|
|
this._platform,
|
|
|
|
this._olm,
|
|
|
|
secretStorage,
|
|
|
|
this._hsApi,
|
|
|
|
this._keyLoader,
|
|
|
|
this._storage,
|
|
|
|
txn
|
|
|
|
);
|
2022-01-28 19:43:58 +05:30
|
|
|
if (keyBackup) {
|
2022-01-27 18:49:37 +05:30
|
|
|
for (const room of this._rooms.values()) {
|
|
|
|
if (room.isEncrypted) {
|
2022-01-28 19:43:58 +05:30
|
|
|
room.enableKeyBackup(keyBackup);
|
2022-01-27 18:49:37 +05:30
|
|
|
}
|
|
|
|
}
|
2022-01-28 19:43:58 +05:30
|
|
|
this._keyBackup.set(keyBackup);
|
|
|
|
return true;
|
2020-09-17 19:28:46 +05:30
|
|
|
}
|
2022-01-27 18:49:37 +05:30
|
|
|
} catch (err) {
|
|
|
|
log.catch(err);
|
2020-09-17 19:28:46 +05:30
|
|
|
}
|
2022-02-01 15:38:13 +05:30
|
|
|
return false;
|
2022-01-27 18:49:37 +05:30
|
|
|
});
|
2020-09-17 19:28:46 +05:30
|
|
|
}
|
|
|
|
|
2022-02-01 15:38:13 +05:30
|
|
|
/**
|
|
|
|
* @type {ObservableValue<KeyBackup | undefined | null}
|
|
|
|
* - `undefined` means, we're not done with catchup sync yet and haven't checked yet if key backup is configured
|
|
|
|
* - `null` means we've checked and key backup hasn't been configured correctly or at all.
|
|
|
|
*/
|
2022-01-26 14:21:48 +05:30
|
|
|
get keyBackup() {
|
|
|
|
return this._keyBackup;
|
2020-10-19 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
2021-09-29 23:37:42 +05:30
|
|
|
get hasIdentity() {
|
|
|
|
return !!this._e2eeAccount;
|
|
|
|
}
|
|
|
|
|
2020-10-23 15:52:52 +05:30
|
|
|
/** @internal */
|
2021-02-23 23:52:25 +05:30
|
|
|
async createIdentity(log) {
|
2020-08-27 22:42:06 +05:30
|
|
|
if (this._olm) {
|
|
|
|
if (!this._e2eeAccount) {
|
2021-10-27 18:38:53 +05:30
|
|
|
this._e2eeAccount = await this._createNewAccount(this._sessionInfo.deviceId, this._storage);
|
2021-02-23 23:52:25 +05:30
|
|
|
log.set("keys", this._e2eeAccount.identityKeys);
|
2020-09-02 18:00:18 +05:30
|
|
|
this._setupEncryption();
|
2020-08-27 22:42:06 +05:30
|
|
|
}
|
2021-02-23 23:52:25 +05:30
|
|
|
await this._e2eeAccount.generateOTKsIfNeeded(this._storage, log);
|
2021-10-27 21:38:50 +05:30
|
|
|
await log.wrap("uploadKeys", log => this._e2eeAccount.uploadKeys(this._storage, false, log));
|
2020-08-27 22:42:06 +05:30
|
|
|
}
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2021-10-26 22:17:46 +05:30
|
|
|
/** @internal */
|
2021-10-27 18:38:53 +05:30
|
|
|
async dehydrateIdentity(dehydratedDevice, log) {
|
|
|
|
log.set("deviceId", dehydratedDevice.deviceId);
|
|
|
|
if (!this._olm) {
|
|
|
|
log.set("no_olm", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (dehydratedDevice.deviceId !== this.deviceId) {
|
|
|
|
log.set("wrong_device", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this._e2eeAccount) {
|
|
|
|
log.set("account_already_setup", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!await dehydratedDevice.claim(this._hsApi, log)) {
|
|
|
|
log.set("already_claimed", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this._e2eeAccount = await E2EEAccount.adoptDehydratedDevice({
|
|
|
|
dehydratedDevice,
|
|
|
|
hsApi: this._hsApi,
|
|
|
|
olm: this._olm,
|
|
|
|
pickleKey: PICKLE_KEY,
|
|
|
|
userId: this._sessionInfo.userId,
|
|
|
|
olmWorker: this._olmWorker,
|
|
|
|
deviceId: this.deviceId,
|
|
|
|
storage: this._storage,
|
2021-10-26 22:17:46 +05:30
|
|
|
});
|
2021-10-27 18:38:53 +05:30
|
|
|
log.set("keys", this._e2eeAccount.identityKeys);
|
|
|
|
this._setupEncryption();
|
|
|
|
return true;
|
2021-10-26 22:17:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
_createNewAccount(deviceId, storage = undefined) {
|
|
|
|
// storage is optional and if omitted the account won't be persisted (useful for dehydrating devices)
|
|
|
|
return E2EEAccount.create({
|
|
|
|
hsApi: this._hsApi,
|
|
|
|
olm: this._olm,
|
|
|
|
pickleKey: PICKLE_KEY,
|
|
|
|
userId: this._sessionInfo.userId,
|
|
|
|
olmWorker: this._olmWorker,
|
|
|
|
deviceId,
|
|
|
|
storage,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setupDehydratedDevice(key, log = null) {
|
|
|
|
return this._platform.logger.wrapOrRun(log, "setupDehydratedDevice", async log => {
|
|
|
|
const dehydrationAccount = await this._createNewAccount("temp-device-id");
|
|
|
|
try {
|
|
|
|
const deviceId = await uploadAccountAsDehydratedDevice(
|
|
|
|
dehydrationAccount, this._hsApi, key, "Dehydrated device", log);
|
2021-10-29 19:18:28 +05:30
|
|
|
log.set("deviceId", deviceId);
|
2021-10-26 22:17:46 +05:30
|
|
|
return deviceId;
|
|
|
|
} finally {
|
|
|
|
dehydrationAccount.dispose();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:29:40 +05:30
|
|
|
/** @internal */
|
2021-02-23 23:52:25 +05:30
|
|
|
async load(log) {
|
2021-03-05 00:17:02 +05:30
|
|
|
const txn = await this._storage.readTxn([
|
2019-05-12 23:56:46 +05:30
|
|
|
this._storage.storeNames.session,
|
|
|
|
this._storage.storeNames.roomSummary,
|
2021-04-20 21:39:48 +05:30
|
|
|
this._storage.storeNames.invites,
|
2020-08-21 21:44:07 +05:30
|
|
|
this._storage.storeNames.roomMembers,
|
2019-05-12 23:56:46 +05:30
|
|
|
this._storage.storeNames.timelineEvents,
|
2019-05-20 00:19:46 +05:30
|
|
|
this._storage.storeNames.timelineFragments,
|
2019-07-27 02:03:33 +05:30
|
|
|
this._storage.storeNames.pendingEvents,
|
2019-05-12 23:56:46 +05:30
|
|
|
]);
|
|
|
|
// restore session object
|
2020-08-27 18:06:50 +05:30
|
|
|
this._syncInfo = await txn.session.get("sync");
|
2020-08-27 22:42:06 +05:30
|
|
|
// 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 14:13:17 +05:30
|
|
|
olmWorker: this._olmWorker,
|
2020-08-27 22:42:06 +05:30
|
|
|
txn
|
|
|
|
});
|
2020-09-02 18:00:18 +05:30
|
|
|
if (this._e2eeAccount) {
|
2021-02-23 23:52:25 +05:30
|
|
|
log.set("keys", this._e2eeAccount.identityKeys);
|
2020-09-02 18:00:18 +05:30
|
|
|
this._setupEncryption();
|
|
|
|
}
|
2020-08-27 22:42:06 +05:30
|
|
|
}
|
2019-07-27 02:03:33 +05:30
|
|
|
const pendingEventsByRoomId = await this._getPendingEventsByRoom(txn);
|
2021-04-20 21:39:48 +05:30
|
|
|
// load invites
|
|
|
|
const invites = await txn.invites.getAll();
|
2021-04-23 21:35:14 +05:30
|
|
|
const inviteLoadPromise = Promise.all(invites.map(async inviteData => {
|
2021-04-20 21:39:48 +05:30
|
|
|
const invite = this.createInvite(inviteData.roomId);
|
|
|
|
log.wrap("invite", log => invite.load(inviteData, log));
|
|
|
|
this._invites.add(invite.id, invite);
|
|
|
|
}));
|
2021-04-23 21:35:14 +05:30
|
|
|
// load rooms
|
|
|
|
const rooms = await txn.roomSummary.getAll();
|
|
|
|
const roomLoadPromise = Promise.all(rooms.map(async summary => {
|
2022-02-02 14:49:49 +05:30
|
|
|
const room = this.createJoinedRoom(summary.roomId, pendingEventsByRoomId.get(summary.roomId));
|
2021-04-23 21:35:14 +05:30
|
|
|
await log.wrap("room", log => room.load(summary, txn, log));
|
|
|
|
this._rooms.add(room.id, room);
|
|
|
|
}));
|
|
|
|
// load invites and rooms in parallel
|
|
|
|
await Promise.all([inviteLoadPromise, roomLoadPromise]);
|
|
|
|
for (const [roomId, invite] of this.invites) {
|
|
|
|
const room = this.rooms.get(roomId);
|
|
|
|
if (room) {
|
|
|
|
room.setInvite(invite);
|
|
|
|
}
|
|
|
|
}
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2020-09-18 16:38:18 +05:30
|
|
|
dispose() {
|
2020-09-11 14:13:17 +05:30
|
|
|
this._olmWorker?.dispose();
|
2021-10-28 15:18:25 +05:30
|
|
|
this._olmWorker = undefined;
|
2022-01-28 19:43:58 +05:30
|
|
|
this._keyBackup.get()?.dispose();
|
|
|
|
this._keyBackup.set(undefined);
|
2021-10-28 15:34:42 +05:30
|
|
|
this._megolmDecryption?.dispose();
|
2021-10-28 15:18:25 +05:30
|
|
|
this._megolmDecryption = undefined;
|
2021-10-26 22:17:46 +05:30
|
|
|
this._e2eeAccount?.dispose();
|
2021-10-28 15:18:25 +05:30
|
|
|
this._e2eeAccount = undefined;
|
2020-09-18 16:38:18 +05:30
|
|
|
for (const room of this._rooms.values()) {
|
|
|
|
room.dispose();
|
|
|
|
}
|
2021-10-28 15:18:25 +05:30
|
|
|
this._rooms = undefined;
|
2020-04-18 22:46:16 +05:30
|
|
|
}
|
|
|
|
|
2020-10-23 16:29:40 +05:30
|
|
|
/**
|
2021-02-12 01:38:06 +05:30
|
|
|
* @internal called from session container when coming back online and catchup syncs have finished.
|
2020-10-23 16:29:40 +05:30
|
|
|
* @param {Object} lastVersionResponse a response from /versions, which is polled while offline,
|
|
|
|
* and useful to store so we can later tell what capabilities
|
|
|
|
* our homeserver has.
|
|
|
|
*/
|
2021-10-29 22:47:31 +05:30
|
|
|
async start(lastVersionResponse, dehydratedDevice, log) {
|
2020-04-20 23:18:21 +05:30
|
|
|
if (lastVersionResponse) {
|
|
|
|
// store /versions response
|
2021-03-05 00:17:02 +05:30
|
|
|
const txn = await this._storage.readWriteTxn([
|
2020-04-20 23:18:21 +05:30
|
|
|
this._storage.storeNames.session
|
|
|
|
]);
|
2020-08-27 17:58:40 +05:30
|
|
|
txn.session.set("serverVersions", lastVersionResponse);
|
2020-04-20 23:18:21 +05:30
|
|
|
// TODO: what can we do if this throws?
|
|
|
|
await txn.complete();
|
|
|
|
}
|
2020-10-23 15:52:52 +05:30
|
|
|
// enable session backup, this requests the latest backup version
|
2022-01-28 19:43:58 +05:30
|
|
|
if (!this._keyBackup.get()) {
|
2021-10-29 22:47:31 +05:30
|
|
|
if (dehydratedDevice) {
|
2021-11-03 06:50:11 +05:30
|
|
|
await log.wrap("SSSSKeyFromDehydratedDeviceKey", async log => {
|
|
|
|
const ssssKey = await createSSSSKeyFromDehydratedDeviceKey(dehydratedDevice.key, this._storage, this._platform);
|
|
|
|
if (ssssKey) {
|
|
|
|
log.set("success", true);
|
2021-11-03 06:50:27 +05:30
|
|
|
await this._writeSSSSKey(ssssKey);
|
2021-11-03 06:50:11 +05:30
|
|
|
}
|
2022-02-01 15:38:13 +05:30
|
|
|
});
|
2021-10-29 22:47:31 +05:30
|
|
|
}
|
2021-03-05 00:17:02 +05:30
|
|
|
const txn = await this._storage.readTxn([
|
2020-10-23 15:52:52 +05:30
|
|
|
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
|
2022-02-01 15:38:13 +05:30
|
|
|
if (await this._createKeyBackup(ssssKey, txn, log)) {
|
|
|
|
this._keyBackup.get()?.flush(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this._keyBackup.get()) {
|
|
|
|
// null means key backup isn't configured yet
|
|
|
|
// as opposed to undefined, which means we're still checking
|
|
|
|
this._keyBackup.set(null);
|
2020-10-23 15:52:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
// restore unfinished operations, like sending out room keys
|
2021-03-05 00:17:02 +05:30
|
|
|
const opsTxn = await this._storage.readWriteTxn([
|
2020-09-11 18:11:40 +05:30
|
|
|
this._storage.storeNames.operations
|
|
|
|
]);
|
|
|
|
const operations = await opsTxn.operations.getAll();
|
|
|
|
const operationsByScope = groupBy(operations, o => o.scope);
|
|
|
|
|
2021-02-23 23:50:58 +05:30
|
|
|
for (const room of this._rooms.values()) {
|
2020-09-11 18:11:40 +05:30
|
|
|
let roomOperationsByType;
|
|
|
|
const roomOperations = operationsByScope.get(room.id);
|
|
|
|
if (roomOperations) {
|
|
|
|
roomOperationsByType = groupBy(roomOperations, r => r.type);
|
|
|
|
}
|
2021-02-23 23:52:25 +05:30
|
|
|
room.start(roomOperationsByType, log);
|
2019-07-27 02:10:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 02:03:33 +05:30
|
|
|
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-21 04:18:16 +05:30
|
|
|
get rooms() {
|
|
|
|
return this._rooms;
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:29:40 +05:30
|
|
|
/** @internal */
|
2022-02-02 14:49:49 +05:30
|
|
|
createJoinedRoom(roomId, pendingEvents) {
|
2021-04-20 21:09:46 +05:30
|
|
|
return new Room({
|
2019-03-09 00:33:18 +05:30
|
|
|
roomId,
|
2020-09-09 13:20:03 +05:30
|
|
|
getSyncToken: this._getSyncToken,
|
2019-03-09 00:33:18 +05:30
|
|
|
storage: this._storage,
|
|
|
|
emitCollectionChange: this._roomUpdateCallback,
|
|
|
|
hsApi: this._hsApi,
|
2020-09-22 17:13:18 +05:30
|
|
|
mediaRepository: this._mediaRepository,
|
|
|
|
pendingEvents,
|
2019-07-29 13:53:15 +05:30
|
|
|
user: this._user,
|
2020-09-11 15:13:40 +05:30
|
|
|
createRoomEncryption: this._createRoomEncryption,
|
2020-11-11 15:17:19 +05:30
|
|
|
platform: this._platform
|
2019-03-09 00:33:18 +05:30
|
|
|
});
|
2021-04-20 21:09:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
2021-05-11 19:39:58 +05:30
|
|
|
_createArchivedRoom(roomId) {
|
|
|
|
const room = new ArchivedRoom({
|
2021-05-10 22:12:30 +05:30
|
|
|
roomId,
|
|
|
|
getSyncToken: this._getSyncToken,
|
|
|
|
storage: this._storage,
|
|
|
|
emitCollectionChange: () => {},
|
2021-05-11 19:39:58 +05:30
|
|
|
releaseCallback: () => this._activeArchivedRooms.delete(roomId),
|
2021-05-12 19:08:54 +05:30
|
|
|
forgetCallback: this._forgetArchivedRoom,
|
2021-05-10 22:12:30 +05:30
|
|
|
hsApi: this._hsApi,
|
|
|
|
mediaRepository: this._mediaRepository,
|
|
|
|
user: this._user,
|
|
|
|
createRoomEncryption: this._createRoomEncryption,
|
|
|
|
platform: this._platform
|
|
|
|
});
|
2021-05-11 19:39:58 +05:30
|
|
|
this._activeArchivedRooms.set(roomId, room);
|
|
|
|
return room;
|
2021-04-20 21:09:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get invites() {
|
|
|
|
return this._invites;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
createInvite(roomId) {
|
|
|
|
return new Invite({
|
|
|
|
roomId,
|
|
|
|
hsApi: this._hsApi,
|
2021-04-20 22:31:40 +05:30
|
|
|
emitCollectionUpdate: this._inviteUpdateCallback,
|
2021-04-27 14:31:33 +05:30
|
|
|
mediaRepository: this._mediaRepository,
|
2021-04-20 21:09:46 +05:30
|
|
|
user: this._user,
|
2021-04-21 19:01:55 +05:30
|
|
|
platform: this._platform,
|
2021-04-20 21:09:46 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-02 14:49:49 +05:30
|
|
|
get roomsBeingCreated() {
|
|
|
|
return this._roomsBeingCreated;
|
|
|
|
}
|
|
|
|
|
2022-02-03 22:27:35 +05:30
|
|
|
createRoom(type, isEncrypted, explicitName, topic, invites, log = undefined) {
|
|
|
|
return this._platform.logger.wrapOrRun(log, "create room", log => {
|
|
|
|
const localId = `room-being-created-${this._platform.random()}`;
|
|
|
|
const roomBeingCreated = new RoomBeingCreated(localId, type, isEncrypted, explicitName, topic, invites, this._roomsBeingCreatedUpdateCallback, this._mediaRepository, log);
|
|
|
|
this._roomsBeingCreated.set(localId, roomBeingCreated);
|
|
|
|
log.wrapDetached("create room network", async log => {
|
|
|
|
roomBeingCreated.start(this._hsApi, log);
|
|
|
|
});
|
|
|
|
return localId;
|
2022-02-02 14:49:49 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-01 19:34:45 +05:30
|
|
|
async obtainSyncLock(syncResponse) {
|
|
|
|
const toDeviceEvents = syncResponse.to_device?.events;
|
|
|
|
if (Array.isArray(toDeviceEvents) && toDeviceEvents.length) {
|
|
|
|
return await this._deviceMessageHandler.obtainSyncLock(toDeviceEvents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async prepareSync(syncResponse, lock, txn, log) {
|
|
|
|
const toDeviceEvents = syncResponse.to_device?.events;
|
|
|
|
if (Array.isArray(toDeviceEvents) && toDeviceEvents.length) {
|
|
|
|
return await log.wrap("deviceMsgs", log => this._deviceMessageHandler.prepareSync(toDeviceEvents, lock, txn, log));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:29:40 +05:30
|
|
|
/** @internal */
|
2021-03-01 19:34:45 +05:30
|
|
|
async writeSync(syncResponse, syncFilterId, preparation, txn, log) {
|
2020-09-24 14:22:56 +05:30
|
|
|
const changes = {
|
|
|
|
syncInfo: null,
|
2022-01-26 19:49:31 +05:30
|
|
|
e2eeAccountChanges: null
|
2020-09-24 14:22:56 +05:30
|
|
|
};
|
2020-08-28 17:26:44 +05:30
|
|
|
const syncToken = syncResponse.next_batch;
|
2020-08-27 18:06:50 +05:30
|
|
|
if (syncToken !== this.syncToken) {
|
|
|
|
const syncInfo = {token: syncToken, filterId: syncFilterId};
|
2020-08-27 17:58:40 +05:30
|
|
|
// don't modify `this` because transaction might still fail
|
2020-08-27 18:06:50 +05:30
|
|
|
txn.session.set("sync", syncInfo);
|
2020-08-28 17:26:44 +05:30
|
|
|
changes.syncInfo = syncInfo;
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
2020-10-01 18:06:22 +05:30
|
|
|
|
|
|
|
const deviceOneTimeKeysCount = syncResponse.device_one_time_keys_count;
|
|
|
|
if (this._e2eeAccount && deviceOneTimeKeysCount) {
|
2021-02-17 23:15:04 +05:30
|
|
|
changes.e2eeAccountChanges = this._e2eeAccount.writeSync(deviceOneTimeKeysCount, txn, log);
|
2020-10-01 18:06:22 +05:30
|
|
|
}
|
2021-02-19 00:26:10 +05:30
|
|
|
|
2021-02-22 15:51:56 +05:30
|
|
|
const deviceLists = syncResponse.device_lists;
|
2021-02-22 15:50:51 +05:30
|
|
|
if (this._deviceTracker && Array.isArray(deviceLists?.changed) && deviceLists.changed.length) {
|
2021-02-19 00:26:10 +05:30
|
|
|
await log.wrap("deviceLists", log => this._deviceTracker.writeDeviceChanges(deviceLists.changed, txn, log));
|
2020-08-31 17:43:21 +05:30
|
|
|
}
|
2020-09-02 18:00:18 +05:30
|
|
|
|
2021-03-01 19:34:45 +05:30
|
|
|
if (preparation) {
|
2022-01-27 20:37:18 +05:30
|
|
|
changes.hasNewRoomKeys = await log.wrap("deviceMsgs", log => this._deviceMessageHandler.writeSync(preparation, txn, log));
|
2020-09-02 18:00:18 +05:30
|
|
|
}
|
2020-09-17 14:09:51 +05:30
|
|
|
|
|
|
|
// 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 17:26:44 +05:30
|
|
|
return changes;
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
|
|
|
|
2020-10-23 16:29:40 +05:30
|
|
|
/** @internal */
|
2020-08-28 17:26:44 +05:30
|
|
|
afterSync({syncInfo, e2eeAccountChanges}) {
|
2020-08-27 18:06:50 +05:30
|
|
|
if (syncInfo) {
|
2020-03-15 01:15:36 +05:30
|
|
|
// sync transaction succeeded, modify object state now
|
2020-08-27 18:06:50 +05:30
|
|
|
this._syncInfo = syncInfo;
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2020-09-21 21:26:23 +05:30
|
|
|
if (this._e2eeAccount) {
|
2020-08-28 17:26:44 +05:30
|
|
|
this._e2eeAccount.afterSync(e2eeAccountChanges);
|
|
|
|
}
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2019-02-07 05:50:27 +05:30
|
|
|
|
2020-10-23 16:29:40 +05:30
|
|
|
/** @internal */
|
2021-02-17 23:15:04 +05:30
|
|
|
async afterSyncCompleted(changes, isCatchupSync, log) {
|
2020-09-21 21:27:01 +05:30
|
|
|
// 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) {
|
2021-02-23 23:52:25 +05:30
|
|
|
const needsToUploadOTKs = await this._e2eeAccount.generateOTKsIfNeeded(this._storage, log);
|
2020-09-21 21:27:01 +05:30
|
|
|
if (needsToUploadOTKs) {
|
2021-10-27 21:38:50 +05:30
|
|
|
await log.wrap("uploadKeys", log => this._e2eeAccount.uploadKeys(this._storage, false, log));
|
2020-09-21 21:27:01 +05:30
|
|
|
}
|
2020-08-28 17:28:17 +05:30
|
|
|
}
|
2022-01-28 17:43:23 +05:30
|
|
|
if (changes.hasNewRoomKeys) {
|
2022-01-28 19:43:58 +05:30
|
|
|
this._keyBackup.get()?.flush(log);
|
2022-01-25 23:18:19 +05:30
|
|
|
}
|
2020-08-28 17:28:17 +05:30
|
|
|
}
|
|
|
|
|
2022-02-03 22:27:35 +05:30
|
|
|
_tryReplaceRoomBeingCreated(roomId) {
|
|
|
|
console.trace("_tryReplaceRoomBeingCreated " + roomId);
|
|
|
|
for (const [,roomBeingCreated] of this._roomsBeingCreated) {
|
|
|
|
if (roomBeingCreated.roomId === roomId) {
|
|
|
|
const observableStatus = this._observedRoomStatus.get(roomBeingCreated.localId);
|
|
|
|
if (observableStatus) {
|
|
|
|
console.log("marking room as replaced", observableStatus.get());
|
|
|
|
observableStatus.set(observableStatus.get() | RoomStatus.Replaced);
|
|
|
|
} else {
|
|
|
|
console.log("no observableStatus");
|
|
|
|
}
|
|
|
|
this._roomsBeingCreated.remove(roomBeingCreated.localId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 22:12:30 +05:30
|
|
|
applyRoomCollectionChangesAfterSync(inviteStates, roomStates, archivedRoomStates) {
|
|
|
|
// update the collections after sync
|
|
|
|
for (const rs of roomStates) {
|
|
|
|
if (rs.shouldAdd) {
|
|
|
|
this._rooms.add(rs.id, rs.room);
|
2022-02-03 22:27:35 +05:30
|
|
|
this._tryReplaceRoomBeingCreated(rs.id);
|
2021-05-10 22:12:30 +05:30
|
|
|
} else if (rs.shouldRemove) {
|
|
|
|
this._rooms.remove(rs.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const is of inviteStates) {
|
|
|
|
if (is.shouldAdd) {
|
|
|
|
this._invites.add(is.id, is.invite);
|
|
|
|
} else if (is.shouldRemove) {
|
|
|
|
this._invites.remove(is.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// now all the collections are updated, update the room status
|
|
|
|
// so any listeners to the status will find the collections
|
|
|
|
// completely up to date
|
|
|
|
if (this._observedRoomStatus.size !== 0) {
|
|
|
|
for (const ars of archivedRoomStates) {
|
|
|
|
if (ars.shouldAdd) {
|
2022-02-03 22:27:35 +05:30
|
|
|
this._observedRoomStatus.get(ars.id)?.set(RoomStatus.Archived);
|
2021-05-10 22:12:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const rs of roomStates) {
|
|
|
|
if (rs.shouldAdd) {
|
2022-02-03 22:27:35 +05:30
|
|
|
this._observedRoomStatus.get(rs.id)?.set(RoomStatus.Joined);
|
2021-05-10 22:12:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const is of inviteStates) {
|
|
|
|
const statusObservable = this._observedRoomStatus.get(is.id);
|
|
|
|
if (statusObservable) {
|
|
|
|
if (is.shouldAdd) {
|
|
|
|
statusObservable.set(statusObservable.get().withInvited());
|
|
|
|
} else if (is.shouldRemove) {
|
|
|
|
statusObservable.set(statusObservable.get().withoutInvited());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 19:08:54 +05:30
|
|
|
_forgetArchivedRoom(roomId) {
|
|
|
|
const statusObservable = this._observedRoomStatus.get(roomId);
|
|
|
|
if (statusObservable) {
|
2022-02-03 22:27:35 +05:30
|
|
|
statusObservable.set(statusObservable.get() ^ RoomStatus.Archived);
|
2021-05-12 19:08:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:29:40 +05:30
|
|
|
/** @internal */
|
2019-05-12 23:56:46 +05:30
|
|
|
get syncToken() {
|
2020-08-27 18:06:50 +05:30
|
|
|
return this._syncInfo?.token;
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2019-06-16 14:23:23 +05:30
|
|
|
|
2020-10-23 16:29:40 +05:30
|
|
|
/** @internal */
|
2019-10-12 23:54:09 +05:30
|
|
|
get syncFilterId() {
|
2020-08-27 18:06:50 +05:30
|
|
|
return this._syncInfo?.filterId;
|
2019-10-12 23:54:09 +05:30
|
|
|
}
|
|
|
|
|
2019-07-29 13:53:15 +05:30
|
|
|
get user() {
|
|
|
|
return this._user;
|
2019-06-16 14:23:23 +05:30
|
|
|
}
|
2021-03-19 01:14:16 +05:30
|
|
|
|
2021-04-21 19:15:51 +05:30
|
|
|
get mediaRepository() {
|
|
|
|
return this._mediaRepository;
|
|
|
|
}
|
|
|
|
|
2021-03-19 01:14:16 +05:30
|
|
|
enablePushNotifications(enable) {
|
|
|
|
if (enable) {
|
|
|
|
return this._enablePush();
|
|
|
|
} else {
|
|
|
|
return this._disablePush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _enablePush() {
|
|
|
|
return this._platform.logger.run("enablePush", async log => {
|
|
|
|
const defaultPayload = Pusher.createDefaultPayload(this._sessionInfo.id);
|
|
|
|
const pusher = await this._platform.notificationService.enablePush(Pusher, defaultPayload);
|
|
|
|
if (!pusher) {
|
|
|
|
log.set("no_pusher", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
await pusher.enable(this._hsApi, log);
|
|
|
|
// store pusher data, so we know we enabled it across reloads,
|
|
|
|
// and we can disable it without too much hassle
|
|
|
|
const txn = await this._storage.readWriteTxn([this._storage.storeNames.session]);
|
|
|
|
txn.session.set(PUSHER_KEY, pusher.serialize());
|
|
|
|
await txn.complete();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async _disablePush() {
|
|
|
|
return this._platform.logger.run("disablePush", async log => {
|
|
|
|
await this._platform.notificationService.disablePush();
|
|
|
|
const readTxn = await this._storage.readTxn([this._storage.storeNames.session]);
|
|
|
|
const pusherData = await readTxn.session.get(PUSHER_KEY);
|
|
|
|
if (!pusherData) {
|
|
|
|
// we've disabled push in the notif service at least
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const pusher = new Pusher(pusherData);
|
|
|
|
await pusher.disable(this._hsApi, log);
|
|
|
|
const txn = await this._storage.readWriteTxn([this._storage.storeNames.session]);
|
|
|
|
txn.session.remove(PUSHER_KEY);
|
|
|
|
await txn.complete();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async arePushNotificationsEnabled() {
|
2021-03-22 23:49:25 +05:30
|
|
|
if (!await this._platform.notificationService.isPushEnabled()) {
|
2021-03-19 01:14:16 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const readTxn = await this._storage.readTxn([this._storage.storeNames.session]);
|
|
|
|
const pusherData = await readTxn.session.get(PUSHER_KEY);
|
|
|
|
return !!pusherData;
|
|
|
|
}
|
2021-04-01 18:29:46 +05:30
|
|
|
|
2021-08-23 22:56:39 +05:30
|
|
|
async checkPusherEnabledOnHomeserver() {
|
2021-04-01 18:29:46 +05:30
|
|
|
const readTxn = await this._storage.readTxn([this._storage.storeNames.session]);
|
|
|
|
const pusherData = await readTxn.session.get(PUSHER_KEY);
|
|
|
|
if (!pusherData) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const myPusher = new Pusher(pusherData);
|
|
|
|
const serverPushersData = await this._hsApi.getPushers().response();
|
|
|
|
const serverPushers = (serverPushersData?.pushers || []).map(data => new Pusher(data));
|
|
|
|
return serverPushers.some(p => p.equals(myPusher));
|
|
|
|
}
|
2021-05-07 16:38:39 +05:30
|
|
|
|
|
|
|
async getRoomStatus(roomId) {
|
2022-02-03 22:27:35 +05:30
|
|
|
const isBeingCreated = !!this._roomsBeingCreated.get(roomId);
|
|
|
|
if (isBeingCreated) {
|
|
|
|
return RoomStatus.BeingCreated;
|
|
|
|
}
|
2021-05-07 16:38:39 +05:30
|
|
|
const isJoined = !!this._rooms.get(roomId);
|
|
|
|
if (isJoined) {
|
2022-02-03 22:27:35 +05:30
|
|
|
return RoomStatus.Joined;
|
2021-05-07 16:38:39 +05:30
|
|
|
} else {
|
|
|
|
const isInvited = !!this._invites.get(roomId);
|
2021-05-11 19:39:58 +05:30
|
|
|
const txn = await this._storage.readTxn([this._storage.storeNames.archivedRoomSummary]);
|
|
|
|
const isArchived = await txn.archivedRoomSummary.has(roomId);
|
2021-05-07 16:38:39 +05:30
|
|
|
if (isInvited && isArchived) {
|
2022-02-03 22:27:35 +05:30
|
|
|
return RoomStatus.Invited | RoomStatus.Archived;
|
2021-05-07 16:38:39 +05:30
|
|
|
} else if (isInvited) {
|
2022-02-03 22:27:35 +05:30
|
|
|
return RoomStatus.Invited;
|
2021-05-07 16:38:39 +05:30
|
|
|
} else if (isArchived) {
|
2022-02-03 22:27:35 +05:30
|
|
|
return RoomStatus.Archived;
|
2021-05-07 16:38:39 +05:30
|
|
|
} else {
|
2022-02-03 22:27:35 +05:30
|
|
|
return RoomStatus.None;
|
2021-05-07 16:38:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 16:39:38 +05:30
|
|
|
|
|
|
|
async observeRoomStatus(roomId) {
|
|
|
|
let observable = this._observedRoomStatus.get(roomId);
|
|
|
|
if (!observable) {
|
|
|
|
const status = await this.getRoomStatus(roomId);
|
2022-02-03 22:27:35 +05:30
|
|
|
observable = new FooRetainedObservableValue(status, () => {
|
2021-05-07 16:39:38 +05:30
|
|
|
this._observedRoomStatus.delete(roomId);
|
|
|
|
});
|
2022-02-03 22:27:35 +05:30
|
|
|
|
2021-05-07 16:39:38 +05:30
|
|
|
this._observedRoomStatus.set(roomId, observable);
|
|
|
|
}
|
|
|
|
return observable;
|
|
|
|
}
|
2021-05-07 16:40:10 +05:30
|
|
|
|
2021-05-11 20:28:16 +05:30
|
|
|
/**
|
|
|
|
Creates an empty (summary isn't loaded) the archived room if it isn't
|
|
|
|
loaded already, assuming sync will either remove it (when rejoining) or
|
|
|
|
write a full summary adopting it from the joined room when leaving
|
|
|
|
|
|
|
|
@internal
|
|
|
|
*/
|
|
|
|
createOrGetArchivedRoomForSync(roomId) {
|
|
|
|
let archivedRoom = this._activeArchivedRooms.get(roomId);
|
|
|
|
if (archivedRoom) {
|
|
|
|
archivedRoom.retain();
|
|
|
|
} else {
|
|
|
|
archivedRoom = this._createArchivedRoom(roomId);
|
|
|
|
}
|
|
|
|
return archivedRoom;
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:40:10 +05:30
|
|
|
loadArchivedRoom(roomId, log = null) {
|
|
|
|
return this._platform.logger.wrapOrRun(log, "loadArchivedRoom", async log => {
|
|
|
|
log.set("id", roomId);
|
2021-05-11 19:39:58 +05:30
|
|
|
const activeArchivedRoom = this._activeArchivedRooms.get(roomId);
|
|
|
|
if (activeArchivedRoom) {
|
|
|
|
activeArchivedRoom.retain();
|
|
|
|
return activeArchivedRoom;
|
|
|
|
}
|
2021-05-07 16:40:10 +05:30
|
|
|
const txn = await this._storage.readTxn([
|
|
|
|
this._storage.storeNames.archivedRoomSummary,
|
|
|
|
this._storage.storeNames.roomMembers,
|
|
|
|
]);
|
|
|
|
const summary = await txn.archivedRoomSummary.get(roomId);
|
|
|
|
if (summary) {
|
2021-05-11 19:39:58 +05:30
|
|
|
const room = this._createArchivedRoom(roomId);
|
2021-05-07 16:40:10 +05:30
|
|
|
await room.load(summary, txn, log);
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-05-18 14:35:31 +05:30
|
|
|
|
|
|
|
joinRoom(roomIdOrAlias, log = null) {
|
|
|
|
return this._platform.logger.wrapOrRun(log, "joinRoom", async log => {
|
2021-05-18 15:19:16 +05:30
|
|
|
const body = await this._hsApi.joinIdOrAlias(roomIdOrAlias, {log}).response();
|
2021-05-18 14:35:31 +05:30
|
|
|
return body.room_id;
|
|
|
|
});
|
|
|
|
}
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
2020-03-15 01:15:36 +05:30
|
|
|
|
2022-02-03 22:27:35 +05:30
|
|
|
class FooRetainedObservableValue extends RetainedObservableValue {
|
|
|
|
set(value) {
|
|
|
|
console.log("setting room status to", value);
|
|
|
|
super.set(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 01:15:36 +05:30
|
|
|
export function tests() {
|
2020-03-15 02:08:37 +05:30
|
|
|
function createStorageMock(session, pendingEvents = []) {
|
2020-03-15 01:15:36 +05:30
|
|
|
return {
|
|
|
|
readTxn() {
|
2020-10-06 15:49:47 +05:30
|
|
|
return {
|
2020-03-15 01:15:36 +05:30
|
|
|
session: {
|
2020-08-27 17:58:40 +05:30
|
|
|
get(key) {
|
|
|
|
return Promise.resolve(session[key]);
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
|
|
|
},
|
|
|
|
pendingEvents: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve(pendingEvents);
|
|
|
|
}
|
2020-03-15 02:08:37 +05:30
|
|
|
},
|
|
|
|
roomSummary: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2021-04-20 21:39:48 +05:30
|
|
|
},
|
|
|
|
invites: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
2020-10-06 15:49:47 +05:30
|
|
|
};
|
2020-03-15 02:08:37 +05:30
|
|
|
},
|
|
|
|
storeNames: {}
|
2020-03-15 01:15:36 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"session data is not modified until after sync": async (assert) => {
|
|
|
|
const session = new Session({storage: createStorageMock({
|
2020-08-27 18:06:50 +05:30
|
|
|
sync: {token: "a", filterId: 5}
|
2020-03-15 02:08:37 +05:30
|
|
|
}), sessionInfo: {userId: ""}});
|
2020-03-15 01:15:36 +05:30
|
|
|
await session.load();
|
2020-08-27 18:06:50 +05:30
|
|
|
let syncSet = false;
|
2020-03-15 01:15:36 +05:30
|
|
|
const syncTxn = {
|
|
|
|
session: {
|
2020-08-27 17:58:40 +05:30
|
|
|
set(key, value) {
|
2020-08-27 18:06:50 +05:30
|
|
|
if (key === "sync") {
|
|
|
|
assert.equal(value.token, "b");
|
|
|
|
assert.equal(value.filterId, 6);
|
|
|
|
syncSet = true;
|
2020-08-27 17:58:40 +05:30
|
|
|
}
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-03-05 21:33:12 +05:30
|
|
|
const newSessionData = await session.writeSync({next_batch: "b"}, 6, null, syncTxn, {});
|
2020-08-27 18:06:50 +05:30
|
|
|
assert(syncSet);
|
2020-03-15 02:08:37 +05:30
|
|
|
assert.equal(session.syncToken, "a");
|
|
|
|
assert.equal(session.syncFilterId, 5);
|
2020-03-15 01:15:36 +05:30
|
|
|
session.afterSync(newSessionData);
|
2020-03-15 02:08:37 +05:30
|
|
|
assert.equal(session.syncToken, "b");
|
|
|
|
assert.equal(session.syncFilterId, 6);
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|