2015-09-18 17:39:16 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-12-20 00:45:24 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-18 17:39:16 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2015-09-18 17:39:16 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-11-01 10:49:47 +00:00
|
|
|
import { SetPresence } from "matrix-js-sdk/src/matrix";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
2020-05-14 02:41:41 +00:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2018-12-11 15:16:00 +00:00
|
|
|
import Timer from "./utils/Timer";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { ActionPayload } from "./dispatcher/payloads";
|
2015-09-18 17:39:16 +00:00
|
|
|
|
2020-10-13 16:39:29 +00:00
|
|
|
// Time in ms after that a user is considered as unavailable/away
|
2017-10-11 16:56:17 +00:00
|
|
|
const UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
|
2020-10-13 16:39:29 +00:00
|
|
|
|
2015-10-26 13:54:54 +00:00
|
|
|
class Presence {
|
2023-02-13 17:01:43 +00:00
|
|
|
private unavailableTimer: Timer | null = null;
|
|
|
|
private dispatcherRef: string | null = null;
|
2023-11-01 10:49:47 +00:00
|
|
|
private state: SetPresence | null = null;
|
2020-10-13 16:39:29 +00:00
|
|
|
|
2015-09-18 17:39:16 +00:00
|
|
|
/**
|
|
|
|
* Start listening the user activity to evaluate his presence state.
|
2019-02-01 00:52:39 +00:00
|
|
|
* Any state change will be sent to the homeserver.
|
2015-09-18 17:39:16 +00:00
|
|
|
*/
|
2023-01-12 13:25:14 +00:00
|
|
|
public async start(): Promise<void> {
|
2020-10-13 16:39:29 +00:00
|
|
|
this.unavailableTimer = new Timer(UNAVAILABLE_TIME_MS);
|
2018-12-11 15:16:00 +00:00
|
|
|
// the user_activity_start action starts the timer
|
2020-10-13 16:39:29 +00:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
|
|
|
while (this.unavailableTimer) {
|
2018-12-11 15:16:00 +00:00
|
|
|
try {
|
2020-10-13 16:39:29 +00:00
|
|
|
await this.unavailableTimer.finished();
|
2023-11-01 10:49:47 +00:00
|
|
|
this.setState(SetPresence.Unavailable);
|
2019-09-18 08:27:43 +00:00
|
|
|
} catch (e) {
|
|
|
|
/* aborted, stop got called */
|
|
|
|
}
|
2015-09-18 17:39:16 +00:00
|
|
|
}
|
2015-10-26 13:54:54 +00:00
|
|
|
}
|
2015-09-18 17:39:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop tracking user activity
|
|
|
|
*/
|
2023-01-12 13:25:14 +00:00
|
|
|
public stop(): void {
|
2020-10-13 16:39:29 +00:00
|
|
|
if (this.dispatcherRef) {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
this.dispatcherRef = null;
|
2018-12-11 15:16:00 +00:00
|
|
|
}
|
2020-10-13 16:39:29 +00:00
|
|
|
if (this.unavailableTimer) {
|
|
|
|
this.unavailableTimer.abort();
|
|
|
|
this.unavailableTimer = null;
|
2015-09-18 17:39:16 +00:00
|
|
|
}
|
2015-10-26 13:54:54 +00:00
|
|
|
}
|
2015-09-18 17:39:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current presence state.
|
|
|
|
* @returns {string} the presence state (see PRESENCE enum)
|
|
|
|
*/
|
2023-11-01 10:49:47 +00:00
|
|
|
public getState(): SetPresence | null {
|
2015-10-26 13:54:54 +00:00
|
|
|
return this.state;
|
|
|
|
}
|
2015-09-18 17:39:16 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onAction = (payload: ActionPayload): void => {
|
2018-12-11 15:25:48 +00:00
|
|
|
if (payload.action === "user_activity") {
|
2023-11-01 10:49:47 +00:00
|
|
|
this.setState(SetPresence.Online);
|
2023-02-13 17:01:43 +00:00
|
|
|
this.unavailableTimer?.restart();
|
2018-12-11 15:16:00 +00:00
|
|
|
}
|
2021-06-29 12:11:58 +00:00
|
|
|
};
|
2018-12-11 15:16:00 +00:00
|
|
|
|
2015-09-18 17:39:16 +00:00
|
|
|
/**
|
|
|
|
* Set the presence state.
|
2019-02-01 00:52:39 +00:00
|
|
|
* If the state has changed, the homeserver will be notified.
|
2015-09-18 17:39:16 +00:00
|
|
|
* @param {string} newState the new presence state (see PRESENCE enum)
|
|
|
|
*/
|
2023-11-01 10:49:47 +00:00
|
|
|
private async setState(newState: SetPresence): Promise<void> {
|
2017-12-25 21:25:13 +00:00
|
|
|
if (newState === this.state) {
|
2015-09-18 17:39:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-13 16:39:29 +00:00
|
|
|
|
2019-09-18 08:27:43 +00:00
|
|
|
const oldState = this.state;
|
2015-10-26 13:54:54 +00:00
|
|
|
this.state = newState;
|
2016-01-05 13:24:05 +00:00
|
|
|
|
2023-06-21 16:29:44 +00:00
|
|
|
if (MatrixClientPeg.safeGet().isGuest()) {
|
2016-01-05 13:24:05 +00:00
|
|
|
return; // don't try to set presence when a guest; it won't work.
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:16:00 +00:00
|
|
|
try {
|
2023-11-01 10:49:47 +00:00
|
|
|
await MatrixClientPeg.safeGet().setSyncPresence(this.state);
|
2023-08-07 08:41:39 +00:00
|
|
|
logger.debug("Presence:", newState);
|
2019-09-18 08:27:43 +00:00
|
|
|
} catch (err) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Failed to set presence:", err);
|
2019-09-18 08:27:43 +00:00
|
|
|
this.state = oldState;
|
2018-03-06 17:48:21 +00:00
|
|
|
}
|
2015-10-26 13:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 00:45:24 +00:00
|
|
|
export default new Presence();
|