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

Commit 4433d01

Browse files
authored
Merge pull request #8537 from matrix-org/rav/simplify_locally_reject_invite
Simplify `_locally_reject_invite`
2 parents 19b15d6 + d9dc618 commit 4433d01

File tree

9 files changed

+56
-65
lines changed

9 files changed

+56
-65
lines changed

changelog.d/8537.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Factor out common code between `RoomMemberHandler._locally_reject_invite` and `EventCreationHandler.create_event`.

synapse/events/builder.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,37 @@ def state_key(self):
9797
def is_state(self):
9898
return self._state_key is not None
9999

100-
async def build(self, prev_event_ids: List[str]) -> EventBase:
100+
async def build(
101+
self, prev_event_ids: List[str], auth_event_ids: Optional[List[str]]
102+
) -> EventBase:
101103
"""Transform into a fully signed and hashed event
102104
103105
Args:
104106
prev_event_ids: The event IDs to use as the prev events
107+
auth_event_ids: The event IDs to use as the auth events.
108+
Should normally be set to None, which will cause them to be calculated
109+
based on the room state at the prev_events.
105110
106111
Returns:
107112
The signed and hashed event.
108113
"""
109-
110-
state_ids = await self._state.get_current_state_ids(
111-
self.room_id, prev_event_ids
112-
)
113-
auth_ids = self._auth.compute_auth_events(self, state_ids)
114+
if auth_event_ids is None:
115+
state_ids = await self._state.get_current_state_ids(
116+
self.room_id, prev_event_ids
117+
)
118+
auth_event_ids = self._auth.compute_auth_events(self, state_ids)
114119

115120
format_version = self.room_version.event_format
116121
if format_version == EventFormatVersions.V1:
117122
# The types of auth/prev events changes between event versions.
118123
auth_events = await self._store.add_event_hashes(
119-
auth_ids
124+
auth_event_ids
120125
) # type: Union[List[str], List[Tuple[str, Dict[str, str]]]]
121126
prev_events = await self._store.add_event_hashes(
122127
prev_event_ids
123128
) # type: Union[List[str], List[Tuple[str, Dict[str, str]]]]
124129
else:
125-
auth_events = auth_ids
130+
auth_events = auth_event_ids
126131
prev_events = prev_event_ids
127132

128133
old_depth = await self._store.get_max_depth_of(prev_event_ids)

synapse/handlers/message.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,9 @@ async def create_event(
437437
self,
438438
requester: Requester,
439439
event_dict: dict,
440-
token_id: Optional[str] = None,
441440
txn_id: Optional[str] = None,
442441
prev_event_ids: Optional[List[str]] = None,
442+
auth_event_ids: Optional[List[str]] = None,
443443
require_consent: bool = True,
444444
) -> Tuple[EventBase, EventContext]:
445445
"""
@@ -453,13 +453,18 @@ async def create_event(
453453
Args:
454454
requester
455455
event_dict: An entire event
456-
token_id
457456
txn_id
458457
prev_event_ids:
459458
the forward extremities to use as the prev_events for the
460459
new event.
461460
462461
If None, they will be requested from the database.
462+
463+
auth_event_ids:
464+
The event ids to use as the auth_events for the new event.
465+
Should normally be left as None, which will cause them to be calculated
466+
based on the room state at the prev_events.
467+
463468
require_consent: Whether to check if the requester has
464469
consented to the privacy policy.
465470
Raises:
@@ -511,14 +516,17 @@ async def create_event(
511516
if require_consent and not is_exempt:
512517
await self.assert_accepted_privacy_policy(requester)
513518

514-
if token_id is not None:
515-
builder.internal_metadata.token_id = token_id
519+
if requester.access_token_id is not None:
520+
builder.internal_metadata.token_id = requester.access_token_id
516521

517522
if txn_id is not None:
518523
builder.internal_metadata.txn_id = txn_id
519524

520525
event, context = await self.create_new_client_event(
521-
builder=builder, requester=requester, prev_event_ids=prev_event_ids,
526+
builder=builder,
527+
requester=requester,
528+
prev_event_ids=prev_event_ids,
529+
auth_event_ids=auth_event_ids,
522530
)
523531

524532
# In an ideal world we wouldn't need the second part of this condition. However,
@@ -726,7 +734,7 @@ async def create_and_send_nonmember_event(
726734
return event, event.internal_metadata.stream_ordering
727735

728736
event, context = await self.create_event(
729-
requester, event_dict, token_id=requester.access_token_id, txn_id=txn_id
737+
requester, event_dict, txn_id=txn_id
730738
)
731739

732740
assert self.hs.is_mine_id(event.sender), "User must be our own: %s" % (
@@ -757,6 +765,7 @@ async def create_new_client_event(
757765
builder: EventBuilder,
758766
requester: Optional[Requester] = None,
759767
prev_event_ids: Optional[List[str]] = None,
768+
auth_event_ids: Optional[List[str]] = None,
760769
) -> Tuple[EventBase, EventContext]:
761770
"""Create a new event for a local client
762771
@@ -769,6 +778,11 @@ async def create_new_client_event(
769778
770779
If None, they will be requested from the database.
771780
781+
auth_event_ids:
782+
The event ids to use as the auth_events for the new event.
783+
Should normally be left as None, which will cause them to be calculated
784+
based on the room state at the prev_events.
785+
772786
Returns:
773787
Tuple of created event, context
774788
"""
@@ -790,7 +804,9 @@ async def create_new_client_event(
790804
builder.type == EventTypes.Create or len(prev_event_ids) > 0
791805
), "Attempting to create an event with no prev_events"
792806

793-
event = await builder.build(prev_event_ids=prev_event_ids)
807+
event = await builder.build(
808+
prev_event_ids=prev_event_ids, auth_event_ids=auth_event_ids
809+
)
794810
context = await self.state.compute_event_context(event)
795811
if requester:
796812
context.app_service = requester.app_service

synapse/handlers/room.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ async def _upgrade_room(
214214
"replacement_room": new_room_id,
215215
},
216216
},
217-
token_id=requester.access_token_id,
218217
)
219218
old_room_version = await self.store.get_room_version_id(old_room_id)
220219
await self.auth.check_from_context(

synapse/handlers/room_member.py

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
import logging
1818
import random
1919
from http import HTTPStatus
20-
from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union
21-
22-
from unpaddedbase64 import encode_base64
20+
from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple
2321

2422
from synapse import types
25-
from synapse.api.constants import MAX_DEPTH, AccountDataTypes, EventTypes, Membership
23+
from synapse.api.constants import AccountDataTypes, EventTypes, Membership
2624
from synapse.api.errors import (
2725
AuthError,
2826
Codes,
@@ -31,12 +29,8 @@
3129
SynapseError,
3230
)
3331
from synapse.api.ratelimiting import Ratelimiter
34-
from synapse.api.room_versions import EventFormatVersions
35-
from synapse.crypto.event_signing import compute_event_reference_hash
3632
from synapse.events import EventBase
37-
from synapse.events.builder import create_local_event_from_event_dict
3833
from synapse.events.snapshot import EventContext
39-
from synapse.events.validator import EventValidator
4034
from synapse.storage.roommember import RoomsForUser
4135
from synapse.types import JsonDict, Requester, RoomAlias, RoomID, StateMap, UserID
4236
from synapse.util.async_helpers import Linearizer
@@ -193,7 +187,6 @@ async def _local_membership_update(
193187
# For backwards compatibility:
194188
"membership": membership,
195189
},
196-
token_id=requester.access_token_id,
197190
txn_id=txn_id,
198191
prev_event_ids=prev_event_ids,
199192
require_consent=require_consent,
@@ -1133,56 +1126,34 @@ async def _locally_reject_invite(
11331126

11341127
room_id = invite_event.room_id
11351128
target_user = invite_event.state_key
1136-
room_version = await self.store.get_room_version(room_id)
11371129

11381130
content["membership"] = Membership.LEAVE
11391131

1140-
# the auth events for the new event are the same as that of the invite, plus
1141-
# the invite itself.
1142-
#
1143-
# the prev_events are just the invite.
1144-
invite_hash = invite_event.event_id # type: Union[str, Tuple]
1145-
if room_version.event_format == EventFormatVersions.V1:
1146-
alg, h = compute_event_reference_hash(invite_event)
1147-
invite_hash = (invite_event.event_id, {alg: encode_base64(h)})
1148-
1149-
auth_events = tuple(invite_event.auth_events) + (invite_hash,)
1150-
prev_events = (invite_hash,)
1151-
1152-
# we cap depth of generated events, to ensure that they are not
1153-
# rejected by other servers (and so that they can be persisted in
1154-
# the db)
1155-
depth = min(invite_event.depth + 1, MAX_DEPTH)
1156-
11571132
event_dict = {
1158-
"depth": depth,
1159-
"auth_events": auth_events,
1160-
"prev_events": prev_events,
11611133
"type": EventTypes.Member,
11621134
"room_id": room_id,
11631135
"sender": target_user,
11641136
"content": content,
11651137
"state_key": target_user,
11661138
}
11671139

1168-
event = create_local_event_from_event_dict(
1169-
clock=self.clock,
1170-
hostname=self.hs.hostname,
1171-
signing_key=self.hs.signing_key,
1172-
room_version=room_version,
1173-
event_dict=event_dict,
1140+
# the auth events for the new event are the same as that of the invite, plus
1141+
# the invite itself.
1142+
#
1143+
# the prev_events are just the invite.
1144+
prev_event_ids = [invite_event.event_id]
1145+
auth_event_ids = invite_event.auth_event_ids() + prev_event_ids
1146+
1147+
event, context = await self.event_creation_handler.create_event(
1148+
requester,
1149+
event_dict,
1150+
txn_id=txn_id,
1151+
prev_event_ids=prev_event_ids,
1152+
auth_event_ids=auth_event_ids,
11741153
)
11751154
event.internal_metadata.outlier = True
11761155
event.internal_metadata.out_of_band_membership = True
1177-
if txn_id is not None:
1178-
event.internal_metadata.txn_id = txn_id
1179-
if requester.access_token_id is not None:
1180-
event.internal_metadata.token_id = requester.access_token_id
1181-
1182-
EventValidator().validate_new(event, self.config)
11831156

1184-
context = await self.state_handler.compute_event_context(event)
1185-
context.app_service = requester.app_service
11861157
result_event = await self.event_creation_handler.handle_new_client_event(
11871158
requester, event, context, extra_users=[UserID.from_string(target_user)],
11881159
)

tests/handlers/test_message.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def _create_duplicate_event(self, txn_id: str) -> Tuple[EventBase, EventContext]
6666
"sender": self.requester.user.to_string(),
6767
"content": {"msgtype": "m.text", "body": random_string(5)},
6868
},
69-
token_id=self.token_id,
7069
txn_id=txn_id,
7170
)
7271
)

tests/handlers/test_presence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def _add_new_user(self, room_id, user_id):
615615
self.store.get_latest_event_ids_in_room(room_id)
616616
)
617617

618-
event = self.get_success(builder.build(prev_event_ids))
618+
event = self.get_success(builder.build(prev_event_ids, None))
619619

620620
self.get_success(self.federation_handler.on_receive_pdu(hostname, event))
621621

tests/replication/test_federation_sender_shard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def create_room_with_remote_server(self, user, token, remote_server="other_serve
226226
}
227227

228228
builder = factory.for_room_version(room_version, event_dict)
229-
join_event = self.get_success(builder.build(prev_event_ids))
229+
join_event = self.get_success(builder.build(prev_event_ids, None))
230230

231231
self.get_success(federation.on_send_join_request(remote_server, join_event))
232232
self.replicate()

tests/storage/test_redaction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ def __init__(self, base_builder, event_id):
236236
self._event_id = event_id
237237

238238
@defer.inlineCallbacks
239-
def build(self, prev_event_ids):
239+
def build(self, prev_event_ids, auth_event_ids):
240240
built_event = yield defer.ensureDeferred(
241-
self._base_builder.build(prev_event_ids)
241+
self._base_builder.build(prev_event_ids, auth_event_ids)
242242
)
243243

244244
built_event._event_id = self._event_id

0 commit comments

Comments
 (0)