This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add a remote user profile cache #2429
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,23 +19,61 @@ | |
|
||
import synapse.types | ||
from synapse.api.errors import SynapseError, AuthError, CodeMessageException | ||
from synapse.types import UserID | ||
from synapse.types import UserID, get_domain_from_id | ||
from ._base import BaseHandler | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ProfileHandler(BaseHandler): | ||
PROFILE_UPDATE_MS = 60 * 1000 | ||
PROFILE_UPDATE_EVERY_MS = 24 * 60 * 60 * 1000 | ||
|
||
def __init__(self, hs): | ||
super(ProfileHandler, self).__init__(hs) | ||
|
||
self.clock = hs.get_clock() | ||
|
||
self.federation = hs.get_replication_layer() | ||
self.federation.register_query_handler( | ||
"profile", self.on_profile_query | ||
) | ||
|
||
self.clock.looping_call(self._update_remote_profile_cache, self.PROFILE_UPDATE_MS) | ||
|
||
@defer.inlineCallbacks | ||
def get_profile(self, user_id): | ||
target_user = UserID.from_string(user_id) | ||
if self.hs.is_mine(target_user): | ||
displayname = yield self.store.get_profile_displayname( | ||
target_user.localpart | ||
) | ||
avatar_url = yield self.store.get_profile_avatar_url( | ||
target_user.localpart | ||
) | ||
|
||
defer.returnValue({ | ||
"displayname": displayname, | ||
"avatar_url": avatar_url, | ||
}) | ||
else: | ||
try: | ||
result = yield self.federation.make_query( | ||
destination=target_user.domain, | ||
query_type="profile", | ||
args={ | ||
"user_id": user_id, | ||
}, | ||
ignore_backoff=True, | ||
) | ||
defer.returnValue(result) | ||
except CodeMessageException as e: | ||
if e.code != 404: | ||
logger.exception("Failed to get displayname") | ||
|
||
raise | ||
|
||
@defer.inlineCallbacks | ||
def get_displayname(self, target_user): | ||
if self.hs.is_mine(target_user): | ||
|
@@ -182,3 +220,44 @@ def _update_join_states(self, requester): | |
"Failed to update join event for room %s - %s", | ||
room_id, str(e.message) | ||
) | ||
|
||
def _update_remote_profile_cache(self): | ||
"""Called periodically to check profiles of remote users we havent' | ||
checked in a while. | ||
""" | ||
entries = yield self.store.get_remote_profile_cache_entries_that_expire( | ||
last_checked=self.clock.time_msec() - self.PROFILE_UPDATE_EVERY_MS | ||
) | ||
|
||
for user_id, displayname, avatar_url in entries: | ||
is_subcscribed = yield self.store.is_subscribed_remote_profile_for_user( | ||
user_id, | ||
) | ||
if not is_subcscribed: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
yield self.store.maybe_delete_remote_profile_cache(user_id) | ||
continue | ||
|
||
try: | ||
profile = yield self.federation.make_query( | ||
destination=get_domain_from_id(user_id), | ||
query_type="profile", | ||
args={ | ||
"user_id": user_id, | ||
}, | ||
ignore_backoff=True, | ||
) | ||
except: | ||
logger.exception("Failed to get avatar_url") | ||
|
||
yield self.store.update_remote_profile_cache( | ||
user_id, displayname, avatar_url | ||
) | ||
continue | ||
|
||
new_name = profile.get("displayname") | ||
new_avatar = profile.get("avatar_url") | ||
|
||
# We always hit update to update the last_check timestamp | ||
yield self.store.update_remote_profile_cache( | ||
user_id, new_name, new_avatar | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from twisted.internet import defer | ||
|
||
from ._base import SQLBaseStore | ||
|
||
|
||
|
@@ -55,3 +57,99 @@ def set_profile_avatar_url(self, user_localpart, new_avatar_url): | |
updatevalues={"avatar_url": new_avatar_url}, | ||
desc="set_profile_avatar_url", | ||
) | ||
|
||
def get_from_remote_profile_cache(self, user_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this actually called anywhere? Or is the cache write-only at the moment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is TBC... |
||
return self._simple_select_one( | ||
table="remote_profile_cache", | ||
keyvalues={"user_id": user_id}, | ||
retcols=("displayname", "avatar_url", "last_check"), | ||
allow_none=True, | ||
desc="get_from_remote_profile_cache", | ||
) | ||
|
||
def add_remote_profile_cache(self, user_id, displayname, avatar_url): | ||
"""Ensure we are caching the remote user's profiles. | ||
|
||
This should only be called when `is_subscribed_remote_profile_for_user` | ||
would return true for the user. | ||
""" | ||
return self._simple_upsert( | ||
table="remote_profile_cache", | ||
keyvalues={"user_id": user_id}, | ||
values={ | ||
"displayname": displayname, | ||
"avatar_url": avatar_url, | ||
"last_check": self._clock.time_msec(), | ||
}, | ||
desc="add_remote_profile_cache", | ||
) | ||
|
||
def update_remote_profile_cache(self, user_id, displayname, avatar_url): | ||
return self._simple_update( | ||
table="remote_profile_cache", | ||
keyvalues={"user_id": user_id}, | ||
values={ | ||
"displayname": displayname, | ||
"avatar_url": avatar_url, | ||
"last_check": self._clock.time_msec(), | ||
}, | ||
desc="update_remote_profile_cache", | ||
) | ||
|
||
@defer.inlineCallbacks | ||
def maybe_delete_remote_profile_cache(self, user_id): | ||
"""Check if we still care about the remote user's profile, and if we | ||
don't then remove their profile from the cache | ||
""" | ||
subscribed = yield self.is_subscribed_remote_profile_for_user(user_id) | ||
if not subscribed: | ||
yield self._simple_delete( | ||
table="remote_profile_cache", | ||
keyvalues={"user_id": user_id}, | ||
desc="delete_remote_profile_cache", | ||
) | ||
|
||
def get_remote_profile_cache_entries_that_expire(self, last_checked): | ||
"""Get all users who haven't been checked since `last_checked` | ||
""" | ||
def _get_remote_profile_cache_entries_that_expire_txn(txn): | ||
sql = """ | ||
SELECT user_id, displayname, avatar_url | ||
FROM remote_profile_cache | ||
WHERE last_check < ? | ||
""" | ||
|
||
txn.execute(sql, (last_checked,)) | ||
|
||
return self.cursor_to_dict(txn) | ||
|
||
return self.runInteraction( | ||
"get_remote_profile_cache_entries_that_expire", | ||
_get_remote_profile_cache_entries_that_expire_txn, | ||
) | ||
|
||
@defer.inlineCallbacks | ||
def is_subscribed_remote_profile_for_user(self, user_id): | ||
"""Check whether we are interested in a remote user's profile. | ||
""" | ||
res = yield self._simple_select_one_onecol( | ||
table="group_users", | ||
keyvalues={"user_id": user_id}, | ||
retcol="user_id", | ||
allow_none=True, | ||
desc="should_update_remote_profile_cache_for_user", | ||
) | ||
|
||
if res: | ||
defer.returnValue(True) | ||
|
||
res = yield self._simple_select_one_onecol( | ||
table="group_invites", | ||
keyvalues={"user_id": user_id}, | ||
retcol="user_id", | ||
allow_none=True, | ||
desc="should_update_remote_profile_cache_for_user", | ||
) | ||
|
||
if res: | ||
defer.returnValue(True) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.