Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Convert Transation and Edu object to attrs #10542

Merged
merged 8 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,17 @@ async def on_backfill_request(
origin, room_id, versions, limit
)

res = self._transaction_from_pdus(pdus).get_dict()
res = self._transaction_dict_from_pdus(pdus)

return 200, res

async def on_incoming_transaction(
self, origin: str, transaction_data: JsonDict
) -> Tuple[int, Dict[str, Any]]:
self,
origin: str,
transaction_id: str,
destination: str,
transaction_data: JsonDict,
) -> Tuple[int, JsonDict]:
# If we receive a transaction we should make sure that kick off handling
# any old events in the staging area.
if not self._started_handling_of_staged_events:
Expand All @@ -212,8 +216,11 @@ async def on_incoming_transaction(
# accurate as possible.
request_time = self._clock.time_msec()

transaction = Transaction(**transaction_data)
transaction_id = transaction.transaction_id # type: ignore
transaction = Transaction(
transaction_id=transaction_id,
destination=destination,
**transaction_data,
)

if not transaction_id:
raise Exception("Transaction missing transaction_id")
Expand Down Expand Up @@ -538,7 +545,7 @@ async def on_pdu_request(
pdu = await self.handler.get_persisted_pdu(origin, event_id)

if pdu:
return 200, self._transaction_from_pdus([pdu]).get_dict()
return 200, self._transaction_dict_from_pdus([pdu])
else:
return 404, ""

Expand Down Expand Up @@ -879,7 +886,7 @@ async def on_openid_userinfo(self, token: str) -> Optional[str]:
ts_now_ms = self._clock.time_msec()
return await self.store.get_user_id_for_open_id_token(token, ts_now_ms)

def _transaction_from_pdus(self, pdu_list: List[EventBase]) -> Transaction:
def _transaction_dict_from_pdus(self, pdu_list: List[EventBase]) -> JsonDict:
"""Returns a new Transaction containing the given PDUs suitable for
transmission.
"""
Expand All @@ -890,7 +897,7 @@ def _transaction_from_pdus(self, pdu_list: List[EventBase]) -> Transaction:
pdus=pdus,
origin_server_ts=int(time_now),
destination=None,
)
).get_dict()

async def _handle_received_pdu(self, origin: str, pdu: EventBase) -> None:
"""Process a PDU received in a federation /send/ transaction.
Expand Down
6 changes: 3 additions & 3 deletions synapse/federation/sender/transaction_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ async def send_new_transaction(
len(edus),
)

transaction = Transaction.create_new(
transaction = Transaction(
origin_server_ts=int(self.clock.time_msec()),
transaction_id=txn_id,
origin=self._server_name,
destination=destination,
pdus=pdus,
edus=edus,
pdus=[p.get_pdu_json() for p in pdus],
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
edus=[edu.get_dict() for edu in edus],
)

self._next_txn_id += 1
Expand Down
11 changes: 1 addition & 10 deletions synapse/federation/transport/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,21 +450,12 @@ async def on_PUT(
len(transaction_data.get("edus", [])),
)

# We should ideally be getting this from the security layer.
# origin = body["origin"]

# Add some extra data to the transaction dict that isn't included
# in the request body.
transaction_data.update(
transaction_id=transaction_id, destination=self.server_name
)

except Exception as e:
logger.exception(e)
return 400, {"error": "Invalid transaction"}

code, response = await self.handler.on_incoming_transaction(
origin, transaction_data
origin, transaction_id, self.server_name, transaction_data
)

return code, response
Expand Down
14 changes: 0 additions & 14 deletions synapse/federation/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,3 @@ def __init__(self, transaction_id=None, pdus: Optional[list] = None, **kwargs):
del kwargs["edus"]

super().__init__(transaction_id=transaction_id, pdus=pdus or [], **kwargs)

@staticmethod
def create_new(pdus, **kwargs):
"""Used to create a new transaction. Will auto fill out
transaction_id and origin_server_ts keys.
"""
if "origin_server_ts" not in kwargs:
raise KeyError("Require 'origin_server_ts' to construct a Transaction")
if "transaction_id" not in kwargs:
raise KeyError("Require 'transaction_id' to construct a Transaction")

kwargs["pdus"] = [p.get_pdu_json() for p in pdus]

return Transaction(**kwargs)