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

Commit

Permalink
Add room details admin endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Stahl <[email protected]>
  • Loading branch information
awesome-manuel committed Apr 24, 2020
1 parent 68384d9 commit 842c87f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
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

The Room Details admin API allows server admins to get all details of a room,
including a list of all room members.

This API is still a draft and details might change!

## Usage

A standard request:

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

Response:

```
{
"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.
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.
"""

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 []

return 200, ret


class JoinRoomAliasServlet(RestServlet):

PATTERNS = admin_patterns("/join/(?P<room_identifier>[^/]*)")
Expand Down
46 changes: 46 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,52 @@ 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.
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

0 comments on commit 842c87f

Please sign in to comment.