Skip to content

Commit 7c34dee

Browse files
authored
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.
1 parent cef5507 commit 7c34dee

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

spec/integ/sliding-sync-sdk.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe("SlidingSyncSdk", () => {
119119
if (testOpts.withCrypto) {
120120
httpBackend!.when("GET", "/room_keys/version").respond(404, {});
121121
await client!.initCrypto();
122-
syncOpts.crypto = client!.crypto;
122+
syncOpts.cryptoCallbacks = syncOpts.crypto = client!.crypto;
123123
}
124124
httpBackend!.when("GET", "/_matrix/client/r0/pushrules").respond(200, {});
125125
sdk = new SlidingSyncSdk(mockSlidingSync, client, testOpts, syncOpts);

src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
14531453
protected buildSyncApiOptions(): SyncApiOptions {
14541454
return {
14551455
crypto: this.crypto,
1456+
cryptoCallbacks: this.cryptoBackend,
14561457
canResetEntireTimeline: (roomId: string): boolean => {
14571458
if (!this.canResetTimelineCallback) {
14581459
return false;

src/common-crypto/CryptoBackend.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { MatrixEvent } from "../models/event";
2020
/**
2121
* Common interface for the crypto implementations
2222
*/
23-
export interface CryptoBackend {
23+
export interface CryptoBackend extends SyncCryptoCallbacks {
2424
/**
2525
* Global override for whether the client should ever send encrypted
2626
* messages to unverified devices. This provides the default for rooms which
@@ -71,3 +71,27 @@ export interface CryptoBackend {
7171
*/
7272
exportRoomKeys(): Promise<IMegolmSessionData[]>;
7373
}
74+
75+
/** The methods which crypto implementations should expose to the Sync api */
76+
export interface SyncCryptoCallbacks {
77+
/**
78+
* Called by the /sync loop after each /sync response is processed.
79+
*
80+
* Used to complete batch processing, or to initiate background processes
81+
*
82+
* @param syncState - information about the completed sync.
83+
*/
84+
onSyncCompleted(syncState: OnSyncCompletedData): void;
85+
}
86+
87+
export interface OnSyncCompletedData {
88+
/**
89+
* The 'next_batch' result from /sync, which will become the 'since' token for the next call to /sync.
90+
*/
91+
nextSyncToken?: string;
92+
93+
/**
94+
* True if we are working our way through a backlog of events after connecting.
95+
*/
96+
catchingUp?: boolean;
97+
}

src/crypto/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import { IContent } from "../models/event";
8888
import { ISyncResponse } from "../sync-accumulator";
8989
import { ISignatures } from "../@types/signed";
9090
import { IMessage } from "./algorithms/olm";
91-
import { CryptoBackend } from "../common-crypto/CryptoBackend";
91+
import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
9292
import { RoomState, RoomStateEvent } from "../models/room-state";
9393

9494
const DeviceVerification = DeviceInfo.DeviceVerification;
@@ -3013,7 +3013,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
30133013
*
30143014
* @param syncData - the data from the 'MatrixClient.sync' event
30153015
*/
3016-
public async onSyncCompleted(syncData: ISyncStateData): Promise<void> {
3016+
public async onSyncCompleted(syncData: OnSyncCompletedData): Promise<void> {
30173017
this.deviceList.setSyncToken(syncData.nextSyncToken ?? null);
30183018
this.deviceList.saveIfDirty();
30193019

src/rust-crypto/rust-crypto.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js";
1818

1919
import type { IEventDecryptionResult, IMegolmSessionData } from "../@types/crypto";
2020
import { MatrixEvent } from "../models/event";
21-
import { CryptoBackend } from "../common-crypto/CryptoBackend";
21+
import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
2222

2323
// import { logger } from "../logger";
2424

@@ -62,4 +62,12 @@ export class RustCrypto implements CryptoBackend {
6262
// TODO
6363
return [];
6464
}
65+
66+
/** called by the sync loop after processing each sync.
67+
*
68+
* TODO: figure out something equivalent for sliding sync.
69+
*
70+
* @param syncState - information on the completed sync.
71+
*/
72+
public onSyncCompleted(syncState: OnSyncCompletedData): void {}
6573
}

src/sync.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ limitations under the License.
2525

2626
import { Optional } from "matrix-events-sdk";
2727

28+
import type { SyncCryptoCallbacks } from "./common-crypto/CryptoBackend";
2829
import { User, UserEvent } from "./models/user";
2930
import { NotificationCountType, Room, RoomEvent } from "./models/room";
3031
import * as utils from "./utils";
@@ -115,9 +116,18 @@ function debuglog(...params: any[]): void {
115116
* Options passed into the constructor of SyncApi by MatrixClient
116117
*/
117118
export interface SyncApiOptions {
118-
// Crypto manager
119+
/**
120+
* Crypto manager
121+
*
122+
* @deprecated in favour of cryptoCallbacks
123+
*/
119124
crypto?: Crypto;
120125

126+
/**
127+
* If crypto is enabled on our client, callbacks into the crypto module
128+
*/
129+
cryptoCallbacks?: SyncCryptoCallbacks;
130+
121131
/**
122132
* A function which is called
123133
* with a room ID and returns a boolean. It should return 'true' if the SDK can
@@ -925,8 +935,8 @@ export class SyncApi {
925935

926936
// tell the crypto module to do its processing. It may block (to do a
927937
// /keys/changes request).
928-
if (this.syncOpts.crypto) {
929-
await this.syncOpts.crypto.onSyncCompleted(syncEventData);
938+
if (this.syncOpts.cryptoCallbacks) {
939+
await this.syncOpts.cryptoCallbacks.onSyncCompleted(syncEventData);
930940
}
931941

932942
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates

0 commit comments

Comments
 (0)