diff --git a/spec/integ/matrix-client-methods.spec.ts b/spec/integ/matrix-client-methods.spec.ts index da417b8c2..48dcccba9 100644 --- a/spec/integ/matrix-client-methods.spec.ts +++ b/spec/integ/matrix-client-methods.spec.ts @@ -26,6 +26,7 @@ import { IFilterDefinition } from "../../src/filter"; import { ISearchResults } from "../../src/@types/search"; import { IStore } from "../../src/store"; import { CryptoBackend } from "../../src/common-crypto/CryptoBackend"; +import { SetPresence } from "../../src/sync"; describe("MatrixClient", function () { const userId = "@alice:localhost"; @@ -1516,6 +1517,16 @@ describe("MatrixClient", function () { expect(mockBackend.setTrustCrossSignedDevices).toHaveBeenLastCalledWith(false); }); }); + + describe("setSyncPresence", () => { + it("should pass calls through to the underlying sync api", () => { + const setPresence = jest.fn(); + // @ts-ignore + client.syncApi = { setPresence }; + client?.setSyncPresence(SetPresence.Unavailable); + expect(setPresence).toHaveBeenCalledWith(SetPresence.Unavailable); + }); + }); }); function withThreadId(event: MatrixEvent, newThreadId: string): MatrixEvent { diff --git a/src/client.ts b/src/client.ts index 9d9e1d3aa..601a2746c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -21,7 +21,7 @@ limitations under the License. import { Optional } from "matrix-events-sdk"; import type { IDeviceKeys, IMegolmSessionData, IOneTimeKey } from "./@types/crypto"; -import { ISyncStateData, SyncApi, SyncApiOptions, SyncState } from "./sync"; +import { ISyncStateData, SetPresence, SyncApi, SyncApiOptions, SyncState } from "./sync"; import { EventStatus, IContent, @@ -5531,6 +5531,16 @@ export class MatrixClient extends TypedEventEmitter { + this.syncApi?.setPresence(presence); + } + /** * @param opts - Options to apply * @returns Promise which resolves diff --git a/src/sliding-sync-sdk.ts b/src/sliding-sync-sdk.ts index 36eed5d73..780144222 100644 --- a/src/sliding-sync-sdk.ts +++ b/src/sliding-sync-sdk.ts @@ -27,6 +27,7 @@ import { SyncApiOptions, defaultClientOpts, defaultSyncApiOpts, + SetPresence, } from "./sync"; import { MatrixEvent } from "./models/event"; import { Crypto } from "./crypto"; @@ -463,6 +464,14 @@ export class SlidingSyncSdk { // TODO } + /** + * Specify the set_presence value to be used for subsequent calls to the Sync API. + * @param presence - the presence to specify to set_presence of sync calls + */ + public setPresence(presence?: SetPresence): void { + // TODO not possible in sliding sync yet + } + /** * Returns the current state of this sync object * @see MatrixClient#event:"sync" diff --git a/src/sync.ts b/src/sync.ts index 1e80ac76c..49bddc045 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -168,7 +168,7 @@ export interface ISyncStateData { fromCache?: boolean; } -enum SetPresence { +export enum SetPresence { Offline = "offline", Online = "online", Unavailable = "unavailable", @@ -225,6 +225,7 @@ export class SyncApi { private notifEvents: MatrixEvent[] = []; // accumulator of sync events in the current sync response private failedSyncCount = 0; // Number of consecutive failed /sync requests private storeIsInvalid = false; // flag set if the store needs to be cleared before we can start + private presence?: SetPresence; /** * Construct an entity which is able to sync with a homeserver. @@ -1009,6 +1010,8 @@ export class SyncApi { if (this.opts.disablePresence) { qps.set_presence = SetPresence.Offline; + } else if (this.presence !== undefined) { + qps.set_presence = this.presence; } if (syncToken) { @@ -1031,6 +1034,14 @@ export class SyncApi { return qps; } + /** + * Specify the set_presence value to be used for subsequent calls to the Sync API. + * @param presence - the presence to specify to set_presence of sync calls + */ + public setPresence(presence?: SetPresence): void { + this.presence = presence; + } + private async onSyncError(err: MatrixError): Promise { if (!this.running) { debuglog("Sync no longer running: exiting");