Skip to content

Commit

Permalink
Fall back to profile lookup in the event whois fails
Browse files Browse the repository at this point in the history
  • Loading branch information
nexy7574 committed Jun 26, 2024
1 parent 1f3f90b commit 96efbe8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
33 changes: 31 additions & 2 deletions src/dendritecli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions src/dendritecli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 96efbe8

Please sign in to comment.