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

Add room details admin endpoint #7317

Merged
merged 9 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions changelog.d/7317.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add room details admin endpoint. Contributed by Awesome Technologies Innovationslabor GmbH.
31 changes: 31 additions & 0 deletions docs/admin_api/rooms.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,34 @@ Response:

Once the `next_token` parameter is no longer present, we know we've reached the
end of the list.

# DRAFT: Room Details API
awesome-manuel marked this conversation as resolved.
Show resolved Hide resolved

The Room Details admin API allows server admins to get all details of a room,
including a list of all room members.
awesome-manuel marked this conversation as resolved.
Show resolved Hide resolved

This API is still a draft and details might change!

## Usage

A standard request:

```
GET /_synapse/admin/v1/rooms/<room_id>

{}
```

Response:
clokep marked this conversation as resolved.
Show resolved Hide resolved

```
{
"room_id": "!OGEhHVWSdvArJzumhm:matrix.org",
"name": "Matrix HQ",
"canonical_alias": "#matrix:matrix.org",
"join_rules": "invite",
"guest_access": "can_join",
"topic": "A room topic",
"members": ["@foo:matrix.org", "@bar:matrix.org"]
}
```
2 changes: 2 additions & 0 deletions synapse/rest/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from synapse.rest.admin.rooms import (
JoinRoomAliasServlet,
ListRoomRestServlet,
RoomRestServlet,
ShutdownRoomRestServlet,
)
from synapse.rest.admin.server_notice_servlet import SendServerNoticeServlet
Expand Down Expand Up @@ -193,6 +194,7 @@ def register_servlets(hs, http_server):
"""
register_servlets_for_client_rest_resource(hs, http_server)
ListRoomRestServlet(hs).register(http_server)
RoomRestServlet(hs).register(http_server)
JoinRoomAliasServlet(hs).register(http_server)
PurgeRoomServlet(hs).register(http_server)
SendServerNoticeServlet(hs).register(http_server)
Expand Down
39 changes: 38 additions & 1 deletion synapse/rest/admin/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ListRoomRestServlet(RestServlet):
in a dictionary containing room information. Supports pagination.
"""

PATTERNS = admin_patterns("/rooms")
PATTERNS = admin_patterns("/rooms$")

def __init__(self, hs):
self.store = hs.get_datastore()
Expand Down Expand Up @@ -253,6 +253,43 @@ async def on_GET(self, request):
return 200, response


class RoomRestServlet(RestServlet):
"""Get room details.
This needs user to have administrator access in Synapse.
awesome-manuel marked this conversation as resolved.
Show resolved Hide resolved
TODO: Return power level for each user
TODO: Add on_POST to allow room creation without joining the room

GET /_synapse/admin/v1/rooms/<room_id>

returns:
200 OK with users if success otherwise an error.
awesome-manuel marked this conversation as resolved.
Show resolved Hide resolved
"""

PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]+)$")

def __init__(self, hs):
self.hs = hs
self.auth = hs.get_auth()
self.store = hs.get_datastore()

async def on_GET(self, request, room_id):
requester = await self.auth.get_user_by_req(request)
await assert_user_is_admin(self.auth, requester.user)

ret = await self.store.get_room_stats_state(room_id)
if not ret:
raise NotFoundError("Room not found")

stats_curr = await self.store.get_room_stats_current(room_id)
if stats_curr:
ret.update(stats_curr)

members = await self.store.get_users_in_room(room_id)
ret["members"] = members if members else []
awesome-manuel marked this conversation as resolved.
Show resolved Hide resolved

return 200, ret


class JoinRoomAliasServlet(RestServlet):

PATTERNS = admin_patterns("/join/(?P<room_identifier>[^/]*)")
Expand Down
47 changes: 47 additions & 0 deletions synapse/storage/data_stores/main/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,53 @@ def get_room(self, room_id):
allow_none=True,
)

def get_room_stats_state(self, room_id):
"""Retrieve room stats state.

Args:
room_id (str): The ID of the room to retrieve.
Returns:
A dict containing the room information, or None if the room is unknown.
"""
return self.db.simple_select_one(
table="room_stats_state",
keyvalues={"room_id": room_id},
retcols=(
"room_id",
"name",
"canonical_alias",
"encryption",
"is_federatable",
"join_rules",
"guest_access",
"history_visibility",
"topic",
),
desc="get_room_stats_state",
allow_none=True,
)

def get_room_stats_current(self, room_id):
"""Retrieve current room stats.

Args:
room_id (str): The ID of the room to retrieve.
clokep marked this conversation as resolved.
Show resolved Hide resolved
Returns:
A dict containing the room information, or None if the room is unknown.
"""
return self.db.simple_select_one(
table="room_stats_state",
keyvalues={"room_id": room_id},
retcols=(
"room_id",
"joined_members",
"local_users_in_room",
"current_state_events",
),
desc="get_room_stats_current",
allow_none=True,
)

def get_public_room_ids(self):
return self.db.simple_select_onecol(
table="rooms",
Expand Down