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+ } ;
0 commit comments