0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-15 04:43:51 +01:00

Convert event ids to be of the form :example.com

This commit is contained in:
Erik Johnston 2014-10-30 17:00:11 +00:00
parent da511334d2
commit 12ce441e67
6 changed files with 54 additions and 28 deletions

View file

@ -21,6 +21,8 @@ from synapse.api.events.room import (
RoomRedactionEvent, RoomRedactionEvent,
) )
from synapse.types import EventID
from synapse.util.stringutils import random_string 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) 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): def create_event(self, etype=None, **kwargs):
kwargs["type"] = etype kwargs["type"] = etype

View file

@ -17,22 +17,11 @@ from .units import Pdu
from synapse.crypto.event_signing import ( from synapse.crypto.event_signing import (
add_event_pdu_content_hash, sign_event_pdu add_event_pdu_content_hash, sign_event_pdu
) )
from synapse.types import EventID
import copy 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): class PduCodec(object):
def __init__(self, hs): def __init__(self, hs):
@ -40,20 +29,28 @@ class PduCodec(object):
self.server_name = hs.hostname self.server_name = hs.hostname
self.event_factory = hs.get_event_factory() self.event_factory = hs.get_event_factory()
self.clock = hs.get_clock() 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): def event_from_pdu(self, pdu):
kwargs = {} 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["room_id"] = pdu.context
kwargs["etype"] = pdu.pdu_type kwargs["etype"] = pdu.pdu_type
kwargs["prev_events"] = [ kwargs["prev_events"] = [
(encode_event_id(i, o), s) (self.encode_event_id(i, o), s)
for i, o, s in pdu.prev_pdus for i, o, s in pdu.prev_pdus
] ]
if hasattr(pdu, "prev_state_id") and hasattr(pdu, "prev_state_origin"): 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 pdu.prev_state_id, pdu.prev_state_origin
) )
@ -75,15 +72,15 @@ class PduCodec(object):
def pdu_from_event(self, event): def pdu_from_event(self, event):
d = event.get_full_dict() d = event.get_full_dict()
d["pdu_id"], d["origin"] = decode_event_id( d["pdu_id"], d["origin"] = self.decode_event_id(
event.event_id, self.server_name event.event_id
) )
d["context"] = event.room_id d["context"] = event.room_id
d["pdu_type"] = event.type d["pdu_type"] = event.type
if hasattr(event, "prev_events"): if hasattr(event, "prev_events"):
def f(e, s): def f(e, s):
i, o = decode_event_id(e, self.server_name) i, o = self.decode_event_id(e)
return i, o, s return i, o, s
d["prev_pdus"] = [ d["prev_pdus"] = [
f(e, s) f(e, s)
@ -92,7 +89,7 @@ class PduCodec(object):
if hasattr(event, "prev_state"): if hasattr(event, "prev_state"):
d["prev_state_id"], d["prev_state_origin"] = ( 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"): if hasattr(event, "state_key"):

View file

@ -20,9 +20,10 @@ from ._base import BaseHandler
from synapse.api.events.room import InviteJoinEvent, RoomMemberEvent from synapse.api.events.room import InviteJoinEvent, RoomMemberEvent
from synapse.api.constants import Membership from synapse.api.constants import Membership
from synapse.util.logutils import log_function 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.api.errors import SynapseError
from synapse.util.async import run_on_reactor from synapse.util.async import run_on_reactor
from synapse.types import EventID
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
@ -358,7 +359,9 @@ class FederationHandler(BaseHandler):
@defer.inlineCallbacks @defer.inlineCallbacks
def get_state_for_pdu(self, pdu_id, pdu_origin): 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( state_groups = yield self.store.get_state_groups(
[event_id] [event_id]

View file

@ -28,7 +28,7 @@ from synapse.handlers import Handlers
from synapse.rest import RestServletFactory from synapse.rest import RestServletFactory
from synapse.state import StateHandler from synapse.state import StateHandler
from synapse.storage import DataStore 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 import Clock
from synapse.util.distributor import Distributor from synapse.util.distributor import Distributor
from synapse.util.lockutils import LockManager from synapse.util.lockutils import LockManager
@ -143,6 +143,11 @@ class BaseHomeServer(object):
object.""" object."""
return RoomID.from_string(s, hs=self) 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): def serialize_event(self, e):
return serialize_event(self, e) return serialize_event(self, e)

View file

@ -16,8 +16,10 @@
from twisted.internet import defer 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.logutils import log_function
from synapse.util.async import run_on_reactor
from synapse.types import EventID
from collections import namedtuple from collections import namedtuple
@ -43,6 +45,7 @@ class StateHandler(object):
self.store = hs.get_datastore() self.store = hs.get_datastore()
self._replication = hs.get_replication_layer() self._replication = hs.get_replication_layer()
self.server_name = hs.hostname self.server_name = hs.hostname
self.hs = hs
@defer.inlineCallbacks @defer.inlineCallbacks
@log_function @log_function
@ -77,15 +80,17 @@ class StateHandler(object):
current_state = snapshot.prev_state_pdu current_state = snapshot.prev_state_pdu
if current_state: if current_state:
event.prev_state = encode_event_id( event.prev_state = EventID.create(
current_state.pdu_id, current_state.origin current_state.pdu_id, current_state.origin, self.hs
) ).to_string()
# TODO check current_state to see if the min power level is less # TODO check current_state to see if the min power level is less
# than the power level of the user # than the power level of the user
# power_level = self._get_power_level_for_event(event) # 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( yield self.store.update_current_state(
pdu_id=pdu_id, pdu_id=pdu_id,
@ -129,6 +134,8 @@ class StateHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
@log_function @log_function
def annotate_state_groups(self, event, old_state=None): def annotate_state_groups(self, event, old_state=None):
yield run_on_reactor()
if old_state: if old_state:
event.state_group = None event.state_group = None
event.old_state_events = old_state event.old_state_events = old_state

View file

@ -78,6 +78,11 @@ class DomainSpecificString(
"""Create a structure on the local domain""" """Create a structure on the local domain"""
return cls(localpart=localpart, domain=hs.hostname, is_mine=True) 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): class UserID(DomainSpecificString):
"""Structure representing a user ID.""" """Structure representing a user ID."""
@ -94,6 +99,11 @@ class RoomID(DomainSpecificString):
SIGIL = "!" SIGIL = "!"
class EventID(DomainSpecificString):
"""Structure representing an event id. """
SIGIL = "$"
class StreamToken( class StreamToken(
namedtuple( namedtuple(
"Token", "Token",