2014-08-12 08:10:52 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 08:10:52 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-08-12 20:14:34 -06:00
|
|
|
|
2016-07-26 09:46:53 -06:00
|
|
|
import logging
|
2020-08-26 06:49:01 -06:00
|
|
|
import random
|
2016-07-26 09:46:53 -06:00
|
|
|
|
2018-08-03 12:08:05 -06:00
|
|
|
from synapse.api.errors import (
|
|
|
|
AuthError,
|
|
|
|
Codes,
|
2019-06-07 03:29:35 -06:00
|
|
|
HttpResponseException,
|
|
|
|
RequestSendFailed,
|
2018-08-03 12:08:05 -06:00
|
|
|
StoreError,
|
|
|
|
SynapseError,
|
|
|
|
)
|
2018-07-25 03:34:48 -06:00
|
|
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
2020-02-21 10:44:03 -07:00
|
|
|
from synapse.types import UserID, create_requester, get_domain_from_id
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
from ._base import BaseHandler
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-08-24 15:33:43 -06:00
|
|
|
MAX_DISPLAYNAME_LEN = 256
|
2019-06-01 03:42:33 -06:00
|
|
|
MAX_AVATAR_URL_LEN = 1000
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-08-22 03:13:40 -06:00
|
|
|
class BaseProfileHandler(BaseHandler):
|
|
|
|
"""Handles fetching and updating user profile information.
|
|
|
|
|
|
|
|
BaseProfileHandler can be instantiated directly on workers and will
|
|
|
|
delegate to master when necessary. The master process should use the
|
|
|
|
subclass MasterProfileHandler
|
|
|
|
"""
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
def __init__(self, hs):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__(hs)
|
2017-08-25 04:21:34 -06:00
|
|
|
|
2018-03-13 07:26:52 -06:00
|
|
|
self.federation = hs.get_federation_client()
|
2018-03-12 10:17:08 -06:00
|
|
|
hs.get_federation_registry().register_query_handler(
|
2014-08-13 10:12:50 -06:00
|
|
|
"profile", self.on_profile_query
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2017-11-29 11:27:05 -07:00
|
|
|
self.user_directory_handler = hs.get_user_directory_handler()
|
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
async def get_profile(self, user_id):
|
2017-08-25 04:21:34 -06:00
|
|
|
target_user = UserID.from_string(user_id)
|
2019-05-08 11:26:56 -06:00
|
|
|
|
2017-08-25 04:21:34 -06:00
|
|
|
if self.hs.is_mine(target_user):
|
2018-08-03 12:08:05 -06:00
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
displayname = await self.store.get_profile_displayname(
|
2018-08-03 12:08:05 -06:00
|
|
|
target_user.localpart
|
|
|
|
)
|
2020-07-17 05:08:30 -06:00
|
|
|
avatar_url = await self.store.get_profile_avatar_url(
|
2018-08-03 12:08:05 -06:00
|
|
|
target_user.localpart
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
raise
|
2017-08-25 04:21:34 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return {"displayname": displayname, "avatar_url": avatar_url}
|
2017-08-25 04:21:34 -06:00
|
|
|
else:
|
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
result = await self.federation.make_query(
|
2017-08-25 04:21:34 -06:00
|
|
|
destination=target_user.domain,
|
|
|
|
query_type="profile",
|
2019-06-20 03:32:02 -06:00
|
|
|
args={"user_id": user_id},
|
2017-08-25 04:21:34 -06:00
|
|
|
ignore_backoff=True,
|
|
|
|
)
|
2019-07-23 07:00:55 -06:00
|
|
|
return result
|
2019-06-07 03:29:35 -06:00
|
|
|
except RequestSendFailed as e:
|
2020-06-16 06:51:47 -06:00
|
|
|
raise SynapseError(502, "Failed to fetch profile") from e
|
2019-06-07 03:29:35 -06:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2017-08-25 04:21:34 -06:00
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
async def get_profile_from_cache(self, user_id):
|
2017-08-25 09:23:58 -06:00
|
|
|
"""Get the profile information from our local cache. If the user is
|
|
|
|
ours then the profile information will always be corect. Otherwise,
|
|
|
|
it may be out of date/missing.
|
|
|
|
"""
|
|
|
|
target_user = UserID.from_string(user_id)
|
|
|
|
if self.hs.is_mine(target_user):
|
2018-08-03 12:08:05 -06:00
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
displayname = await self.store.get_profile_displayname(
|
2018-08-03 12:08:05 -06:00
|
|
|
target_user.localpart
|
|
|
|
)
|
2020-07-17 05:08:30 -06:00
|
|
|
avatar_url = await self.store.get_profile_avatar_url(
|
2018-08-03 12:08:05 -06:00
|
|
|
target_user.localpart
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
raise
|
2017-08-25 09:23:58 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return {"displayname": displayname, "avatar_url": avatar_url}
|
2017-08-25 09:23:58 -06:00
|
|
|
else:
|
2020-07-17 05:08:30 -06:00
|
|
|
profile = await self.store.get_from_remote_profile_cache(user_id)
|
2019-07-23 07:00:55 -06:00
|
|
|
return profile or {}
|
2017-08-25 09:23:58 -06:00
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
async def get_displayname(self, target_user):
|
2014-12-02 03:42:28 -07:00
|
|
|
if self.hs.is_mine(target_user):
|
2018-08-03 12:08:05 -06:00
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
displayname = await self.store.get_profile_displayname(
|
2018-08-03 12:08:05 -06:00
|
|
|
target_user.localpart
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
raise
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return displayname
|
2014-08-13 10:14:42 -06:00
|
|
|
else:
|
2014-08-12 08:10:52 -06:00
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
result = await self.federation.make_query(
|
2014-08-12 08:10:52 -06:00
|
|
|
destination=target_user.domain,
|
2014-08-13 10:12:50 -06:00
|
|
|
query_type="profile",
|
2019-06-20 03:32:02 -06:00
|
|
|
args={"user_id": target_user.to_string(), "field": "displayname"},
|
2017-03-23 05:10:36 -06:00
|
|
|
ignore_backoff=True,
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
2019-06-07 03:29:35 -06:00
|
|
|
except RequestSendFailed as e:
|
2020-06-16 06:51:47 -06:00
|
|
|
raise SynapseError(502, "Failed to fetch profile") from e
|
2019-06-07 03:29:35 -06:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2018-10-03 04:34:30 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return result["displayname"]
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
async def set_displayname(
|
|
|
|
self, target_user, requester, new_displayname, by_admin=False
|
|
|
|
):
|
2019-03-26 11:48:30 -06:00
|
|
|
"""Set the displayname of a user
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_user (UserID): the user whose displayname is to be changed.
|
|
|
|
requester (Requester): The user attempting to make this change.
|
|
|
|
new_displayname (str): The displayname to give this user.
|
|
|
|
by_admin (bool): Whether this change was made by an administrator.
|
|
|
|
"""
|
2014-12-02 03:42:28 -07:00
|
|
|
if not self.hs.is_mine(target_user):
|
2019-11-12 06:08:12 -07:00
|
|
|
raise SynapseError(400, "User is not hosted on this homeserver")
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-10-04 14:30:51 -06:00
|
|
|
if not by_admin and target_user != requester.user:
|
2014-08-12 08:10:52 -06:00
|
|
|
raise AuthError(400, "Cannot set another user's displayname")
|
|
|
|
|
2020-03-27 13:15:23 -06:00
|
|
|
if not by_admin and not self.hs.config.enable_set_displayname:
|
2020-05-01 08:15:36 -06:00
|
|
|
profile = await self.store.get_profileinfo(target_user.localpart)
|
2020-03-27 13:15:23 -06:00
|
|
|
if profile.display_name:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Changing display name is disabled on this server",
|
|
|
|
Codes.FORBIDDEN,
|
|
|
|
)
|
|
|
|
|
2020-09-01 11:02:41 -06:00
|
|
|
if not isinstance(new_displayname, str):
|
|
|
|
raise SynapseError(400, "Invalid displayname")
|
|
|
|
|
2019-06-01 03:42:33 -06:00
|
|
|
if len(new_displayname) > MAX_DISPLAYNAME_LEN:
|
|
|
|
raise SynapseError(
|
2019-06-20 03:32:02 -06:00
|
|
|
400, "Displayname is too long (max %i)" % (MAX_DISPLAYNAME_LEN,)
|
2019-06-01 03:42:33 -06:00
|
|
|
)
|
|
|
|
|
2019-06-20 03:32:02 -06:00
|
|
|
if new_displayname == "":
|
2015-05-14 07:19:10 -06:00
|
|
|
new_displayname = None
|
|
|
|
|
2020-02-21 10:44:03 -07:00
|
|
|
# If the admin changes the display name of a user, the requesting user cannot send
|
|
|
|
# the join event to update the displayname in the rooms.
|
|
|
|
# This must be done by the target user himself.
|
|
|
|
if by_admin:
|
|
|
|
requester = create_requester(target_user)
|
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
await self.store.set_profile_displayname(target_user.localpart, new_displayname)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2017-12-04 07:58:39 -07:00
|
|
|
if self.hs.config.user_directory_search_all_users:
|
2020-05-01 08:15:36 -06:00
|
|
|
profile = await self.store.get_profileinfo(target_user.localpart)
|
|
|
|
await self.user_directory_handler.handle_local_profile_change(
|
2018-08-17 08:24:16 -06:00
|
|
|
target_user.to_string(), profile
|
|
|
|
)
|
2017-11-29 11:27:05 -07:00
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
await self._update_join_states(requester, target_user)
|
2014-09-17 08:05:09 -06:00
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
async def get_avatar_url(self, target_user):
|
2014-12-02 03:42:28 -07:00
|
|
|
if self.hs.is_mine(target_user):
|
2018-08-03 12:08:05 -06:00
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
avatar_url = await self.store.get_profile_avatar_url(
|
2018-08-03 12:08:05 -06:00
|
|
|
target_user.localpart
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
raise
|
2019-07-23 07:00:55 -06:00
|
|
|
return avatar_url
|
2014-08-13 10:14:42 -06:00
|
|
|
else:
|
2014-08-12 08:10:52 -06:00
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
result = await self.federation.make_query(
|
2014-08-13 10:12:50 -06:00
|
|
|
destination=target_user.domain,
|
|
|
|
query_type="profile",
|
2019-06-20 03:32:02 -06:00
|
|
|
args={"user_id": target_user.to_string(), "field": "avatar_url"},
|
2017-03-23 05:10:36 -06:00
|
|
|
ignore_backoff=True,
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
2019-06-07 03:29:35 -06:00
|
|
|
except RequestSendFailed as e:
|
2020-06-16 06:51:47 -06:00
|
|
|
raise SynapseError(502, "Failed to fetch profile") from e
|
2019-06-07 03:29:35 -06:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return result["avatar_url"]
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
async def set_avatar_url(
|
|
|
|
self, target_user, requester, new_avatar_url, by_admin=False
|
|
|
|
):
|
2020-08-26 06:49:01 -06:00
|
|
|
"""Set a new avatar URL for a user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_user (UserID): the user whose avatar URL is to be changed.
|
|
|
|
requester (Requester): The user attempting to make this change.
|
|
|
|
new_avatar_url (str): The avatar URL to give this user.
|
|
|
|
by_admin (bool): Whether this change was made by an administrator.
|
|
|
|
"""
|
2014-12-02 03:42:28 -07:00
|
|
|
if not self.hs.is_mine(target_user):
|
2019-11-12 06:08:12 -07:00
|
|
|
raise SynapseError(400, "User is not hosted on this homeserver")
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-10-04 14:30:51 -06:00
|
|
|
if not by_admin and target_user != requester.user:
|
2014-08-12 08:10:52 -06:00
|
|
|
raise AuthError(400, "Cannot set another user's avatar_url")
|
|
|
|
|
2020-03-27 13:15:23 -06:00
|
|
|
if not by_admin and not self.hs.config.enable_set_avatar_url:
|
2020-05-01 08:15:36 -06:00
|
|
|
profile = await self.store.get_profileinfo(target_user.localpart)
|
2020-03-27 13:15:23 -06:00
|
|
|
if profile.avatar_url:
|
|
|
|
raise SynapseError(
|
|
|
|
400, "Changing avatar is disabled on this server", Codes.FORBIDDEN
|
|
|
|
)
|
|
|
|
|
2020-09-01 11:02:41 -06:00
|
|
|
if not isinstance(new_avatar_url, str):
|
|
|
|
raise SynapseError(400, "Invalid displayname")
|
|
|
|
|
2019-06-01 03:42:33 -06:00
|
|
|
if len(new_avatar_url) > MAX_AVATAR_URL_LEN:
|
|
|
|
raise SynapseError(
|
2019-06-20 03:32:02 -06:00
|
|
|
400, "Avatar URL is too long (max %i)" % (MAX_AVATAR_URL_LEN,)
|
2019-06-01 03:42:33 -06:00
|
|
|
)
|
|
|
|
|
2020-02-21 10:44:03 -07:00
|
|
|
# Same like set_displayname
|
|
|
|
if by_admin:
|
|
|
|
requester = create_requester(target_user)
|
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
await self.store.set_profile_avatar_url(target_user.localpart, new_avatar_url)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-08-17 08:24:16 -06:00
|
|
|
if self.hs.config.user_directory_search_all_users:
|
2020-05-01 08:15:36 -06:00
|
|
|
profile = await self.store.get_profileinfo(target_user.localpart)
|
|
|
|
await self.user_directory_handler.handle_local_profile_change(
|
2017-12-04 08:11:38 -07:00
|
|
|
target_user.to_string(), profile
|
2017-11-29 11:27:05 -07:00
|
|
|
)
|
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
await self._update_join_states(requester, target_user)
|
2014-09-17 08:05:09 -06:00
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
async def on_profile_query(self, args):
|
2015-01-23 04:47:15 -07:00
|
|
|
user = UserID.from_string(args["user_id"])
|
2014-12-02 03:42:28 -07:00
|
|
|
if not self.hs.is_mine(user):
|
2019-11-12 06:08:12 -07:00
|
|
|
raise SynapseError(400, "User is not hosted on this homeserver")
|
2014-08-13 10:12:50 -06:00
|
|
|
|
|
|
|
just_field = args.get("field", None)
|
|
|
|
|
|
|
|
response = {}
|
2018-08-03 12:08:05 -06:00
|
|
|
try:
|
|
|
|
if just_field is None or just_field == "displayname":
|
2020-07-17 05:08:30 -06:00
|
|
|
response["displayname"] = await self.store.get_profile_displayname(
|
2018-08-03 12:08:05 -06:00
|
|
|
user.localpart
|
|
|
|
)
|
2014-08-13 10:12:50 -06:00
|
|
|
|
2018-08-03 12:08:05 -06:00
|
|
|
if just_field is None or just_field == "avatar_url":
|
2020-07-17 05:08:30 -06:00
|
|
|
response["avatar_url"] = await self.store.get_profile_avatar_url(
|
2018-08-03 12:08:05 -06:00
|
|
|
user.localpart
|
|
|
|
)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
raise
|
2014-08-13 10:12:50 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return response
|
2014-09-17 08:05:09 -06:00
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
async def _update_join_states(self, requester, target_user):
|
2017-11-03 11:25:04 -06:00
|
|
|
if not self.hs.is_mine(target_user):
|
2014-09-17 08:05:09 -06:00
|
|
|
return
|
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
await self.ratelimit(requester)
|
2014-12-19 10:36:33 -07:00
|
|
|
|
2020-08-26 06:49:01 -06:00
|
|
|
# Do not actually update the room state for shadow-banned users.
|
|
|
|
if requester.shadow_banned:
|
|
|
|
# We randomly sleep a bit just to annoy the requester.
|
|
|
|
await self.clock.sleep(random.randint(1, 10))
|
|
|
|
return
|
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
room_ids = await self.store.get_rooms_for_user(target_user.to_string())
|
2014-09-17 08:05:09 -06:00
|
|
|
|
2017-03-16 05:51:46 -06:00
|
|
|
for room_id in room_ids:
|
2018-03-01 03:54:37 -07:00
|
|
|
handler = self.hs.get_room_member_handler()
|
2015-03-06 09:24:05 -07:00
|
|
|
try:
|
2017-11-03 11:25:04 -06:00
|
|
|
# Assume the target_user isn't a guest,
|
|
|
|
# because we don't let guests set profile or avatar data.
|
2020-05-01 08:15:36 -06:00
|
|
|
await handler.update_membership(
|
2016-02-15 10:14:34 -07:00
|
|
|
requester,
|
2017-11-03 11:25:04 -06:00
|
|
|
target_user,
|
2017-03-16 05:51:46 -06:00
|
|
|
room_id,
|
2016-02-15 10:14:34 -07:00
|
|
|
"join", # We treat a profile update like a join.
|
2016-02-16 04:52:46 -07:00
|
|
|
ratelimit=False, # Try to hide that these events aren't atomic.
|
2016-02-15 10:14:34 -07:00
|
|
|
)
|
2015-03-06 09:24:05 -07:00
|
|
|
except Exception as e:
|
2019-10-31 04:23:24 -06:00
|
|
|
logger.warning(
|
2019-06-20 03:32:02 -06:00
|
|
|
"Failed to update join event for room %s - %s", room_id, str(e)
|
2015-03-06 09:24:05 -07:00
|
|
|
)
|
2017-08-25 04:21:34 -06:00
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
async def check_profile_query_allowed(self, target_user, requester=None):
|
2019-05-08 11:26:56 -06:00
|
|
|
"""Checks whether a profile query is allowed. If the
|
|
|
|
'require_auth_for_profile_requests' config flag is set to True and a
|
|
|
|
'requester' is provided, the query is only allowed if the two users
|
|
|
|
share a room.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_user (UserID): The owner of the queried profile.
|
|
|
|
requester (None|UserID): The user querying for the profile.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
SynapseError(403): The two users share no room, or ne user couldn't
|
|
|
|
be found to be in any room the server is in, and therefore the query
|
|
|
|
is denied.
|
|
|
|
"""
|
2019-12-16 09:11:55 -07:00
|
|
|
|
2019-05-08 11:26:56 -06:00
|
|
|
# Implementation of MSC1301: don't allow looking up profiles if the
|
|
|
|
# requester isn't in the same room as the target. We expect requester to
|
|
|
|
# be None when this function is called outside of a profile query, e.g.
|
|
|
|
# when building a membership event. In this case, we must allow the
|
|
|
|
# lookup.
|
2019-12-16 09:11:55 -07:00
|
|
|
if (
|
|
|
|
not self.hs.config.limit_profile_requests_to_users_who_share_rooms
|
|
|
|
or not requester
|
|
|
|
):
|
2019-05-08 11:26:56 -06:00
|
|
|
return
|
|
|
|
|
2019-07-08 10:31:00 -06:00
|
|
|
# Always allow the user to query their own profile.
|
|
|
|
if target_user.to_string() == requester.to_string():
|
|
|
|
return
|
|
|
|
|
2019-05-08 11:26:56 -06:00
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
requester_rooms = await self.store.get_rooms_for_user(requester.to_string())
|
|
|
|
target_user_rooms = await self.store.get_rooms_for_user(
|
2019-06-20 03:32:02 -06:00
|
|
|
target_user.to_string()
|
2019-05-08 11:26:56 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
# Check if the room lists have no elements in common.
|
|
|
|
if requester_rooms.isdisjoint(target_user_rooms):
|
|
|
|
raise SynapseError(403, "Profile isn't available", Codes.FORBIDDEN)
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
# This likely means that one of the users doesn't exist,
|
|
|
|
# so we act as if we couldn't find the profile.
|
|
|
|
raise SynapseError(403, "Profile isn't available", Codes.FORBIDDEN)
|
|
|
|
raise
|
|
|
|
|
2018-08-17 04:43:16 -06:00
|
|
|
|
2018-08-22 03:13:40 -06:00
|
|
|
class MasterProfileHandler(BaseProfileHandler):
|
2018-08-17 04:43:16 -06:00
|
|
|
PROFILE_UPDATE_MS = 60 * 1000
|
|
|
|
PROFILE_UPDATE_EVERY_MS = 24 * 60 * 60 * 1000
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__(hs)
|
2018-08-17 04:43:16 -06:00
|
|
|
|
2018-08-22 03:11:21 -06:00
|
|
|
assert hs.config.worker_app is None
|
|
|
|
|
2018-08-17 04:43:16 -06:00
|
|
|
self.clock.looping_call(
|
2019-06-20 03:32:02 -06:00
|
|
|
self._start_update_remote_profile_cache, self.PROFILE_UPDATE_MS
|
2018-08-17 04:43:16 -06:00
|
|
|
)
|
|
|
|
|
2018-07-25 03:34:48 -06:00
|
|
|
def _start_update_remote_profile_cache(self):
|
2018-07-26 04:44:26 -06:00
|
|
|
return run_as_background_process(
|
2019-06-20 03:32:02 -06:00
|
|
|
"Update remote profile", self._update_remote_profile_cache
|
2018-07-25 03:34:48 -06:00
|
|
|
)
|
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
async def _update_remote_profile_cache(self):
|
2017-08-25 07:45:20 -06:00
|
|
|
"""Called periodically to check profiles of remote users we haven't
|
2017-08-25 04:21:34 -06:00
|
|
|
checked in a while.
|
|
|
|
"""
|
2020-07-17 05:08:30 -06:00
|
|
|
entries = await self.store.get_remote_profile_cache_entries_that_expire(
|
2017-08-25 04:21:34 -06:00
|
|
|
last_checked=self.clock.time_msec() - self.PROFILE_UPDATE_EVERY_MS
|
|
|
|
)
|
|
|
|
|
|
|
|
for user_id, displayname, avatar_url in entries:
|
2020-07-17 05:08:30 -06:00
|
|
|
is_subscribed = await self.store.is_subscribed_remote_profile_for_user(
|
2019-06-20 03:32:02 -06:00
|
|
|
user_id
|
2017-08-25 04:21:34 -06:00
|
|
|
)
|
2017-08-25 07:45:20 -06:00
|
|
|
if not is_subscribed:
|
2020-07-17 05:08:30 -06:00
|
|
|
await self.store.maybe_delete_remote_profile_cache(user_id)
|
2017-08-25 04:21:34 -06:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2020-07-17 05:08:30 -06:00
|
|
|
profile = await self.federation.make_query(
|
2017-08-25 04:21:34 -06:00
|
|
|
destination=get_domain_from_id(user_id),
|
|
|
|
query_type="profile",
|
2019-06-20 03:32:02 -06:00
|
|
|
args={"user_id": user_id},
|
2017-08-25 04:21:34 -06:00
|
|
|
ignore_backoff=True,
|
|
|
|
)
|
2017-10-23 08:52:32 -06:00
|
|
|
except Exception:
|
2017-08-25 04:21:34 -06:00
|
|
|
logger.exception("Failed to get avatar_url")
|
|
|
|
|
2020-07-17 05:08:30 -06:00
|
|
|
await self.store.update_remote_profile_cache(
|
2017-08-25 04:21:34 -06:00
|
|
|
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
|
2020-07-17 05:08:30 -06:00
|
|
|
await self.store.update_remote_profile_cache(user_id, new_name, new_avatar)
|