SYN-144: Remove bad keys from pdu json objects, convert age_ts to age

for all pdus sent.
This commit is contained in:
Mark Haines 2014-11-18 19:18:36 +00:00
parent 572a1ca42a
commit 428581dd05
2 changed files with 29 additions and 15 deletions

View File

@ -117,10 +117,21 @@ class SynapseEvent(JsonEncodedObject):
""" """
raise NotImplementedError("get_content_template not implemented.") 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 = self.get_full_dict()
pdu_json.pop("destination", None) pdu_json.pop("destination", None)
pdu_json.pop("outlier", 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 return pdu_json

View File

@ -399,19 +399,21 @@ class ReplicationLayer(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def on_make_join_request(self, context, user_id): def on_make_join_request(self, context, user_id):
pdu = yield self.handler.on_make_join_request(context, user_id) pdu = yield self.handler.on_make_join_request(context, user_id)
time_now = self._clock.time_msec()
defer.returnValue({ defer.returnValue({
"event": pdu.get_pdu_json(), "event": pdu.get_pdu_json(time_now),
}) })
@defer.inlineCallbacks @defer.inlineCallbacks
def on_invite_request(self, origin, content): def on_invite_request(self, origin, content):
pdu = self.event_from_pdu_json(content) pdu = self.event_from_pdu_json(content)
ret_pdu = yield self.handler.on_invite_request(origin, pdu) ret_pdu = yield self.handler.on_invite_request(origin, pdu)
time_now = self._clock.time_msec()
defer.returnValue( defer.returnValue(
( (
200, 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): def on_send_join_request(self, origin, content):
pdu = self.event_from_pdu_json(content) pdu = self.event_from_pdu_json(content)
res_pdus = yield self.handler.on_send_join_request(origin, pdu) res_pdus = yield self.handler.on_send_join_request(origin, pdu)
time_now = self._clock.time_msec()
defer.returnValue((200, { defer.returnValue((200, {
"state": [p.get_pdu_json() for p in res_pdus["state"]], "state": [p.get_pdu_json(time_now) for p in res_pdus["state"]],
"auth_chain": [p.get_pdu_json() for p in res_pdus["auth_chain"]], "auth_chain": [p.get_pdu_json(time_now) for p in res_pdus["auth_chain"]],
})) }))
@defer.inlineCallbacks @defer.inlineCallbacks
def on_event_auth(self, origin, context, event_id): 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) auth_pdus = yield self.handler.on_event_auth(event_id)
defer.returnValue( defer.returnValue(
( (
200, 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 @defer.inlineCallbacks
def send_join(self, destination, pdu): def send_join(self, destination, pdu):
time_now = self._clock.time_msec()
_, content = yield self.transport_layer.send_join( _, content = yield self.transport_layer.send_join(
destination, destination,
pdu.room_id, pdu.room_id,
pdu.event_id, pdu.event_id,
pdu.get_pdu_json(), pdu.get_pdu_json(time_now),
) )
logger.debug("Got content: %s", content) logger.debug("Got content: %s", content)
@ -479,11 +483,12 @@ class ReplicationLayer(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def send_invite(self, destination, context, event_id, pdu): def send_invite(self, destination, context, event_id, pdu):
time_now = self._clock.time_msec()
code, content = yield self.transport_layer.send_invite( code, content = yield self.transport_layer.send_invite(
destination=destination, destination=destination,
context=context, context=context,
event_id=event_id, event_id=event_id,
content=pdu.get_pdu_json(), content=pdu.get_pdu_json(time_now),
) )
pdu_dict = content["event"] pdu_dict = content["event"]
@ -505,13 +510,8 @@ class ReplicationLayer(object):
"""Returns a new Transaction containing the given PDUs suitable for """Returns a new Transaction containing the given PDUs suitable for
transmission. transmission.
""" """
pdus = [p.get_pdu_json() for p in pdu_list]
time_now = self._clock.time_msec() time_now = self._clock.time_msec()
for p in pdus: pdus = [p.get_pdu_json(time_now) for p in pdu_list]
if "age_ts" in p:
age = time_now - p["age_ts"]
p.setdefault("unsigned", {})["age"] = int(age)
del p["age_ts"]
return Transaction( return Transaction(
origin=self.server_name, origin=self.server_name,
pdus=pdus, pdus=pdus,
@ -582,6 +582,9 @@ class ReplicationLayer(object):
#TODO: Check we have all the PDU keys here #TODO: Check we have all the PDU keys here
pdu_json.setdefault("hashes", {}) pdu_json.setdefault("hashes", {})
pdu_json.setdefault("signatures", {}) 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( return self.event_factory.create_event(
pdu_json["type"], outlier=outlier, **pdu_json pdu_json["type"], outlier=outlier, **pdu_json
) )