server_notice_servlet.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import re
  16. from twisted.internet import defer
  17. from synapse.api.constants import EventTypes
  18. from synapse.api.errors import SynapseError
  19. from synapse.http.servlet import (
  20. RestServlet,
  21. assert_params_in_dict,
  22. parse_json_object_from_request,
  23. )
  24. from synapse.rest.admin import assert_requester_is_admin
  25. from synapse.rest.client.transactions import HttpTransactionCache
  26. from synapse.types import UserID
  27. class SendServerNoticeServlet(RestServlet):
  28. """Servlet which will send a server notice to a given user
  29. POST /_synapse/admin/v1/send_server_notice
  30. {
  31. "user_id": "@target_user:server_name",
  32. "content": {
  33. "msgtype": "m.text",
  34. "body": "This is my message"
  35. }
  36. }
  37. returns:
  38. {
  39. "event_id": "$1895723857jgskldgujpious"
  40. }
  41. """
  42. def __init__(self, hs):
  43. """
  44. Args:
  45. hs (synapse.server.HomeServer): server
  46. """
  47. self.hs = hs
  48. self.auth = hs.get_auth()
  49. self.txns = HttpTransactionCache(hs)
  50. self.snm = hs.get_server_notices_manager()
  51. def register(self, json_resource):
  52. PATTERN = "^/_synapse/admin/v1/send_server_notice"
  53. json_resource.register_paths("POST", (re.compile(PATTERN + "$"),), self.on_POST)
  54. json_resource.register_paths(
  55. "PUT", (re.compile(PATTERN + "/(?P<txn_id>[^/]*)$"),), self.on_PUT
  56. )
  57. @defer.inlineCallbacks
  58. def on_POST(self, request, txn_id=None):
  59. yield assert_requester_is_admin(self.auth, request)
  60. body = parse_json_object_from_request(request)
  61. assert_params_in_dict(body, ("user_id", "content"))
  62. event_type = body.get("type", EventTypes.Message)
  63. state_key = body.get("state_key")
  64. if not self.snm.is_enabled():
  65. raise SynapseError(400, "Server notices are not enabled on this server")
  66. user_id = body["user_id"]
  67. UserID.from_string(user_id)
  68. if not self.hs.is_mine_id(user_id):
  69. raise SynapseError(400, "Server notices can only be sent to local users")
  70. event = yield self.snm.send_notice(
  71. user_id=body["user_id"],
  72. type=event_type,
  73. state_key=state_key,
  74. event_content=body["content"],
  75. )
  76. return (200, {"event_id": event.event_id})
  77. def on_PUT(self, request, txn_id):
  78. return self.txns.fetch_or_execute_request(
  79. request, self.on_POST, request, txn_id
  80. )