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