Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to influence set_presence on /sync API calls #3578

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions spec/integ/matrix-client-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -5531,6 +5531,16 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return getHttpUriForMxc(this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks);
}

/**
* Specify the set_presence value to be used for subsequent calls to the Sync API.
* This has an advantage over calls to the PUT /presence API in that it
* doesn't clobber status_msg set by other devices.
* @param presence - the presence to specify to set_presence of sync calls
*/
public async setSyncPresence(presence?: SetPresence): Promise<void> {
this.syncApi?.setPresence(presence);
}

/**
* @param opts - Options to apply
* @returns Promise which resolves
Expand Down
9 changes: 9 additions & 0 deletions src/sliding-sync-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
SyncApiOptions,
defaultClientOpts,
defaultSyncApiOpts,
SetPresence,
} from "./sync";
import { MatrixEvent } from "./models/event";
import { Crypto } from "./crypto";
Expand Down Expand Up @@ -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"
Expand Down
13 changes: 12 additions & 1 deletion src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export interface ISyncStateData {
fromCache?: boolean;
}

enum SetPresence {
export enum SetPresence {
Offline = "offline",
Online = "online",
Unavailable = "unavailable",
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1005,6 +1006,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) {
Expand All @@ -1027,6 +1030,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<boolean> {
if (!this.running) {
debuglog("Sync no longer running: exiting");
Expand Down