From 428581dd056e40adaca41c3593d79f76339f3220 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Tue, 18 Nov 2014 19:18:36 +0000 Subject: SYN-144: Remove bad keys from pdu json objects, convert age_ts to age for all pdus sent. --- synapse/api/events/__init__.py | 13 ++++++++++++- synapse/federation/replication.py | 31 +++++++++++++++++-------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/synapse/api/events/__init__.py b/synapse/api/events/__init__.py index 63c0bd7ae7..0019789dd1 100644 --- a/synapse/api/events/__init__.py +++ b/synapse/api/events/__init__.py @@ -117,10 +117,21 @@ class SynapseEvent(JsonEncodedObject): """ raise NotImplementedError("get_content_template not implemented.") - def get_pdu_json(self): + def get_pdu_json(self, time_now=None): pdu_json = self.get_full_dict() pdu_json.pop("destination", None) pdu_json.pop("outlier", None) + pdu_json.pop("replaces_state", None) + pdu_json.pop("redacted", None) + state_hash = pdu_json.pop("state_hash", None) + if state_hash is not None: + pdu_json.setdefault("unsigned", {})["state_hash"] = state_hash + content = pdu_json.get("content", {}) + content.pop("prev", None) + if time_now is not None and "age_ts" in pdu_json: + age = time_now - pdu_json["age_ts"] + pdu_json.setdefault("unsigned", {})["age"] = int(age) + del pdu_json["age_ts"] return pdu_json diff --git a/synapse/federation/replication.py b/synapse/federation/replication.py index 8ee74de005..2ed303e12d 100644 --- a/synapse/federation/replication.py +++ b/synapse/federation/replication.py @@ -399,19 +399,21 @@ class ReplicationLayer(object): @defer.inlineCallbacks def on_make_join_request(self, context, user_id): pdu = yield self.handler.on_make_join_request(context, user_id) + time_now = self._clock.time_msec() defer.returnValue({ - "event": pdu.get_pdu_json(), + "event": pdu.get_pdu_json(time_now), }) @defer.inlineCallbacks def on_invite_request(self, origin, content): pdu = self.event_from_pdu_json(content) ret_pdu = yield self.handler.on_invite_request(origin, pdu) + time_now = self._clock.time_msec() defer.returnValue( ( 200, { - "event": ret_pdu.get_pdu_json(), + "event": ret_pdu.get_pdu_json(time_now), } ) ) @@ -420,20 +422,21 @@ class ReplicationLayer(object): def on_send_join_request(self, origin, content): pdu = self.event_from_pdu_json(content) res_pdus = yield self.handler.on_send_join_request(origin, pdu) - + time_now = self._clock.time_msec() defer.returnValue((200, { - "state": [p.get_pdu_json() for p in res_pdus["state"]], - "auth_chain": [p.get_pdu_json() for p in res_pdus["auth_chain"]], + "state": [p.get_pdu_json(time_now) for p in res_pdus["state"]], + "auth_chain": [p.get_pdu_json(time_now) for p in res_pdus["auth_chain"]], })) @defer.inlineCallbacks def on_event_auth(self, origin, context, event_id): + time_now = self._clock.time_msec() auth_pdus = yield self.handler.on_event_auth(event_id) defer.returnValue( ( 200, { - "auth_chain": [a.get_pdu_json() for a in auth_pdus], + "auth_chain": [a.get_pdu_json(time_now) for a in auth_pdus], } ) ) @@ -454,11 +457,12 @@ class ReplicationLayer(object): @defer.inlineCallbacks def send_join(self, destination, pdu): + time_now = self._clock.time_msec() _, content = yield self.transport_layer.send_join( destination, pdu.room_id, pdu.event_id, - pdu.get_pdu_json(), + pdu.get_pdu_json(time_now), ) logger.debug("Got content: %s", content) @@ -479,11 +483,12 @@ class ReplicationLayer(object): @defer.inlineCallbacks def send_invite(self, destination, context, event_id, pdu): + time_now = self._clock.time_msec() code, content = yield self.transport_layer.send_invite( destination=destination, context=context, event_id=event_id, - content=pdu.get_pdu_json(), + content=pdu.get_pdu_json(time_now), ) pdu_dict = content["event"] @@ -505,13 +510,8 @@ class ReplicationLayer(object): """Returns a new Transaction containing the given PDUs suitable for transmission. """ - pdus = [p.get_pdu_json() for p in pdu_list] time_now = self._clock.time_msec() - for p in pdus: - if "age_ts" in p: - age = time_now - p["age_ts"] - p.setdefault("unsigned", {})["age"] = int(age) - del p["age_ts"] + pdus = [p.get_pdu_json(time_now) for p in pdu_list] return Transaction( origin=self.server_name, pdus=pdus, @@ -582,6 +582,9 @@ class ReplicationLayer(object): #TODO: Check we have all the PDU keys here pdu_json.setdefault("hashes", {}) pdu_json.setdefault("signatures", {}) + state_hash = pdu_json.get("unsigned", {}).pop("state_hash", None) + if state_hash is not None: + pdu_json["state_hash"] = state_hash return self.event_factory.create_event( pdu_json["type"], outlier=outlier, **pdu_json ) -- cgit 1.4.1