2019-05-02 06:59:16 -04:00
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-03-08 10:34:38 -05:00
|
|
|
from typing import TYPE_CHECKING, Optional, Tuple
|
|
|
|
|
2019-05-02 06:59:16 -04:00
|
|
|
from synapse.api.constants import EventTypes
|
|
|
|
from synapse.api.errors import SynapseError
|
2021-03-08 10:34:38 -05:00
|
|
|
from synapse.http.server import HttpServer
|
2019-05-02 06:59:16 -04:00
|
|
|
from synapse.http.servlet import (
|
|
|
|
RestServlet,
|
|
|
|
assert_params_in_dict,
|
|
|
|
parse_json_object_from_request,
|
|
|
|
)
|
2021-03-08 10:34:38 -05:00
|
|
|
from synapse.http.site import SynapseRequest
|
2019-05-02 06:59:16 -04:00
|
|
|
from synapse.rest.admin import assert_requester_is_admin
|
2020-09-17 07:04:15 -04:00
|
|
|
from synapse.rest.admin._base import admin_patterns
|
2019-05-02 06:59:16 -04:00
|
|
|
from synapse.rest.client.transactions import HttpTransactionCache
|
2021-03-08 10:34:38 -05:00
|
|
|
from synapse.types import JsonDict, UserID
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
2019-05-02 06:59:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SendServerNoticeServlet(RestServlet):
|
|
|
|
"""Servlet which will send a server notice to a given user
|
|
|
|
|
|
|
|
POST /_synapse/admin/v1/send_server_notice
|
|
|
|
{
|
|
|
|
"user_id": "@target_user:server_name",
|
|
|
|
"content": {
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "This is my message"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
returns:
|
|
|
|
|
|
|
|
{
|
|
|
|
"event_id": "$1895723857jgskldgujpious"
|
|
|
|
}
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-03-08 10:34:38 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2019-05-02 06:59:16 -04:00
|
|
|
self.hs = hs
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.txns = HttpTransactionCache(hs)
|
|
|
|
self.snm = hs.get_server_notices_manager()
|
|
|
|
|
2021-03-08 10:34:38 -05:00
|
|
|
def register(self, json_resource: HttpServer):
|
2020-09-17 07:04:15 -04:00
|
|
|
PATTERN = "/send_server_notice"
|
2019-05-02 06:59:16 -04:00
|
|
|
json_resource.register_paths(
|
2020-09-17 07:04:15 -04:00
|
|
|
"POST", admin_patterns(PATTERN + "$"), self.on_POST, self.__class__.__name__
|
2019-07-24 08:07:35 -04:00
|
|
|
)
|
|
|
|
json_resource.register_paths(
|
|
|
|
"PUT",
|
2020-09-17 07:04:15 -04:00
|
|
|
admin_patterns(PATTERN + "/(?P<txn_id>[^/]*)$"),
|
2019-07-24 08:07:35 -04:00
|
|
|
self.on_PUT,
|
|
|
|
self.__class__.__name__,
|
2019-05-02 06:59:16 -04:00
|
|
|
)
|
|
|
|
|
2021-03-08 10:34:38 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, txn_id: Optional[str] = None
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-10-11 07:20:24 -04:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2019-05-02 06:59:16 -04:00
|
|
|
body = parse_json_object_from_request(request)
|
|
|
|
assert_params_in_dict(body, ("user_id", "content"))
|
|
|
|
event_type = body.get("type", EventTypes.Message)
|
|
|
|
state_key = body.get("state_key")
|
|
|
|
|
|
|
|
if not self.snm.is_enabled():
|
|
|
|
raise SynapseError(400, "Server notices are not enabled on this server")
|
|
|
|
|
|
|
|
user_id = body["user_id"]
|
|
|
|
UserID.from_string(user_id)
|
|
|
|
if not self.hs.is_mine_id(user_id):
|
|
|
|
raise SynapseError(400, "Server notices can only be sent to local users")
|
|
|
|
|
2019-10-11 07:20:24 -04:00
|
|
|
event = await self.snm.send_notice(
|
2019-05-02 06:59:16 -04:00
|
|
|
user_id=body["user_id"],
|
|
|
|
type=event_type,
|
|
|
|
state_key=state_key,
|
|
|
|
event_content=body["content"],
|
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, {"event_id": event.event_id}
|
2019-05-02 06:59:16 -04:00
|
|
|
|
2021-03-08 10:34:38 -05:00
|
|
|
def on_PUT(self, request: SynapseRequest, txn_id: str) -> Tuple[int, JsonDict]:
|
2019-05-02 06:59:16 -04:00
|
|
|
return self.txns.fetch_or_execute_request(
|
|
|
|
request, self.on_POST, request, txn_id
|
|
|
|
)
|