Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit e0bf34d

Browse files
David Robertsonclokep
andauthored
Don't alter directory entries for local users when setting a per-room nickname (#11002)
Co-authored-by: Patrick Cloke <[email protected]>
1 parent 96fe77c commit e0bf34d

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

changelog.d/11002.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where local users' per-room nicknames/avatars were visible to anyone who could see you in the user_directory.

synapse/handlers/user_directory.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ async def _handle_deltas(self, deltas: List[Dict[str, Any]]) -> None:
203203
public_value=Membership.JOIN,
204204
)
205205

206+
is_remote = not self.is_mine_id(state_key)
206207
if change is MatchChange.now_false:
207208
# Need to check if the server left the room entirely, if so
208209
# we might need to remove all the users in that room
@@ -224,15 +225,20 @@ async def _handle_deltas(self, deltas: List[Dict[str, Any]]) -> None:
224225
else:
225226
logger.debug("Server is still in room: %r", room_id)
226227

227-
include_in_dir = not self.is_mine_id(
228-
state_key
229-
) or await self.store.should_include_local_user_in_dir(state_key)
228+
include_in_dir = (
229+
is_remote
230+
or await self.store.should_include_local_user_in_dir(state_key)
231+
)
230232
if include_in_dir:
231233
if change is MatchChange.no_change:
232-
# Handle any profile changes
233-
await self._handle_profile_change(
234-
state_key, room_id, prev_event_id, event_id
235-
)
234+
# Handle any profile changes for remote users.
235+
# (For local users we are not forced to scan membership
236+
# events; instead the rest of the application calls
237+
# `handle_local_profile_change`.)
238+
if is_remote:
239+
await self._handle_profile_change(
240+
state_key, room_id, prev_event_id, event_id
241+
)
236242
continue
237243

238244
if change is MatchChange.now_true: # The user joined

tests/handlers/test_user_directory.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,40 @@ def test_process_join_after_server_leaves_room(self) -> None:
402402
public3 = self.get_success(self.user_dir_helper.get_users_in_public_rooms())
403403
self.assertEqual(set(public3), {(alice, room2), (bob, room2)})
404404

405+
def test_per_room_profile_doesnt_alter_directory_entry(self) -> None:
406+
alice = self.register_user("alice", "pass")
407+
alice_token = self.login(alice, "pass")
408+
bob = self.register_user("bob", "pass")
409+
410+
# Alice should have a user directory entry created at registration.
411+
users = self.get_success(self.user_dir_helper.get_profiles_in_user_directory())
412+
self.assertEqual(
413+
users[alice], ProfileInfo(display_name="alice", avatar_url=None)
414+
)
415+
416+
# Alice makes a room for herself.
417+
room = self.helper.create_room_as(alice, is_public=True, tok=alice_token)
418+
419+
# Alice sets a nickname unique to that room.
420+
self.helper.send_state(
421+
room,
422+
"m.room.member",
423+
{
424+
"displayname": "Freddy Mercury",
425+
"membership": "join",
426+
},
427+
alice_token,
428+
state_key=alice,
429+
)
430+
431+
# Alice's display name remains the same in the user directory.
432+
search_result = self.get_success(self.handler.search_users(bob, alice, 10))
433+
self.assertEqual(
434+
search_result["results"],
435+
[{"display_name": "alice", "avatar_url": None, "user_id": alice}],
436+
0,
437+
)
438+
405439
def test_private_room(self) -> None:
406440
"""
407441
A user can be searched for only by people that are either in a public

0 commit comments

Comments
 (0)