2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 10:10:52 -04: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.
|
2016-07-26 11:46:53 -04:00
|
|
|
import logging
|
2020-08-26 08:49:01 -04:00
|
|
|
import random
|
2020-10-21 06:44:31 -04:00
|
|
|
from typing import TYPE_CHECKING, Optional
|
2016-07-26 11:46:53 -04:00
|
|
|
|
2018-08-03 14:08:05 -04:00
|
|
|
from synapse.api.errors import (
|
|
|
|
AuthError,
|
|
|
|
Codes,
|
2019-06-07 05:29:35 -04:00
|
|
|
HttpResponseException,
|
|
|
|
RequestSendFailed,
|
2018-08-03 14:08:05 -04:00
|
|
|
StoreError,
|
|
|
|
SynapseError,
|
|
|
|
)
|
2022-06-06 13:20:05 -04:00
|
|
|
from synapse.types import JsonDict, Requester, UserID, create_requester
|
2022-01-28 09:41:33 -05:00
|
|
|
from synapse.util.caches.descriptors import cached
|
|
|
|
from synapse.util.stringutils import parse_and_validate_mxc_uri
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2020-10-21 06:44:31 -04:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2020-10-21 06:44:31 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-08-24 17:33:43 -04:00
|
|
|
MAX_DISPLAYNAME_LEN = 256
|
2019-06-01 05:42:33 -04:00
|
|
|
MAX_AVATAR_URL_LEN = 1000
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-10-08 07:44:43 -04:00
|
|
|
class ProfileHandler:
|
2018-08-22 05:13:40 -04:00
|
|
|
"""Handles fetching and updating user profile information.
|
|
|
|
|
2020-10-13 08:20:32 -04:00
|
|
|
ProfileHandler can be instantiated directly on workers and will
|
|
|
|
delegate to master when necessary.
|
2018-08-22 05:13:40 -04:00
|
|
|
"""
|
|
|
|
|
2020-10-21 06:44:31 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-10-08 07:44:43 -04:00
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.hs = hs
|
2017-08-25 06:21:34 -04:00
|
|
|
|
2018-03-13 09:26:52 -04:00
|
|
|
self.federation = hs.get_federation_client()
|
2018-03-12 12:17:08 -04:00
|
|
|
hs.get_federation_registry().register_query_handler(
|
2014-08-13 12:12:50 -04:00
|
|
|
"profile", self.on_profile_query
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2017-11-29 13:27:05 -05:00
|
|
|
self.user_directory_handler = hs.get_user_directory_handler()
|
2021-10-08 07:44:43 -04:00
|
|
|
self.request_ratelimiter = hs.get_request_ratelimiter()
|
2017-11-29 13:27:05 -05:00
|
|
|
|
2022-01-28 09:41:33 -05:00
|
|
|
self.max_avatar_size = hs.config.server.max_avatar_size
|
|
|
|
self.allowed_avatar_mimetypes = hs.config.server.allowed_avatar_mimetypes
|
|
|
|
|
2023-05-05 10:06:22 -04:00
|
|
|
self._is_mine_server_name = hs.is_mine_server_name
|
2022-01-28 09:41:33 -05:00
|
|
|
|
2023-05-04 10:18:22 -04:00
|
|
|
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules
|
2022-03-01 10:00:03 -05:00
|
|
|
|
2023-03-16 07:44:11 -04:00
|
|
|
async def get_profile(self, user_id: str, ignore_backoff: bool = True) -> JsonDict:
|
2017-08-25 06:21:34 -04:00
|
|
|
target_user = UserID.from_string(user_id)
|
2019-05-08 13:26:56 -04:00
|
|
|
|
2017-08-25 06:21:34 -04:00
|
|
|
if self.hs.is_mine(target_user):
|
2023-06-02 20:24:13 -04:00
|
|
|
profileinfo = await self.store.get_profileinfo(target_user)
|
2022-07-07 07:02:09 -04:00
|
|
|
if profileinfo.display_name is None:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"displayname": profileinfo.display_name,
|
|
|
|
"avatar_url": profileinfo.avatar_url,
|
|
|
|
}
|
2017-08-25 06:21:34 -04:00
|
|
|
else:
|
|
|
|
try:
|
2020-07-17 07:08:30 -04:00
|
|
|
result = await self.federation.make_query(
|
2017-08-25 06:21:34 -04:00
|
|
|
destination=target_user.domain,
|
|
|
|
query_type="profile",
|
|
|
|
args={"user_id": user_id},
|
2023-03-16 07:44:11 -04:00
|
|
|
ignore_backoff=ignore_backoff,
|
2017-08-25 06:21:34 -04:00
|
|
|
)
|
2019-07-23 09:00:55 -04:00
|
|
|
return result
|
2019-06-07 05:29:35 -04:00
|
|
|
except RequestSendFailed as e:
|
2020-06-16 08:51:47 -04:00
|
|
|
raise SynapseError(502, "Failed to fetch profile") from e
|
2019-06-07 05:29:35 -04:00
|
|
|
except HttpResponseException as e:
|
2020-10-26 09:55:21 -04:00
|
|
|
if e.code < 500 and e.code != 404:
|
|
|
|
# Other codes are not allowed in c2s API
|
|
|
|
logger.info(
|
|
|
|
"Server replied with wrong response: %s %s", e.code, e.msg
|
|
|
|
)
|
|
|
|
|
|
|
|
raise SynapseError(502, "Failed to fetch profile")
|
2019-06-07 05:29:35 -04:00
|
|
|
raise e.to_synapse_error()
|
2017-08-25 06:21:34 -04:00
|
|
|
|
2020-10-26 14:17:31 -04:00
|
|
|
async def get_displayname(self, target_user: UserID) -> Optional[str]:
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(target_user):
|
2018-08-03 14:08:05 -04:00
|
|
|
try:
|
2023-06-02 20:24:13 -04:00
|
|
|
displayname = await self.store.get_profile_displayname(target_user)
|
2018-08-03 14:08:05 -04:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
raise
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return displayname
|
2014-08-13 12:14:42 -04:00
|
|
|
else:
|
2014-08-12 10:10:52 -04:00
|
|
|
try:
|
2020-07-17 07:08:30 -04:00
|
|
|
result = await self.federation.make_query(
|
2014-08-12 10:10:52 -04:00
|
|
|
destination=target_user.domain,
|
2014-08-13 12:12:50 -04:00
|
|
|
query_type="profile",
|
|
|
|
args={"user_id": target_user.to_string(), "field": "displayname"},
|
2017-03-23 07:10:36 -04:00
|
|
|
ignore_backoff=True,
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
2019-06-07 05:29:35 -04:00
|
|
|
except RequestSendFailed as e:
|
2020-06-16 08:51:47 -04:00
|
|
|
raise SynapseError(502, "Failed to fetch profile") from e
|
2019-06-07 05:29:35 -04:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2018-10-03 06:34:30 -04:00
|
|
|
|
2021-01-06 07:29:03 -05:00
|
|
|
return result.get("displayname")
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2020-05-01 10:15:36 -04:00
|
|
|
async def set_displayname(
|
2020-10-21 06:44:31 -04:00
|
|
|
self,
|
|
|
|
target_user: UserID,
|
|
|
|
requester: Requester,
|
|
|
|
new_displayname: str,
|
|
|
|
by_admin: bool = False,
|
2022-03-01 10:00:03 -05:00
|
|
|
deactivation: bool = False,
|
2020-10-21 06:44:31 -04:00
|
|
|
) -> None:
|
2019-03-26 13:48:30 -04:00
|
|
|
"""Set the displayname of a user
|
|
|
|
|
|
|
|
Args:
|
2020-10-21 06:44:31 -04:00
|
|
|
target_user: the user whose displayname is to be changed.
|
|
|
|
requester: The user attempting to make this change.
|
|
|
|
new_displayname: The displayname to give this user.
|
|
|
|
by_admin: Whether this change was made by an administrator.
|
2022-03-01 10:00:03 -05:00
|
|
|
deactivation: Whether this change was made while deactivating the user.
|
2019-03-26 13:48:30 -04:00
|
|
|
"""
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(target_user):
|
2019-11-12 08:08:12 -05:00
|
|
|
raise SynapseError(400, "User is not hosted on this homeserver")
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-10-04 16:30:51 -04:00
|
|
|
if not by_admin and target_user != requester.user:
|
2014-08-12 10:10:52 -04:00
|
|
|
raise AuthError(400, "Cannot set another user's displayname")
|
|
|
|
|
2021-10-04 07:18:54 -04:00
|
|
|
if not by_admin and not self.hs.config.registration.enable_set_displayname:
|
2023-06-02 20:24:13 -04:00
|
|
|
profile = await self.store.get_profileinfo(target_user)
|
2020-03-27 15:15:23 -04:00
|
|
|
if profile.display_name:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Changing display name is disabled on this server",
|
|
|
|
Codes.FORBIDDEN,
|
|
|
|
)
|
|
|
|
|
2020-09-01 13:02:41 -04:00
|
|
|
if not isinstance(new_displayname, str):
|
2020-11-02 13:01:09 -05:00
|
|
|
raise SynapseError(
|
|
|
|
400, "'displayname' must be a string", errcode=Codes.INVALID_PARAM
|
|
|
|
)
|
2020-09-01 13:02:41 -04:00
|
|
|
|
2019-06-01 05:42:33 -04:00
|
|
|
if len(new_displayname) > MAX_DISPLAYNAME_LEN:
|
|
|
|
raise SynapseError(
|
|
|
|
400, "Displayname is too long (max %i)" % (MAX_DISPLAYNAME_LEN,)
|
|
|
|
)
|
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
displayname_to_set: Optional[str] = new_displayname
|
2015-05-14 09:19:10 -04:00
|
|
|
if new_displayname == "":
|
2020-10-21 06:44:31 -04:00
|
|
|
displayname_to_set = None
|
2015-05-14 09:19:10 -04:00
|
|
|
|
2020-02-21 12:44:03 -05:00
|
|
|
# If the admin changes the display name of a user, the requesting user cannot send
|
2023-05-05 10:51:46 -04:00
|
|
|
# the join event to update the display name in the rooms.
|
|
|
|
# This must be done by the target user themselves.
|
2020-02-21 12:44:03 -05:00
|
|
|
if by_admin:
|
2020-11-17 05:51:25 -05:00
|
|
|
requester = create_requester(
|
|
|
|
target_user,
|
|
|
|
authenticated_entity=requester.authenticated_entity,
|
|
|
|
)
|
2020-02-21 12:44:03 -05:00
|
|
|
|
2023-04-26 19:03:26 -04:00
|
|
|
await self.store.set_profile_displayname(target_user, displayname_to_set)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2023-06-02 20:24:13 -04:00
|
|
|
profile = await self.store.get_profileinfo(target_user)
|
2021-09-21 08:02:34 -04:00
|
|
|
await self.user_directory_handler.handle_local_profile_change(
|
|
|
|
target_user.to_string(), profile
|
|
|
|
)
|
2017-11-29 13:27:05 -05:00
|
|
|
|
2022-03-01 10:00:03 -05:00
|
|
|
await self._third_party_rules.on_profile_update(
|
|
|
|
target_user.to_string(), profile, by_admin, deactivation
|
|
|
|
)
|
|
|
|
|
2020-05-01 10:15:36 -04:00
|
|
|
await self._update_join_states(requester, target_user)
|
2014-09-17 10:05:09 -04:00
|
|
|
|
2020-10-26 14:17:31 -04:00
|
|
|
async def get_avatar_url(self, target_user: UserID) -> Optional[str]:
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(target_user):
|
2018-08-03 14:08:05 -04:00
|
|
|
try:
|
2023-06-02 20:24:13 -04:00
|
|
|
avatar_url = await self.store.get_profile_avatar_url(target_user)
|
2018-08-03 14:08:05 -04:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
raise
|
2019-07-23 09:00:55 -04:00
|
|
|
return avatar_url
|
2014-08-13 12:14:42 -04:00
|
|
|
else:
|
2014-08-12 10:10:52 -04:00
|
|
|
try:
|
2020-07-17 07:08:30 -04:00
|
|
|
result = await self.federation.make_query(
|
2014-08-13 12:12:50 -04:00
|
|
|
destination=target_user.domain,
|
|
|
|
query_type="profile",
|
|
|
|
args={"user_id": target_user.to_string(), "field": "avatar_url"},
|
2017-03-23 07:10:36 -04:00
|
|
|
ignore_backoff=True,
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
2019-06-07 05:29:35 -04:00
|
|
|
except RequestSendFailed as e:
|
2020-06-16 08:51:47 -04:00
|
|
|
raise SynapseError(502, "Failed to fetch profile") from e
|
2019-06-07 05:29:35 -04:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-01-06 07:29:03 -05:00
|
|
|
return result.get("avatar_url")
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2020-05-01 10:15:36 -04:00
|
|
|
async def set_avatar_url(
|
2020-10-21 06:44:31 -04:00
|
|
|
self,
|
|
|
|
target_user: UserID,
|
|
|
|
requester: Requester,
|
|
|
|
new_avatar_url: str,
|
|
|
|
by_admin: bool = False,
|
2022-03-01 10:00:03 -05:00
|
|
|
deactivation: bool = False,
|
2021-09-20 08:56:23 -04:00
|
|
|
) -> None:
|
2020-08-26 08:49:01 -04:00
|
|
|
"""Set a new avatar URL for a user.
|
|
|
|
|
|
|
|
Args:
|
2020-10-21 06:44:31 -04:00
|
|
|
target_user: the user whose avatar URL is to be changed.
|
|
|
|
requester: The user attempting to make this change.
|
|
|
|
new_avatar_url: The avatar URL to give this user.
|
|
|
|
by_admin: Whether this change was made by an administrator.
|
2022-03-01 10:00:03 -05:00
|
|
|
deactivation: Whether this change was made while deactivating the user.
|
2020-08-26 08:49:01 -04:00
|
|
|
"""
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(target_user):
|
2019-11-12 08:08:12 -05:00
|
|
|
raise SynapseError(400, "User is not hosted on this homeserver")
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-10-04 16:30:51 -04:00
|
|
|
if not by_admin and target_user != requester.user:
|
2014-08-12 10:10:52 -04:00
|
|
|
raise AuthError(400, "Cannot set another user's avatar_url")
|
|
|
|
|
2021-10-04 07:18:54 -04:00
|
|
|
if not by_admin and not self.hs.config.registration.enable_set_avatar_url:
|
2023-06-02 20:24:13 -04:00
|
|
|
profile = await self.store.get_profileinfo(target_user)
|
2020-03-27 15:15:23 -04:00
|
|
|
if profile.avatar_url:
|
|
|
|
raise SynapseError(
|
|
|
|
400, "Changing avatar is disabled on this server", Codes.FORBIDDEN
|
|
|
|
)
|
|
|
|
|
2020-09-01 13:02:41 -04:00
|
|
|
if not isinstance(new_avatar_url, str):
|
2020-11-02 13:01:09 -05:00
|
|
|
raise SynapseError(
|
|
|
|
400, "'avatar_url' must be a string", errcode=Codes.INVALID_PARAM
|
|
|
|
)
|
2020-09-01 13:02:41 -04:00
|
|
|
|
2019-06-01 05:42:33 -04:00
|
|
|
if len(new_avatar_url) > MAX_AVATAR_URL_LEN:
|
|
|
|
raise SynapseError(
|
|
|
|
400, "Avatar URL is too long (max %i)" % (MAX_AVATAR_URL_LEN,)
|
|
|
|
)
|
|
|
|
|
2022-01-28 09:41:33 -05:00
|
|
|
if not await self.check_avatar_size_and_mime_type(new_avatar_url):
|
|
|
|
raise SynapseError(403, "This avatar is not allowed", Codes.FORBIDDEN)
|
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
avatar_url_to_set: Optional[str] = new_avatar_url
|
2021-01-12 16:30:15 -05:00
|
|
|
if new_avatar_url == "":
|
|
|
|
avatar_url_to_set = None
|
|
|
|
|
2020-02-21 12:44:03 -05:00
|
|
|
# Same like set_displayname
|
|
|
|
if by_admin:
|
2020-11-17 05:51:25 -05:00
|
|
|
requester = create_requester(
|
|
|
|
target_user, authenticated_entity=requester.authenticated_entity
|
|
|
|
)
|
2020-02-21 12:44:03 -05:00
|
|
|
|
2023-04-26 19:03:26 -04:00
|
|
|
await self.store.set_profile_avatar_url(target_user, avatar_url_to_set)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2023-06-02 20:24:13 -04:00
|
|
|
profile = await self.store.get_profileinfo(target_user)
|
2021-09-21 08:02:34 -04:00
|
|
|
await self.user_directory_handler.handle_local_profile_change(
|
|
|
|
target_user.to_string(), profile
|
|
|
|
)
|
2017-11-29 13:27:05 -05:00
|
|
|
|
2022-03-01 10:00:03 -05:00
|
|
|
await self._third_party_rules.on_profile_update(
|
|
|
|
target_user.to_string(), profile, by_admin, deactivation
|
|
|
|
)
|
|
|
|
|
2020-05-01 10:15:36 -04:00
|
|
|
await self._update_join_states(requester, target_user)
|
2014-09-17 10:05:09 -04:00
|
|
|
|
2022-01-28 09:41:33 -05:00
|
|
|
@cached()
|
|
|
|
async def check_avatar_size_and_mime_type(self, mxc: str) -> bool:
|
|
|
|
"""Check that the size and content type of the avatar at the given MXC URI are
|
|
|
|
within the configured limits.
|
|
|
|
|
2022-03-25 09:28:42 -04:00
|
|
|
If the given `mxc` is empty, no checks are performed. (Users are always able to
|
|
|
|
unset their avatar.)
|
|
|
|
|
2022-01-28 09:41:33 -05:00
|
|
|
Args:
|
|
|
|
mxc: The MXC URI at which the avatar can be found.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A boolean indicating whether the file can be allowed to be set as an avatar.
|
|
|
|
"""
|
2022-03-25 09:28:42 -04:00
|
|
|
if mxc == "":
|
|
|
|
return True
|
|
|
|
|
2022-01-28 09:41:33 -05:00
|
|
|
if not self.max_avatar_size and not self.allowed_avatar_mimetypes:
|
|
|
|
return True
|
|
|
|
|
2022-10-26 10:51:23 -04:00
|
|
|
host, port, media_id = parse_and_validate_mxc_uri(mxc)
|
|
|
|
if port is not None:
|
|
|
|
server_name = host + ":" + str(port)
|
|
|
|
else:
|
|
|
|
server_name = host
|
2022-01-28 09:41:33 -05:00
|
|
|
|
2023-05-05 10:06:22 -04:00
|
|
|
if self._is_mine_server_name(server_name):
|
2022-01-28 09:41:33 -05:00
|
|
|
media_info = await self.store.get_local_media(media_id)
|
|
|
|
else:
|
|
|
|
media_info = await self.store.get_cached_remote_media(server_name, media_id)
|
|
|
|
|
|
|
|
if media_info is None:
|
|
|
|
# Both configuration options need to access the file's metadata, and
|
|
|
|
# retrieving remote avatars just for this becomes a bit of a faff, especially
|
|
|
|
# if e.g. the file is too big. It's also generally safe to assume most files
|
|
|
|
# used as avatar are uploaded locally, or if the upload didn't happen as part
|
|
|
|
# of a PUT request on /avatar_url that the file was at least previewed by the
|
|
|
|
# user locally (and therefore downloaded to the remote media cache).
|
|
|
|
logger.warning("Forbidding avatar change to %s: avatar not on server", mxc)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.max_avatar_size:
|
|
|
|
# Ensure avatar does not exceed max allowed avatar size
|
|
|
|
if media_info["media_length"] > self.max_avatar_size:
|
|
|
|
logger.warning(
|
|
|
|
"Forbidding avatar change to %s: %d bytes is above the allowed size "
|
|
|
|
"limit",
|
|
|
|
mxc,
|
|
|
|
media_info["media_length"],
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.allowed_avatar_mimetypes:
|
|
|
|
# Ensure the avatar's file type is allowed
|
|
|
|
if (
|
|
|
|
self.allowed_avatar_mimetypes
|
|
|
|
and media_info["media_type"] not in self.allowed_avatar_mimetypes
|
|
|
|
):
|
|
|
|
logger.warning(
|
|
|
|
"Forbidding avatar change to %s: mimetype %s not allowed",
|
|
|
|
mxc,
|
|
|
|
media_info["media_type"],
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2020-10-21 06:44:31 -04:00
|
|
|
async def on_profile_query(self, args: JsonDict) -> JsonDict:
|
2021-02-19 08:19:54 -05:00
|
|
|
"""Handles federation profile query requests."""
|
2021-02-19 04:50:41 -05:00
|
|
|
|
2021-09-23 12:03:01 -04:00
|
|
|
if not self.hs.config.federation.allow_profile_lookup_over_federation:
|
2021-02-19 04:50:41 -05:00
|
|
|
raise SynapseError(
|
|
|
|
403,
|
|
|
|
"Profile lookup over federation is disabled on this homeserver",
|
|
|
|
Codes.FORBIDDEN,
|
|
|
|
)
|
|
|
|
|
2015-01-23 06:47:15 -05:00
|
|
|
user = UserID.from_string(args["user_id"])
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(user):
|
2019-11-12 08:08:12 -05:00
|
|
|
raise SynapseError(400, "User is not hosted on this homeserver")
|
2014-08-13 12:12:50 -04:00
|
|
|
|
|
|
|
just_field = args.get("field", None)
|
|
|
|
|
|
|
|
response = {}
|
2018-08-03 14:08:05 -04:00
|
|
|
try:
|
|
|
|
if just_field is None or just_field == "displayname":
|
2023-06-02 20:24:13 -04:00
|
|
|
response["displayname"] = await self.store.get_profile_displayname(user)
|
2014-08-13 12:12:50 -04:00
|
|
|
|
2018-08-03 14:08:05 -04:00
|
|
|
if just_field is None or just_field == "avatar_url":
|
2023-06-02 20:24:13 -04:00
|
|
|
response["avatar_url"] = await self.store.get_profile_avatar_url(user)
|
2018-08-03 14:08:05 -04:00
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
|
|
|
|
raise
|
2014-08-13 12:12:50 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return response
|
2014-09-17 10:05:09 -04:00
|
|
|
|
2020-10-21 06:44:31 -04:00
|
|
|
async def _update_join_states(
|
|
|
|
self, requester: Requester, target_user: UserID
|
|
|
|
) -> None:
|
2017-11-03 13:25:04 -04:00
|
|
|
if not self.hs.is_mine(target_user):
|
2014-09-17 10:05:09 -04:00
|
|
|
return
|
|
|
|
|
2021-10-08 07:44:43 -04:00
|
|
|
await self.request_ratelimiter.ratelimit(requester)
|
2014-12-19 12:36:33 -05:00
|
|
|
|
2020-08-26 08:49:01 -04: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 10:15:36 -04:00
|
|
|
room_ids = await self.store.get_rooms_for_user(target_user.to_string())
|
2014-09-17 10:05:09 -04:00
|
|
|
|
2017-03-16 07:51:46 -04:00
|
|
|
for room_id in room_ids:
|
2018-03-01 05:54:37 -05:00
|
|
|
handler = self.hs.get_room_member_handler()
|
2015-03-06 11:24:05 -05:00
|
|
|
try:
|
2017-11-03 13:25:04 -04:00
|
|
|
# Assume the target_user isn't a guest,
|
|
|
|
# because we don't let guests set profile or avatar data.
|
2020-05-01 10:15:36 -04:00
|
|
|
await handler.update_membership(
|
2016-02-15 12:14:34 -05:00
|
|
|
requester,
|
2017-11-03 13:25:04 -04:00
|
|
|
target_user,
|
2017-03-16 07:51:46 -04:00
|
|
|
room_id,
|
2016-02-15 12:14:34 -05:00
|
|
|
"join", # We treat a profile update like a join.
|
2016-02-16 06:52:46 -05:00
|
|
|
ratelimit=False, # Try to hide that these events aren't atomic.
|
2016-02-15 12:14:34 -05:00
|
|
|
)
|
2015-03-06 11:24:05 -05:00
|
|
|
except Exception as e:
|
2019-10-31 06:23:24 -04:00
|
|
|
logger.warning(
|
2015-03-06 11:24:05 -05:00
|
|
|
"Failed to update join event for room %s - %s", room_id, str(e)
|
|
|
|
)
|
2017-08-25 06:21:34 -04:00
|
|
|
|
2020-10-21 06:44:31 -04:00
|
|
|
async def check_profile_query_allowed(
|
|
|
|
self, target_user: UserID, requester: Optional[UserID] = None
|
|
|
|
) -> None:
|
2019-05-08 13:26:56 -04: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:
|
2020-10-21 06:44:31 -04:00
|
|
|
target_user: The owner of the queried profile.
|
|
|
|
requester: The user querying for the profile.
|
2019-05-08 13:26:56 -04:00
|
|
|
|
|
|
|
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 11:11:55 -05:00
|
|
|
|
2019-05-08 13:26:56 -04: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 11:11:55 -05:00
|
|
|
if (
|
2021-09-29 06:44:15 -04:00
|
|
|
not self.hs.config.server.limit_profile_requests_to_users_who_share_rooms
|
2019-12-16 11:11:55 -05:00
|
|
|
or not requester
|
|
|
|
):
|
2019-05-08 13:26:56 -04:00
|
|
|
return
|
|
|
|
|
2019-07-08 12:31:00 -04:00
|
|
|
# Always allow the user to query their own profile.
|
|
|
|
if target_user.to_string() == requester.to_string():
|
|
|
|
return
|
|
|
|
|
2019-05-08 13:26:56 -04:00
|
|
|
try:
|
2020-07-17 07:08:30 -04:00
|
|
|
requester_rooms = await self.store.get_rooms_for_user(requester.to_string())
|
|
|
|
target_user_rooms = await self.store.get_rooms_for_user(
|
2019-05-08 13:26:56 -04:00
|
|
|
target_user.to_string()
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|