Skip to content

Commit 774e1ff

Browse files
committed
Refactor unsigned data inclusions
Check for requester == sender before looking up keys to include in the unsigned data. Do this in an if branch instead of storing the check to a variable, because otherwise mypy doesn't know that the requester is not None.
1 parent 453fb12 commit 774e1ff

1 file changed

Lines changed: 42 additions & 42 deletions

File tree

synapse/events/utils.py

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -475,49 +475,49 @@ def serialize_event(
475475
config=config,
476476
)
477477

478-
requester_is_sender = lambda: config.requester is not None and config.requester.user.to_string() == e.sender
479-
480-
# If we have a txn_id saved in the internal_metadata, we should include it in the
481-
# unsigned section of the event if it was sent by the same session as the one
482-
# requesting the event.
483-
txn_id: str | None = getattr(e.internal_metadata, "txn_id", None)
484-
if txn_id is not None and requester_is_sender():
485-
# Some events do not have the device ID stored in the internal metadata,
486-
# this includes old events as well as those created by appservice, guests,
487-
# or with tokens minted with the admin API. For those events, fallback
488-
# to using the access token instead.
489-
event_device_id: str | None = getattr(e.internal_metadata, "device_id", None)
490-
if event_device_id is not None:
491-
if event_device_id == config.requester.device_id:
492-
d["unsigned"]["transaction_id"] = txn_id
493-
494-
else:
495-
# Fallback behaviour: only include the transaction ID if the event
496-
# was sent from the same access token.
497-
#
498-
# For regular users, the access token ID can be used to determine this.
499-
# This includes access tokens minted with the admin API.
500-
#
501-
# For guests and appservice users, we can't check the access token ID
502-
# so assume it is the same session.
503-
event_token_id: int | None = getattr(e.internal_metadata, "token_id", None)
504-
if (
505-
(
506-
event_token_id is not None
507-
and config.requester.access_token_id is not None
508-
and event_token_id == config.requester.access_token_id
478+
# If we have applicable fields saved in the internal_metadata, include them in the
479+
# unsigned section of the event if the event was sent by the same session (or when
480+
# appropriate, just the same sender) as the one requesting the event.
481+
if config.requester is not None and config.requester.user.to_string() == e.sender:
482+
txn_id: str | None = getattr(e.internal_metadata, "txn_id", None)
483+
if txn_id is not None:
484+
# Some events do not have the device ID stored in the internal metadata,
485+
# this includes old events as well as those created by appservice, guests,
486+
# or with tokens minted with the admin API. For those events, fallback
487+
# to using the access token instead.
488+
event_device_id: str | None = getattr(
489+
e.internal_metadata, "device_id", None
490+
)
491+
if event_device_id is not None:
492+
if event_device_id == config.requester.device_id:
493+
d["unsigned"]["transaction_id"] = txn_id
494+
495+
else:
496+
# Fallback behaviour: only include the transaction ID if the event
497+
# was sent from the same access token.
498+
#
499+
# For regular users, the access token ID can be used to determine this.
500+
# This includes access tokens minted with the admin API.
501+
#
502+
# For guests and appservice users, we can't check the access token ID
503+
# so assume it is the same session.
504+
event_token_id: int | None = getattr(
505+
e.internal_metadata, "token_id", None
509506
)
510-
or config.requester.is_guest
511-
or config.requester.app_service
512-
):
513-
d["unsigned"]["transaction_id"] = txn_id
514-
515-
# If we have a delay_id saved in the internal_metadata, we should include it in the
516-
# unsigned section of the event if it was sent by the same sender (with any session)
517-
# as the one requesting the event.
518-
delay_id: str | None = getattr(e.internal_metadata, "delay_id", None)
519-
if delay_id is not None and requester_is_sender():
520-
d["unsigned"]["org.matrix.msc4140.delay_id"] = delay_id
507+
if (
508+
(
509+
event_token_id is not None
510+
and config.requester.access_token_id is not None
511+
and event_token_id == config.requester.access_token_id
512+
)
513+
or config.requester.is_guest
514+
or config.requester.app_service
515+
):
516+
d["unsigned"]["transaction_id"] = txn_id
517+
518+
delay_id: str | None = getattr(e.internal_metadata, "delay_id", None)
519+
if delay_id is not None:
520+
d["unsigned"]["org.matrix.msc4140.delay_id"] = delay_id
521521

522522
# invite_room_state and knock_room_state are a list of stripped room state events
523523
# that are meant to provide metadata about a room to an invitee/knocker. They are

0 commit comments

Comments
 (0)