Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad4c14e

Browse files
authoredSep 30, 2022
Clarifications in user directory for users who share rooms tracking (#13966)
Spawned while working on [`get_users_in_room` mis-uses](#13958 (comment)) and thinking we could use `get_local_users_in_room` here but we can't. From first glance, it seemed like this was only using local users from all of the `is_mine_id(user_id)` checks but I see that it does actually use remote users. Just making things a little more clear here what it does and mentions remote users so maybe that will be more obvious in the future.
1 parent 535f8c8 commit ad4c14e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed
 

‎changelog.d/13966.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor language in user directory `_track_user_joined_room` code to make it more clear that we use both local and remote users.

‎synapse/handlers/user_directory.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
import logging
16-
from typing import TYPE_CHECKING, Any, Dict, List, Optional
16+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple
1717

1818
import synapse.metrics
1919
from synapse.api.constants import EventTypes, HistoryVisibility, JoinRules, Membership
@@ -379,7 +379,7 @@ async def _upsert_directory_entry_for_remote_user(
379379
user_id, event.content.get("displayname"), event.content.get("avatar_url")
380380
)
381381

382-
async def _track_user_joined_room(self, room_id: str, user_id: str) -> None:
382+
async def _track_user_joined_room(self, room_id: str, joining_user_id: str) -> None:
383383
"""Someone's just joined a room. Update `users_in_public_rooms` or
384384
`users_who_share_private_rooms` as appropriate.
385385
@@ -390,32 +390,44 @@ async def _track_user_joined_room(self, room_id: str, user_id: str) -> None:
390390
room_id
391391
)
392392
if is_public:
393-
await self.store.add_users_in_public_rooms(room_id, (user_id,))
393+
await self.store.add_users_in_public_rooms(room_id, (joining_user_id,))
394394
else:
395395
users_in_room = await self.store.get_users_in_room(room_id)
396396
other_users_in_room = [
397397
other
398398
for other in users_in_room
399-
if other != user_id
399+
if other != joining_user_id
400400
and (
401+
# We can't apply any special rules to remote users so
402+
# they're always included
401403
not self.is_mine_id(other)
404+
# Check the special rules whether the local user should be
405+
# included in the user directory
402406
or await self.store.should_include_local_user_in_dir(other)
403407
)
404408
]
405-
to_insert = set()
409+
updates_to_users_who_share_rooms: Set[Tuple[str, str]] = set()
406410

407-
# First, if they're our user then we need to update for every user
408-
if self.is_mine_id(user_id):
411+
# First, if the joining user is our local user then we need an
412+
# update for every other user in the room.
413+
if self.is_mine_id(joining_user_id):
409414
for other_user_id in other_users_in_room:
410-
to_insert.add((user_id, other_user_id))
415+
updates_to_users_who_share_rooms.add(
416+
(joining_user_id, other_user_id)
417+
)
411418

412-
# Next we need to update for every local user in the room
419+
# Next, we need an update for every other local user in the room
420+
# that they now share a room with the joining user.
413421
for other_user_id in other_users_in_room:
414422
if self.is_mine_id(other_user_id):
415-
to_insert.add((other_user_id, user_id))
423+
updates_to_users_who_share_rooms.add(
424+
(other_user_id, joining_user_id)
425+
)
416426

417-
if to_insert:
418-
await self.store.add_users_who_share_private_room(room_id, to_insert)
427+
if updates_to_users_who_share_rooms:
428+
await self.store.add_users_who_share_private_room(
429+
room_id, updates_to_users_who_share_rooms
430+
)
419431

420432
async def _handle_remove_user(self, room_id: str, user_id: str) -> None:
421433
"""Called when when someone leaves a room. The user may be local or remote.

0 commit comments

Comments
 (0)
This repository has been archived.