Skip to content

Commit a2a92f7

Browse files
committed
Refactor get_profile: do not return missing fields.
1 parent ebc21a8 commit a2a92f7

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

synapse/api/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,8 @@ class ApprovalNoticeMedium:
320320
class Direction(enum.Enum):
321321
BACKWARDS = "b"
322322
FORWARDS = "f"
323+
324+
325+
class ProfileFields:
326+
DISPLAYNAME: Final = "displayname"
327+
AVATAR_URL: Final = "avatar_url"

synapse/handlers/profile.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import random
2323
from typing import TYPE_CHECKING, List, Optional, Union
2424

25+
from synapse.api.constants import ProfileFields
2526
from synapse.api.errors import (
2627
AuthError,
2728
Codes,
@@ -92,10 +93,13 @@ async def get_profile(self, user_id: str, ignore_backoff: bool = True) -> JsonDi
9293
if profileinfo.display_name is None and profileinfo.avatar_url is None:
9394
raise SynapseError(404, "Profile was not found", Codes.NOT_FOUND)
9495

95-
return {
96-
"displayname": profileinfo.display_name,
97-
"avatar_url": profileinfo.avatar_url,
98-
}
96+
# Do not include display name or avatar are denoted if unset.
97+
ret = {}
98+
if profileinfo.display_name is not None:
99+
ret[ProfileFields.DISPLAYNAME] = profileinfo.display_name
100+
if profileinfo.avatar_url is not None:
101+
ret[ProfileFields.AVATAR_URL] = profileinfo.avatar_url
102+
return ret
99103
else:
100104
try:
101105
result = await self.federation.make_query(

synapse/handlers/sso.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from twisted.web.iweb import IRequest
4444
from twisted.web.server import Request
4545

46-
from synapse.api.constants import LoginType
46+
from synapse.api.constants import LoginType, ProfileFields
4747
from synapse.api.errors import Codes, NotFoundError, RedirectException, SynapseError
4848
from synapse.config.sso import SsoAttributeRequirement
4949
from synapse.handlers.device import DeviceHandler
@@ -813,9 +813,10 @@ def is_allowed_mime_type(content_type: str) -> bool:
813813

814814
# bail if user already has the same avatar
815815
profile = await self._profile_handler.get_profile(user_id)
816-
if profile["avatar_url"] is not None:
817-
server_name = profile["avatar_url"].split("/")[-2]
818-
media_id = profile["avatar_url"].split("/")[-1]
816+
if ProfileFields.AVATAR_URL in profile:
817+
avatar_url_parts = profile[ProfileFields.AVATAR_URL].split("/")
818+
server_name = avatar_url_parts[-2]
819+
media_id = avatar_url_parts[-1]
819820
if self._is_mine_server_name(server_name):
820821
media = await self._media_repo.store.get_local_media(media_id) # type: ignore[has-type]
821822
if media is not None and upload_name == media.upload_name:

synapse/handlers/user_directory.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
from twisted.internet.interfaces import IDelayedCall
2727

2828
import synapse.metrics
29-
from synapse.api.constants import EventTypes, HistoryVisibility, JoinRules, Membership
29+
from synapse.api.constants import (
30+
EventTypes,
31+
HistoryVisibility,
32+
JoinRules,
33+
Membership,
34+
ProfileFields,
35+
)
3036
from synapse.api.errors import Codes, SynapseError
3137
from synapse.handlers.state_deltas import MatchChange, StateDeltasHandler
3238
from synapse.metrics.background_process_metrics import run_as_background_process
@@ -756,6 +762,10 @@ async def _unsafe_refresh_remote_profiles_for_remote_server(
756762

757763
await self.store.update_profile_in_user_dir(
758764
user_id,
759-
display_name=non_null_str_or_none(profile.get("displayname")),
760-
avatar_url=non_null_str_or_none(profile.get("avatar_url")),
765+
display_name=non_null_str_or_none(
766+
profile.get(ProfileFields.DISPLAYNAME)
767+
),
768+
avatar_url=non_null_str_or_none(
769+
profile.get(ProfileFields.AVATAR_URL)
770+
),
761771
)

synapse/module_api/__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from twisted.web.resource import Resource
4646

4747
from synapse.api import errors
48+
from synapse.api.constants import ProfileFields
4849
from synapse.api.errors import SynapseError
4950
from synapse.api.presence import UserPresenceState
5051
from synapse.config import ConfigError
@@ -1086,7 +1087,10 @@ async def update_room_membership(
10861087
content = {}
10871088

10881089
# Set the profile if not already done by the module.
1089-
if "avatar_url" not in content or "displayname" not in content:
1090+
if (
1091+
ProfileFields.AVATAR_URL not in content
1092+
or ProfileFields.DISPLAYNAME not in content
1093+
):
10901094
try:
10911095
# Try to fetch the user's profile.
10921096
profile = await self._hs.get_profile_handler().get_profile(
@@ -1095,8 +1099,8 @@ async def update_room_membership(
10951099
except SynapseError as e:
10961100
# If the profile couldn't be found, use default values.
10971101
profile = {
1098-
"displayname": target_user_id.localpart,
1099-
"avatar_url": None,
1102+
ProfileFields.DISPLAYNAME: target_user_id.localpart,
1103+
ProfileFields.AVATAR_URL: None,
11001104
}
11011105

11021106
if e.code != 404:
@@ -1109,11 +1113,9 @@ async def update_room_membership(
11091113
)
11101114

11111115
# Set the profile where it needs to be set.
1112-
if "avatar_url" not in content:
1113-
content["avatar_url"] = profile["avatar_url"]
1114-
1115-
if "displayname" not in content:
1116-
content["displayname"] = profile["displayname"]
1116+
for field_name in [ProfileFields.AVATAR_URL, ProfileFields.DISPLAYNAME]:
1117+
if field_name not in content and field_name in profile:
1118+
content[field_name] = profile[field_name]
11171119

11181120
event_id, _ = await self._hs.get_room_member_handler().update_membership(
11191121
requester=requester,

0 commit comments

Comments
 (0)