Merge pull request #11366 from matrix-org/backport-11365-to-staging

Merge pull request #11365 from matrix-org/andybalaam/revert-to-presence-api
This commit is contained in:
Andy Balaam 2023-08-04 07:10:22 +01:00 committed by GitHub
commit c609829056
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,7 +17,6 @@ limitations under the License.
*/ */
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import { SetPresence } from "matrix-js-sdk/src/sync";
import { MatrixClientPeg } from "./MatrixClientPeg"; import { MatrixClientPeg } from "./MatrixClientPeg";
import dis from "./dispatcher/dispatcher"; import dis from "./dispatcher/dispatcher";
@ -27,10 +26,16 @@ import { ActionPayload } from "./dispatcher/payloads";
// Time in ms after that a user is considered as unavailable/away // Time in ms after that a user is considered as unavailable/away
const UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins const UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
enum State {
Online = "online",
Offline = "offline",
Unavailable = "unavailable",
}
class Presence { class Presence {
private unavailableTimer: Timer | null = null; private unavailableTimer: Timer | null = null;
private dispatcherRef: string | null = null; private dispatcherRef: string | null = null;
private state: SetPresence | null = null; private state: State | null = null;
/** /**
* Start listening the user activity to evaluate his presence state. * Start listening the user activity to evaluate his presence state.
@ -43,7 +48,7 @@ class Presence {
while (this.unavailableTimer) { while (this.unavailableTimer) {
try { try {
await this.unavailableTimer.finished(); await this.unavailableTimer.finished();
this.setState(SetPresence.Unavailable); this.setState(State.Unavailable);
} catch (e) { } catch (e) {
/* aborted, stop got called */ /* aborted, stop got called */
} }
@ -68,13 +73,13 @@ class Presence {
* Get the current presence state. * Get the current presence state.
* @returns {string} the presence state (see PRESENCE enum) * @returns {string} the presence state (see PRESENCE enum)
*/ */
public getState(): SetPresence | null { public getState(): State | null {
return this.state; return this.state;
} }
private onAction = (payload: ActionPayload): void => { private onAction = (payload: ActionPayload): void => {
if (payload.action === "user_activity") { if (payload.action === "user_activity") {
this.setState(SetPresence.Online); this.setState(State.Online);
this.unavailableTimer?.restart(); this.unavailableTimer?.restart();
} }
}; };
@ -84,7 +89,7 @@ class Presence {
* If the state has changed, the homeserver will be notified. * If the state has changed, the homeserver will be notified.
* @param {string} newState the new presence state (see PRESENCE enum) * @param {string} newState the new presence state (see PRESENCE enum)
*/ */
private async setState(newState: SetPresence): Promise<void> { private async setState(newState: State): Promise<void> {
if (newState === this.state) { if (newState === this.state) {
return; return;
} }
@ -97,7 +102,7 @@ class Presence {
} }
try { try {
await MatrixClientPeg.safeGet().setSyncPresence(this.state); await MatrixClientPeg.safeGet().setPresence({ presence: this.state });
logger.info("Presence:", newState); logger.info("Presence:", newState);
} catch (err) { } catch (err) {
logger.error("Failed to set presence:", err); logger.error("Failed to set presence:", err);