element-web/src/Presence.js

121 lines
3.3 KiB
JavaScript
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
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.
*/
const MatrixClientPeg = require("./MatrixClientPeg");
const dis = require("./dispatcher");
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
const PRESENCE_STATES = ["online", "offline", "unavailable"];
2015-09-18 17:39:16 +00:00
class Presence {
2015-09-18 17:39:16 +00:00
/**
* Start listening the user activity to evaluate his presence state.
* Any state change will be sent to the Home Server.
*/
start() {
2015-09-18 17:39:16 +00:00
this.running = true;
if (undefined === this.state) {
2015-09-18 17:39:16 +00:00
this._resetTimer();
this.dispatcherRef = dis.register(this._onAction.bind(this));
2015-09-18 17:39:16 +00:00
}
}
2015-09-18 17:39:16 +00:00
/**
* Stop tracking user activity
*/
stop() {
2015-09-18 17:39:16 +00:00
this.running = false;
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
dis.unregister(this.dispatcherRef);
2015-09-18 17:39:16 +00:00
}
this.state = undefined;
}
2015-09-18 17:39:16 +00:00
/**
* Get the current presence state.
* @returns {string} the presence state (see PRESENCE enum)
*/
getState() {
return this.state;
}
2015-09-18 17:39:16 +00:00
/**
* Set the presence state.
* If the state has changed, the Home Server will be notified.
* @param {string} newState the new presence state (see PRESENCE enum)
*/
setState(newState) {
if (newState === this.state) {
2015-09-18 17:39:16 +00:00
return;
}
if (PRESENCE_STATES.indexOf(newState) === -1) {
throw new Error("Bad presence state: " + newState);
}
if (!this.running) {
return;
}
const old_state = 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.
}
const self = this;
MatrixClientPeg.get().setPresence(this.state).done(function() {
2015-09-18 17:39:16 +00:00
console.log("Presence: %s", newState);
}, function(err) {
console.error("Failed to set presence: %s", err);
self.state = old_state;
2015-09-18 17:39:16 +00:00
});
}
2015-09-18 17:39:16 +00:00
/**
* Callback called when the user made no action on the page for UNAVAILABLE_TIME ms.
* @private
*/
_onUnavailableTimerFire() {
2015-09-18 17:39:16 +00:00
this.setState("unavailable");
}
_onAction(payload) {
if (payload.action === "user_activity") {
this._resetTimer();
}
}
2015-09-18 17:39:16 +00:00
/**
* Callback called when the user made an action on the page
* @private
*/
_resetTimer() {
const self = this;
2015-09-18 17:39:16 +00:00
this.setState("online");
// Re-arm the timer
clearTimeout(this.timer);
this.timer = setTimeout(function() {
2015-09-18 17:39:16 +00:00
self._onUnavailableTimerFire();
}, UNAVAILABLE_TIME_MS);
}
}
module.exports = new Presence();