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

Add a bulk user info endpoint and deprecate the old one #46

Merged
merged 15 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,19 @@ async def check_server_matches_acl(self, server_name: str, room_id: str):

raise AuthError(code=403, msg="Server is banned from room")

async def on_user_info(self, user_ids: List[str]):
"""Return user information given a list of user ids

Args:
user_ids: A list of users to return information about

Returns:
A dictionary mapping each user ID to a dict with the following keys:
* expired (bool) - whether this is an expired user
babolivier marked this conversation as resolved.
Show resolved Hide resolved
* deactivated (bool) - whether this is a deactivated user
"""
return await self.store.get_info_for_users(user_ids)


def server_matches_acl_event(server_name: str, acl_event: EventBase) -> bool:
"""Check if the given server is allowed by the ACL event
Expand Down
16 changes: 15 additions & 1 deletion synapse/federation/transport/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.

import logging
from typing import Any, Dict, Optional
from typing import Any, Dict, List, Optional

from six.moves import urllib

Expand Down Expand Up @@ -1021,6 +1021,20 @@ def get_room_complexity(self, destination, room_id):

return self.client.get_json(destination=destination, path=path)

def get_info_of_users(self, destination: str, user_ids: List[str]):
"""
Args:
destination: The remote server
user_ids: A list of user IDs to query info about

Returns:
Deferred[List]: A dictionary of User ID to information about that user.
"""
path = _create_path(FEDERATION_UNSTABLE_PREFIX, "/user/info")
data = {"user_ids": user_ids}

return self.client.post_json(destination=destination, path=path, data=data)


def _create_path(federation_prefix, path, *args):
"""
Expand Down
53 changes: 53 additions & 0 deletions synapse/federation/transport/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from synapse.http.endpoint import parse_and_validate_server_name
from synapse.http.server import JsonResource
from synapse.http.servlet import (
assert_params_in_dict,
parse_boolean_from_args,
parse_integer_from_args,
parse_json_object_from_request,
Expand Down Expand Up @@ -849,6 +850,57 @@ async def on_POST(self, origin, content, query):
return 200, data


class FederationUserInfoServlet(BaseFederationServlet):
"""
Return information about a set of users.

This API returns expiration and deactivation information about a set of
users. Requested users not local to this homeserver will be ignored.

Example request:
POST /userinfo

{
"user_ids": [
"@alice:example.com",
"@bob:example.com"
]
}

Example response
{
"@alice:example.com": {
"expired": false,
"deactivated": true
}
}
"""

PATH = "/user/info"
PREFIX = FEDERATION_UNSTABLE_PREFIX

def __init__(self, handler, authenticator, ratelimiter, server_name):
super(FederationUserInfoServlet, self).__init__(
handler, authenticator, ratelimiter, server_name
)
self.handler = handler

async def on_POST(self, origin, content, query):
assert_params_in_dict(content, required=["user_ids"])

user_ids = content.get("user_ids", [])

if not isinstance(user_ids, list):
raise SynapseError(
400,
"'user_ids' must be a list of user ID strings",
errcode=Codes.INVALID_PARAM,
)

data = await self.handler.on_user_info(user_ids)
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
return 200, data


class FederationVersionServlet(BaseFederationServlet):
PATH = "/version"

Expand Down Expand Up @@ -1410,6 +1462,7 @@ async def on_GET(self, origin, content, query, room_id):
On3pidBindServlet,
FederationVersionServlet,
RoomComplexityServlet,
FederationUserInfoServlet,
) # type: Tuple[Type[BaseFederationServlet], ...]

OPENID_SERVLET_CLASSES = (
Expand Down