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

Separate creating an event context and persisting the event in the fed handler #9800

Merged
merged 13 commits into from
Apr 14, 2021
42 changes: 27 additions & 15 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2240,18 +2240,14 @@ async def _prep_event(
if c and c.type == EventTypes.Create:
auth_events[(c.type, c.state_key)] = c

context = await self.do_auth(origin, event, context, auth_events=auth_events)

if not context.rejected:
await self._check_for_soft_fail(event, state, backfilled)

if event.type == EventTypes.GuestAccess and not context.rejected:
await self.maybe_kick_guest_users(event)

# If we are going to send this event over federation we precaclculate
# the joined hosts.
if event.internal_metadata.get_send_on_behalf_of():
await self.event_creation_handler.cache_joined_hosts_for_event(event)
context = await self.do_auth(
origin,
event,
context,
state=state,
auth_events=auth_events,
backfilled=backfilled,
)

return context

Expand Down Expand Up @@ -2370,14 +2366,18 @@ async def do_auth(
origin: str,
event: EventBase,
context: EventContext,
state: Optional[Iterable[EventBase]],
auth_events: MutableStateMap[EventBase],
backfilled: bool,
) -> EventContext:
"""

Args:
origin:
event:
context:
origin: The host the event originates from.
event: The event itself.
context: The event context.
state: The state events to calculate the event context from. This is
ignored if context is provided.
auth_events:
Map from (event_type, state_key) to event

Expand All @@ -2387,6 +2387,7 @@ async def do_auth(
server.

Also NB that this function adds entries to it.
backfilled: True if the event was backfilled.
Returns:
updated context object
"""
Expand Down Expand Up @@ -2414,6 +2415,17 @@ async def do_auth(
logger.warning("Failed auth resolution for %r because %s", event, e)
context.rejected = RejectedReason.AUTH_ERROR

if not context.rejected:
await self._check_for_soft_fail(event, state, backfilled)

if event.type == EventTypes.GuestAccess and not context.rejected:
await self.maybe_kick_guest_users(event)

# If we are going to send this event over federation we precaclculate
# the joined hosts.
if event.internal_metadata.get_send_on_behalf_of():
await self.event_creation_handler.cache_joined_hosts_for_event(event)

richvdh marked this conversation as resolved.
Show resolved Hide resolved
return context

async def _update_auth_events_and_context_for_auth(
Expand Down
6 changes: 4 additions & 2 deletions tests/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def setUp(self):
)

self.handler = self.homeserver.get_federation_handler()
self.handler.do_auth = lambda origin, event, context, auth_events: succeed(
context
self.handler.do_auth = (
lambda origin, event, context, state, auth_events, backfilled: succeed(
context
)
)
self.client = self.homeserver.get_federation_client()
self.client._check_sigs_and_hash_and_fetch = lambda dest, pdus, **k: succeed(
Expand Down