2015-09-18 20:39:16 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2018-03-06 20:48:21 +03:00
|
|
|
Copyright 2018 New Vector Ltd
|
2019-12-20 03:45:24 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2015-09-18 20:39:16 +03: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.
|
|
|
|
*/
|
|
|
|
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from "./MatrixClientPeg";
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2018-12-11 18:16:00 +03:00
|
|
|
import Timer from './utils/Timer';
|
2015-09-18 20:39:16 +03:00
|
|
|
|
|
|
|
// Time in ms after that a user is considered as unavailable/away
|
2017-10-11 19:56:17 +03:00
|
|
|
const UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
|
|
|
|
const PRESENCE_STATES = ["online", "offline", "unavailable"];
|
2015-09-18 20:39:16 +03:00
|
|
|
|
2015-10-26 16:54:54 +03:00
|
|
|
class Presence {
|
2018-12-11 18:16:00 +03:00
|
|
|
constructor() {
|
|
|
|
this._activitySignal = null;
|
|
|
|
this._unavailableTimer = null;
|
|
|
|
this._onAction = this._onAction.bind(this);
|
|
|
|
this._dispatcherRef = null;
|
|
|
|
}
|
2015-09-18 20:39:16 +03:00
|
|
|
/**
|
|
|
|
* Start listening the user activity to evaluate his presence state.
|
2019-02-01 03:52:39 +03:00
|
|
|
* Any state change will be sent to the homeserver.
|
2015-09-18 20:39:16 +03:00
|
|
|
*/
|
2018-12-11 18:16:00 +03:00
|
|
|
async start() {
|
|
|
|
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("unavailable");
|
2019-09-18 11:27:43 +03:00
|
|
|
} catch (e) { /* aborted, stop got called */ }
|
2015-09-18 20:39:16 +03:00
|
|
|
}
|
2015-10-26 16:54:54 +03:00
|
|
|
}
|
2015-09-18 20:39:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop tracking user activity
|
|
|
|
*/
|
2015-10-26 16:54:54 +03:00
|
|
|
stop() {
|
2018-12-11 18:16:00 +03:00
|
|
|
if (this._dispatcherRef) {
|
|
|
|
dis.unregister(this._dispatcherRef);
|
|
|
|
this._dispatcherRef = null;
|
|
|
|
}
|
|
|
|
if (this._unavailableTimer) {
|
|
|
|
this._unavailableTimer.abort();
|
|
|
|
this._unavailableTimer = null;
|
2015-09-18 20:39:16 +03:00
|
|
|
}
|
2015-10-26 16:54:54 +03:00
|
|
|
}
|
2015-09-18 20:39:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current presence state.
|
|
|
|
* @returns {string} the presence state (see PRESENCE enum)
|
|
|
|
*/
|
2015-10-26 16:54:54 +03:00
|
|
|
getState() {
|
|
|
|
return this.state;
|
|
|
|
}
|
2015-09-18 20:39:16 +03:00
|
|
|
|
2018-12-11 18:16:00 +03:00
|
|
|
_onAction(payload) {
|
2018-12-11 18:25:48 +03:00
|
|
|
if (payload.action === 'user_activity') {
|
2018-12-11 18:16:00 +03:00
|
|
|
this.setState("online");
|
|
|
|
this._unavailableTimer.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 20:39:16 +03:00
|
|
|
/**
|
|
|
|
* Set the presence state.
|
2019-02-01 03:52:39 +03:00
|
|
|
* If the state has changed, the homeserver will be notified.
|
2015-09-18 20:39:16 +03:00
|
|
|
* @param {string} newState the new presence state (see PRESENCE enum)
|
|
|
|
*/
|
2018-12-11 18:16:00 +03:00
|
|
|
async setState(newState) {
|
2017-12-26 00:25:13 +03:00
|
|
|
if (newState === this.state) {
|
2015-09-18 20:39:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (PRESENCE_STATES.indexOf(newState) === -1) {
|
|
|
|
throw new Error("Bad presence state: " + newState);
|
|
|
|
}
|
2019-09-18 11:27:43 +03:00
|
|
|
const oldState = this.state;
|
2015-10-26 16:54:54 +03:00
|
|
|
this.state = newState;
|
2016-01-05 16:24:05 +03:00
|
|
|
|
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
|
|
|
return; // don't try to set presence when a guest; it won't work.
|
|
|
|
}
|
|
|
|
|
2018-12-11 18:16:00 +03:00
|
|
|
try {
|
|
|
|
await MatrixClientPeg.get().setPresence(this.state);
|
2019-11-26 04:14:03 +03:00
|
|
|
console.info("Presence: %s", newState);
|
2019-09-18 11:27:43 +03:00
|
|
|
} catch (err) {
|
2015-09-18 20:39:16 +03:00
|
|
|
console.error("Failed to set presence: %s", err);
|
2019-09-18 11:27:43 +03:00
|
|
|
this.state = oldState;
|
2018-03-06 20:48:21 +03:00
|
|
|
}
|
2015-10-26 16:54:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
export default new Presence();
|