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

Commit 84facf7

Browse files
Fix /messages throwing a 500 when querying for non-existent room (#12683)
Fix #12678 Complement test added: matrix-org/complement#369 **Before:** 500 internal server error **After:** According to the [spec](https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3roomsroomidmessages), calling `/messages` against a non-existent `room_id` should throw a 403 forbidden (since you're not part of the room). This also matches the behavior before #12370 which regressed Synapse to the 500 behavior. ```json { "errcode": "M_FORBIDDEN", "error": "User @test:my.synapse.server not in room !dne:my.synapse.server, and room previews are disabled" } ```
1 parent c72d26c commit 84facf7

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

changelog.d/12683.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug introduced in Synapse 1.57.0 where `/messages` would throw a 500 error when querying for a non-existent room.

synapse/handlers/pagination.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ async def get_messages(
448448
)
449449
# We expect `/messages` to use historic pagination tokens by default but
450450
# `/messages` should still works with live tokens when manually provided.
451-
assert from_token.room_key.topological
451+
assert from_token.room_key.topological is not None
452452

453453
if pagin_config.limit is None:
454454
# This shouldn't happen as we've set a default limit before this

synapse/storage/databases/main/stream.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -785,22 +785,14 @@ async def get_last_event_in_room_before_stream_ordering(
785785
return None
786786

787787
async def get_current_room_stream_token_for_room_id(
788-
self, room_id: Optional[str] = None
788+
self, room_id: str
789789
) -> RoomStreamToken:
790-
"""Returns the current position of the rooms stream.
791-
792-
By default, it returns a live token with the current global stream
793-
token. Specifying a `room_id` causes it to return a historic token with
794-
the room specific topological token.
795-
"""
790+
"""Returns the current position of the rooms stream (historic token)."""
796791
stream_ordering = self.get_room_max_stream_ordering()
797-
if room_id is None:
798-
return RoomStreamToken(None, stream_ordering)
799-
else:
800-
topo = await self.db_pool.runInteraction(
801-
"_get_max_topological_txn", self._get_max_topological_txn, room_id
802-
)
803-
return RoomStreamToken(topo, stream_ordering)
792+
topo = await self.db_pool.runInteraction(
793+
"_get_max_topological_txn", self._get_max_topological_txn, room_id
794+
)
795+
return RoomStreamToken(topo, stream_ordering)
804796

805797
def get_stream_id_for_event_txn(
806798
self,
@@ -870,7 +862,11 @@ def _get_max_topological_txn(self, txn: LoggingTransaction, room_id: str) -> int
870862
)
871863

872864
rows = txn.fetchall()
873-
return rows[0][0] if rows else 0
865+
# An aggregate function like MAX() will always return one row per group
866+
# so we can safely rely on the lookup here. For example, when a we
867+
# lookup a `room_id` which does not exist, `rows` will look like
868+
# `[(None,)]`
869+
return rows[0][0] if rows[0][0] is not None else 0
874870

875871
@staticmethod
876872
def _set_before_and_after(

0 commit comments

Comments
 (0)