This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add admin API to send a server_notice to one/all users #3531
Closed
krombel
wants to merge
3
commits into
matrix-org:develop
from
krombel:extend_server_notices_sendToAll
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add admin api to send a server notice to one or all users |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Server Notices | ||
|
|
||
| The API to send notices is as follows: | ||
|
|
||
| ``` | ||
| POST /_matrix/client/r0/admin/send_server_notice/[<user_id>] | ||
| ``` | ||
|
|
||
| including an `access_token` of a server admin. | ||
|
|
||
| If the user_id is missing, the message is meant to go to all users on the server. | ||
|
|
||
| The request body should contain the following: | ||
|
|
||
| ```json | ||
| { | ||
| "event": { | ||
| "msgtype":"m.text", | ||
| "body": "This is my message" | ||
| } | ||
| } | ||
| ``` | ||
| or as shortcut you can also send | ||
| ```json | ||
| { | ||
| "event_body": "This is my message" | ||
| } | ||
| ``` | ||
|
|
||
| ## Notes: | ||
| 1) You have to configure server notices in [homeserver.yaml](../server_notices.md) before | ||
| you can use this API | ||
| 2) This query sends a message to all users (and creates a room for each one if it was not | ||
| done before). So please be aware that you will put your server under heavy load if you | ||
| have a large number of registered users. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -557,7 +557,7 @@ class ResetPasswordRestServlet(ClientV1RestServlet): | |
| """Post request to allow an administrator reset password for a user. | ||
| This needs user to have administrator access in Synapse. | ||
| Example: | ||
| http://localhost:8008/_matrix/client/api/v1/admin/reset_password/ | ||
| http://localhost:8008/_matrix/client/r0/admin/reset_password/ | ||
| @user:to_reset_password?access_token=admin_access_token | ||
| JsonBodyToSend: | ||
| { | ||
|
|
@@ -603,7 +603,7 @@ class GetUsersPaginatedRestServlet(ClientV1RestServlet): | |
| """Get request to get specific number of users from Synapse. | ||
| This needs user to have administrator access in Synapse. | ||
| Example: | ||
| http://localhost:8008/_matrix/client/api/v1/admin/users_paginate/ | ||
| http://localhost:8008/_matrix/client/r0/admin/users_paginate/ | ||
| @admin:user?access_token=admin_access_token&start=0&limit=10 | ||
| Returns: | ||
| 200 OK with json object {list[dict[str, Any]], count} or empty object. | ||
|
|
@@ -652,7 +652,7 @@ def on_POST(self, request, target_user_id): | |
| """Post request to get specific number of users from Synapse.. | ||
| This needs user to have administrator access in Synapse. | ||
| Example: | ||
| http://localhost:8008/_matrix/client/api/v1/admin/users_paginate/ | ||
| http://localhost:8008/_matrix/client/r0/admin/users_paginate/ | ||
| @admin:user?access_token=admin_access_token | ||
| JsonBodyToSend: | ||
| { | ||
|
|
@@ -687,7 +687,7 @@ class SearchUsersRestServlet(ClientV1RestServlet): | |
| search term. | ||
| This needs user to have administrator access in Synapse. | ||
| Example: | ||
| http://localhost:8008/_matrix/client/api/v1/admin/search_users/ | ||
| http://localhost:8008/_matrix/client/r0/admin/search_users/ | ||
| @admin:user?access_token=admin_access_token&term=alice | ||
| Returns: | ||
| 200 OK with json object {list[dict[str, Any]], count} or empty object. | ||
|
|
@@ -730,6 +730,68 @@ def on_GET(self, request, target_user_id): | |
| defer.returnValue((200, ret)) | ||
|
|
||
|
|
||
| class ServerNoticeRestServlet(ClientV1RestServlet): | ||
| PATTERNS = client_path_patterns("/admin/send_server_notice(/(?P<user_id>[^/]+))?") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to figure out whether there is a difference in behaviour between I'd suggest that you should not accept |
||
|
|
||
| def __init__(self, hs): | ||
| super(ServerNoticeRestServlet, self).__init__(hs) | ||
| self._notice_manager = hs.get_server_notices_manager() | ||
|
|
||
| @defer.inlineCallbacks | ||
| def on_POST(self, request, user_id): | ||
| """ | ||
| This lets you send a server_notice to one or all users | ||
| Example: | ||
| http://localhost:8008/_matrix/client/r0/admin/send_server_notice/@user:host | ||
| JsonBodyToSend: | ||
| { | ||
| "event": {'msgtype':'m.text', 'body':'This is my message'} | ||
| } | ||
| or simply | ||
| { | ||
| 'event_body': 'This is my message' | ||
| } | ||
| Returns: | ||
| 200 OK. | ||
|
|
||
| """ | ||
| body = parse_json_object_from_request(request, allow_empty_body=False) | ||
| event = body.get("event", None) | ||
| if event is None: | ||
| event_body = body.get("event_body", None) | ||
| if event_body is None: | ||
| raise SynapseError( | ||
| http_client.BAD_REQUEST, | ||
| "Either 'event' or 'event_body' is required", | ||
| Codes.BAD_JSON, | ||
| ) | ||
| event = { | ||
| "msgtype": "m.text", | ||
| "body": event_body | ||
| } | ||
| else: | ||
| if event.get("msgtype") is None or event.get("body") is None: | ||
| raise SynapseError( | ||
| http_client.BAD_REQUEST, | ||
| "event needs 'msgtype' and 'body' as content", | ||
| Codes.BAD_JSON, | ||
| ) | ||
|
|
||
| if user_id is not None: | ||
| UserID.from_string(user_id) | ||
| requester = yield self.auth.get_user_by_req(request) | ||
| is_admin = yield self.auth.is_server_admin(requester.user) | ||
|
|
||
| if not is_admin: | ||
| raise AuthError(403, "You are not a server admin") | ||
|
|
||
| if user_id is not None: | ||
| yield self._notice_manager.send_notice(user_id, event) | ||
| else: | ||
| yield self._notice_manager.send_notice_to_all_users(event) | ||
| defer.returnValue((200, {})) | ||
|
|
||
|
|
||
| def register_servlets(hs, http_server): | ||
| WhoisRestServlet(hs).register(http_server) | ||
| PurgeMediaCacheRestServlet(hs).register(http_server) | ||
|
|
@@ -744,3 +806,4 @@ def register_servlets(hs, http_server): | |
| QuarantineMediaInRoom(hs).register(http_server) | ||
| ListMediaInRoom(hs).register(http_server) | ||
| UserRegisterServlet(hs).register(http_server) | ||
| ServerNoticeRestServlet(hs).register(http_server) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems like it is actually
v1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is that class but it adds the
/client/r0/endpoints as well.