Skip to content

Commit

Permalink
Use event_auth table to get previous membership
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Aug 28, 2024
1 parent 6f9932d commit ab414f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
31 changes: 12 additions & 19 deletions synapse/storage/databases/main/events_bg_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,33 +1937,27 @@ def _find_memberships_to_update_txn(
return 0

def _find_previous_membership_txn(
txn: LoggingTransaction, room_id: str, user_id: str, stream_ordering: int
txn: LoggingTransaction, event_id: str, user_id: str
) -> Tuple[str, str]:
# Find the previous invite/knock event before the leave event
# Find the previous invite/knock event before the leave event. This
# is done by looking at the auth events of the invite/knock and
# finding the corresponding membership event.
txn.execute(
"""
SELECT event_id, membership
FROM room_memberships
WHERE
room_id = ?
AND user_id = ?
AND event_stream_ordering < ?
ORDER BY event_stream_ordering DESC
LIMIT 1
SELECT m.event_id, m.membership
FROM event_auth AS a
INNER JOIN room_memberships AS m ON (a.auth_id = m.event_id)
WHERE a.event_id = ? AND m.user_id = ?
""",
(
room_id,
user_id,
stream_ordering,
),
(event_id, user_id),
)
row = txn.fetchone()

# We should see a corresponding previous invite/knock event
assert row is not None
event_id, membership = row
previous_event_id, membership = row

return event_id, membership
return previous_event_id, membership

# Map from (room_id, user_id) to ...
to_insert_membership_snapshots: Dict[
Expand Down Expand Up @@ -2057,9 +2051,8 @@ def _find_previous_membership_txn(
await self.db_pool.runInteraction(
"sliding_sync_membership_snapshots_bg_update._find_previous_membership",
_find_previous_membership_txn,
room_id,
membership_event_id,
user_id,
membership_event_stream_ordering,
)
)

Expand Down
10 changes: 6 additions & 4 deletions tests/storage/test_sliding_sync_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ def _create_remote_invite_room_for_user(
return invite_room_id, persisted_event

def _retract_remote_invite_for_user(
self,
user_id: str,
remote_room_id: str,
self, user_id: str, remote_room_id: str, invite_event_id: str
) -> EventBase:
"""
Create a fake invite retraction for a remote room and persist it.
Expand All @@ -285,6 +283,7 @@ def _retract_remote_invite_for_user(
user_id: The person who was invited and we're going to retract the
invite for.
remote_room_id: The room ID that the invite was for.
invite_event_id: The event ID of the invite
Returns:
The persisted leave (kick) event.
Expand All @@ -298,7 +297,7 @@ def _retract_remote_invite_for_user(
"origin_server_ts": 1,
"type": EventTypes.Member,
"content": {"membership": Membership.LEAVE},
"auth_events": [],
"auth_events": [invite_event_id],
"prev_events": [],
}

Expand Down Expand Up @@ -2202,6 +2201,7 @@ def test_non_join_retracted_remote_invite(self) -> None:
remote_invite_retraction_event = self._retract_remote_invite_for_user(
user_id=user1_id,
remote_room_id=remote_invite_room_id,
invite_event_id=remote_invite_event.event_id,
)

# No one local is joined to the remote room
Expand Down Expand Up @@ -3549,13 +3549,15 @@ def test_membership_snapshots_background_update_remote_invite_rejections_and_ret
room_id_no_info_leave_event = self._retract_remote_invite_for_user(
user_id=user1_id,
remote_room_id=room_id_no_info,
invite_event_id=room_id_no_info_invite_event.event_id,
)
room_id_with_info_leave_event_response = self.helper.leave(
room_id_with_info, user1_id, tok=user1_tok
)
space_room_id_leave_event = self._retract_remote_invite_for_user(
user_id=user1_id,
remote_room_id=space_room_id,
invite_event_id=space_room_id_invite_event.event_id,
)

# Clean-up the `sliding_sync_membership_snapshots` table as if the inserts did not
Expand Down

0 comments on commit ab414f2

Please sign in to comment.