element-web/src/Presence.ts

115 lines
3.4 KiB
TypeScript
Raw Normal View History

2015-09-18 17:39:16 +00:00
/*
2016-01-07 04:06:39 +00:00
Copyright 2015, 2016 OpenMarket Ltd
Copyright 2018 New Vector Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
2015-09-18 17:39:16 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { logger } from "matrix-js-sdk/src/logger";
2021-06-29 12:11:58 +00:00
import { MatrixClientPeg } from "./MatrixClientPeg";
import dis from "./dispatcher/dispatcher";
2022-12-12 11:24:14 +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
// Time in ms after that a user is considered as unavailable/away
const UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
enum State {
Online = "online",
Offline = "offline",
Unavailable = "unavailable",
}
2015-09-18 17:39:16 +00:00
class Presence {
private unavailableTimer: Timer = null;
private dispatcherRef: string = null;
private state: State = null;
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
*/
public async start(): Promise<void> {
this.unavailableTimer = new Timer(UNAVAILABLE_TIME_MS);
// the user_activity_start action starts the timer
this.dispatcherRef = dis.register(this.onAction);
while (this.unavailableTimer) {
try {
await this.unavailableTimer.finished();
this.setState(State.Unavailable);
2022-12-12 11:24:14 +00:00
} catch (e) {
/* aborted, stop got called */
}
2015-09-18 17:39:16 +00:00
}
}
2015-09-18 17:39:16 +00:00
/**
* Stop tracking user activity
*/
public stop(): void {
if (this.dispatcherRef) {
dis.unregister(this.dispatcherRef);
this.dispatcherRef = null;
}
if (this.unavailableTimer) {
this.unavailableTimer.abort();
this.unavailableTimer = null;
2015-09-18 17:39:16 +00:00
}
}
2015-09-18 17:39:16 +00:00
/**
* Get the current presence state.
* @returns {string} the presence state (see PRESENCE enum)
*/
public getState(): State {
return this.state;
}
2015-09-18 17:39:16 +00:00
private onAction = (payload: ActionPayload): void => {
2022-12-12 11:24:14 +00:00
if (payload.action === "user_activity") {
this.setState(State.Online);
this.unavailableTimer.restart();
}
2021-06-29 12:11:58 +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)
*/
private async setState(newState: State): Promise<void> {
if (newState === this.state) {
2015-09-18 17:39:16 +00:00
return;
}
const oldState = this.state;
this.state = newState;
2016-01-05 13:24:05 +00:00
if (MatrixClientPeg.get().isGuest()) {
return; // don't try to set presence when a guest; it won't work.
}
try {
2021-06-29 12:11:58 +00:00
await MatrixClientPeg.get().setPresence({ presence: this.state });
logger.info("Presence:", newState);
} catch (err) {
logger.error("Failed to set presence:", err);
this.state = oldState;
}
}
}
export default new Presence();