2015-09-18 19:39:16 +02:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-18 19:39:16 +02: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.
|
|
|
|
*/
|
|
|
|
|
2017-10-11 18:56:17 +02:00
|
|
|
const MatrixClientPeg = require("./MatrixClientPeg");
|
|
|
|
const dis = require("./dispatcher");
|
2015-09-18 19:39:16 +02:00
|
|
|
|
|
|
|
// Time in ms after that a user is considered as unavailable/away
|
2017-10-11 18:56:17 +02:00
|
|
|
const UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
|
|
|
|
const PRESENCE_STATES = ["online", "offline", "unavailable"];
|
2015-09-18 19:39:16 +02:00
|
|
|
|
2015-10-26 14:54:54 +01:00
|
|
|
class Presence {
|
2015-09-18 19:39:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start listening the user activity to evaluate his presence state.
|
|
|
|
* Any state change will be sent to the Home Server.
|
|
|
|
*/
|
2015-10-26 14:54:54 +01:00
|
|
|
start() {
|
2015-09-18 19:39:16 +02:00
|
|
|
this.running = true;
|
2015-10-26 14:54:54 +01:00
|
|
|
if (undefined === this.state) {
|
2015-09-18 19:39:16 +02:00
|
|
|
this._resetTimer();
|
2015-10-26 14:54:54 +01:00
|
|
|
this.dispatcherRef = dis.register(this._onUserActivity.bind(this));
|
2015-09-18 19:39:16 +02:00
|
|
|
}
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
2015-09-18 19:39:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop tracking user activity
|
|
|
|
*/
|
2015-10-26 14:54:54 +01:00
|
|
|
stop() {
|
2015-09-18 19:39:16 +02:00
|
|
|
this.running = false;
|
2015-10-26 14:54:54 +01:00
|
|
|
if (this.timer) {
|
|
|
|
clearInterval(this.timer);
|
|
|
|
this.timer = undefined;
|
|
|
|
dis.unregister(this.dispatcherRef);
|
2015-09-18 19:39:16 +02:00
|
|
|
}
|
2015-10-26 14:54:54 +01:00
|
|
|
this.state = undefined;
|
|
|
|
}
|
2015-09-18 19:39:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current presence state.
|
|
|
|
* @returns {string} the presence state (see PRESENCE enum)
|
|
|
|
*/
|
2015-10-26 14:54:54 +01:00
|
|
|
getState() {
|
|
|
|
return this.state;
|
|
|
|
}
|
2015-09-18 19:39:16 +02:00
|
|
|
|
2017-10-14 22:37:47 +02:00
|
|
|
/**
|
|
|
|
* Get the current status message.
|
|
|
|
* @returns {String} the status message, may be null
|
|
|
|
*/
|
|
|
|
getStatusMessage() {
|
|
|
|
return this.statusMessage;
|
|
|
|
}
|
|
|
|
|
2015-09-18 19:39:16 +02: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)
|
2017-10-14 22:37:47 +02:00
|
|
|
* @param {String} statusMessage an optional status message for the presence
|
|
|
|
* @param {boolean} maintain true to have this status maintained by this tracker
|
2015-09-18 19:39:16 +02:00
|
|
|
*/
|
2017-10-14 22:37:47 +02:00
|
|
|
setState(newState, statusMessage=null, maintain=false) {
|
|
|
|
if (this.maintain) {
|
|
|
|
// Don't update presence if we're maintaining a particular status
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (newState === this.state && statusMessage === this.statusMessage) {
|
2015-09-18 19:39:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (PRESENCE_STATES.indexOf(newState) === -1) {
|
|
|
|
throw new Error("Bad presence state: " + newState);
|
|
|
|
}
|
|
|
|
if (!this.running) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-11 18:56:17 +02:00
|
|
|
const old_state = this.state;
|
2017-10-14 22:37:47 +02:00
|
|
|
const old_message = this.statusMessage;
|
2015-10-26 14:54:54 +01:00
|
|
|
this.state = newState;
|
2017-10-14 22:37:47 +02:00
|
|
|
this.statusMessage = statusMessage;
|
|
|
|
this.maintain = maintain;
|
2016-01-05 14:24:05 +01:00
|
|
|
|
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
|
|
|
return; // don't try to set presence when a guest; it won't work.
|
|
|
|
}
|
|
|
|
|
2017-10-14 22:37:47 +02:00
|
|
|
const updateContent = {
|
|
|
|
presence: this.state,
|
|
|
|
status_msg: this.statusMessage ? this.statusMessage : '',
|
|
|
|
};
|
|
|
|
|
2017-10-11 18:56:17 +02:00
|
|
|
const self = this;
|
2017-10-14 22:37:47 +02:00
|
|
|
MatrixClientPeg.get().setPresence(updateContent).done(function() {
|
2015-09-18 19:39:16 +02:00
|
|
|
console.log("Presence: %s", newState);
|
2017-10-14 22:37:47 +02:00
|
|
|
|
|
|
|
// We have to dispatch because the js-sdk is unreliable at telling us about our own presence
|
|
|
|
dis.dispatch({action: "self_presence_updated", statusInfo: updateContent});
|
2015-09-18 19:39:16 +02:00
|
|
|
}, function(err) {
|
|
|
|
console.error("Failed to set presence: %s", err);
|
2015-11-18 10:57:14 +01:00
|
|
|
self.state = old_state;
|
2017-10-14 22:37:47 +02:00
|
|
|
self.statusMessage = old_message;
|
2015-09-18 19:39:16 +02:00
|
|
|
});
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
2015-09-18 19:39:16 +02:00
|
|
|
|
2017-10-14 22:37:47 +02:00
|
|
|
stopMaintainingStatus() {
|
|
|
|
this.maintain = false;
|
|
|
|
}
|
|
|
|
|
2015-09-18 19:39:16 +02:00
|
|
|
/**
|
|
|
|
* Callback called when the user made no action on the page for UNAVAILABLE_TIME ms.
|
|
|
|
* @private
|
|
|
|
*/
|
2015-10-26 14:54:54 +01:00
|
|
|
_onUnavailableTimerFire() {
|
2015-09-18 19:39:16 +02:00
|
|
|
this.setState("unavailable");
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
2017-10-14 22:37:47 +02:00
|
|
|
_onUserActivity(payload) {
|
|
|
|
if (payload.action === "sync_state" || payload.action === "self_presence_updated") return;
|
2015-10-26 14:54:54 +01:00
|
|
|
this._resetTimer();
|
|
|
|
}
|
2015-09-18 19:39:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback called when the user made an action on the page
|
|
|
|
* @private
|
|
|
|
*/
|
2015-10-26 14:54:54 +01:00
|
|
|
_resetTimer() {
|
2017-10-11 18:56:17 +02:00
|
|
|
const self = this;
|
2015-09-18 19:39:16 +02:00
|
|
|
this.setState("online");
|
|
|
|
// Re-arm the timer
|
2015-10-26 14:54:54 +01:00
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = setTimeout(function() {
|
2015-09-18 19:39:16 +02:00
|
|
|
self._onUnavailableTimerFire();
|
|
|
|
}, UNAVAILABLE_TIME_MS);
|
2017-01-20 15:22:27 +01:00
|
|
|
}
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new Presence();
|