Determine and send user presence state

This commit is contained in:
Emmanuel ROHEE 2014-08-26 16:25:27 +02:00
parent b07bc9bdbd
commit c21fcb3373
4 changed files with 140 additions and 5 deletions

View file

@ -20,9 +20,9 @@ limitations under the License.
'use strict'; 'use strict';
angular.module('MatrixWebClientController', ['matrixService']) angular.module('MatrixWebClientController', ['matrixService', 'mPresence', 'eventStreamService'])
.controller('MatrixWebClientController', ['$scope', '$location', '$rootScope', 'matrixService', 'eventStreamService', .controller('MatrixWebClientController', ['$scope', '$location', '$rootScope', 'matrixService', 'mPresence', 'eventStreamService',
function($scope, $location, $rootScope, matrixService, eventStreamService) { function($scope, $location, $rootScope, matrixService, mPresence, eventStreamService) {
// Check current URL to avoid to display the logout button on the login page // Check current URL to avoid to display the logout button on the login page
$scope.location = $location.path(); $scope.location = $location.path();
@ -34,6 +34,7 @@ angular.module('MatrixWebClientController', ['matrixService'])
if (matrixService.isUserLoggedIn()) { if (matrixService.isUserLoggedIn()) {
// eventStreamService.resume(); // eventStreamService.resume();
mPresence.start();
} }
$scope.go = function(url) { $scope.go = function(url) {
@ -42,9 +43,13 @@ angular.module('MatrixWebClientController', ['matrixService'])
// Logs the user out // Logs the user out
$scope.logout = function() { $scope.logout = function() {
// kill the event stream // kill the event stream
eventStreamService.stop(); eventStreamService.stop();
// Do not update presence anymore
mPresence.stop();
// Clean permanent data // Clean permanent data
matrixService.setConfig({}); matrixService.setConfig({});
matrixService.saveConfig(); matrixService.saveConfig();
@ -67,7 +72,6 @@ angular.module('MatrixWebClientController', ['matrixService'])
} }
}; };
}]); }]);

View file

@ -359,6 +359,23 @@ angular.module('matrixService', [])
} }
}, },
// Enum of presence state
presence: {
offline: "offline",
unavailable: "unavailable",
online: "online",
free_for_chat: "free_for_chat"
},
// Set the logged in user presence state
setUserPresence: function(presence) {
var path = "/presence/$user_id/status";
path = path.replace("$user_id", config.user_id);
return doRequest("PUT", path, undefined, {
state: presence
});
},
/****** Permanent storage of user information ******/ /****** Permanent storage of user information ******/
// Returns the current config // Returns the current config

View file

@ -0,0 +1,113 @@
/*
Copyright 2014 matrix.org
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.
*/
'use strict';
/*
* This service tracks user activity on the page to determine his presence state.
* Any state change will be sent to the Home Server.
*/
angular.module('mPresence', [])
.service('mPresence', ['$timeout', 'matrixService', function ($timeout, matrixService) {
// Time in ms after that a user is considered as offline/away
var OFFLINE_TIME = 5 * 60000; // 5 mins
// The current presence state
var state = undefined;
var self =this;
var timer;
/**
* Start listening the user activity to evaluate his presence state.
* Any state change will be sent to the Home Server.
*/
this.start = function() {
if (undefined === state) {
// The user is online if he moves the mouser or press a key
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;
resetTimer();
}
};
/**
* Stop tracking user activity
*/
this.stop = function() {
if (timer) {
$timeout.cancel(timer);
timer = undefined;
}
state = undefined;
};
/**
* Get the current presence state.
* @returns {matrixService.presence} the presence state
*/
this.getState = function() {
return state;
};
/**
* Set the presence state.
* If the state has changed, the Home Server will be notified.
* @param {matrixService.presence} newState the new presence state
*/
this.setState = function(newState) {
if (newState !== state) {
console.log("mPresence - New state: " + newState);
state = newState;
// Inform the HS on the new user state
matrixService.setUserPresence(state).then(
function() {
},
function(error) {
console.log("mPresence - Failed to send new presence state: " + JSON.stringify(error));
});
}
};
/**
* Callback called when the user made no action on the page for OFFLINE_TIME ms.
* @private
*/
function onOfflineTimerFire() {
self.setState(matrixService.presence.offline);
}
/**
* Callback called when the user made an action on the page
* @private
*/
function resetTimer() {
// User is still here
self.setState(matrixService.presence.online);
// Re-arm the timer
$timeout.cancel(timer);
timer = $timeout(onOfflineTimerFire, OFFLINE_TIME);
}
}]);

View file

@ -26,6 +26,7 @@
<script src="components/matrix/matrix-service.js"></script> <script src="components/matrix/matrix-service.js"></script>
<script src="components/matrix/event-stream-service.js"></script> <script src="components/matrix/event-stream-service.js"></script>
<script src="components/matrix/event-handler-service.js"></script> <script src="components/matrix/event-handler-service.js"></script>
<script src="components/matrix/presence-service.js"></script>
<script src="components/fileInput/file-input-directive.js"></script> <script src="components/fileInput/file-input-directive.js"></script>
<script src="components/fileUpload/file-upload-service.js"></script> <script src="components/fileUpload/file-upload-service.js"></script>
<script src="components/utilities/utilities-service.js"></script> <script src="components/utilities/utilities-service.js"></script>