Server notices: add an autojoin setting for the notices room (#16699)

Co-authored-by: Patrick Cloke <clokep@users.noreply.github.com>
This commit is contained in:
Mathieu Velten 2023-12-04 13:31:42 +01:00 committed by GitHub
parent 506f5c7553
commit 9e7f80037d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 1 deletions

View file

@ -0,0 +1 @@
Add an autojoin setting for the notices room so users get joined directly instead of receiving an invite.

View file

@ -46,6 +46,7 @@ server_notices:
system_mxid_display_name: "Server Notices" system_mxid_display_name: "Server Notices"
system_mxid_avatar_url: "mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ" system_mxid_avatar_url: "mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ"
room_name: "Server Notices" room_name: "Server Notices"
auto_join: true
``` ```
The only compulsory setting is `system_mxid_localpart`, which defines the user 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 `system_mxid_display_name` and `system_mxid_avatar_url` can be used to set the
displayname and avatar of the Server Notices user. displayname and avatar of the Server Notices user.
`auto_join` will autojoin users to the notices room instead of sending an invite.
## Sending notices ## Sending notices
To send server notices to users you can use the To send server notices to users you can use the

View file

@ -3815,6 +3815,8 @@ Sub-options for this setting include:
* `system_mxid_display_name`: set the display name of the "notices" user * `system_mxid_display_name`: set the display name of the "notices" user
* `system_mxid_avatar_url`: set the avatar for 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 * `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: Example configuration:
```yaml ```yaml
@ -3823,6 +3825,7 @@ server_notices:
system_mxid_display_name: "Server Notices" system_mxid_display_name: "Server Notices"
system_mxid_avatar_url: "mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ" system_mxid_avatar_url: "mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ"
room_name: "Server Notices" room_name: "Server Notices"
auto_join: true
``` ```
--- ---
### `enable_room_list_search` ### `enable_room_list_search`

View file

@ -48,6 +48,7 @@ class ServerNoticesConfig(Config):
self.server_notices_mxid_display_name: Optional[str] = None self.server_notices_mxid_display_name: Optional[str] = None
self.server_notices_mxid_avatar_url: Optional[str] = None self.server_notices_mxid_avatar_url: Optional[str] = None
self.server_notices_room_name: 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: def read_config(self, config: JsonDict, **kwargs: Any) -> None:
c = config.get("server_notices") c = config.get("server_notices")
@ -62,3 +63,4 @@ class ServerNoticesConfig(Config):
self.server_notices_mxid_avatar_url = c.get("system_mxid_avatar_url", None) self.server_notices_mxid_avatar_url = c.get("system_mxid_avatar_url", None)
# todo: i18n # todo: i18n
self.server_notices_room_name = c.get("room_name", "Server Notices") self.server_notices_room_name = c.get("room_name", "Server Notices")
self.server_notices_auto_join = c.get("auto_join", False)

View file

@ -224,14 +224,27 @@ class ServerNoticesManager:
if room.room_id == room_id: if room.room_id == room_id:
return return
user_id_obj = UserID.from_string(user_id)
await self._room_member_handler.update_membership( await self._room_member_handler.update_membership(
requester=requester, requester=requester,
target=UserID.from_string(user_id), target=user_id_obj,
room_id=room_id, room_id=room_id,
action="invite", action="invite",
ratelimit=False, 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( async def _update_notice_user_profile_if_changed(
self, self,
requester: Requester, requester: Requester,

View file

@ -477,6 +477,33 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
# second room has new ID # second room has new ID
self.assertNotEqual(first_room_id, second_room_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.
"""
# 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"},
}
self.make_request(
"POST",
self.url,
access_token=self.admin_user_tok,
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"}}) @override_config({"server_notices": {"system_mxid_localpart": "notices"}})
def test_update_notice_user_name_when_changed(self) -> None: def test_update_notice_user_name_when_changed(self) -> None:
""" """