summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
Diffstat (limited to 'synapse')
-rwxr-xr-xsynapse/app/homeserver.py65
-rw-r--r--synapse/handlers/__init__.py2
-rw-r--r--synapse/handlers/typing.py146
-rw-r--r--synapse/rest/__init__.py4
-rw-r--r--synapse/rest/directory.py2
-rw-r--r--synapse/rest/presence.py2
-rw-r--r--synapse/rest/public.py33
-rw-r--r--synapse/rest/room.py89
-rw-r--r--synapse/server.py5
9 files changed, 273 insertions, 75 deletions
diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py
index 2d0340f0f1..6d292ccf9a 100755
--- a/synapse/app/homeserver.py
+++ b/synapse/app/homeserver.py
@@ -43,6 +43,22 @@ import re
 logger = logging.getLogger(__name__)
 
 
+SCHEMAS = [
+    "transactions",
+    "pdu",
+    "users",
+    "profiles",
+    "presence",
+    "im",
+    "room_aliases",
+]
+
+
+# Remember to update this number every time an incompatible change is made to
+# database schema files, so the users will be informed on server restarts.
+SCHEMA_VERSION = 1
+
+
 class SynapseHomeServer(HomeServer):
 
     def build_http_client(self):
@@ -65,31 +81,39 @@ class SynapseHomeServer(HomeServer):
         don't have to worry about overwriting existing content.
         """
         logging.info("Preparing database: %s...", self.db_name)
-        pool = adbapi.ConnectionPool(
-            'sqlite3', self.db_name, check_same_thread=False,
-            cp_min=1, cp_max=1)
 
-        schemas = [
-            "transactions",
-            "pdu",
-            "users",
-            "profiles",
-            "presence",
-            "im",
-            "room_aliases",
-        ]
+        with sqlite3.connect(self.db_name) as db_conn:
+            c = db_conn.cursor()
+            c.execute("PRAGMA user_version")
+            row = c.fetchone()
 
-        for sql_loc in schemas:
-            sql_script = read_schema(sql_loc)
+            if row and row[0]:
+                user_version = row[0]
 
-            with sqlite3.connect(self.db_name) as db_conn:
-                c = db_conn.cursor()
-                c.executescript(sql_script)
-                c.close()
-                db_conn.commit()
+                if user_version < SCHEMA_VERSION:
+                    # TODO(paul): add some kind of intelligent fixup here
+                    raise ValueError("Cannot use this database as the " +
+                        "schema version (%d) does not match (%d)" %
+                        (user_version, SCHEMA_VERSION)
+                    )
+
+            else:
+                for sql_loc in SCHEMAS:
+                    sql_script = read_schema(sql_loc)
+
+                    c.executescript(sql_script)
+                    db_conn.commit()
+
+                c.execute("PRAGMA user_version = %d" % SCHEMA_VERSION)
+
+            c.close()
 
         logging.info("Database prepared in %s.", self.db_name)
 
+        pool = adbapi.ConnectionPool(
+            'sqlite3', self.db_name, check_same_thread=False,
+            cp_min=1, cp_max=1)
+
         return pool
 
     def create_resource_tree(self, web_client, redirect_root_to_web_client):
@@ -184,6 +208,7 @@ class SynapseHomeServer(HomeServer):
 
     def start_listening(self, port):
         reactor.listenTCP(port, Site(self.root_resource))
+        logger.info("Synapse now listening on port %d", port)
 
 
 def setup_logging(verbosity=0, filename=None, config_path=None):
@@ -278,7 +303,7 @@ def setup():
         redirect_root_to_web_client=True)
     hs.start_listening(args.port)
 
-    hs.build_db_pool()
+    hs.get_db_pool()
 
     if args.manhole:
         f = twisted.manhole.telnet.ShellFactory()
diff --git a/synapse/handlers/__init__.py b/synapse/handlers/__init__.py
index 7417a02cea..b645977767 100644
--- a/synapse/handlers/__init__.py
+++ b/synapse/handlers/__init__.py
@@ -23,6 +23,7 @@ from .login import LoginHandler
 from .profile import ProfileHandler
 from .presence import PresenceHandler
 from .directory import DirectoryHandler
+from .typing import TypingNotificationHandler
 
 
 class Handlers(object):
@@ -46,3 +47,4 @@ class Handlers(object):
         self.room_list_handler = RoomListHandler(hs)
         self.login_handler = LoginHandler(hs)
         self.directory_handler = DirectoryHandler(hs)
+        self.typing_notification_handler = TypingNotificationHandler(hs)
diff --git a/synapse/handlers/typing.py b/synapse/handlers/typing.py
new file mode 100644
index 0000000000..9d38a7336e
--- /dev/null
+++ b/synapse/handlers/typing.py
@@ -0,0 +1,146 @@
+# -*- 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.
+
+from twisted.internet import defer
+
+from ._base import BaseHandler
+
+import logging
+
+from collections import namedtuple
+
+
+logger = logging.getLogger(__name__)
+
+
+# A tiny object useful for storing a user's membership in a room, as a mapping
+# key
+RoomMember = namedtuple("RoomMember", ("room_id", "user"))
+
+
+class TypingNotificationHandler(BaseHandler):
+    def __init__(self, hs):
+        super(TypingNotificationHandler, self).__init__(hs)
+
+        self.homeserver = hs
+
+        self.clock = hs.get_clock()
+
+        self.federation = hs.get_replication_layer()
+
+        self.federation.register_edu_handler("m.typing", self._recv_edu)
+
+        self._member_typing_until = {}
+
+    @defer.inlineCallbacks
+    def started_typing(self, target_user, auth_user, room_id, timeout):
+        if not target_user.is_mine:
+            raise SynapseError(400, "User is not hosted on this Home Server")
+
+        if target_user != auth_user:
+            raise AuthError(400, "Cannot set another user's typing state")
+
+        until = self.clock.time_msec() + timeout
+        member = RoomMember(room_id=room_id, user=target_user)
+
+        was_present = member in self._member_typing_until
+
+        self._member_typing_until[member] = until
+
+        if was_present:
+            # No point sending another notification
+            defer.returnValue(None)
+
+        yield self._push_update(
+            room_id=room_id,
+            user=target_user,
+            typing=True,
+        )
+
+    @defer.inlineCallbacks
+    def stopped_typing(self, target_user, auth_user, room_id):
+        if not target_user.is_mine:
+            raise SynapseError(400, "User is not hosted on this Home Server")
+
+        if target_user != auth_user:
+            raise AuthError(400, "Cannot set another user's typing state")
+
+        member = RoomMember(room_id=room_id, user=target_user)
+
+        if member not in self._member_typing_until:
+            # No point
+            defer.returnValue(None)
+
+        yield self._push_update(
+            room_id=room_id,
+            user=target_user,
+            typing=False,
+        )
+
+    @defer.inlineCallbacks
+    def _push_update(self, room_id, user, typing):
+        localusers = set()
+        remotedomains = set()
+
+        rm_handler = self.homeserver.get_handlers().room_member_handler
+        yield rm_handler.fetch_room_distributions_into(room_id,
+                localusers=localusers, remotedomains=remotedomains,
+                ignore_user=user)
+
+        for u in localusers:
+            self.push_update_to_clients(
+                room_id=room_id,
+                observer_user=u,
+                observed_user=user,
+                typing=typing,
+            )
+
+        deferreds = []
+        for domain in remotedomains:
+            deferreds.append(self.federation.send_edu(
+                destination=domain,
+                edu_type="m.typing",
+                content={
+                    "room_id": room_id,
+                    "user_id": user.to_string(),
+                    "typing": typing,
+                },
+            ))
+
+        yield defer.DeferredList(deferreds, consumeErrors=False)
+
+    @defer.inlineCallbacks
+    def _recv_edu(self, origin, content):
+        room_id = content["room_id"]
+        user = self.homeserver.parse_userid(content["user_id"])
+
+        localusers = set()
+
+        rm_handler = self.homeserver.get_handlers().room_member_handler
+        yield rm_handler.fetch_room_distributions_into(room_id,
+                localusers=localusers)
+
+        for u in localusers:
+            self.push_update_to_clients(
+                room_id=room_id,
+                observer_user=u,
+                observed_user=user,
+                typing=content["typing"]
+            )
+
+    def push_update_to_clients(self, room_id, observer_user, observed_user,
+            typing):
+        # TODO(paul) steal this from presence.py
+        pass
diff --git a/synapse/rest/__init__.py b/synapse/rest/__init__.py
index 47896612ce..f33024e72a 100644
--- a/synapse/rest/__init__.py
+++ b/synapse/rest/__init__.py
@@ -15,8 +15,7 @@
 
 
 from . import (
-    room, events, register, login, profile, public, presence, initial_sync,
-    directory
+    room, events, register, login, profile, presence, initial_sync, directory
 )
 
 
@@ -40,7 +39,6 @@ class RestServletFactory(object):
         register.register_servlets(hs, client_resource)
         login.register_servlets(hs, client_resource)
         profile.register_servlets(hs, client_resource)
-        public.register_servlets(hs, client_resource)
         presence.register_servlets(hs, client_resource)
         initial_sync.register_servlets(hs, client_resource)
         directory.register_servlets(hs, client_resource)
diff --git a/synapse/rest/directory.py b/synapse/rest/directory.py
index be9a3f5f9f..dc347652a0 100644
--- a/synapse/rest/directory.py
+++ b/synapse/rest/directory.py
@@ -31,7 +31,7 @@ def register_servlets(hs, http_server):
 
 
 class ClientDirectoryServer(RestServlet):
-    PATTERN = client_path_pattern("/ds/room/(?P<room_alias>[^/]*)$")
+    PATTERN = client_path_pattern("/directory/room/(?P<room_alias>[^/]*)$")
 
     @defer.inlineCallbacks
     def on_GET(self, request, room_alias):
diff --git a/synapse/rest/presence.py b/synapse/rest/presence.py
index 6043848595..e013b20853 100644
--- a/synapse/rest/presence.py
+++ b/synapse/rest/presence.py
@@ -68,7 +68,7 @@ class PresenceStatusRestServlet(RestServlet):
 
 
 class PresenceListRestServlet(RestServlet):
-    PATTERN = client_path_pattern("/presence_list/(?P<user_id>[^/]*)")
+    PATTERN = client_path_pattern("/presence/list/(?P<user_id>[^/]*)")
 
     @defer.inlineCallbacks
     def on_GET(self, request, user_id):
diff --git a/synapse/rest/public.py b/synapse/rest/public.py
deleted file mode 100644
index 3430c8049f..0000000000
--- a/synapse/rest/public.py
+++ /dev/null
@@ -1,33 +0,0 @@
-# -*- 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 REST servlets to do with public paths: /public"""
-from twisted.internet import defer
-
-from base import RestServlet, client_path_pattern
-
-
-class PublicRoomListRestServlet(RestServlet):
-    PATTERN = client_path_pattern("/public/rooms$")
-
-    @defer.inlineCallbacks
-    def on_GET(self, request):
-        handler = self.handlers.room_list_handler
-        data = yield handler.get_public_room_list()
-        defer.returnValue((200, data))
-
-
-def register_servlets(hs, http_server):
-    PublicRoomListRestServlet(hs).register(http_server)
diff --git a/synapse/rest/room.py b/synapse/rest/room.py
index b8d5cb87fd..ebe4e24432 100644
--- a/synapse/rest/room.py
+++ b/synapse/rest/room.py
@@ -34,31 +34,28 @@ class RoomCreateRestServlet(RestServlet):
     # No PATTERN; we have custom dispatch rules here
 
     def register(self, http_server):
-        # /rooms OR /rooms/<roomid>
-        http_server.register_path("POST",
-                                  client_path_pattern("/rooms$"),
-                                  self.on_POST)
-        http_server.register_path("PUT",
-                                  client_path_pattern(
-                                      "/rooms/(?P<room_id>[^/]*)$"),
-                                  self.on_PUT)
+        PATTERN = "/createRoom"
+        register_txn_path(self, PATTERN, http_server)
         # define CORS for all of /rooms in RoomCreateRestServlet for simplicity
         http_server.register_path("OPTIONS",
                                   client_path_pattern("/rooms(?:/.*)?$"),
                                   self.on_OPTIONS)
+        # define CORS for /createRoom[/txnid]
+        http_server.register_path("OPTIONS",
+                                  client_path_pattern("/createRoom(?:/.*)?$"),
+                                  self.on_OPTIONS)
 
     @defer.inlineCallbacks
-    def on_PUT(self, request, room_id):
-        room_id = urllib.unquote(room_id)
-        auth_user = yield self.auth.get_user_by_req(request)
+    def on_PUT(self, request, txn_id):
+        try:
+            defer.returnValue(self.txns.get_client_transaction(request, txn_id))
+        except KeyError:
+            pass
 
-        if not room_id:
-            raise SynapseError(400, "PUT must specify a room ID")
+        response = yield self.on_POST(request)
 
-        room_config = self.get_room_config(request)
-        info = yield self.make_room(room_config, auth_user, room_id)
-        room_config.update(info)
-        defer.returnValue((200, info))
+        self.txns.store_client_transaction(request, txn_id, response)
+        defer.returnValue(response)
 
     @defer.inlineCallbacks
     def on_POST(self, request):
@@ -268,6 +265,17 @@ class JoinRoomAliasServlet(RestServlet):
 
 
 # TODO: Needs unit testing
+class PublicRoomListRestServlet(RestServlet):
+    PATTERN = client_path_pattern("/publicRooms$")
+
+    @defer.inlineCallbacks
+    def on_GET(self, request):
+        handler = self.handlers.room_list_handler
+        data = yield handler.get_public_room_list()
+        defer.returnValue((200, data))
+
+
+# TODO: Needs unit testing
 class RoomMemberListRestServlet(RestServlet):
     PATTERN = client_path_pattern("/rooms/(?P<room_id>[^/]*)/members$")
 
@@ -314,6 +322,50 @@ class RoomMessageListRestServlet(RestServlet):
         defer.returnValue((200, msgs))
 
 
+# TODO: Needs unit testing
+class RoomStateRestServlet(RestServlet):
+    PATTERN = client_path_pattern("/rooms/(?P<room_id>[^/]*)/state$")
+
+    @defer.inlineCallbacks
+    def on_GET(self, request, room_id):
+        user = yield self.auth.get_user_by_req(request)
+        # TODO: Get all the current state for this room and return in the same
+        # format as initial sync, that is:
+        # [
+        #   { state event }, { state event }
+        # ]
+        defer.returnValue((200, []))
+
+
+# TODO: Needs unit testing
+class RoomInitialSyncRestServlet(RestServlet):
+    PATTERN = client_path_pattern("/rooms/(?P<room_id>[^/]*)/initialSync$")
+
+    @defer.inlineCallbacks
+    def on_GET(self, request, room_id):
+        user = yield self.auth.get_user_by_req(request)
+        # TODO: Get all the initial sync data for this room and return in the
+        # same format as initial sync, that is:
+        # {
+        #   membership: join,
+        #   messages: [
+        #       chunk: [ msg events ],
+        #       start: s_tok,
+        #       end: e_tok
+        #   ],
+        #   room_id: foo,
+        #   state: [
+        #       { state event } , { state event }
+        #   ]
+        # }
+        # Probably worth keeping the keys room_id and membership for parity with
+        # /initialSync even though they must be joined to sync this and know the
+        # room ID, so clients can reuse the same code (room_id and membership
+        # are MANDATORY for /initialSync, so the code will expect it to be
+        # there)
+        defer.returnValue((200, {}))
+
+
 class RoomTriggerBackfill(RestServlet):
     PATTERN = client_path_pattern("/rooms/(?P<room_id>[^/]*)/backfill$")
 
@@ -427,3 +479,6 @@ def register_servlets(hs, http_server):
     RoomTriggerBackfill(hs).register(http_server)
     RoomMembershipRestServlet(hs).register(http_server)
     RoomSendEventRestServlet(hs).register(http_server)
+    PublicRoomListRestServlet(hs).register(http_server)
+    RoomStateRestServlet(hs).register(http_server)
+    RoomInitialSyncRestServlet(hs).register(http_server)
diff --git a/synapse/server.py b/synapse/server.py
index 9de88b6642..94facf9d99 100644
--- a/synapse/server.py
+++ b/synapse/server.py
@@ -124,6 +124,11 @@ class BaseHomeServer(object):
         object."""
         return UserID.from_string(s, hs=self)
 
+    def parse_roomid(self, s):
+        """Parse the string given by 's' as a Room ID and return a RoomID
+        object."""
+        return RoomID.from_string(s, hs=self)
+
     def parse_roomalias(self, s):
         """Parse the string given by 's' as a Room Alias and return a RoomAlias
         object."""