Trigger profile replication on profile change

This commit is contained in:
David Baker 2018-04-11 10:17:07 +01:00
parent e654230a51
commit 4e12b10c7c
2 changed files with 23 additions and 6 deletions

View File

@ -18,6 +18,7 @@ import logging
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
from synapse.api.errors import SynapseError, AuthError, CodeMessageException from synapse.api.errors import SynapseError, AuthError, CodeMessageException
from synapse.util.logcontext import run_in_background
from synapse.types import UserID, get_domain_from_id from synapse.types import UserID, get_domain_from_id
from ._base import BaseHandler from ._base import BaseHandler
@ -205,8 +206,10 @@ class ProfileHandler(BaseHandler):
if new_displayname == '': if new_displayname == '':
new_displayname = None new_displayname = None
new_batchnum = (yield self.store.get_latest_profile_replication_batch_number()) + 1
yield self.store.set_profile_displayname( yield self.store.set_profile_displayname(
target_user.localpart, new_displayname target_user.localpart, new_displayname, new_batchnum
) )
if self.hs.config.user_directory_search_all_users: if self.hs.config.user_directory_search_all_users:
@ -217,6 +220,9 @@ class ProfileHandler(BaseHandler):
yield self._update_join_states(requester, target_user) yield self._update_join_states(requester, target_user)
# start a profile replication push
run_in_background(self._replicate_profiles)
@defer.inlineCallbacks @defer.inlineCallbacks
def get_avatar_url(self, target_user): def get_avatar_url(self, target_user):
if self.hs.is_mine(target_user): if self.hs.is_mine(target_user):
@ -255,8 +261,10 @@ class ProfileHandler(BaseHandler):
if not by_admin and target_user != requester.user: if not by_admin and target_user != requester.user:
raise AuthError(400, "Cannot set another user's avatar_url") raise AuthError(400, "Cannot set another user's avatar_url")
new_batchnum = yield self.store.get_latest_profile_replication_batch_number() + 1
yield self.store.set_profile_avatar_url( yield self.store.set_profile_avatar_url(
target_user.localpart, new_avatar_url target_user.localpart, new_avatar_url, new_batchnum,
) )
if self.hs.config.user_directory_search_all_users: if self.hs.config.user_directory_search_all_users:
@ -267,6 +275,9 @@ class ProfileHandler(BaseHandler):
yield self._update_join_states(requester, target_user) yield self._update_join_states(requester, target_user)
# start a profile replication push
run_in_background(self._replicate_profiles)
@defer.inlineCallbacks @defer.inlineCallbacks
def on_profile_query(self, args): def on_profile_query(self, args):
user = UserID.from_string(args["user_id"]) user = UserID.from_string(args["user_id"])

View File

@ -64,11 +64,14 @@ class ProfileStore(SQLBaseStore):
desc="get_profile_displayname", desc="get_profile_displayname",
) )
def set_profile_displayname(self, user_localpart, new_displayname): def set_profile_displayname(self, user_localpart, new_displayname, batchnum):
return self._simple_update_one( return self._simple_update_one(
table="profiles", table="profiles",
keyvalues={"user_id": user_localpart}, keyvalues={"user_id": user_localpart},
updatevalues={"displayname": new_displayname}, updatevalues={
"displayname": new_displayname,
"batch": batchnum,
},
desc="set_profile_displayname", desc="set_profile_displayname",
) )
@ -80,11 +83,14 @@ class ProfileStore(SQLBaseStore):
desc="get_profile_avatar_url", desc="get_profile_avatar_url",
) )
def set_profile_avatar_url(self, user_localpart, new_avatar_url): def set_profile_avatar_url(self, user_localpart, new_avatar_url, batchnum):
return self._simple_update_one( return self._simple_update_one(
table="profiles", table="profiles",
keyvalues={"user_id": user_localpart}, keyvalues={"user_id": user_localpart},
updatevalues={"avatar_url": new_avatar_url}, updatevalues={
"avatar_url": new_avatar_url,
"batch": batchnum,
},
desc="set_profile_avatar_url", desc="set_profile_avatar_url",
) )