summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-10-30 17:00:11 +0000
committerErik Johnston <erik@matrix.org>2014-10-30 17:00:11 +0000
commit12ce441e67a40bb73ac5aca0283c2fe4afac4021 (patch)
tree0a4b6d32dc2a7f149153ad7868d5c5e117335859
parentMake federation return the old current state, so that we can use it to do auth (diff)
downloadsynapse-12ce441e67a40bb73ac5aca0283c2fe4afac4021.tar.xz
Convert event ids to be of the form :example.com
-rw-r--r--synapse/api/events/factory.py6
-rw-r--r--synapse/federation/pdu_codec.py35
-rw-r--r--synapse/handlers/federation.py7
-rw-r--r--synapse/server.py7
-rw-r--r--synapse/state.py17
-rw-r--r--synapse/types.py10
6 files changed, 54 insertions, 28 deletions
diff --git a/synapse/api/events/factory.py b/synapse/api/events/factory.py
index 06f3bf232b..750096c618 100644
--- a/synapse/api/events/factory.py
+++ b/synapse/api/events/factory.py
@@ -21,6 +21,8 @@ from synapse.api.events.room import (
     RoomRedactionEvent,
 )
 
+from synapse.types import EventID
+
 from synapse.util.stringutils import random_string
 
 
@@ -59,7 +61,9 @@ class EventFactory(object):
 
         local_part = str(int(self.clock.time())) + i + random_string(5)
 
-        return "%s@%s" % (local_part, self.hs.hostname)
+        e_id = EventID.create_local(local_part, self.hs)
+
+        return e_id.to_string()
 
     def create_event(self, etype=None, **kwargs):
         kwargs["type"] = etype
diff --git a/synapse/federation/pdu_codec.py b/synapse/federation/pdu_codec.py
index dccbccb85b..6d31286290 100644
--- a/synapse/federation/pdu_codec.py
+++ b/synapse/federation/pdu_codec.py
@@ -17,22 +17,11 @@ from .units import Pdu
 from synapse.crypto.event_signing import (
     add_event_pdu_content_hash, sign_event_pdu
 )
+from synapse.types import EventID
 
 import copy
 
 
-def decode_event_id(event_id, server_name):
-    parts = event_id.split("@")
-    if len(parts) < 2:
-        return (event_id, server_name)
-    else:
-        return (parts[0], "".join(parts[1:]))
-
-
-def encode_event_id(pdu_id, origin):
-    return "%s@%s" % (pdu_id, origin)
-
-
 class PduCodec(object):
 
     def __init__(self, hs):
@@ -40,20 +29,28 @@ class PduCodec(object):
         self.server_name = hs.hostname
         self.event_factory = hs.get_event_factory()
         self.clock = hs.get_clock()
+        self.hs = hs
+
+    def encode_event_id(self, local, domain):
+        return EventID.create(local, domain, self.hs).to_string()
+
+    def decode_event_id(self, event_id):
+        e_id = self.hs.parse_eventid(event_id)
+        return e_id.localpart, e_id.domain
 
     def event_from_pdu(self, pdu):
         kwargs = {}
 
-        kwargs["event_id"] = encode_event_id(pdu.pdu_id, pdu.origin)
+        kwargs["event_id"] = self.encode_event_id(pdu.pdu_id, pdu.origin)
         kwargs["room_id"] = pdu.context
         kwargs["etype"] = pdu.pdu_type
         kwargs["prev_events"] = [
-            (encode_event_id(i, o), s)
+            (self.encode_event_id(i, o), s)
             for i, o, s in pdu.prev_pdus
         ]
 
         if hasattr(pdu, "prev_state_id") and hasattr(pdu, "prev_state_origin"):
-            kwargs["prev_state"] = encode_event_id(
+            kwargs["prev_state"] = self.encode_event_id(
                 pdu.prev_state_id, pdu.prev_state_origin
             )
 
@@ -75,15 +72,15 @@ class PduCodec(object):
     def pdu_from_event(self, event):
         d = event.get_full_dict()
 
-        d["pdu_id"], d["origin"] = decode_event_id(
-            event.event_id, self.server_name
+        d["pdu_id"], d["origin"] = self.decode_event_id(
+            event.event_id
         )
         d["context"] = event.room_id
         d["pdu_type"] = event.type
 
         if hasattr(event, "prev_events"):
             def f(e, s):
-                i, o = decode_event_id(e, self.server_name)
+                i, o = self.decode_event_id(e)
                 return i, o, s
             d["prev_pdus"] = [
                 f(e, s)
@@ -92,7 +89,7 @@ class PduCodec(object):
 
         if hasattr(event, "prev_state"):
             d["prev_state_id"], d["prev_state_origin"] = (
-                decode_event_id(event.prev_state, self.server_name)
+                self.decode_event_id(event.prev_state)
             )
 
         if hasattr(event, "state_key"):
diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py
index da99a4b449..1daeee833b 100644
--- a/synapse/handlers/federation.py
+++ b/synapse/handlers/federation.py
@@ -20,9 +20,10 @@ from ._base import BaseHandler
 from synapse.api.events.room import InviteJoinEvent, RoomMemberEvent
 from synapse.api.constants import Membership
 from synapse.util.logutils import log_function
-from synapse.federation.pdu_codec import PduCodec, encode_event_id
+from synapse.federation.pdu_codec import PduCodec
 from synapse.api.errors import SynapseError
 from synapse.util.async import run_on_reactor
+from synapse.types import EventID
 
 from twisted.internet import defer, reactor
 
@@ -358,7 +359,9 @@ class FederationHandler(BaseHandler):
 
     @defer.inlineCallbacks
     def get_state_for_pdu(self, pdu_id, pdu_origin):
-        event_id = encode_event_id(pdu_id, pdu_origin)
+        yield run_on_reactor()
+
+        event_id = EventID.create(pdu_id, pdu_origin, self.hs).to_string()
 
         state_groups = yield self.store.get_state_groups(
             [event_id]
diff --git a/synapse/server.py b/synapse/server.py
index a4d2d4aba5..d770b20b19 100644
--- a/synapse/server.py
+++ b/synapse/server.py
@@ -28,7 +28,7 @@ from synapse.handlers import Handlers
 from synapse.rest import RestServletFactory
 from synapse.state import StateHandler
 from synapse.storage import DataStore
-from synapse.types import UserID, RoomAlias, RoomID
+from synapse.types import UserID, RoomAlias, RoomID, EventID
 from synapse.util import Clock
 from synapse.util.distributor import Distributor
 from synapse.util.lockutils import LockManager
@@ -143,6 +143,11 @@ class BaseHomeServer(object):
         object."""
         return RoomID.from_string(s, hs=self)
 
+    def parse_eventid(self, s):
+        """Parse the string given by 's' as a Event ID and return a EventID
+        object."""
+        return EventID.from_string(s, hs=self)
+
     def serialize_event(self, e):
         return serialize_event(self, e)
 
diff --git a/synapse/state.py b/synapse/state.py
index a59688e3b4..414701b272 100644
--- a/synapse/state.py
+++ b/synapse/state.py
@@ -16,8 +16,10 @@
 
 from twisted.internet import defer
 
-from synapse.federation.pdu_codec import encode_event_id, decode_event_id
 from synapse.util.logutils import log_function
+from synapse.util.async import run_on_reactor
+
+from synapse.types import EventID
 
 from collections import namedtuple
 
@@ -43,6 +45,7 @@ class StateHandler(object):
         self.store = hs.get_datastore()
         self._replication = hs.get_replication_layer()
         self.server_name = hs.hostname
+        self.hs = hs
 
     @defer.inlineCallbacks
     @log_function
@@ -77,15 +80,17 @@ class StateHandler(object):
         current_state = snapshot.prev_state_pdu
 
         if current_state:
-            event.prev_state = encode_event_id(
-                current_state.pdu_id, current_state.origin
-            )
+            event.prev_state = EventID.create(
+                current_state.pdu_id, current_state.origin, self.hs
+            ).to_string()
 
         # TODO check current_state to see if the min power level is less
         # than the power level of the user
         # power_level = self._get_power_level_for_event(event)
 
-        pdu_id, origin = decode_event_id(event.event_id, self.server_name)
+        e_id = self.hs.parse_eventid(event.event_id)
+        pdu_id = e_id.localpart
+        origin = e_id.domain
 
         yield self.store.update_current_state(
             pdu_id=pdu_id,
@@ -129,6 +134,8 @@ class StateHandler(object):
     @defer.inlineCallbacks
     @log_function
     def annotate_state_groups(self, event, old_state=None):
+        yield run_on_reactor()
+
         if old_state:
             event.state_group = None
             event.old_state_events = old_state
diff --git a/synapse/types.py b/synapse/types.py
index c51bc8e4f2..649ff2f7d7 100644
--- a/synapse/types.py
+++ b/synapse/types.py
@@ -78,6 +78,11 @@ class DomainSpecificString(
         """Create a structure on the local domain"""
         return cls(localpart=localpart, domain=hs.hostname, is_mine=True)
 
+    @classmethod
+    def create(cls, localpart, domain, hs):
+        is_mine = domain == hs.hostname
+        return cls(localpart=localpart, domain=domain, is_mine=is_mine)
+
 
 class UserID(DomainSpecificString):
     """Structure representing a user ID."""
@@ -94,6 +99,11 @@ class RoomID(DomainSpecificString):
     SIGIL = "!"
 
 
+class EventID(DomainSpecificString):
+    """Structure representing an event id. """
+    SIGIL = "$"
+
+
 class StreamToken(
     namedtuple(
         "Token",