Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Commit ec0edff

Browse files
MadLittleModsMic92
authored andcommitted
Use fully-qualified PersistedEventPosition when returning RoomsForUser (element-hq#17265)
Use fully-qualified `PersistedEventPosition` (`instance_name` and `stream_ordering`) when returning `RoomsForUser` to facilitate proper comparisons and `RoomStreamToken` generation. Spawning from element-hq#17187 where we want to utilize this change
1 parent 51147d4 commit ec0edff

File tree

11 files changed

+85
-75
lines changed

11 files changed

+85
-75
lines changed

changelog.d/17265.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use fully-qualified `PersistedEventPosition` when returning `RoomsForUser` to facilitate proper comparisons and `RoomStreamToken` generation.

synapse/federation/federation_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ async def on_make_join_request(
674674
# This is in addition to the HS-level rate limiting applied by
675675
# BaseFederationServlet.
676676
# type-ignore: mypy doesn't seem able to deduce the type of the limiter(!?)
677-
await self._room_member_handler._join_rate_per_room_limiter.ratelimit( # type: ignore[has-type]
677+
await self._room_member_handler._join_rate_per_room_limiter.ratelimit(
678678
requester=None,
679679
key=room_id,
680680
update=False,
@@ -717,7 +717,7 @@ async def on_send_join_request(
717717
SynapseTags.SEND_JOIN_RESPONSE_IS_PARTIAL_STATE,
718718
caller_supports_partial_state,
719719
)
720-
await self._room_member_handler._join_rate_per_room_limiter.ratelimit( # type: ignore[has-type]
720+
await self._room_member_handler._join_rate_per_room_limiter.ratelimit(
721721
requester=None,
722722
key=room_id,
723723
update=False,

synapse/handlers/admin.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,7 @@ async def export_user_data(self, user_id: str, writer: "ExfiltrationWriter") ->
126126
# Get all rooms the user is in or has been in
127127
rooms = await self._store.get_rooms_for_local_user_where_membership_is(
128128
user_id,
129-
membership_list=(
130-
Membership.JOIN,
131-
Membership.LEAVE,
132-
Membership.BAN,
133-
Membership.INVITE,
134-
Membership.KNOCK,
135-
),
129+
membership_list=Membership.LIST,
136130
)
137131

138132
# We only try and fetch events for rooms the user has been in. If
@@ -179,7 +173,7 @@ async def export_user_data(self, user_id: str, writer: "ExfiltrationWriter") ->
179173
if room.membership == Membership.JOIN:
180174
stream_ordering = self._store.get_room_max_stream_ordering()
181175
else:
182-
stream_ordering = room.stream_ordering
176+
stream_ordering = room.event_pos.stream
183177

184178
from_key = RoomStreamToken(topological=0, stream=0)
185179
to_key = RoomStreamToken(stream=stream_ordering)

synapse/handlers/initial_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ async def handle_room(event: RoomsForUser) -> None:
199199
)
200200
elif event.membership == Membership.LEAVE:
201201
room_end_token = RoomStreamToken(
202-
stream=event.stream_ordering,
202+
stream=event.event_pos.stream,
203203
)
204204
deferred_room_state = run_in_background(
205205
self._state_storage_controller.get_state_for_events,

synapse/handlers/pagination.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from synapse.api.errors import SynapseError
2828
from synapse.api.filtering import Filter
2929
from synapse.events.utils import SerializeEventConfig
30-
from synapse.handlers.room import ShutdownRoomParams, ShutdownRoomResponse
3130
from synapse.handlers.worker_lock import NEW_EVENT_DURING_PURGE_LOCK_NAME
3231
from synapse.logging.opentracing import trace
3332
from synapse.metrics.background_process_metrics import run_as_background_process
@@ -38,6 +37,8 @@
3837
JsonMapping,
3938
Requester,
4039
ScheduledTask,
40+
ShutdownRoomParams,
41+
ShutdownRoomResponse,
4142
StreamKeyType,
4243
TaskStatus,
4344
)

synapse/handlers/room.py

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
)
4141

4242
import attr
43-
from typing_extensions import TypedDict
4443

4544
import synapse.events.snapshot
4645
from synapse.api.constants import (
@@ -81,6 +80,8 @@
8180
RoomAlias,
8281
RoomID,
8382
RoomStreamToken,
83+
ShutdownRoomParams,
84+
ShutdownRoomResponse,
8485
StateMap,
8586
StrCollection,
8687
StreamKeyType,
@@ -1780,63 +1781,6 @@ def get_current_key_for_room(self, room_id: str) -> Awaitable[RoomStreamToken]:
17801781
return self.store.get_current_room_stream_token_for_room_id(room_id)
17811782

17821783

1783-
class ShutdownRoomParams(TypedDict):
1784-
"""
1785-
Attributes:
1786-
requester_user_id:
1787-
User who requested the action. Will be recorded as putting the room on the
1788-
blocking list.
1789-
new_room_user_id:
1790-
If set, a new room will be created with this user ID
1791-
as the creator and admin, and all users in the old room will be
1792-
moved into that room. If not set, no new room will be created
1793-
and the users will just be removed from the old room.
1794-
new_room_name:
1795-
A string representing the name of the room that new users will
1796-
be invited to. Defaults to `Content Violation Notification`
1797-
message:
1798-
A string containing the first message that will be sent as
1799-
`new_room_user_id` in the new room. Ideally this will clearly
1800-
convey why the original room was shut down.
1801-
Defaults to `Sharing illegal content on this server is not
1802-
permitted and rooms in violation will be blocked.`
1803-
block:
1804-
If set to `true`, this room will be added to a blocking list,
1805-
preventing future attempts to join the room. Defaults to `false`.
1806-
purge:
1807-
If set to `true`, purge the given room from the database.
1808-
force_purge:
1809-
If set to `true`, the room will be purged from database
1810-
even if there are still users joined to the room.
1811-
"""
1812-
1813-
requester_user_id: Optional[str]
1814-
new_room_user_id: Optional[str]
1815-
new_room_name: Optional[str]
1816-
message: Optional[str]
1817-
block: bool
1818-
purge: bool
1819-
force_purge: bool
1820-
1821-
1822-
class ShutdownRoomResponse(TypedDict):
1823-
"""
1824-
Attributes:
1825-
kicked_users: An array of users (`user_id`) that were kicked.
1826-
failed_to_kick_users:
1827-
An array of users (`user_id`) that that were not kicked.
1828-
local_aliases:
1829-
An array of strings representing the local aliases that were
1830-
migrated from the old room to the new.
1831-
new_room_id: A string representing the room ID of the new room.
1832-
"""
1833-
1834-
kicked_users: List[str]
1835-
failed_to_kick_users: List[str]
1836-
local_aliases: List[str]
1837-
new_room_id: Optional[str]
1838-
1839-
18401784
class RoomShutdownHandler:
18411785
DEFAULT_MESSAGE = (
18421786
"Sharing illegal content on this server is not permitted and rooms in"

synapse/handlers/sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,7 @@ async def _get_room_changes_for_initial_sync(
28052805
continue
28062806

28072807
leave_token = now_token.copy_and_replace(
2808-
StreamKeyType.ROOM, RoomStreamToken(stream=event.stream_ordering)
2808+
StreamKeyType.ROOM, RoomStreamToken(stream=event.event_pos.stream)
28092809
)
28102810
room_entries.append(
28112811
RoomSyncResultBuilder(

synapse/storage/databases/main/roommember.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def _get_rooms_for_local_user_where_membership_is_txn(
476476
)
477477

478478
sql = """
479-
SELECT room_id, e.sender, c.membership, event_id, e.stream_ordering, r.room_version
479+
SELECT room_id, e.sender, c.membership, event_id, e.instance_name, e.stream_ordering, r.room_version
480480
FROM local_current_membership AS c
481481
INNER JOIN events AS e USING (room_id, event_id)
482482
INNER JOIN rooms AS r USING (room_id)
@@ -488,7 +488,17 @@ def _get_rooms_for_local_user_where_membership_is_txn(
488488
)
489489

490490
txn.execute(sql, (user_id, *args))
491-
results = [RoomsForUser(*r) for r in txn]
491+
results = [
492+
RoomsForUser(
493+
room_id=room_id,
494+
sender=sender,
495+
membership=membership,
496+
event_id=event_id,
497+
event_pos=PersistedEventPosition(instance_name, stream_ordering),
498+
room_version_id=room_version,
499+
)
500+
for room_id, sender, membership, event_id, instance_name, stream_ordering, room_version in txn
501+
]
492502

493503
return results
494504

synapse/storage/roommember.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class RoomsForUser:
3535
sender: str
3636
membership: str
3737
event_id: str
38-
stream_ordering: int
38+
event_pos: PersistedEventPosition
3939
room_version_id: str
4040

4141

synapse/types/__init__.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,3 +1279,60 @@ class ScheduledTask:
12791279
result: Optional[JsonMapping]
12801280
# Optional error that should be assigned a value when the status is FAILED
12811281
error: Optional[str]
1282+
1283+
1284+
class ShutdownRoomParams(TypedDict):
1285+
"""
1286+
Attributes:
1287+
requester_user_id:
1288+
User who requested the action. Will be recorded as putting the room on the
1289+
blocking list.
1290+
new_room_user_id:
1291+
If set, a new room will be created with this user ID
1292+
as the creator and admin, and all users in the old room will be
1293+
moved into that room. If not set, no new room will be created
1294+
and the users will just be removed from the old room.
1295+
new_room_name:
1296+
A string representing the name of the room that new users will
1297+
be invited to. Defaults to `Content Violation Notification`
1298+
message:
1299+
A string containing the first message that will be sent as
1300+
`new_room_user_id` in the new room. Ideally this will clearly
1301+
convey why the original room was shut down.
1302+
Defaults to `Sharing illegal content on this server is not
1303+
permitted and rooms in violation will be blocked.`
1304+
block:
1305+
If set to `true`, this room will be added to a blocking list,
1306+
preventing future attempts to join the room. Defaults to `false`.
1307+
purge:
1308+
If set to `true`, purge the given room from the database.
1309+
force_purge:
1310+
If set to `true`, the room will be purged from database
1311+
even if there are still users joined to the room.
1312+
"""
1313+
1314+
requester_user_id: Optional[str]
1315+
new_room_user_id: Optional[str]
1316+
new_room_name: Optional[str]
1317+
message: Optional[str]
1318+
block: bool
1319+
purge: bool
1320+
force_purge: bool
1321+
1322+
1323+
class ShutdownRoomResponse(TypedDict):
1324+
"""
1325+
Attributes:
1326+
kicked_users: An array of users (`user_id`) that were kicked.
1327+
failed_to_kick_users:
1328+
An array of users (`user_id`) that that were not kicked.
1329+
local_aliases:
1330+
An array of strings representing the local aliases that were
1331+
migrated from the old room to the new.
1332+
new_room_id: A string representing the room ID of the new room.
1333+
"""
1334+
1335+
kicked_users: List[str]
1336+
failed_to_kick_users: List[str]
1337+
local_aliases: List[str]
1338+
new_room_id: Optional[str]

0 commit comments

Comments
 (0)