1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

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

* Add methods to influence set_presence on /sync API calls

* Tweak comment

* Improve coverage
This commit is contained in:
Michael Telatynski
2023-07-11 14:31:12 +01:00
committed by GitHub
parent 1fdc0af5b7
commit f2471b6dbd
4 changed files with 43 additions and 2 deletions

View File

@ -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 {

View File

@ -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<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

View File

@ -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"

View File

@ -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<boolean> {
if (!this.running) {
debuglog("Sync no longer running: exiting");