Make EventBase abstract base class

This commit is contained in:
Erik Johnston 2018-02-28 14:52:44 +00:00
parent aa11e60fc7
commit ea7735b6f3
3 changed files with 109 additions and 35 deletions

View file

@ -16,6 +16,8 @@
from synapse.util.frozenutils import freeze from synapse.util.frozenutils import freeze
from synapse.util.caches import intern_dict from synapse.util.caches import intern_dict
import abc
# Whether we should use frozen_dict in FrozenEvent. Using frozen_dicts prevents # Whether we should use frozen_dict in FrozenEvent. Using frozen_dicts prevents
# bugs where we accidentally share e.g. signature dicts. However, converting # bugs where we accidentally share e.g. signature dicts. However, converting
@ -64,30 +66,46 @@ def _event_dict_property(key):
class EventBase(object): class EventBase(object):
def __init__(self, event_dict, signatures={}, unsigned={}, __metaclass__ = abc.ABCMeta
def __init__(self, signatures={}, unsigned={},
internal_metadata_dict={}, rejected_reason=None): internal_metadata_dict={}, rejected_reason=None):
self.signatures = signatures self.signatures = signatures
self.unsigned = unsigned self.unsigned = unsigned
self.rejected_reason = rejected_reason self.rejected_reason = rejected_reason
self._event_dict = event_dict
self.internal_metadata = _EventInternalMetadata( self.internal_metadata = _EventInternalMetadata(
internal_metadata_dict internal_metadata_dict
) )
auth_events = _event_dict_property("auth_events") auth_events = abc.abstractproperty()
depth = _event_dict_property("depth") depth = abc.abstractproperty()
content = _event_dict_property("content") content = abc.abstractproperty()
hashes = _event_dict_property("hashes") hashes = abc.abstractproperty()
origin = _event_dict_property("origin") origin = abc.abstractproperty()
origin_server_ts = _event_dict_property("origin_server_ts") origin_server_ts = abc.abstractproperty()
prev_events = _event_dict_property("prev_events") prev_events = abc.abstractproperty()
prev_state = _event_dict_property("prev_state") prev_state = abc.abstractproperty()
redacts = _event_dict_property("redacts") redacts = abc.abstractproperty()
room_id = _event_dict_property("room_id") room_id = abc.abstractproperty()
sender = _event_dict_property("sender") sender = abc.abstractproperty()
user_id = _event_dict_property("sender") user_id = abc.abstractproperty()
event_id = abc.abstractproperty()
state_key = abc.abstractproperty()
type = abc.abstractproperty()
@abc.abstractmethod
def get_dict(self):
raise NotImplementedError()
@abc.abstractmethod
def get(self, key, default=None):
raise NotImplementedError()
@abc.abstractmethod
def iteritems(self):
raise NotImplementedError()
@property @property
def membership(self): def membership(self):
@ -96,18 +114,6 @@ class EventBase(object):
def is_state(self): def is_state(self):
return hasattr(self, "state_key") and self.state_key is not None return hasattr(self, "state_key") and self.state_key is not None
def get_dict(self):
d = dict(self._event_dict)
d.update({
"signatures": self.signatures,
"unsigned": dict(self.unsigned),
})
return d
def get(self, key, default=None):
return self._event_dict.get(key, default)
def get_internal_metadata_dict(self): def get_internal_metadata_dict(self):
return self.internal_metadata.get_dict() return self.internal_metadata.get_dict()
@ -127,9 +133,6 @@ class EventBase(object):
def __set__(self, instance, value): def __set__(self, instance, value):
raise AttributeError("Unrecognized attribute %s" % (instance,)) raise AttributeError("Unrecognized attribute %s" % (instance,))
def iteritems(self):
return self._event_dict.iteritems()
class FrozenEvent(EventBase): class FrozenEvent(EventBase):
def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None): def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
@ -153,19 +156,60 @@ class FrozenEvent(EventBase):
else: else:
frozen_dict = event_dict frozen_dict = event_dict
self.event_id = event_dict["event_id"] self._event_id = event_dict["event_id"]
self.type = event_dict["type"] self._type = event_dict["type"]
if "state_key" in event_dict: if "state_key" in event_dict:
self.state_key = event_dict["state_key"] self._state_key = event_dict["state_key"]
self._event_dict = frozen_dict
super(FrozenEvent, self).__init__( super(FrozenEvent, self).__init__(
frozen_dict,
signatures=signatures, signatures=signatures,
unsigned=unsigned, unsigned=unsigned,
internal_metadata_dict=internal_metadata_dict, internal_metadata_dict=internal_metadata_dict,
rejected_reason=rejected_reason, rejected_reason=rejected_reason,
) )
auth_events = _event_dict_property("auth_events")
depth = _event_dict_property("depth")
content = _event_dict_property("content")
hashes = _event_dict_property("hashes")
origin = _event_dict_property("origin")
origin_server_ts = _event_dict_property("origin_server_ts")
prev_events = _event_dict_property("prev_events")
prev_state = _event_dict_property("prev_state")
redacts = _event_dict_property("redacts")
room_id = _event_dict_property("room_id")
sender = _event_dict_property("sender")
user_id = _event_dict_property("sender")
@property
def event_id(self):
return self._event_id
@property
def type(self):
return self._type
@property
def state_key(self):
return self._state_key
def get_dict(self):
d = dict(self._event_dict)
d.update({
"signatures": self.signatures,
"unsigned": dict(self.unsigned),
})
return d
def get(self, key, default=None):
return self._event_dict.get(key, default)
def iteritems(self):
return self._event_dict.iteritems()
@staticmethod @staticmethod
def from_event(event): def from_event(event):
e = FrozenEvent( e = FrozenEvent(

View file

@ -27,8 +27,9 @@ class EventBuilder(EventBase):
signatures = copy.deepcopy(key_values.pop("signatures", {})) signatures = copy.deepcopy(key_values.pop("signatures", {}))
unsigned = copy.deepcopy(key_values.pop("unsigned", {})) unsigned = copy.deepcopy(key_values.pop("unsigned", {}))
self._event_dict = key_values
super(EventBuilder, self).__init__( super(EventBuilder, self).__init__(
key_values,
signatures=signatures, signatures=signatures,
unsigned=unsigned, unsigned=unsigned,
internal_metadata_dict=internal_metadata_dict, internal_metadata_dict=internal_metadata_dict,
@ -38,6 +39,34 @@ class EventBuilder(EventBase):
state_key = _event_dict_property("state_key") state_key = _event_dict_property("state_key")
type = _event_dict_property("type") type = _event_dict_property("type")
auth_events = _event_dict_property("auth_events")
depth = _event_dict_property("depth")
content = _event_dict_property("content")
hashes = _event_dict_property("hashes")
origin = _event_dict_property("origin")
origin_server_ts = _event_dict_property("origin_server_ts")
prev_events = _event_dict_property("prev_events")
prev_state = _event_dict_property("prev_state")
redacts = _event_dict_property("redacts")
room_id = _event_dict_property("room_id")
sender = _event_dict_property("sender")
user_id = _event_dict_property("sender")
def get_dict(self):
d = dict(self._event_dict)
d.update({
"signatures": self.signatures,
"unsigned": dict(self.unsigned),
})
return d
def get(self, key, default=None):
return self._event_dict.get(key, default)
def iteritems(self):
return self._event_dict.iteritems()
def build(self): def build(self):
return FrozenEvent.from_event(self) return FrozenEvent.from_event(self)

View file

@ -624,6 +624,7 @@ class EventCreationHandler(object):
context.app_service = requester.app_service context.app_service = requester.app_service
if builder.is_state(): if builder.is_state():
logger.info("Added prev_state")
builder.prev_state = yield self.store.add_event_hashes( builder.prev_state = yield self.store.add_event_hashes(
context.prev_state_events context.prev_state_events
) )