1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-12-04 05:02:41 +03:00

Merge branch 'develop' into madlittlemods/stablize-msc3030-timestamp-to-event

Conflicts:
	spec/unit/matrix-client.spec.ts
This commit is contained in:
Eric Eastwood
2022-12-16 00:56:06 -06:00
15 changed files with 797 additions and 104 deletions

View File

@@ -211,6 +211,7 @@ import { LocalNotificationSettings } from "./@types/local_notifications";
import { UNREAD_THREAD_NOTIFICATIONS } from "./@types/sync";
import { buildFeatureSupportMap, Feature, ServerSupport } from "./feature";
import { CryptoBackend } from "./common-crypto/CryptoBackend";
import { RUST_SDK_STORE_PREFIX } from "./rust-crypto/constants";
export type Store = IStore;
@@ -1657,6 +1658,41 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
if (this.cryptoStore) {
promises.push(this.cryptoStore.deleteAllData());
}
// delete the stores used by the rust matrix-sdk-crypto, in case they were used
const deleteRustSdkStore = async (): Promise<void> => {
let indexedDB: IDBFactory;
try {
indexedDB = global.indexedDB;
} catch (e) {
// No indexeddb support
return;
}
for (const dbname of [
`${RUST_SDK_STORE_PREFIX}::matrix-sdk-crypto`,
`${RUST_SDK_STORE_PREFIX}::matrix-sdk-crypto-meta`,
]) {
const prom = new Promise((resolve, reject) => {
logger.info(`Removing IndexedDB instance ${dbname}`);
const req = indexedDB.deleteDatabase(dbname);
req.onsuccess = (_): void => {
logger.info(`Removed IndexedDB instance ${dbname}`);
resolve(0);
};
req.onerror = (e): void => {
logger.error(`Failed to remove IndexedDB instance ${dbname}: ${e}`);
reject(new Error(`Error clearing storage: ${e}`));
};
req.onblocked = (e): void => {
logger.info(`cannot yet remove IndexedDB instance ${dbname}`);
//reject(new Error(`Error clearing storage: ${e}`));
};
});
await prom;
}
};
promises.push(deleteRustSdkStore());
return Promise.all(promises).then(); // .then to fix types
}
@@ -1672,6 +1708,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return null;
}
/**
* Get the user-id of the logged-in user
*
* @returns MXID for the logged-in user
* @throws Error if not logged in
*/
public getSafeUserId(): string {
const userId = this.getUserId();
if (!userId) {
throw new Error("Expected logged in user but found none.");
}
return userId;
}
/**
* Get the domain for this client's MXID
* @returns Domain of this MXID
@@ -2051,6 +2101,46 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
});
}
/**
* Initialise support for end-to-end encryption in this client, using the rust matrix-sdk-crypto.
*
* An alternative to {@link initCrypto}.
*
* *WARNING*: this API is very experimental, should not be used in production, and may change without notice!
* Eventually it will be deprecated and `initCrypto` will do the same thing.
*
* @experimental
*
* @returns a Promise which will resolve when the crypto layer has been
* successfully initialised.
*/
public async initRustCrypto(): Promise<void> {
if (this.cryptoBackend) {
logger.warn("Attempt to re-initialise e2e encryption on MatrixClient");
return;
}
const userId = this.getUserId();
if (userId === null) {
throw new Error(
`Cannot enable encryption on MatrixClient with unknown userId: ` +
`ensure userId is passed in createClient().`,
);
}
const deviceId = this.getDeviceId();
if (deviceId === null) {
throw new Error(
`Cannot enable encryption on MatrixClient with unknown deviceId: ` +
`ensure deviceId is passed in createClient().`,
);
}
// importing rust-crypto will download the webassembly, so we delay it until we know it will be
// needed.
const RustCrypto = await import("./rust-crypto");
this.cryptoBackend = await RustCrypto.initRustCrypto(userId, deviceId);
}
/**
* Is end-to-end crypto enabled for this client.
* @returns True if end-to-end is enabled.
@@ -3766,6 +3856,24 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
}
}
public async deleteAccountData(eventType: string): Promise<void> {
const msc3391DeleteAccountDataServerSupport = this.canSupport.get(Feature.AccountDataDeletion);
// if deletion is not supported overwrite with empty content
if (msc3391DeleteAccountDataServerSupport === ServerSupport.Unsupported) {
await this.setAccountData(eventType, {});
return;
}
const path = utils.encodeUri("/user/$userId/account_data/$type", {
$userId: this.getSafeUserId(),
$type: eventType,
});
const options =
msc3391DeleteAccountDataServerSupport === ServerSupport.Unstable
? { prefix: "/_matrix/client/unstable/org.matrix.msc3391" }
: undefined;
return await this.http.authedRequest(Method.Delete, path, undefined, undefined, options);
}
/**
* Gets the users that are ignored by this client
* @returns The array of users that are ignored (empty if none)