You've already forked matrix-js-sdk
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:
committed by
GitHub
parent
cef5507ab1
commit
7c34deecb6
@ -119,7 +119,7 @@ describe("SlidingSyncSdk", () => {
|
||||
if (testOpts.withCrypto) {
|
||||
httpBackend!.when("GET", "/room_keys/version").respond(404, {});
|
||||
await client!.initCrypto();
|
||||
syncOpts.crypto = client!.crypto;
|
||||
syncOpts.cryptoCallbacks = syncOpts.crypto = client!.crypto;
|
||||
}
|
||||
httpBackend!.when("GET", "/_matrix/client/r0/pushrules").respond(200, {});
|
||||
sdk = new SlidingSyncSdk(mockSlidingSync, client, testOpts, syncOpts);
|
||||
|
@ -1453,6 +1453,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
protected buildSyncApiOptions(): SyncApiOptions {
|
||||
return {
|
||||
crypto: this.crypto,
|
||||
cryptoCallbacks: this.cryptoBackend,
|
||||
canResetEntireTimeline: (roomId: string): boolean => {
|
||||
if (!this.canResetTimelineCallback) {
|
||||
return false;
|
||||
|
@ -20,7 +20,7 @@ import { MatrixEvent } from "../models/event";
|
||||
/**
|
||||
* Common interface for the crypto implementations
|
||||
*/
|
||||
export interface CryptoBackend {
|
||||
export interface CryptoBackend extends SyncCryptoCallbacks {
|
||||
/**
|
||||
* Global override for whether the client should ever send encrypted
|
||||
* messages to unverified devices. This provides the default for rooms which
|
||||
@ -71,3 +71,27 @@ export interface CryptoBackend {
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ import { IContent } from "../models/event";
|
||||
import { ISyncResponse } from "../sync-accumulator";
|
||||
import { ISignatures } from "../@types/signed";
|
||||
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";
|
||||
|
||||
const DeviceVerification = DeviceInfo.DeviceVerification;
|
||||
@ -3013,7 +3013,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
*
|
||||
* @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.saveIfDirty();
|
||||
|
||||
|
@ -18,7 +18,7 @@ import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js";
|
||||
|
||||
import type { IEventDecryptionResult, IMegolmSessionData } from "../@types/crypto";
|
||||
import { MatrixEvent } from "../models/event";
|
||||
import { CryptoBackend } from "../common-crypto/CryptoBackend";
|
||||
import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
|
||||
|
||||
// import { logger } from "../logger";
|
||||
|
||||
@ -62,4 +62,12 @@ export class RustCrypto implements CryptoBackend {
|
||||
// TODO
|
||||
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 {}
|
||||
}
|
||||
|
16
src/sync.ts
16
src/sync.ts
@ -25,6 +25,7 @@ limitations under the License.
|
||||
|
||||
import { Optional } from "matrix-events-sdk";
|
||||
|
||||
import type { SyncCryptoCallbacks } from "./common-crypto/CryptoBackend";
|
||||
import { User, UserEvent } from "./models/user";
|
||||
import { NotificationCountType, Room, RoomEvent } from "./models/room";
|
||||
import * as utils from "./utils";
|
||||
@ -115,9 +116,18 @@ function debuglog(...params: any[]): void {
|
||||
* Options passed into the constructor of SyncApi by MatrixClient
|
||||
*/
|
||||
export interface SyncApiOptions {
|
||||
// Crypto manager
|
||||
/**
|
||||
* Crypto manager
|
||||
*
|
||||
* @deprecated in favour of cryptoCallbacks
|
||||
*/
|
||||
crypto?: Crypto;
|
||||
|
||||
/**
|
||||
* If crypto is enabled on our client, callbacks into the crypto module
|
||||
*/
|
||||
cryptoCallbacks?: SyncCryptoCallbacks;
|
||||
|
||||
/**
|
||||
* A function which is called
|
||||
* 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
|
||||
// /keys/changes request).
|
||||
if (this.syncOpts.crypto) {
|
||||
await this.syncOpts.crypto.onSyncCompleted(syncEventData);
|
||||
if (this.syncOpts.cryptoCallbacks) {
|
||||
await this.syncOpts.cryptoCallbacks.onSyncCompleted(syncEventData);
|
||||
}
|
||||
|
||||
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
|
||||
|
Reference in New Issue
Block a user