summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrendan Abolivier <babolivier@matrix.org>2022-05-11 11:29:48 +0100
committerBrendan Abolivier <babolivier@matrix.org>2022-05-11 11:29:48 +0100
commit99496fcf3d9c19fb327558de1af834e545d4433a (patch)
tree60620f54d6f6e4ca357efc3fbe2968446c16eaa5
parentFix `/messages` throwing a 500 when querying for non-existent room (#12683) (diff)
downloadsynapse-99496fcf3d9c19fb327558de1af834e545d4433a.tar.xz
Always try to invite the user when creating a server notice room
-rw-r--r--synapse/server_notices/server_notices_manager.py23
-rw-r--r--tests/rest/admin/test_server_notice.py31
2 files changed, 47 insertions, 7 deletions
diff --git a/synapse/server_notices/server_notices_manager.py b/synapse/server_notices/server_notices_manager.py
index 48eae5fa06..57f0ef274a 100644
--- a/synapse/server_notices/server_notices_manager.py
+++ b/synapse/server_notices/server_notices_manager.py
@@ -66,7 +66,6 @@ class ServerNoticesManager:
             txn_id: The transaction ID.
         """
         room_id = await self.get_or_create_notice_room_for_user(user_id)
-        await self.maybe_invite_user_to_room(user_id, room_id)
 
         assert self.server_notices_mxid is not None
         requester = create_requester(
@@ -90,13 +89,33 @@ class ServerNoticesManager:
         )
         return event
 
-    @cached()
     async def get_or_create_notice_room_for_user(self, user_id: str) -> str:
         """Get the room for notices for a given user
 
         If we have not yet created a notice room for this user, create it, but don't
         invite the user to it.
 
+        Also checks if the user needs to be invited into the room, and invites them if
+        necessary.
+
+        Args:
+            user_id: complete user id for the user we want a room for
+
+        Returns:
+            room id of notice room.
+        """
+
+        room_id = await self._get_or_create_notice_room_for_user(user_id)
+        await self.maybe_invite_user_to_room(user_id, room_id)
+        return room_id
+
+    @cached()
+    async def _get_or_create_notice_room_for_user(self, user_id: str) -> str:
+        """Get the room for notices for a given user
+
+        If we have not yet created a notice room for this user, create it, but don't
+        invite the user to it.
+
         Args:
             user_id: complete user id for the user we want a room for
 
diff --git a/tests/rest/admin/test_server_notice.py b/tests/rest/admin/test_server_notice.py
index dbcba2663c..bcc48954a2 100644
--- a/tests/rest/admin/test_server_notice.py
+++ b/tests/rest/admin/test_server_notice.py
@@ -214,7 +214,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
         self.assertEqual(messages[0]["sender"], "@notices:test")
 
         # invalidate cache of server notices room_ids
-        self.server_notices_manager.get_or_create_notice_room_for_user.invalidate_all()
+        self.server_notices_manager._get_or_create_notice_room_for_user.invalidate_all()
 
         # send second message
         channel = self.make_request(
@@ -289,7 +289,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
         # invalidate cache of server notices room_ids
         # if server tries to send to a cached room_id the user gets the message
         # in old room
-        self.server_notices_manager.get_or_create_notice_room_for_user.invalidate_all()
+        self.server_notices_manager._get_or_create_notice_room_for_user.invalidate_all()
 
         # send second message
         channel = self.make_request(
@@ -376,7 +376,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
 
         # invalidate cache of server notices room_ids
         # if server tries to send to a cached room_id it gives an error
-        self.server_notices_manager.get_or_create_notice_room_for_user.invalidate_all()
+        self.server_notices_manager._get_or_create_notice_room_for_user.invalidate_all()
 
         # send second message
         channel = self.make_request(
@@ -432,7 +432,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
         self.server_notices_manager._config.servernotices.server_notices_mxid_display_name = (
             new_display_name
         )
-        self.server_notices_manager.get_or_create_notice_room_for_user.cache.invalidate_all()
+        self.server_notices_manager._get_or_create_notice_room_for_user.invalidate_all()
 
         self.make_request(
             "POST",
@@ -478,7 +478,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
         self.server_notices_manager._config.servernotices.server_notices_mxid_avatar_url = (
             new_avatar_url
         )
-        self.server_notices_manager.get_or_create_notice_room_for_user.cache.invalidate_all()
+        self.server_notices_manager._get_or_create_notice_room_for_user.invalidate_all()
 
         self.make_request(
             "POST",
@@ -501,6 +501,27 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
         )
         self.assertEqual(notice_user_state["avatar_url"], new_avatar_url)
 
+    @override_config({"server_notices": {"system_mxid_localpart": "notices"}})
+    def test_always_invite(self) -> None:
+        """Tests that calling get_or_create_notice_room_for_user always end up inviting
+        the user if necessary.
+        """
+        room_id = self.get_success(
+            self.server_notices_manager.get_or_create_notice_room_for_user(
+                self.other_user,
+            )
+        )
+
+        invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0)
+
+        invited = False
+        for invited_room in invited_rooms:
+            if invited_room.room_id == room_id:
+                invited = True
+                break
+
+        self.assertTrue(invited)
+
     def _check_invite_and_join_status(
         self, user_id: str, expected_invites: int, expected_memberships: int
     ) -> List[RoomsForUser]: