Skip to content

Commit 9d110d5

Browse files
committed
Manage presence of the client (same semantics as angular)
1 parent bcab2f2 commit 9d110d5

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

‎src/Presence.js‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
"use strict";
17+
var MatrixClientPeg = require("./MatrixClientPeg");
18+
19+
// Time in ms after that a user is considered as unavailable/away
20+
var UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
21+
var PRESENCE_STATES = ["online", "offline", "unavailable"];
22+
23+
// The current presence state
24+
var state, timer;
25+
26+
module.exports = {
27+
28+
/**
29+
* Start listening the user activity to evaluate his presence state.
30+
* Any state change will be sent to the Home Server.
31+
*/
32+
start: function() {
33+
var self = this;
34+
this.running = true;
35+
if (undefined === state) {
36+
// The user is online if they move the mouse or press a key
37+
document.onmousemove = function() { self._resetTimer(); };
38+
document.onkeypress = function() { self._resetTimer(); };
39+
this._resetTimer();
40+
}
41+
},
42+
43+
/**
44+
* Stop tracking user activity
45+
*/
46+
stop: function() {
47+
this.running = false;
48+
if (timer) {
49+
clearTimeout(timer);
50+
timer = undefined;
51+
}
52+
state = undefined;
53+
},
54+
55+
/**
56+
* Get the current presence state.
57+
* @returns {string} the presence state (see PRESENCE enum)
58+
*/
59+
getState: function() {
60+
return state;
61+
},
62+
63+
/**
64+
* Set the presence state.
65+
* If the state has changed, the Home Server will be notified.
66+
* @param {string} newState the new presence state (see PRESENCE enum)
67+
*/
68+
setState: function(newState) {
69+
if (newState === state) {
70+
return;
71+
}
72+
if (PRESENCE_STATES.indexOf(newState) === -1) {
73+
throw new Error("Bad presence state: " + newState);
74+
}
75+
if (!this.running) {
76+
return;
77+
}
78+
state = newState;
79+
MatrixClientPeg.get().setPresence(state).catch(function(err) {
80+
console.error("Failed to set presence: %s", err);
81+
});
82+
},
83+
84+
/**
85+
* Callback called when the user made no action on the page for UNAVAILABLE_TIME ms.
86+
* @private
87+
*/
88+
_onUnavailableTimerFire: function() {
89+
this.setState("unavailable");
90+
},
91+
92+
/**
93+
* Callback called when the user made an action on the page
94+
* @private
95+
*/
96+
_resetTimer: function() {
97+
var self = this;
98+
this.setState("online");
99+
// Re-arm the timer
100+
clearTimeout(timer);
101+
timer = setTimeout(function() {
102+
self._onUnavailableTimerFire();
103+
}, UNAVAILABLE_TIME_MS);
104+
}
105+
};

‎src/controllers/pages/MatrixChat.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var Loader = require("react-loader");
2121

2222
var MatrixClientPeg = require("../../MatrixClientPeg");
2323
var RoomListSorter = require("../../RoomListSorter");
24-
24+
var Presence = require("../../Presence");
2525
var dis = require("../../dispatcher");
2626

2727
var ComponentBroker = require('../../ComponentBroker');
@@ -89,6 +89,7 @@ module.exports = {
8989
window.localStorage.clear();
9090
}
9191
Notifier.stop();
92+
Presence.stop();
9293
MatrixClientPeg.get().removeAllListeners();
9394
MatrixClientPeg.replace(null);
9495
break;
@@ -187,6 +188,7 @@ module.exports = {
187188
});
188189
});
189190
Notifier.start();
191+
Presence.start();
190192
cli.startClient();
191193
},
192194

0 commit comments

Comments
 (0)