mirror of
https://github.com/element-hq/synapse.git
synced 2026-01-11 19:56:31 +00:00
Some checks are pending
Build docker images / Build and push image for linux/arm64 (push) Waiting to run
Build docker images / Push merged images to docker.io/matrixdotorg/synapse (push) Blocked by required conditions
Deploy the documentation / Calculate variables for GitHub Pages deployment (push) Waiting to run
Deploy the documentation / GitHub Pages (push) Blocked by required conditions
Build release artifacts / Calculate list of debian distros (push) Waiting to run
Build release artifacts / Build .deb packages (push) Blocked by required conditions
Build release artifacts / Build sdist (push) Waiting to run
Schema / Ensure Synapse config schema is valid (push) Waiting to run
Schema / Ensure generated documentation is up-to-date (push) Waiting to run
Tests / lint-clippy (push) Blocked by required conditions
Tests / lint-clippy-nightly (push) Blocked by required conditions
Tests / cargo-bench (push) Blocked by required conditions
Tests / tests-done (push) Blocked by required conditions
Tests / check-sampleconfig (push) Blocked by required conditions
Tests / check-schema-delta (push) Blocked by required conditions
Tests / check-lockfile (push) Waiting to run
Tests / lint-rustfmt (push) Blocked by required conditions
Tests / linting-done (push) Blocked by required conditions
Tests / calculate-test-jobs (push) Blocked by required conditions
Tests / trial-olddeps (push) Blocked by required conditions
Tests / trial-pypy (all, pypy-3.10) (push) Blocked by required conditions
Tests / sytest (push) Blocked by required conditions
Build docker images / Build and push image for linux/amd64 (push) Waiting to run
Build docker images / Push merged images to ghcr.io/element-hq/synapse (push) Blocked by required conditions
Build release artifacts / Build wheels on ubuntu-24.04 (push) Waiting to run
Build release artifacts / Build wheels on ubuntu-24.04-arm (push) Waiting to run
Build release artifacts / Attach assets to release (push) Blocked by required conditions
Tests / cargo-test (push) Blocked by required conditions
Tests / changes (push) Waiting to run
Tests / lint (push) Blocked by required conditions
Tests / Typechecking (push) Blocked by required conditions
Tests / lint-crlf (push) Waiting to run
Tests / lint-newsfile (push) Waiting to run
Tests / lint-rust (push) Blocked by required conditions
Tests / lint-readme (push) Blocked by required conditions
Tests / trial (push) Blocked by required conditions
Tests / portdb (14, 3.10) (push) Blocked by required conditions
Tests / portdb (17, 3.14) (push) Blocked by required conditions
Tests / complement (monolith, Postgres) (push) Blocked by required conditions
Tests / complement (monolith, SQLite) (push) Blocked by required conditions
Tests / export-data (push) Blocked by required conditions
Tests / complement (workers, Postgres) (push) Blocked by required conditions
The goal is to avoid that an user could scrape the user directory too quickly.
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
#
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
#
|
|
# Copyright 2017 Vector Creations Ltd
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# See the GNU Affero General Public License for more details:
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
#
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
#
|
|
# [This file includes modifications made by New Vector Limited]
|
|
#
|
|
#
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING
|
|
|
|
from synapse.api.errors import SynapseError
|
|
from synapse.api.ratelimiting import Ratelimiter
|
|
from synapse.http.server import HttpServer
|
|
from synapse.http.servlet import RestServlet, parse_json_object_from_request
|
|
from synapse.http.site import SynapseRequest
|
|
from synapse.types import JsonMapping
|
|
|
|
from ._base import client_patterns
|
|
|
|
if TYPE_CHECKING:
|
|
from synapse.server import HomeServer
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class UserDirectorySearchRestServlet(RestServlet):
|
|
PATTERNS = client_patterns("/user_directory/search$")
|
|
CATEGORY = "User directory search requests"
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
super().__init__()
|
|
self.hs = hs
|
|
self.auth = hs.get_auth()
|
|
self.user_directory_handler = hs.get_user_directory_handler()
|
|
|
|
self._per_user_limiter = Ratelimiter(
|
|
store=hs.get_datastores().main,
|
|
clock=hs.get_clock(),
|
|
cfg=hs.config.ratelimiting.rc_user_directory,
|
|
)
|
|
|
|
async def on_POST(self, request: SynapseRequest) -> tuple[int, JsonMapping]:
|
|
"""Searches for users in directory
|
|
|
|
Returns:
|
|
dict of the form::
|
|
|
|
{
|
|
"limited": <bool>, # whether there were more results or not
|
|
"results": [ # Ordered by best match first
|
|
{
|
|
"user_id": <user_id>,
|
|
"display_name": <display_name>,
|
|
"avatar_url": <avatar_url>
|
|
}
|
|
]
|
|
}
|
|
"""
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=False)
|
|
user_id = requester.user.to_string()
|
|
|
|
if not self.hs.config.userdirectory.user_directory_search_enabled:
|
|
return 200, {"limited": False, "results": []}
|
|
|
|
await self._per_user_limiter.ratelimit(requester)
|
|
|
|
body = parse_json_object_from_request(request)
|
|
|
|
limit = int(body.get("limit", 10))
|
|
limit = max(min(limit, 50), 0)
|
|
|
|
try:
|
|
search_term = body["search_term"]
|
|
except Exception:
|
|
raise SynapseError(400, "`search_term` is required field")
|
|
|
|
results = await self.user_directory_handler.search_users(
|
|
user_id, search_term, limit
|
|
)
|
|
|
|
return 200, results
|
|
|
|
|
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
|
UserDirectorySearchRestServlet(hs).register(http_server)
|