1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-06 12:02:40 +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 { ISearchResults } from "../../src/@types/search";
import { IStore } from "../../src/store"; import { IStore } from "../../src/store";
import { CryptoBackend } from "../../src/common-crypto/CryptoBackend"; import { CryptoBackend } from "../../src/common-crypto/CryptoBackend";
import { SetPresence } from "../../src/sync";
describe("MatrixClient", function () { describe("MatrixClient", function () {
const userId = "@alice:localhost"; const userId = "@alice:localhost";
@@ -1516,6 +1517,16 @@ describe("MatrixClient", function () {
expect(mockBackend.setTrustCrossSignedDevices).toHaveBeenLastCalledWith(false); 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 { function withThreadId(event: MatrixEvent, newThreadId: string): MatrixEvent {

View File

@@ -21,7 +21,7 @@ limitations under the License.
import { Optional } from "matrix-events-sdk"; import { Optional } from "matrix-events-sdk";
import type { IDeviceKeys, IMegolmSessionData, IOneTimeKey } from "./@types/crypto"; import type { IDeviceKeys, IMegolmSessionData, IOneTimeKey } from "./@types/crypto";
import { ISyncStateData, SyncApi, SyncApiOptions, SyncState } from "./sync"; import { ISyncStateData, SetPresence, SyncApi, SyncApiOptions, SyncState } from "./sync";
import { import {
EventStatus, EventStatus,
IContent, IContent,
@@ -5531,6 +5531,16 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return getHttpUriForMxc(this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks); 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 * @param opts - Options to apply
* @returns Promise which resolves * @returns Promise which resolves

View File

@@ -27,6 +27,7 @@ import {
SyncApiOptions, SyncApiOptions,
defaultClientOpts, defaultClientOpts,
defaultSyncApiOpts, defaultSyncApiOpts,
SetPresence,
} from "./sync"; } from "./sync";
import { MatrixEvent } from "./models/event"; import { MatrixEvent } from "./models/event";
import { Crypto } from "./crypto"; import { Crypto } from "./crypto";
@@ -463,6 +464,14 @@ export class SlidingSyncSdk {
// TODO // 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 * Returns the current state of this sync object
* @see MatrixClient#event:"sync" * @see MatrixClient#event:"sync"

View File

@@ -168,7 +168,7 @@ export interface ISyncStateData {
fromCache?: boolean; fromCache?: boolean;
} }
enum SetPresence { export enum SetPresence {
Offline = "offline", Offline = "offline",
Online = "online", Online = "online",
Unavailable = "unavailable", Unavailable = "unavailable",
@@ -225,6 +225,7 @@ export class SyncApi {
private notifEvents: MatrixEvent[] = []; // accumulator of sync events in the current sync response private notifEvents: MatrixEvent[] = []; // accumulator of sync events in the current sync response
private failedSyncCount = 0; // Number of consecutive failed /sync requests 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 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. * Construct an entity which is able to sync with a homeserver.
@@ -1009,6 +1010,8 @@ export class SyncApi {
if (this.opts.disablePresence) { if (this.opts.disablePresence) {
qps.set_presence = SetPresence.Offline; qps.set_presence = SetPresence.Offline;
} else if (this.presence !== undefined) {
qps.set_presence = this.presence;
} }
if (syncToken) { if (syncToken) {
@@ -1031,6 +1034,14 @@ export class SyncApi {
return qps; 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> { private async onSyncError(err: MatrixError): Promise<boolean> {
if (!this.running) { if (!this.running) {
debuglog("Sync no longer running: exiting"); debuglog("Sync no longer running: exiting");