Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to filter by state event type on admin room state endpoint #18035

Merged
merged 6 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/18035.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Adds a query param `type` to `/_synapse/admin/v1/rooms/{room_id}/state` that filters the state event
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
query by state event type.
7 changes: 7 additions & 0 deletions docs/admin_api/rooms.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ The API is:
GET /_synapse/admin/v1/rooms/<room_id>/state
```

**Parameters**

The following query parameter is available:

* `type` - The type of room state event to filter by, eg "m.room.create". If provided, only state events
of this type will be returned.
H-Shay marked this conversation as resolved.
Show resolved Hide resolved

A response body like the following is returned:

```json
Expand Down
19 changes: 18 additions & 1 deletion synapse/rest/admin/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import TYPE_CHECKING, List, Optional, Tuple, cast

import attr
from immutabledict import immutabledict

from synapse.api.constants import Direction, EventTypes, JoinRules, Membership
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
Expand Down Expand Up @@ -463,7 +464,23 @@ async def on_GET(
if not room:
raise NotFoundError("Room not found")

event_ids = await self._storage_controllers.state.get_current_state_ids(room_id)
state_filter = None
type = parse_string(request, "type")

if type:
if not type.startswith("m.room."):
raise SynapseError(
HTTPStatus.BAD_REQUEST,
"Type must be a room state event.",
)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
state_filter = StateFilter(
types=immutabledict({type: None}),
include_others=False,
)

event_ids = await self._storage_controllers.state.get_current_state_ids(
room_id, state_filter
)
events = await self.store.get_events(event_ids.values())
now = self.clock.time_msec()
room_state = await self._event_serializer.serialize_events(events.values(), now)
Expand Down
28 changes: 28 additions & 0 deletions tests/rest/admin/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,34 @@ def test_room_state(self) -> None:
# the create_room already does the right thing, so no need to verify that we got
# the state events it created.

def test_room_state_param(self) -> None:
"""Test that filtering by state event type works when requesting state"""
room_id = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok)

channel = self.make_request(
"GET",
f"/_synapse/admin/v1/rooms/{room_id}/state?type=m.room.member",
access_token=self.admin_user_tok,
)
self.assertEqual(200, channel.code)
state = channel.json_body["state"]
# only one member has joined so there should be one membership event
self.assertEqual(1, len(state))
event = state[0]
self.assertEqual(event["type"], "m.room.member")
self.assertEqual(event["state_key"], self.admin_user)

def test_room_state_bad_param(self) -> None:
"""Test that request with param not conforming to 'm.room.*' will be rejected"""
room_id = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok)

channel = self.make_request(
"GET",
f"/_synapse/admin/v1/rooms/{room_id}/state?type=m.something.else",
access_token=self.admin_user_tok,
)
self.assertEqual(400, channel.code)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved

H-Shay marked this conversation as resolved.
Show resolved Hide resolved
def _set_canonical_alias(
self, room_id: str, test_alias: str, admin_user_tok: str
) -> None:
Expand Down
Loading