0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-14 13:43:51 +01:00

Add admin endpoint to get members in a room. (#7842)

This commit is contained in:
Michael Albert 2020-07-16 22:43:23 +02:00 committed by GitHub
parent f460da6031
commit fff483ea96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 107 additions and 1 deletions

1
changelog.d/7842.feature Normal file
View file

@ -0,0 +1 @@
Add an admin API to list the users in a room. Contributed by Awesome Technologies Innovationslabor GmbH.

View file

@ -319,6 +319,38 @@ Response:
} }
``` ```
# Room Members API
The Room Members admin API allows server admins to get a list of all members of a room.
The response includes the following fields:
* `members` - A list of all the members that are present in the room, represented by their ids.
* `total` - Total number of members in the room.
## Usage
A standard request:
```
GET /_synapse/admin/v1/rooms/<room_id>/members
{}
```
Response:
```
{
"members": [
"@foo:matrix.org",
"@bar:matrix.org",
"@foobar:matrix.org
],
"total": 3
}
```
# Delete Room API # Delete Room API
The Delete Room admin API allows server admins to remove rooms from server The Delete Room admin API allows server admins to remove rooms from server

View file

@ -38,6 +38,7 @@ from synapse.rest.admin.rooms import (
DeleteRoomRestServlet, DeleteRoomRestServlet,
JoinRoomAliasServlet, JoinRoomAliasServlet,
ListRoomRestServlet, ListRoomRestServlet,
RoomMembersRestServlet,
RoomRestServlet, RoomRestServlet,
ShutdownRoomRestServlet, ShutdownRoomRestServlet,
) )
@ -201,6 +202,7 @@ def register_servlets(hs, http_server):
register_servlets_for_client_rest_resource(hs, http_server) register_servlets_for_client_rest_resource(hs, http_server)
ListRoomRestServlet(hs).register(http_server) ListRoomRestServlet(hs).register(http_server)
RoomRestServlet(hs).register(http_server) RoomRestServlet(hs).register(http_server)
RoomMembersRestServlet(hs).register(http_server)
DeleteRoomRestServlet(hs).register(http_server) DeleteRoomRestServlet(hs).register(http_server)
JoinRoomAliasServlet(hs).register(http_server) JoinRoomAliasServlet(hs).register(http_server)
PurgeRoomServlet(hs).register(http_server) PurgeRoomServlet(hs).register(http_server)

View file

@ -231,6 +231,31 @@ class RoomRestServlet(RestServlet):
return 200, ret return 200, ret
class RoomMembersRestServlet(RestServlet):
"""
Get members list of a room.
"""
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]+)/members")
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):
await assert_requester_is_admin(self.auth, request)
ret = await self.store.get_room(room_id)
if not ret:
raise NotFoundError("Room not found")
members = await self.store.get_users_in_room(room_id)
ret = {"members": members, "total": len(members)}
return 200, ret
class JoinRoomAliasServlet(RestServlet): class JoinRoomAliasServlet(RestServlet):
PATTERNS = admin_patterns("/join/(?P<room_identifier>[^/]*)") PATTERNS = admin_patterns("/join/(?P<room_identifier>[^/]*)")

View file

@ -1136,6 +1136,52 @@ class RoomTestCase(unittest.HomeserverTestCase):
self.assertEqual(room_id_1, channel.json_body["room_id"]) self.assertEqual(room_id_1, channel.json_body["room_id"])
def test_room_members(self):
"""Test that room members can be requested correctly"""
# Create two test rooms
room_id_1 = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok)
room_id_2 = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok)
# Have another user join the room
user_1 = self.register_user("foo", "pass")
user_tok_1 = self.login("foo", "pass")
self.helper.join(room_id_1, user_1, tok=user_tok_1)
# Have another user join the room
user_2 = self.register_user("bar", "pass")
user_tok_2 = self.login("bar", "pass")
self.helper.join(room_id_1, user_2, tok=user_tok_2)
self.helper.join(room_id_2, user_2, tok=user_tok_2)
# Have another user join the room
user_3 = self.register_user("foobar", "pass")
user_tok_3 = self.login("foobar", "pass")
self.helper.join(room_id_2, user_3, tok=user_tok_3)
url = "/_synapse/admin/v1/rooms/%s/members" % (room_id_1,)
request, channel = self.make_request(
"GET", url.encode("ascii"), access_token=self.admin_user_tok,
)
self.render(request)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertCountEqual(
["@admin:test", "@foo:test", "@bar:test"], channel.json_body["members"]
)
self.assertEqual(channel.json_body["total"], 3)
url = "/_synapse/admin/v1/rooms/%s/members" % (room_id_2,)
request, channel = self.make_request(
"GET", url.encode("ascii"), access_token=self.admin_user_tok,
)
self.render(request)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertCountEqual(
["@admin:test", "@bar:test", "@foobar:test"], channel.json_body["members"]
)
self.assertEqual(channel.json_body["total"], 3)
class JoinAliasRoomTestCase(unittest.HomeserverTestCase): class JoinAliasRoomTestCase(unittest.HomeserverTestCase):