diff --git a/src/dendritecli/api.py b/src/dendritecli/api.py index aa8cd82..452a387 100644 --- a/src/dendritecli/api.py +++ b/src/dendritecli/api.py @@ -5,13 +5,14 @@ import secrets import sys import typing -from urllib.parse import quote, urlparse from pathlib import Path -from ._sql import SQLHandler +from urllib.parse import quote, urlparse import httpx import toml +from ._sql import SQLHandler + try: import h2 except ImportError: @@ -375,6 +376,34 @@ def register(self, nonce: typing.Optional[str] = None, **kwargs) -> typing.Union response.raise_for_status() return response.json() + def get_profile(self, user_id: str) -> dict: + """ + Fetches information about a user, less than the whois function. + + Docs: + 1. https://spec.matrix.org/v1.11/client-server-api/#get_matrixclientv3profileuserid + + :param user_id: The user ID to fetch information about. + :return: The user's information (see docs). + :raises: httpx.HTTPError - if the request failed. + """ + log.info("Fetching the profile for user %s", user_id) + domain = user_id.split(":", 1)[1] + user_id = quote(user_id) + url = f"/_matrix/client/v3/profile/{user_id}" + if domain != self.client.base_url.host: + log.warning( + "User %s is not local to this server - will contact %r instead.", + user_id, + domain, + ) + url = f"{self.resolve_delegation(domain)}{url}" + + response = self.client.get(url) + log.info("Done fetching information about user %s", user_id) + response.raise_for_status() + return response.json() + def whois(self, user_id: str) -> dict: """ Fetches information about a user. diff --git a/src/dendritecli/main.py b/src/dendritecli/main.py index 9dab4d5..1a7fae5 100644 --- a/src/dendritecli/main.py +++ b/src/dendritecli/main.py @@ -263,9 +263,10 @@ def whois(http: api.HTTPAPIManager, user_id: str): _user = http.whois(user_id) except HTTPStatusError as e: if e.response.status_code == 401: - console.print("[red]Unauthorised. If you are looking up a remote user, you may not have permission.") - return - raise + console.print("[red]Unauthorised. Performing profile lookup instead.") + _user = http.get_profile(user_id) + else: + raise console.print(_user)