From 2fbc5148bb54fc7b887510216ef165f3106134d6 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Fri, 9 Sep 2022 18:25:09 +0100 Subject: [PATCH 1/4] Deduplicate is_server_notice_room --- synapse/handlers/message.py | 12 +++--------- synapse/handlers/room_member.py | 4 ++-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 72157d5a36e7..c8bcb42f5dd2 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -752,20 +752,14 @@ async def _is_exempt_from_privacy_policy( if builder.type == EventTypes.Member: membership = builder.content.get("membership", None) if membership == Membership.JOIN: - return await self._is_server_notices_room(builder.room_id) + return await self.hs.get_room_member_handler().is_server_notice_room( + builder.room_id + ) elif membership == Membership.LEAVE: # the user is always allowed to leave (but not kick people) return builder.state_key == requester.user.to_string() return False - async def _is_server_notices_room(self, room_id: str) -> bool: - if self.config.servernotices.server_notices_mxid is None: - return False - is_server_notices_room = await self.store.check_local_user_in_room( - user_id=self.config.servernotices.server_notices_mxid, room_id=room_id - ) - return is_server_notices_room - async def assert_accepted_privacy_policy(self, requester: Requester) -> None: """Check if a user has accepted the privacy policy diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 5d4adf5bfd74..2e9946dd909f 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -837,7 +837,7 @@ async def update_membership_locked( old_membership == Membership.INVITE and effective_membership_state == Membership.LEAVE ): - is_blocked = await self._is_server_notice_room(room_id) + is_blocked = await self.is_server_notice_room(room_id) if is_blocked: raise SynapseError( HTTPStatus.FORBIDDEN, @@ -1617,7 +1617,7 @@ async def _is_host_in_room(self, current_state_ids: StateMap[str]) -> bool: return False - async def _is_server_notice_room(self, room_id: str) -> bool: + async def is_server_notice_room(self, room_id: str) -> bool: if self._server_notices_mxid is None: return False is_server_notices_room = await self.store.check_local_user_in_room( From 8356925d8c79312afa8497bee09037efc4db22e4 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Mon, 12 Sep 2022 17:24:03 +0100 Subject: [PATCH 2/4] Newsfile Signed-off-by: Olivier Wilkinson (reivilibre) --- changelog.d/13780.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/13780.misc diff --git a/changelog.d/13780.misc b/changelog.d/13780.misc new file mode 100644 index 000000000000..1bcac51cad46 --- /dev/null +++ b/changelog.d/13780.misc @@ -0,0 +1 @@ +Deduplicate `is_server_notices_room`. \ No newline at end of file From 75f034640dcdac90d8b49c0c1fb5fa57368f15d1 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Wed, 14 Sep 2022 12:27:38 +0100 Subject: [PATCH 3/4] Move to the datastore instead --- synapse/handlers/message.py | 4 +--- synapse/handlers/room_member.py | 10 +--------- synapse/storage/databases/main/roommember.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index c8bcb42f5dd2..e07cda133a4a 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -752,9 +752,7 @@ async def _is_exempt_from_privacy_policy( if builder.type == EventTypes.Member: membership = builder.content.get("membership", None) if membership == Membership.JOIN: - return await self.hs.get_room_member_handler().is_server_notice_room( - builder.room_id - ) + return await self.store.is_server_notice_room(builder.room_id) elif membership == Membership.LEAVE: # the user is always allowed to leave (but not kick people) return builder.state_key == requester.user.to_string() diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 2e9946dd909f..8d01f4bf2be5 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -837,7 +837,7 @@ async def update_membership_locked( old_membership == Membership.INVITE and effective_membership_state == Membership.LEAVE ): - is_blocked = await self.is_server_notice_room(room_id) + is_blocked = await self.store.is_server_notice_room(room_id) if is_blocked: raise SynapseError( HTTPStatus.FORBIDDEN, @@ -1617,14 +1617,6 @@ async def _is_host_in_room(self, current_state_ids: StateMap[str]) -> bool: return False - async def is_server_notice_room(self, room_id: str) -> bool: - if self._server_notices_mxid is None: - return False - is_server_notices_room = await self.store.check_local_user_in_room( - user_id=self._server_notices_mxid, room_id=room_id - ) - return is_server_notices_room - class RoomMemberMasterHandler(RoomMemberHandler): def __init__(self, hs: "HomeServer"): diff --git a/synapse/storage/databases/main/roommember.py b/synapse/storage/databases/main/roommember.py index fdb4684e128e..84384557fb82 100644 --- a/synapse/storage/databases/main/roommember.py +++ b/synapse/storage/databases/main/roommember.py @@ -88,6 +88,8 @@ def __init__( # at a time. Keyed by room_id. self._joined_host_linearizer = Linearizer("_JoinedHostsCache") + self._server_notices_mxid = hs.config.servernotices.server_notices_mxid + if ( self.hs.config.worker.run_background_tasks and self.hs.config.metrics.metrics_flags.known_servers @@ -504,6 +506,14 @@ async def check_local_user_in_room(self, user_id: str, room_id: str) -> bool: return membership == Membership.JOIN + async def is_server_notice_room(self, room_id: str) -> bool: + if self._server_notices_mxid is None: + return False + is_server_notices_room = await self.check_local_user_in_room( + user_id=self._server_notices_mxid, room_id=room_id + ) + return is_server_notices_room + async def get_local_current_membership_for_user_in_room( self, user_id: str, room_id: str ) -> Tuple[Optional[str], Optional[str]]: From cc82b7883a5266ba0f8b4790a2bb78332927986d Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Wed, 14 Sep 2022 15:41:08 +0100 Subject: [PATCH 4/4] Add docstring --- synapse/storage/databases/main/roommember.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/synapse/storage/databases/main/roommember.py b/synapse/storage/databases/main/roommember.py index 84384557fb82..a8d224602a41 100644 --- a/synapse/storage/databases/main/roommember.py +++ b/synapse/storage/databases/main/roommember.py @@ -507,6 +507,13 @@ async def check_local_user_in_room(self, user_id: str, room_id: str) -> bool: return membership == Membership.JOIN async def is_server_notice_room(self, room_id: str) -> bool: + """ + Determines whether the given room is a 'Server Notices' room, used for + sending server notices to a user. + + This is determined by seeing whether the server notices user is present + in the room. + """ if self._server_notices_mxid is None: return False is_server_notices_room = await self.check_local_user_in_room(