From 0f2efa9ba3cf7d4870c5deadcdb3b90a3069fa4b Mon Sep 17 00:00:00 2001 From: miruka Date: Sun, 23 Aug 2020 16:18:54 -0400 Subject: [PATCH] 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. --- src/backend/backend.py | 16 ---------------- src/backend/matrix_client.py | 21 ++++++++++++++++++--- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/backend/backend.py b/src/backend/backend.py index 959efa47..d7f099a1 100644 --- a/src/backend/backend.py +++ b/src/backend/backend.py @@ -371,22 +371,6 @@ class Backend: # 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( self, server_name: str, media_id: str, width: int, height: int, ) -> nio.ThumbnailResponse: diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 7980aefa..94b9d5d6 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -431,10 +431,25 @@ class MatrixClient(nio.AsyncClient): 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: """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.set_fields( @@ -1928,7 +1943,7 @@ class MatrixClient(nio.AsyncClient): If the member isn't found in the room (e.g. they left) and `can_fetch_from_network` is `True`, their - profile is retrieved using `MatrixClient.backend.get_profile()`. + profile is retrieved using `MatrixClient.get_profile()`. """ try: @@ -1940,7 +1955,7 @@ class MatrixClient(nio.AsyncClient): return ("", "", True) 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) except MatrixError: return ("", "", False)