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

Commit 6be4953

Browse files
Mutual rooms: Remove dependency on user directory (#12836)
1 parent bda4600 commit 6be4953

File tree

5 files changed

+27
-58
lines changed

5 files changed

+27
-58
lines changed

changelog.d/12836.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove Mutual Rooms ([MSC2666](https://github.com/matrix-org/matrix-spec-proposals/pull/2666)) endpoint dependency on the User Directory.

synapse/rest/client/mutual_rooms.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,10 @@ def __init__(self, hs: "HomeServer"):
4242
super().__init__()
4343
self.auth = hs.get_auth()
4444
self.store = hs.get_datastores().main
45-
self.user_directory_search_enabled = (
46-
hs.config.userdirectory.user_directory_search_enabled
47-
)
4845

4946
async def on_GET(
5047
self, request: SynapseRequest, user_id: str
5148
) -> Tuple[int, JsonDict]:
52-
53-
if not self.user_directory_search_enabled:
54-
raise SynapseError(
55-
code=400,
56-
msg="User directory searching is disabled. Cannot determine shared rooms.",
57-
errcode=Codes.UNKNOWN,
58-
)
59-
6049
UserID.from_string(user_id)
6150

6251
requester = await self.auth.get_user_by_req(request)
@@ -67,8 +56,8 @@ async def on_GET(
6756
errcode=Codes.FORBIDDEN,
6857
)
6958

70-
rooms = await self.store.get_mutual_rooms_for_users(
71-
requester.user.to_string(), user_id
59+
rooms = await self.store.get_mutual_rooms_between_users(
60+
frozenset((requester.user.to_string(), user_id))
7261
)
7362

7463
return 200, {"joined": list(rooms)}

synapse/storage/databases/main/roommember.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,30 @@ async def get_users_who_share_room_with_user(
670670

671671
return user_who_share_room
672672

673+
@cached(cache_context=True, iterable=True)
674+
async def get_mutual_rooms_between_users(
675+
self, user_ids: FrozenSet[str], cache_context: _CacheContext
676+
) -> FrozenSet[str]:
677+
"""
678+
Returns the set of rooms that all users in `user_ids` share.
679+
680+
Args:
681+
user_ids: A frozen set of all users to investigate and return
682+
overlapping joined rooms for.
683+
cache_context
684+
"""
685+
shared_room_ids: Optional[FrozenSet[str]] = None
686+
for user_id in user_ids:
687+
room_ids = await self.get_rooms_for_user(
688+
user_id, on_invalidate=cache_context.invalidate
689+
)
690+
if shared_room_ids is not None:
691+
shared_room_ids &= room_ids
692+
else:
693+
shared_room_ids = room_ids
694+
695+
return shared_room_ids or frozenset()
696+
673697
async def get_joined_users_from_context(
674698
self, event: EventBase, context: EventContext
675699
) -> Dict[str, ProfileInfo]:

synapse/storage/databases/main/user_directory.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -729,49 +729,6 @@ async def get_user_dir_rooms_user_is_in(self, user_id: str) -> List[str]:
729729
users.update(rows)
730730
return list(users)
731731

732-
async def get_mutual_rooms_for_users(
733-
self, user_id: str, other_user_id: str
734-
) -> Set[str]:
735-
"""
736-
Returns the rooms that a local user shares with another local or remote user.
737-
738-
Args:
739-
user_id: The MXID of a local user
740-
other_user_id: The MXID of the other user
741-
742-
Returns:
743-
A set of room ID's that the users share.
744-
"""
745-
746-
def _get_mutual_rooms_for_users_txn(
747-
txn: LoggingTransaction,
748-
) -> List[Dict[str, str]]:
749-
txn.execute(
750-
"""
751-
SELECT p1.room_id
752-
FROM users_in_public_rooms as p1
753-
INNER JOIN users_in_public_rooms as p2
754-
ON p1.room_id = p2.room_id
755-
AND p1.user_id = ?
756-
AND p2.user_id = ?
757-
UNION
758-
SELECT room_id
759-
FROM users_who_share_private_rooms
760-
WHERE
761-
user_id = ?
762-
AND other_user_id = ?
763-
""",
764-
(user_id, other_user_id, user_id, other_user_id),
765-
)
766-
rows = self.db_pool.cursor_to_dict(txn)
767-
return rows
768-
769-
rows = await self.db_pool.runInteraction(
770-
"get_mutual_rooms_for_users", _get_mutual_rooms_for_users_txn
771-
)
772-
773-
return {row["room_id"] for row in rows}
774-
775732
async def get_user_directory_stream_pos(self) -> Optional[int]:
776733
"""
777734
Get the stream ID of the user directory stream.

tests/rest/client/test_mutual_rooms.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ class UserMutualRoomsTest(unittest.HomeserverTestCase):
3636

3737
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
3838
config = self.default_config()
39-
config["update_user_directory"] = True
4039
return self.setup_test_homeserver(config=config)
4140

4241
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
4342
self.store = hs.get_datastores().main
44-
self.handler = hs.get_user_directory_handler()
4543

4644
def _get_mutual_rooms(self, token: str, other_user: str) -> FakeChannel:
4745
return self.make_request(

0 commit comments

Comments
 (0)