From c21fcb3373b10e85fd2533570479cac6caf5da4e Mon Sep 17 00:00:00 2001 From: Emmanuel ROHEE Date: Tue, 26 Aug 2014 16:25:27 +0200 Subject: [PATCH] Determine and send user presence state --- webclient/app-controller.js | 14 ++- webclient/components/matrix/matrix-service.js | 17 +++ .../components/matrix/presence-service.js | 113 ++++++++++++++++++ webclient/index.html | 1 + 4 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 webclient/components/matrix/presence-service.js diff --git a/webclient/app-controller.js b/webclient/app-controller.js index 84cb94dc7..f210119e2 100644 --- a/webclient/app-controller.js +++ b/webclient/app-controller.js @@ -20,9 +20,9 @@ limitations under the License. 'use strict'; -angular.module('MatrixWebClientController', ['matrixService']) -.controller('MatrixWebClientController', ['$scope', '$location', '$rootScope', 'matrixService', 'eventStreamService', - function($scope, $location, $rootScope, matrixService, eventStreamService) { +angular.module('MatrixWebClientController', ['matrixService', 'mPresence', 'eventStreamService']) +.controller('MatrixWebClientController', ['$scope', '$location', '$rootScope', 'matrixService', 'mPresence', 'eventStreamService', + function($scope, $location, $rootScope, matrixService, mPresence, eventStreamService) { // Check current URL to avoid to display the logout button on the login page $scope.location = $location.path(); @@ -34,6 +34,7 @@ angular.module('MatrixWebClientController', ['matrixService']) if (matrixService.isUserLoggedIn()) { // eventStreamService.resume(); + mPresence.start(); } $scope.go = function(url) { @@ -42,9 +43,13 @@ angular.module('MatrixWebClientController', ['matrixService']) // Logs the user out $scope.logout = function() { + // kill the event stream eventStreamService.stop(); - + + // Do not update presence anymore + mPresence.stop(); + // Clean permanent data matrixService.setConfig({}); matrixService.saveConfig(); @@ -67,7 +72,6 @@ angular.module('MatrixWebClientController', ['matrixService']) } }; - }]); diff --git a/webclient/components/matrix/matrix-service.js b/webclient/components/matrix/matrix-service.js index d376724e4..237dd6d8a 100644 --- a/webclient/components/matrix/matrix-service.js +++ b/webclient/components/matrix/matrix-service.js @@ -358,6 +358,23 @@ angular.module('matrixService', []) return false; } }, + + // 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 ******/ diff --git a/webclient/components/matrix/presence-service.js b/webclient/components/matrix/presence-service.js new file mode 100644 index 000000000..6a1edcaf4 --- /dev/null +++ b/webclient/components/matrix/presence-service.js @@ -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); + } + +}]); + + diff --git a/webclient/index.html b/webclient/index.html index 938d70c86..6031036e9 100644 --- a/webclient/index.html +++ b/webclient/index.html @@ -26,6 +26,7 @@ +