Don't pick a random client to get user profiles

Causes problem if one of the candidate client is on a server that
doesn't federate (e.g. a local one). Move the `get_profile()` function
from Backend to MatrixClient.
This commit is contained in:
miruka 2020-08-23 16:18:54 -04:00
parent 7502c1a9b4
commit 0f2efa9ba3
2 changed files with 18 additions and 19 deletions

View File

@ -371,22 +371,6 @@ class Backend:
# Client functions that don't need authentification # Client functions that don't need authentification
async def get_profile(
self, user_id: str, use_cache: bool = True,
) -> nio.ProfileGetResponse:
"""Cache and return the matrix profile of `user_id`."""
async with self.get_profile_locks[user_id]:
if use_cache and user_id in self.profile_cache:
return self.profile_cache[user_id]
client = self.clients.get(user_id) or await self.get_any_client()
response = await client.get_profile(user_id)
self.profile_cache[user_id] = response
return response
async def thumbnail( async def thumbnail(
self, server_name: str, media_id: str, width: int, height: int, self, server_name: str, media_id: str, width: int, height: int,
) -> nio.ThumbnailResponse: ) -> nio.ThumbnailResponse:

View File

@ -431,10 +431,25 @@ class MatrixClient(nio.AsyncClient):
self.first_sync_done.clear() self.first_sync_done.clear()
async def get_profile(
self, user_id: str, use_cache: bool = True,
) -> nio.ProfileGetResponse:
"""Cache and return the matrix profile of `user_id`."""
async with self.backend.get_profile_locks[user_id]:
if use_cache and user_id in self.backend.profile_cache:
return self.backend.profile_cache[user_id]
response = await super().get_profile(user_id)
self.backend.profile_cache[user_id] = response
return response
async def update_own_profile(self) -> None: async def update_own_profile(self) -> None:
"""Fetch our profile from server and Update our model `Account`.""" """Fetch our profile from server and Update our model `Account`."""
resp = await self.backend.get_profile(self.user_id, use_cache=False) resp = await self.get_profile(self.user_id, use_cache=False)
account = self.models["accounts"][self.user_id] account = self.models["accounts"][self.user_id]
account.set_fields( account.set_fields(
@ -1928,7 +1943,7 @@ class MatrixClient(nio.AsyncClient):
If the member isn't found in the room (e.g. they left) and If the member isn't found in the room (e.g. they left) and
`can_fetch_from_network` is `True`, their `can_fetch_from_network` is `True`, their
profile is retrieved using `MatrixClient.backend.get_profile()`. profile is retrieved using `MatrixClient.get_profile()`.
""" """
try: try:
@ -1940,7 +1955,7 @@ class MatrixClient(nio.AsyncClient):
return ("", "", True) return ("", "", True)
try: try:
info = await self.backend.get_profile(user_id) info = await self.get_profile(user_id)
return (info.displayname or "", info.avatar_url or "", False) return (info.displayname or "", info.avatar_url or "", False)
except MatrixError: except MatrixError:
return ("", "", False) return ("", "", False)