Skip to content

Commit

Permalink
Add methods to influence set_presence on /sync API calls (#3578)
Browse files Browse the repository at this point in the history
* Add methods to influence set_presence on /sync API calls

* Tweak comment

* Improve coverage
  • Loading branch information
t3chguy authored Jul 11, 2023
1 parent 1fdc0af commit f2471b6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
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 @@ -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) {
Expand All @@ -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<boolean> {
if (!this.running) {
debuglog("Sync no longer running: exiting");
Expand Down

0 comments on commit f2471b6

Please sign in to comment.