From e7192eaace1028adc60213c54430961bb50ec696 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Fri, 24 Nov 2023 14:11:24 +0100 Subject: [PATCH 1/5] Server notices: add an autojoin setting for the notices room --- changelog.d/16699.feature | 1 + docs/server_notices.md | 3 +++ .../configuration/config_documentation.md | 3 ++- synapse/config/server_notices.py | 2 ++ .../server_notices/server_notices_manager.py | 15 +++++++++++- tests/rest/admin/test_server_notice.py | 24 +++++++++++++++++++ 6 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 changelog.d/16699.feature diff --git a/changelog.d/16699.feature b/changelog.d/16699.feature new file mode 100644 index 000000000000..7ede50f3264c --- /dev/null +++ b/changelog.d/16699.feature @@ -0,0 +1 @@ +Add an autojoin setting for the notices room so users get joined directly instead of receiving an invite. diff --git a/docs/server_notices.md b/docs/server_notices.md index 339d10a0ab3f..aae25d23b82e 100644 --- a/docs/server_notices.md +++ b/docs/server_notices.md @@ -46,6 +46,7 @@ server_notices: system_mxid_display_name: "Server Notices" system_mxid_avatar_url: "mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ" room_name: "Server Notices" + auto_join: true ``` The only compulsory setting is `system_mxid_localpart`, which defines the user @@ -55,6 +56,8 @@ room which will be created. `system_mxid_display_name` and `system_mxid_avatar_url` can be used to set the displayname and avatar of the Server Notices user. +`auto_join` will autojoin users to the notices room instead of sending an invite. + ## Sending notices To send server notices to users you can use the diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index 7c4e742cd5d5..f539d001280b 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -3815,7 +3815,7 @@ Sub-options for this setting include: * `system_mxid_display_name`: set the display name of the "notices" user * `system_mxid_avatar_url`: set the avatar for the "notices" user * `room_name`: set the room name of the server notices room - +* `auto_join`: the user will be automatically joined to the room instead of being invited Example configuration: ```yaml server_notices: @@ -3823,6 +3823,7 @@ server_notices: system_mxid_display_name: "Server Notices" system_mxid_avatar_url: "mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ" room_name: "Server Notices" + auto_join: true ``` --- ### `enable_room_list_search` diff --git a/synapse/config/server_notices.py b/synapse/config/server_notices.py index ce041abe9bb3..a8badba0f872 100644 --- a/synapse/config/server_notices.py +++ b/synapse/config/server_notices.py @@ -48,6 +48,7 @@ def __init__(self, *args: Any): self.server_notices_mxid_display_name: Optional[str] = None self.server_notices_mxid_avatar_url: Optional[str] = None self.server_notices_room_name: Optional[str] = None + self.server_notices_auto_join: bool = False def read_config(self, config: JsonDict, **kwargs: Any) -> None: c = config.get("server_notices") @@ -62,3 +63,4 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: self.server_notices_mxid_avatar_url = c.get("system_mxid_avatar_url", None) # todo: i18n self.server_notices_room_name = c.get("room_name", "Server Notices") + self.server_notices_auto_join = c.get("auto_join", False) diff --git a/synapse/server_notices/server_notices_manager.py b/synapse/server_notices/server_notices_manager.py index 44b999677a1e..2353b5d47fb6 100644 --- a/synapse/server_notices/server_notices_manager.py +++ b/synapse/server_notices/server_notices_manager.py @@ -224,14 +224,27 @@ async def maybe_invite_user_to_room(self, user_id: str, room_id: str) -> None: if room.room_id == room_id: return + user_id_obj = UserID.from_string(user_id) await self._room_member_handler.update_membership( requester=requester, - target=UserID.from_string(user_id), + target=user_id_obj, room_id=room_id, action="invite", ratelimit=False, ) + if self._config.servernotices.server_notices_auto_join: + user_requester = create_requester( + user_id, authenticated_entity=self._server_name + ) + await self._room_member_handler.update_membership( + requester=user_requester, + target=user_id_obj, + room_id=room_id, + action="join", + ratelimit=False, + ) + async def _update_notice_user_profile_if_changed( self, requester: Requester, diff --git a/tests/rest/admin/test_server_notice.py b/tests/rest/admin/test_server_notice.py index dfd14f5751bf..f0e79a87b957 100644 --- a/tests/rest/admin/test_server_notice.py +++ b/tests/rest/admin/test_server_notice.py @@ -477,6 +477,30 @@ def test_send_server_notice_delete_room(self) -> None: # second room has new ID self.assertNotEqual(first_room_id, second_room_id) + @override_config( + {"server_notices": {"system_mxid_localpart": "notices", "auto_join": True}} + ) + def test_auto_join(self) -> None: + """ + Tests that the user get automatically joined to the notice room + when `auto_join` setting is used. + """ + self._check_invite_and_join_status(self.other_user, 0, 0) + + server_notice_request_content = { + "user_id": self.other_user, + "content": {"msgtype": "m.text", "body": "test msg one"}, + } + + self.make_request( + "POST", + self.url, + access_token=self.admin_user_tok, + content=server_notice_request_content, + ) + + self._check_invite_and_join_status(self.other_user, 0, 1) + @override_config({"server_notices": {"system_mxid_localpart": "notices"}}) def test_update_notice_user_name_when_changed(self) -> None: """ From 8597dd14e5fef677f43ce485bb48261219e51e36 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Thu, 30 Nov 2023 15:18:50 +0100 Subject: [PATCH 2/5] Update tests/rest/admin/test_server_notice.py Co-authored-by: Patrick Cloke --- tests/rest/admin/test_server_notice.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/rest/admin/test_server_notice.py b/tests/rest/admin/test_server_notice.py index f0e79a87b957..2398bc503a4d 100644 --- a/tests/rest/admin/test_server_notice.py +++ b/tests/rest/admin/test_server_notice.py @@ -485,8 +485,10 @@ def test_auto_join(self) -> None: Tests that the user get automatically joined to the notice room when `auto_join` setting is used. """ + # user has no room memberships self._check_invite_and_join_status(self.other_user, 0, 0) + # send server notice server_notice_request_content = { "user_id": self.other_user, "content": {"msgtype": "m.text", "body": "test msg one"}, @@ -499,6 +501,7 @@ def test_auto_join(self) -> None: content=server_notice_request_content, ) + # user has joined the room self._check_invite_and_join_status(self.other_user, 0, 1) @override_config({"server_notices": {"system_mxid_localpart": "notices"}}) From 7187ad367b70c9ce04ee6629808d7f3598d0b34c Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Thu, 30 Nov 2023 15:21:30 +0100 Subject: [PATCH 3/5] comments --- docs/usage/configuration/config_documentation.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index f539d001280b..da01693263e2 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -3815,7 +3815,8 @@ Sub-options for this setting include: * `system_mxid_display_name`: set the display name of the "notices" user * `system_mxid_avatar_url`: set the avatar for the "notices" user * `room_name`: set the room name of the server notices room -* `auto_join`: the user will be automatically joined to the room instead of being invited +* `auto_join`: the user will be automatically joined to the room instead of being invited. + Defaults to false. _Added in Synapse 1.98.0._ Example configuration: ```yaml server_notices: From a7084fd51ac69e308fc8a1e8f042718abfc57359 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Mon, 4 Dec 2023 11:55:51 +0000 Subject: [PATCH 4/5] Tweak docs wording --- docs/usage/configuration/config_documentation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index da01693263e2..91f6adb4a477 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -3815,7 +3815,7 @@ Sub-options for this setting include: * `system_mxid_display_name`: set the display name of the "notices" user * `system_mxid_avatar_url`: set the avatar for the "notices" user * `room_name`: set the room name of the server notices room -* `auto_join`: the user will be automatically joined to the room instead of being invited. +* `auto_join`: boolean. If true, the user will be automatically joined to the room instead of being invited. Defaults to false. _Added in Synapse 1.98.0._ Example configuration: ```yaml From 622415554abb7aca369294acdb6ef66fe4d85946 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Mon, 4 Dec 2023 11:57:02 +0000 Subject: [PATCH 5/5] Fix docs spacing --- docs/usage/configuration/config_documentation.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index 91f6adb4a477..812a7d429b76 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -3817,6 +3817,7 @@ Sub-options for this setting include: * `room_name`: set the room name of the server notices room * `auto_join`: boolean. If true, the user will be automatically joined to the room instead of being invited. Defaults to false. _Added in Synapse 1.98.0._ + Example configuration: ```yaml server_notices: