From b281f6428992132de0485aba5a8b39e950e28322 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 27 Oct 2020 17:56:33 +0000 Subject: [PATCH 1/3] Abstract code for stripping room state into a separate method --- synapse/handlers/message.py | 35 +++--------- .../storage/databases/main/events_worker.py | 54 ++++++++++++++++++- 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index f1b4d35182cc..4ead75ec3a3c 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -1100,34 +1100,13 @@ async def persist_and_notify_client_event( if event.type == EventTypes.Member: if event.content["membership"] == Membership.INVITE: - - def is_inviter_member_event(e): - return e.type == EventTypes.Member and e.sender == event.sender - - current_state_ids = await context.get_current_state_ids() - - # We know this event is not an outlier, so this must be - # non-None. - assert current_state_ids is not None - - state_to_include_ids = [ - e_id - for k, e_id in current_state_ids.items() - if k[0] in self.room_invite_state_types - or k == (EventTypes.Member, event.sender) - ] - - state_to_include = await self.store.get_events(state_to_include_ids) - - event.unsigned["invite_room_state"] = [ - { - "type": e.type, - "state_key": e.state_key, - "content": e.content, - "sender": e.sender, - } - for e in state_to_include.values() - ] + event.unsigned[ + "invite_room_state" + ] = await self.store.get_stripped_room_state_from_event_context( + context, + self.room_invite_state_types, + membership_user_id=event.sender, + ) invitee = UserID.from_string(event.state_key) if not self.hs.is_mine(invitee): diff --git a/synapse/storage/databases/main/events_worker.py b/synapse/storage/databases/main/events_worker.py index 6e7f16f39c05..b8871a1e04e8 100644 --- a/synapse/storage/databases/main/events_worker.py +++ b/synapse/storage/databases/main/events_worker.py @@ -31,6 +31,7 @@ RoomVersions, ) from synapse.events import EventBase, make_event_from_dict +from synapse.events.snapshot import EventContext from synapse.events.utils import prune_event from synapse.logging.context import PreserveLoggingContext, current_context from synapse.metrics.background_process_metrics import ( @@ -44,7 +45,7 @@ from synapse.storage.database import DatabasePool from synapse.storage.engines import PostgresEngine from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator -from synapse.types import Collection, get_domain_from_id +from synapse.types import Collection, JsonDict, get_domain_from_id from synapse.util.caches.descriptors import cached from synapse.util.caches.lrucache import LruCache from synapse.util.iterutils import batch_iter @@ -525,6 +526,57 @@ def _get_events_from_cache(self, events, allow_rejected, update_metrics=True): return event_map + async def get_stripped_room_state_from_event_context( + self, + context: EventContext, + state_types_to_include: List[EventTypes], + membership_user_id: Optional[str], + ) -> List[JsonDict]: + """ + Retrieve the stripped state from a room, given an event context to retrieve state + from as well as the state types to include. Optionally additionally include membership + events from a specific user. + + "Stripped" state means that only the `type`, `state_key`, `content` and `sender` keys + are included from each state event. + + Args: + context: The event context to retrieve state of the room from. + state_types_to_include: The type of state events to include. + membership_user_id: An optional user ID to include the stripped membership state + events of. This is useful when generating the stripped state of a room for + invites. We'd want to send membership events of the inviter, so that the + invitee can display the inviter's profile information if the room lacks any. + + Returns: + A list of dictionaries, each representing a stripped state event from the room. + """ + current_state_ids = await context.get_current_state_ids() + + # We know this event is not an outlier, so this must be + # non-None. + assert current_state_ids is not None + + # The state to include + state_to_include_ids = [ + e_id + for k, e_id in current_state_ids.items() + if k[0] in state_types_to_include + or (membership_user_id and k == (EventTypes.Member, membership_user_id)) + ] + + state_to_include = await self.get_events(state_to_include_ids) + + return [ + { + "type": e.type, + "state_key": e.state_key, + "content": e.content, + "sender": e.sender, + } + for e in state_to_include.values() + ] + def _do_fetch(self, conn): """Takes a database connection and waits for requests for events from the _event_fetch_list queue. From 6fd773f645e6687090d090ff1097362e6d2ab759 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 27 Oct 2020 18:09:34 +0000 Subject: [PATCH 2/3] Add changelog --- changelog.d/8671.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/8671.misc diff --git a/changelog.d/8671.misc b/changelog.d/8671.misc new file mode 100644 index 000000000000..bef8dc425a01 --- /dev/null +++ b/changelog.d/8671.misc @@ -0,0 +1 @@ +Abstract some invite-related code in preparation for landing knocking. \ No newline at end of file From baf461ee59d3e3dd38606be8a4798ae1c6938376 Mon Sep 17 00:00:00 2001 From: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> Date: Tue, 27 Oct 2020 18:42:00 +0000 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Patrick Cloke --- synapse/storage/databases/main/events_worker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/storage/databases/main/events_worker.py b/synapse/storage/databases/main/events_worker.py index b8871a1e04e8..cd1f31aa6209 100644 --- a/synapse/storage/databases/main/events_worker.py +++ b/synapse/storage/databases/main/events_worker.py @@ -534,7 +534,7 @@ async def get_stripped_room_state_from_event_context( ) -> List[JsonDict]: """ Retrieve the stripped state from a room, given an event context to retrieve state - from as well as the state types to include. Optionally additionally include membership + from as well as the state types to include. Optionally, include the membership events from a specific user. "Stripped" state means that only the `type`, `state_key`, `content` and `sender` keys @@ -545,7 +545,7 @@ async def get_stripped_room_state_from_event_context( state_types_to_include: The type of state events to include. membership_user_id: An optional user ID to include the stripped membership state events of. This is useful when generating the stripped state of a room for - invites. We'd want to send membership events of the inviter, so that the + invites. We want to send membership events of the inviter, so that the invitee can display the inviter's profile information if the room lacks any. Returns: