2019-05-02 06:59:16 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2019-05-02 06:59:16 -04:00
|
|
|
#
|
|
|
|
#
|
2021-11-29 17:19:45 -05:00
|
|
|
from http import HTTPStatus
|
2023-03-07 11:05:22 -05:00
|
|
|
from typing import TYPE_CHECKING, Optional, Tuple
|
2021-03-08 10:34:38 -05:00
|
|
|
|
2019-05-02 06:59:16 -04:00
|
|
|
from synapse.api.constants import EventTypes
|
2021-08-27 05:16:40 -04:00
|
|
|
from synapse.api.errors import NotFoundError, 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
|
2023-03-07 11:05:22 -05:00
|
|
|
from synapse.logging.opentracing import set_tag
|
|
|
|
from synapse.rest.admin._base import admin_patterns, assert_user_is_admin
|
2019-05-02 06:59:16 -04:00
|
|
|
from synapse.rest.client.transactions import HttpTransactionCache
|
2023-03-07 11:05:22 -05:00
|
|
|
from synapse.types import JsonDict, Requester, UserID
|
2021-03-08 10:34:38 -05:00
|
|
|
|
|
|
|
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.auth = hs.get_auth()
|
2021-08-27 05:16:40 -04:00
|
|
|
self.server_notices_manager = hs.get_server_notices_manager()
|
|
|
|
self.admin_handler = hs.get_admin_handler()
|
2019-05-02 06:59:16 -04:00
|
|
|
self.txns = HttpTransactionCache(hs)
|
2021-12-08 11:59:40 -05:00
|
|
|
self.is_mine = hs.is_mine
|
2019-05-02 06:59:16 -04:00
|
|
|
|
2021-09-15 08:45:32 -04:00
|
|
|
def register(self, json_resource: HttpServer) -> None:
|
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
|
|
|
)
|
|
|
|
|
2023-03-07 11:05:22 -05:00
|
|
|
async def _do(
|
|
|
|
self,
|
|
|
|
request: SynapseRequest,
|
|
|
|
requester: Requester,
|
|
|
|
txn_id: Optional[str],
|
2021-03-08 10:34:38 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2023-03-07 11:05:22 -05:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
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")
|
|
|
|
|
2021-05-18 09:13:45 -04:00
|
|
|
# We grab the server notices manager here as its initialisation has a check for worker processes,
|
|
|
|
# but worker processes still need to initialise SendServerNoticeServlet (as it is part of the
|
|
|
|
# admin api).
|
2021-08-27 05:16:40 -04:00
|
|
|
if not self.server_notices_manager.is_enabled():
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, "Server notices are not enabled on this server"
|
|
|
|
)
|
2019-05-02 06:59:16 -04:00
|
|
|
|
2021-08-27 05:16:40 -04:00
|
|
|
target_user = UserID.from_string(body["user_id"])
|
2021-12-08 11:59:40 -05:00
|
|
|
if not self.is_mine(target_user):
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, "Server notices can only be sent to local users"
|
|
|
|
)
|
2019-05-02 06:59:16 -04:00
|
|
|
|
2021-08-27 05:16:40 -04:00
|
|
|
if not await self.admin_handler.get_user(target_user):
|
|
|
|
raise NotFoundError("User not found")
|
|
|
|
|
|
|
|
event = await self.server_notices_manager.send_notice(
|
|
|
|
user_id=target_user.to_string(),
|
2019-05-02 06:59:16 -04:00
|
|
|
type=event_type,
|
|
|
|
state_key=state_key,
|
|
|
|
event_content=body["content"],
|
2021-08-27 05:16:40 -04:00
|
|
|
txn_id=txn_id,
|
2019-05-02 06:59:16 -04:00
|
|
|
)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"event_id": event.event_id}
|
2019-05-02 06:59:16 -04:00
|
|
|
|
2023-03-07 11:05:22 -05:00
|
|
|
async def on_POST(
|
|
|
|
self,
|
|
|
|
request: SynapseRequest,
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
return await self._do(request, requester, None)
|
|
|
|
|
|
|
|
async def on_PUT(
|
2021-09-03 09:22:22 -04:00
|
|
|
self, request: SynapseRequest, txn_id: str
|
2023-03-07 11:05:22 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await self.auth.get_user_by_req(request)
|
|
|
|
set_tag("txn_id", txn_id)
|
|
|
|
return await self.txns.fetch_or_execute_request(
|
|
|
|
request, requester, self._do, request, requester, txn_id
|
2019-05-02 06:59:16 -04:00
|
|
|
)
|