diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py
index 1d0f6bcd6f..5371336529 100644
--- a/synapse/handlers/federation.py
+++ b/synapse/handlers/federation.py
@@ -993,6 +993,7 @@ class FederationHandler:
(
event,
unpersisted_context,
+ _,
) = await self.event_creation_handler.create_new_client_event(
builder=builder
)
@@ -1183,7 +1184,7 @@ class FederationHandler:
},
)
- event, _ = await self.event_creation_handler.create_new_client_event(
+ event, _, _ = await self.event_creation_handler.create_new_client_event(
builder=builder
)
@@ -1236,9 +1237,10 @@ class FederationHandler:
(
event,
unpersisted_context,
+ _,
) = await self.event_creation_handler.create_new_client_event(builder=builder)
- event_allowed, _ = await self.third_party_event_rules.check_event_allowed(
+ event_allowed, _, _ = await self.third_party_event_rules.check_event_allowed(
event, unpersisted_context
)
if not event_allowed:
@@ -1413,6 +1415,7 @@ class FederationHandler:
(
event,
unpersisted_context,
+ _,
) = await self.event_creation_handler.create_new_client_event(
builder=builder
)
@@ -1495,6 +1498,7 @@ class FederationHandler:
(
event,
unpersisted_context,
+ _,
) = await self.event_creation_handler.create_new_client_event(
builder=builder
)
@@ -1577,6 +1581,7 @@ class FederationHandler:
(
event,
unpersisted_context,
+ _,
) = await self.event_creation_handler.create_new_client_event(builder=builder)
EventValidator().validate_new(event, self.config)
diff --git a/synapse/handlers/federation_event.py b/synapse/handlers/federation_event.py
index b7136f8d1c..0b974afe4d 100644
--- a/synapse/handlers/federation_event.py
+++ b/synapse/handlers/federation_event.py
@@ -404,9 +404,11 @@ class FederationEventHandler:
# for knock events, we run the third-party event rules. It's not entirely clear
# why we don't do this for other sorts of membership events.
if event.membership == Membership.KNOCK:
- event_allowed, _ = await self._third_party_event_rules.check_event_allowed(
- event, context
- )
+ (
+ event_allowed,
+ _,
+ _,
+ ) = await self._third_party_event_rules.check_event_allowed(event, context)
if not event_allowed:
logger.info("Sending of knock %s forbidden by third-party rules", event)
raise SynapseError(
diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py
index aa90d0000d..77d92f1574 100644
--- a/synapse/handlers/message.py
+++ b/synapse/handlers/message.py
@@ -574,7 +574,7 @@ class EventCreationHandler:
state_map: Optional[StateMap[str]] = None,
for_batch: bool = False,
current_state_group: Optional[int] = None,
- ) -> Tuple[EventBase, EventContext]:
+ ) -> Tuple[EventBase, EventContext, Optional[dict]]:
"""
Given a dict from a client, create a new event. If bool for_batch is true, will
create an event using the prev_event_ids, and will create an event context for
@@ -646,7 +646,9 @@ class EventCreationHandler:
exceeded
Returns:
- Tuple of created event, Context
+ Tuple of created event, Context, and an optional event dict to form the basis
+ of a new event if third_party_rules would like to send an additional event as a
+ consequence of this event.
"""
await self.auth_blocking.check_auth_blocking(requester=requester)
@@ -708,7 +710,7 @@ class EventCreationHandler:
builder.internal_metadata.historical = historical
- event, unpersisted_context = await self.create_new_client_event(
+ event, unpersisted_context, new_event = await self.create_new_client_event(
builder=builder,
requester=requester,
allow_no_prev_events=allow_no_prev_events,
@@ -765,7 +767,7 @@ class EventCreationHandler:
self.validator.validate_new(event, self.config)
- return event, context
+ return event, context, new_event
async def _is_exempt_from_privacy_policy(
self, builder: EventBuilder, requester: Requester
@@ -1005,7 +1007,7 @@ class EventCreationHandler:
max_retries = 5
for i in range(max_retries):
try:
- event, context = await self.create_event(
+ event, context, third_party_event_dict = await self.create_event(
requester,
event_dict,
txn_id=txn_id,
@@ -1053,9 +1055,24 @@ class EventCreationHandler:
Codes.FORBIDDEN,
)
+ events_and_context = [(event, context)]
+ if third_party_event_dict:
+ (
+ third_party_event,
+ unpersisted_third_party_context,
+ _,
+ ) = await self.create_event(
+ requester,
+ third_party_event_dict,
+ )
+ third_party_context = await unpersisted_third_party_context.persist(
+ third_party_event
+ )
+ events_and_context.append((third_party_event, third_party_context))
+
ev = await self.handle_new_client_event(
requester=requester,
- events_and_context=[(event, context)],
+ events_and_context=events_and_context,
ratelimit=ratelimit,
ignore_shadow_ban=ignore_shadow_ban,
)
@@ -1085,7 +1102,7 @@ class EventCreationHandler:
state_map: Optional[StateMap[str]] = None,
for_batch: bool = False,
current_state_group: Optional[int] = None,
- ) -> Tuple[EventBase, UnpersistedEventContextBase]:
+ ) -> Tuple[EventBase, UnpersistedEventContextBase, Optional[dict]]:
"""Create a new event for a local client. If bool for_batch is true, will
create an event using the prev_event_ids, and will create an event context for
the event using the parameters state_map and current_state_group, thus these parameters
@@ -1134,7 +1151,9 @@ class EventCreationHandler:
batch persisting
Returns:
- Tuple of created event, UnpersistedEventContext
+ Tuple of created event, UnpersistedEventContext, and an optional event dict
+ to form the basis of a new event if third_party_rules would like to send an
+ additional event as a consequence of this event.
"""
# Strip down the state_event_ids to only what we need to auth the event.
# For example, we don't need extra m.room.member that don't match event.sender
@@ -1269,9 +1288,11 @@ class EventCreationHandler:
if requester:
context.app_service = requester.app_service
- res, new_content = await self.third_party_event_rules.check_event_allowed(
- event, context
- )
+ (
+ res,
+ new_content,
+ new_event,
+ ) = await self.third_party_event_rules.check_event_allowed(event, context)
if res is False:
logger.info(
"Event %s forbidden by third-party rules",
@@ -1291,7 +1312,7 @@ class EventCreationHandler:
await self._validate_event_relation(event)
logger.debug("Created event %s", event.event_id)
- return event, context
+ return event, context, new_event
async def _validate_event_relation(self, event: EventBase) -> None:
"""
@@ -2046,7 +2067,7 @@ class EventCreationHandler:
max_retries = 5
for i in range(max_retries):
try:
- event, context = await self.create_event(
+ event, context, _ = await self.create_event(
requester,
{
"type": EventTypes.Dummy,
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index 837dabb3b7..9fb7209549 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -212,6 +212,7 @@ class RoomCreationHandler:
(
tombstone_event,
tombstone_context,
+ _,
) = await self.event_creation_handler.create_event(
requester,
{
@@ -1091,7 +1092,7 @@ class RoomCreationHandler:
content: JsonDict,
for_batch: bool,
**kwargs: Any,
- ) -> Tuple[EventBase, synapse.events.snapshot.EventContext]:
+ ) -> Tuple[EventBase, synapse.events.snapshot.EventContext, Optional[dict]]:
"""
Creates an event and associated event context.
Args:
@@ -1110,7 +1111,11 @@ class RoomCreationHandler:
event_dict = create_event_dict(etype, content, **kwargs)
- new_event, new_context = await self.event_creation_handler.create_event(
+ (
+ new_event,
+ new_context,
+ third_party_event,
+ ) = await self.event_creation_handler.create_event(
creator,
event_dict,
prev_event_ids=prev_event,
@@ -1123,7 +1128,7 @@ class RoomCreationHandler:
prev_event = [new_event.event_id]
state_map[(new_event.type, new_event.state_key)] = new_event.event_id
- return new_event, new_context
+ return new_event, new_context, third_party_event
try:
config = self._presets_dict[preset_config]
@@ -1133,7 +1138,7 @@ class RoomCreationHandler:
)
creation_content.update({"creator": creator_id})
- creation_event, creation_context = await create_event(
+ creation_event, creation_context, _ = await create_event(
EventTypes.Create, creation_content, False
)
@@ -1173,15 +1178,18 @@ class RoomCreationHandler:
current_state_group = event_to_state[member_event_id]
events_to_send = []
+ third_party_events_to_append = []
# We treat the power levels override specially as this needs to be one
# of the first events that get sent into a room.
pl_content = initial_state.pop((EventTypes.PowerLevels, ""), None)
if pl_content is not None:
- power_event, power_context = await create_event(
+ power_event, power_context, power_tp_event = await create_event(
EventTypes.PowerLevels, pl_content, True
)
current_state_group = power_context._state_group
events_to_send.append((power_event, power_context))
+ if power_tp_event:
+ third_party_events_to_append.append(power_tp_event)
else:
power_level_content: JsonDict = {
"users": {creator_id: 100},
@@ -1224,64 +1232,98 @@ class RoomCreationHandler:
# apply those.
if power_level_content_override:
power_level_content.update(power_level_content_override)
- pl_event, pl_context = await create_event(
+ pl_event, pl_context, pl_tp_event = await create_event(
EventTypes.PowerLevels,
power_level_content,
True,
)
current_state_group = pl_context._state_group
events_to_send.append((pl_event, pl_context))
+ if pl_tp_event:
+ third_party_events_to_append.append(pl_tp_event)
if room_alias and (EventTypes.CanonicalAlias, "") not in initial_state:
- room_alias_event, room_alias_context = await create_event(
+ room_alias_event, room_alias_context, ra_tp_event = await create_event(
EventTypes.CanonicalAlias, {"alias": room_alias.to_string()}, True
)
current_state_group = room_alias_context._state_group
events_to_send.append((room_alias_event, room_alias_context))
+ if ra_tp_event:
+ third_party_events_to_append.append(ra_tp_event)
if (EventTypes.JoinRules, "") not in initial_state:
- join_rules_event, join_rules_context = await create_event(
+ join_rules_event, join_rules_context, jr_tp_event = await create_event(
EventTypes.JoinRules,
{"join_rule": config["join_rules"]},
True,
)
current_state_group = join_rules_context._state_group
events_to_send.append((join_rules_event, join_rules_context))
+ if jr_tp_event:
+ third_party_events_to_append.append(jr_tp_event)
if (EventTypes.RoomHistoryVisibility, "") not in initial_state:
- visibility_event, visibility_context = await create_event(
+ visibility_event, visibility_context, vis_tp_event = await create_event(
EventTypes.RoomHistoryVisibility,
{"history_visibility": config["history_visibility"]},
True,
)
current_state_group = visibility_context._state_group
events_to_send.append((visibility_event, visibility_context))
+ if vis_tp_event:
+ third_party_events_to_append.append(vis_tp_event)
if config["guest_can_join"]:
if (EventTypes.GuestAccess, "") not in initial_state:
- guest_access_event, guest_access_context = await create_event(
+ (
+ guest_access_event,
+ guest_access_context,
+ ga_tp_event,
+ ) = await create_event(
EventTypes.GuestAccess,
{EventContentFields.GUEST_ACCESS: GuestAccess.CAN_JOIN},
True,
)
current_state_group = guest_access_context._state_group
events_to_send.append((guest_access_event, guest_access_context))
+ if ga_tp_event:
+ third_party_events_to_append.append(ga_tp_event)
for (etype, state_key), content in initial_state.items():
- event, context = await create_event(
+ event, context, tp_event = await create_event(
etype, content, True, state_key=state_key
)
current_state_group = context._state_group
events_to_send.append((event, context))
+ if tp_event:
+ third_party_events_to_append.append(tp_event)
if config["encrypted"]:
- encryption_event, encryption_context = await create_event(
+ encryption_event, encryption_context, encrypt_tp_event = await create_event(
EventTypes.RoomEncryption,
{"algorithm": RoomEncryptionAlgorithms.DEFAULT},
True,
state_key="",
)
events_to_send.append((encryption_event, encryption_context))
+ if encrypt_tp_event:
+ third_party_events_to_append.append(encrypt_tp_event)
+
+ for event_dict in third_party_events_to_append:
+ (
+ event,
+ unpersisted_context,
+ _,
+ ) = await self.event_creation_handler.create_event(
+ creator,
+ event_dict,
+ prev_event_ids=prev_event,
+ state_map=state_map,
+ for_batch=True,
+ current_state_group=current_state_group,
+ )
+ context = await unpersisted_context.persist(event)
+ events_to_send.append((event, context))
last_event = await self.event_creation_handler.handle_new_client_event(
creator,
diff --git a/synapse/handlers/room_batch.py b/synapse/handlers/room_batch.py
index c73d2adaad..3d432ac295 100644
--- a/synapse/handlers/room_batch.py
+++ b/synapse/handlers/room_batch.py
@@ -327,7 +327,7 @@ class RoomBatchHandler:
# Mark all events as historical
event_dict["content"][EventContentFields.MSC2716_HISTORICAL] = True
- event, context = await self.event_creation_handler.create_event(
+ event, context, _ = await self.event_creation_handler.create_event(
await self.create_requester_for_user_id_from_app_service(
ev["sender"], app_service_requester.app_service
),
diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py
index a965c7ec76..f136ee0e97 100644
--- a/synapse/handlers/room_member.py
+++ b/synapse/handlers/room_member.py
@@ -414,7 +414,11 @@ class RoomMemberHandler(metaclass=abc.ABCMeta):
max_retries = 5
for i in range(max_retries):
try:
- event, context = await self.event_creation_handler.create_event(
+ (
+ event,
+ context,
+ third_party_event,
+ ) = await self.event_creation_handler.create_event(
requester,
{
"type": EventTypes.Member,
@@ -468,6 +472,20 @@ class RoomMemberHandler(metaclass=abc.ABCMeta):
ratelimit=ratelimit,
)
)
+ if third_party_event:
+ (
+ tp_event,
+ tp_unpersisted_context,
+ _,
+ ) = await self.event_creation_handler.create_event(
+ requester,
+ third_party_event,
+ prev_event_ids=[result_event.event_id],
+ )
+ tp_context = await tp_unpersisted_context.persist(tp_event)
+ await self.event_creation_handler.handle_new_client_event(
+ requester, events_and_context=[(tp_event, tp_context)]
+ )
if event.membership == Membership.LEAVE:
if prev_member_event_id:
@@ -1944,7 +1962,11 @@ class RoomMemberMasterHandler(RoomMemberHandler):
max_retries = 5
for i in range(max_retries):
try:
- event, context = await self.event_creation_handler.create_event(
+ (
+ event,
+ context,
+ third_party_event_dict,
+ ) = await self.event_creation_handler.create_event(
requester,
event_dict,
txn_id=txn_id,
@@ -1954,10 +1976,24 @@ class RoomMemberMasterHandler(RoomMemberHandler):
)
event.internal_metadata.out_of_band_membership = True
+ events_and_context = [(event, context)]
+ if third_party_event_dict:
+ (
+ third_party_event,
+ third_party_unpersisted_context,
+ _,
+ ) = await self.event_creation_handler.create_event(
+ requester, third_party_event_dict
+ )
+ third_party_context = await third_party_unpersisted_context.persist(
+ event
+ )
+ events_and_context.append((third_party_event, third_party_context))
+
result_event = (
await self.event_creation_handler.handle_new_client_event(
requester,
- events_and_context=[(event, context)],
+ events_and_context=events_and_context,
extra_users=[UserID.from_string(target_user)],
)
)
|