1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Pass CryptoBackend into SyncApi (#3010)

I need to start calling back into the new rust crypto implementation from the /sync loops, so I need to pass it into SyncApi. To reduce the coupling, I've defined a new interface specifying the methods which exist for that purpose. Currently it's only onSyncCompleted.
This commit is contained in:
Richard van der Hoff
2023-01-03 15:37:51 +00:00
committed by GitHub
parent cef5507ab1
commit 7c34deecb6
6 changed files with 51 additions and 8 deletions

View File

@ -119,7 +119,7 @@ describe("SlidingSyncSdk", () => {
if (testOpts.withCrypto) { if (testOpts.withCrypto) {
httpBackend!.when("GET", "/room_keys/version").respond(404, {}); httpBackend!.when("GET", "/room_keys/version").respond(404, {});
await client!.initCrypto(); await client!.initCrypto();
syncOpts.crypto = client!.crypto; syncOpts.cryptoCallbacks = syncOpts.crypto = client!.crypto;
} }
httpBackend!.when("GET", "/_matrix/client/r0/pushrules").respond(200, {}); httpBackend!.when("GET", "/_matrix/client/r0/pushrules").respond(200, {});
sdk = new SlidingSyncSdk(mockSlidingSync, client, testOpts, syncOpts); sdk = new SlidingSyncSdk(mockSlidingSync, client, testOpts, syncOpts);

View File

@ -1453,6 +1453,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
protected buildSyncApiOptions(): SyncApiOptions { protected buildSyncApiOptions(): SyncApiOptions {
return { return {
crypto: this.crypto, crypto: this.crypto,
cryptoCallbacks: this.cryptoBackend,
canResetEntireTimeline: (roomId: string): boolean => { canResetEntireTimeline: (roomId: string): boolean => {
if (!this.canResetTimelineCallback) { if (!this.canResetTimelineCallback) {
return false; return false;

View File

@ -20,7 +20,7 @@ import { MatrixEvent } from "../models/event";
/** /**
* Common interface for the crypto implementations * Common interface for the crypto implementations
*/ */
export interface CryptoBackend { export interface CryptoBackend extends SyncCryptoCallbacks {
/** /**
* Global override for whether the client should ever send encrypted * Global override for whether the client should ever send encrypted
* messages to unverified devices. This provides the default for rooms which * messages to unverified devices. This provides the default for rooms which
@ -71,3 +71,27 @@ export interface CryptoBackend {
*/ */
exportRoomKeys(): Promise<IMegolmSessionData[]>; exportRoomKeys(): Promise<IMegolmSessionData[]>;
} }
/** The methods which crypto implementations should expose to the Sync api */
export interface SyncCryptoCallbacks {
/**
* Called by the /sync loop after each /sync response is processed.
*
* Used to complete batch processing, or to initiate background processes
*
* @param syncState - information about the completed sync.
*/
onSyncCompleted(syncState: OnSyncCompletedData): void;
}
export interface OnSyncCompletedData {
/**
* The 'next_batch' result from /sync, which will become the 'since' token for the next call to /sync.
*/
nextSyncToken?: string;
/**
* True if we are working our way through a backlog of events after connecting.
*/
catchingUp?: boolean;
}

View File

@ -88,7 +88,7 @@ import { IContent } from "../models/event";
import { ISyncResponse } from "../sync-accumulator"; import { ISyncResponse } from "../sync-accumulator";
import { ISignatures } from "../@types/signed"; import { ISignatures } from "../@types/signed";
import { IMessage } from "./algorithms/olm"; import { IMessage } from "./algorithms/olm";
import { CryptoBackend } from "../common-crypto/CryptoBackend"; import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
import { RoomState, RoomStateEvent } from "../models/room-state"; import { RoomState, RoomStateEvent } from "../models/room-state";
const DeviceVerification = DeviceInfo.DeviceVerification; const DeviceVerification = DeviceInfo.DeviceVerification;
@ -3013,7 +3013,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
* *
* @param syncData - the data from the 'MatrixClient.sync' event * @param syncData - the data from the 'MatrixClient.sync' event
*/ */
public async onSyncCompleted(syncData: ISyncStateData): Promise<void> { public async onSyncCompleted(syncData: OnSyncCompletedData): Promise<void> {
this.deviceList.setSyncToken(syncData.nextSyncToken ?? null); this.deviceList.setSyncToken(syncData.nextSyncToken ?? null);
this.deviceList.saveIfDirty(); this.deviceList.saveIfDirty();

View File

@ -18,7 +18,7 @@ import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js";
import type { IEventDecryptionResult, IMegolmSessionData } from "../@types/crypto"; import type { IEventDecryptionResult, IMegolmSessionData } from "../@types/crypto";
import { MatrixEvent } from "../models/event"; import { MatrixEvent } from "../models/event";
import { CryptoBackend } from "../common-crypto/CryptoBackend"; import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
// import { logger } from "../logger"; // import { logger } from "../logger";
@ -62,4 +62,12 @@ export class RustCrypto implements CryptoBackend {
// TODO // TODO
return []; return [];
} }
/** called by the sync loop after processing each sync.
*
* TODO: figure out something equivalent for sliding sync.
*
* @param syncState - information on the completed sync.
*/
public onSyncCompleted(syncState: OnSyncCompletedData): void {}
} }

View File

@ -25,6 +25,7 @@ limitations under the License.
import { Optional } from "matrix-events-sdk"; import { Optional } from "matrix-events-sdk";
import type { SyncCryptoCallbacks } from "./common-crypto/CryptoBackend";
import { User, UserEvent } from "./models/user"; import { User, UserEvent } from "./models/user";
import { NotificationCountType, Room, RoomEvent } from "./models/room"; import { NotificationCountType, Room, RoomEvent } from "./models/room";
import * as utils from "./utils"; import * as utils from "./utils";
@ -115,9 +116,18 @@ function debuglog(...params: any[]): void {
* Options passed into the constructor of SyncApi by MatrixClient * Options passed into the constructor of SyncApi by MatrixClient
*/ */
export interface SyncApiOptions { export interface SyncApiOptions {
// Crypto manager /**
* Crypto manager
*
* @deprecated in favour of cryptoCallbacks
*/
crypto?: Crypto; crypto?: Crypto;
/**
* If crypto is enabled on our client, callbacks into the crypto module
*/
cryptoCallbacks?: SyncCryptoCallbacks;
/** /**
* A function which is called * A function which is called
* with a room ID and returns a boolean. It should return 'true' if the SDK can * with a room ID and returns a boolean. It should return 'true' if the SDK can
@ -925,8 +935,8 @@ export class SyncApi {
// tell the crypto module to do its processing. It may block (to do a // tell the crypto module to do its processing. It may block (to do a
// /keys/changes request). // /keys/changes request).
if (this.syncOpts.crypto) { if (this.syncOpts.cryptoCallbacks) {
await this.syncOpts.crypto.onSyncCompleted(syncEventData); await this.syncOpts.cryptoCallbacks.onSyncCompleted(syncEventData);
} }
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates // keep emitting SYNCING -> SYNCING for clients who want to do bulk updates