From 7d79021c428594d5b5b95197f7fc259605c2ae7b Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 26 Aug 2014 12:54:43 +0100 Subject: [PATCH 1/5] Added servlet for /rooms/$roomid/[invite|join|leave] --- synapse/rest/room.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/synapse/rest/room.py b/synapse/rest/room.py index 2d681bd89..118208ddc 100644 --- a/synapse/rest/room.py +++ b/synapse/rest/room.py @@ -366,6 +366,21 @@ class RoomTriggerBackfill(RestServlet): defer.returnValue((200, res)) +class RoomMembershipRestServlet(RestServlet): + + def register(self, http_server): + # /rooms/$roomid/[invite|join|leave] + PATTERN = ("/rooms/(?P[^/]*)/" + + "(?Pjoin|invite|leave)") + register_txn_path(self, PATTERN, http_server) + + def on_POST(self, request, room_id, membership_action): + return (200, "Not implemented") + + def on_PUT(self, request, room_id, membership_action, txn_id): + return (200, "Not implemented") + + def _parse_json(request): try: content = json.loads(request.content.read()) @@ -377,6 +392,30 @@ def _parse_json(request): raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON) +def register_txn_path(servlet, regex_string, http_server): + """Registers a transaction-based path. + + This registers two paths: + PUT regex_string/$txnid + POST regex_string + + Args: + regex_string (str): The regex string to register. Must NOT have a + trailing $ as this string will be appended to. + http_server : The http_server to register paths with. + """ + http_server.register_path( + "POST", + client_path_pattern(regex_string + "$"), + servlet.on_POST + ) + http_server.register_path( + "PUT", + client_path_pattern(regex_string + "/(?P[^/]*)$"), + servlet.on_PUT + ) + + def register_servlets(hs, http_server): RoomStateEventRestServlet(hs).register(http_server) MessageRestServlet(hs).register(http_server) @@ -386,3 +425,4 @@ def register_servlets(hs, http_server): RoomMessageListRestServlet(hs).register(http_server) JoinRoomAliasServlet(hs).register(http_server) RoomTriggerBackfill(hs).register(http_server) + RoomMembershipRestServlet(hs).register(http_server) From 732d954f89bb48e885072b9a50cf01c2cdd3e2cd Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 26 Aug 2014 14:13:32 +0100 Subject: [PATCH 2/5] Added basic in-memory REST transaction storage. Only the latest transaction for a given path/access_token combo is stored in order to prevent storing ALL request/response pairs. --- synapse/rest/base.py | 2 + synapse/rest/room.py | 10 +++- synapse/rest/transactions.py | 93 ++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 synapse/rest/transactions.py diff --git a/synapse/rest/base.py b/synapse/rest/base.py index 6a88cbe86..e855d293e 100644 --- a/synapse/rest/base.py +++ b/synapse/rest/base.py @@ -15,6 +15,7 @@ """ This module contains base REST classes for constructing REST servlets. """ from synapse.api.urls import CLIENT_PREFIX +from synapse.rest.transactions import HttpTransactionStore import re @@ -59,6 +60,7 @@ class RestServlet(object): self.handlers = hs.get_handlers() self.event_factory = hs.get_event_factory() self.auth = hs.get_auth() + self.txns = HttpTransactionStore() def register(self, http_server): """ Register this servlet with the given HTTP server. """ diff --git a/synapse/rest/room.py b/synapse/rest/room.py index 118208ddc..4ca7e7e78 100644 --- a/synapse/rest/room.py +++ b/synapse/rest/room.py @@ -375,10 +375,16 @@ class RoomMembershipRestServlet(RestServlet): register_txn_path(self, PATTERN, http_server) def on_POST(self, request, room_id, membership_action): - return (200, "Not implemented") + return (200, "POST Not implemented") def on_PUT(self, request, room_id, membership_action, txn_id): - return (200, "Not implemented") + (code, response) = self.txns.get_client_transaction(request, txn_id) + if code: + return (code, response) + + response = (200, "PUT not implemented txnid %s" % txn_id) + self.txns.store_client_transaction(request, txn_id, response) + return response def _parse_json(request): diff --git a/synapse/rest/transactions.py b/synapse/rest/transactions.py new file mode 100644 index 000000000..10be10e90 --- /dev/null +++ b/synapse/rest/transactions.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- +# 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. + +"""This module contains logic for storing HTTP PUT transactions. This is used +to ensure idempotency when performing PUTs using the REST API.""" +import logging + +logger = logging.getLogger(__name__) + + +class HttpTransactionStore(object): + + def __init__(self): + # { key : (txn_id, response) } + self.transactions = {} + + def get_response(self, key, txn_id): + """Retrieve a response for this request. + + Args: + key (str): A transaction-independent key for this request. Typically + this is a combination of the path (without the transaction id) and + the user's access token. + txn_id (str): The transaction ID for this request + Returns: + A tuple of (HTTP response code, response content) or None. + """ + try: + logger.debug("get_response Key: %s TxnId: %s", key, txn_id) + (last_txn_id, response) = self.transactions[key] + if txn_id == last_txn_id: + return response + except KeyError: + pass + return None + + def store_response(self, key, txn_id, response): + """Stores an HTTP response tuple. + + Args: + key (str): A transaction-independent key for this request. Typically + this is a combination of the path (without the transaction id) and + the user's access token. + txn_id (str): The transaction ID for this request. + response (tuple): A tuple of (HTTP response code, response content) + """ + logger.debug("store_response Key: %s TxnId: %s", key, txn_id) + self.transactions[key] = (txn_id, response) + + def store_client_transaction(self, request, txn_id, response): + """Stores the request/response pair of an HTTP transaction. + + Args: + request (twisted.web.http.Request): The twisted HTTP request. This + request must have the transaction ID as the last path segment. + response (tuple): A tuple of (response code, response dict) + txn_id (str): The transaction ID for this request. + """ + self.store_response(self._get_key(request), txn_id, response) + + def get_client_transaction(self, request, txn_id): + """Retrieves a stored response if there was one. + + Args: + request (twisted.web.http.Request): The twisted HTTP request. This + request must have the transaction ID as the last path segment. + txn_id (str): The transaction ID for this request. + Returns: + The response tuple or (None, None). + """ + response = self.get_response(self._get_key(request), txn_id) + if response is None: + return (None, None) + return response + + def _get_key(self, request): + token = request.args["access_token"][0] + path_without_txn_id = request.path.rsplit("/", 1)[0] + return path_without_txn_id + "/" + token + + From 5c0be8fde3602b9b7396cfdb2d689447f59217f7 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 26 Aug 2014 14:49:44 +0100 Subject: [PATCH 3/5] Implemented /rooms/$roomid/[invite|join|leave] with POST / PUT (incl txn ids) --- synapse/rest/room.py | 36 ++++++++++++++++++++++++++++++------ synapse/rest/transactions.py | 7 +++++-- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/synapse/rest/room.py b/synapse/rest/room.py index 4ca7e7e78..731317227 100644 --- a/synapse/rest/room.py +++ b/synapse/rest/room.py @@ -374,17 +374,41 @@ class RoomMembershipRestServlet(RestServlet): "(?Pjoin|invite|leave)") register_txn_path(self, PATTERN, http_server) + @defer.inlineCallbacks def on_POST(self, request, room_id, membership_action): - return (200, "POST Not implemented") + user = yield self.auth.get_user_by_req(request) + content = _parse_json(request) + + # target user is you unless it is an invite + state_key = user.to_string() + if membership_action == "invite": + if "user_id" not in content: + raise SynapseError(400, "Missing user_id key.") + state_key = content["user_id"] + + event = self.event_factory.create_event( + etype=RoomMemberEvent.TYPE, + content={"membership": unicode(membership_action)}, + room_id=urllib.unquote(room_id), + user_id=user.to_string(), + state_key=state_key + ) + handler = self.handlers.room_member_handler + yield handler.change_membership(event) + defer.returnValue((200, "")) + + @defer.inlineCallbacks def on_PUT(self, request, room_id, membership_action, txn_id): - (code, response) = self.txns.get_client_transaction(request, txn_id) - if code: - return (code, response) + try: + defer.returnValue(self.txns.get_client_transaction(request, txn_id)) + except: + pass + + response = yield self.on_POST(request, room_id, membership_action) - response = (200, "PUT not implemented txnid %s" % txn_id) self.txns.store_client_transaction(request, txn_id, response) - return response + defer.returnValue(response) def _parse_json(request): diff --git a/synapse/rest/transactions.py b/synapse/rest/transactions.py index 10be10e90..b8aa1ef11 100644 --- a/synapse/rest/transactions.py +++ b/synapse/rest/transactions.py @@ -41,6 +41,7 @@ class HttpTransactionStore(object): logger.debug("get_response Key: %s TxnId: %s", key, txn_id) (last_txn_id, response) = self.transactions[key] if txn_id == last_txn_id: + logger.info("get_response: Returning a response for %s", key) return response except KeyError: pass @@ -78,11 +79,13 @@ class HttpTransactionStore(object): request must have the transaction ID as the last path segment. txn_id (str): The transaction ID for this request. Returns: - The response tuple or (None, None). + The response tuple. + Raises: + KeyError if the transaction was not found. """ response = self.get_response(self._get_key(request), txn_id) if response is None: - return (None, None) + raise KeyError("Transaction not found.") return response def _get_key(self, request): From 9ff9caeb744bd75e033a45c429a39f8afe4754b1 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 26 Aug 2014 14:59:31 +0100 Subject: [PATCH 4/5] webclient: Updated to use /rooms/$roomid/[invite|join|leave] --- webclient/components/matrix/matrix-service.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/webclient/components/matrix/matrix-service.js b/webclient/components/matrix/matrix-service.js index b5b1815cf..d376724e4 100644 --- a/webclient/components/matrix/matrix-service.js +++ b/webclient/components/matrix/matrix-service.js @@ -115,7 +115,7 @@ angular.module('matrixService', []) // Joins a room join: function(room_id) { - return this.membershipChange(room_id, config.user_id, "join"); + return this.membershipChange(room_id, undefined, "join"); }, joinAlias: function(room_alias) { @@ -134,18 +134,22 @@ angular.module('matrixService', []) // Leaves a room leave: function(room_id) { - return this.membershipChange(room_id, config.user_id, "leave"); + return this.membershipChange(room_id, undefined, "leave"); }, membershipChange: function(room_id, user_id, membershipValue) { // The REST path spec - var path = "/rooms/$room_id/state/m.room.member/$user_id"; + var path = "/rooms/$room_id/$membership"; path = path.replace("$room_id", encodeURIComponent(room_id)); - path = path.replace("$user_id", encodeURIComponent(user_id)); + path = path.replace("$membership", encodeURIComponent(membershipValue)); - return doRequest("PUT", path, undefined, { - membership: membershipValue - }); + var data = {}; + if (user_id !== undefined) { + data = { user_id: user_id }; + } + + // TODO: Use PUT with transaction IDs + return doRequest("POST", path, undefined, data); }, // Retrieves the room ID corresponding to a room alias From c21fcb3373b10e85fd2533570479cac6caf5da4e Mon Sep 17 00:00:00 2001 From: Emmanuel ROHEE Date: Tue, 26 Aug 2014 16:25:27 +0200 Subject: [PATCH 5/5] 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 @@ +