report_event.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket 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 logging
  16. from http import HTTPStatus
  17. from synapse.api.errors import Codes, SynapseError
  18. from synapse.http.servlet import (
  19. RestServlet,
  20. assert_params_in_dict,
  21. parse_json_object_from_request,
  22. )
  23. from ._base import client_patterns
  24. logger = logging.getLogger(__name__)
  25. class ReportEventRestServlet(RestServlet):
  26. PATTERNS = client_patterns("/rooms/(?P<room_id>[^/]*)/report/(?P<event_id>[^/]*)$")
  27. def __init__(self, hs):
  28. super(ReportEventRestServlet, self).__init__()
  29. self.hs = hs
  30. self.auth = hs.get_auth()
  31. self.clock = hs.get_clock()
  32. self.store = hs.get_datastore()
  33. async def on_POST(self, request, room_id, event_id):
  34. requester = await self.auth.get_user_by_req(request)
  35. user_id = requester.user.to_string()
  36. body = parse_json_object_from_request(request)
  37. assert_params_in_dict(body, ("reason", "score"))
  38. if not isinstance(body["reason"], str):
  39. raise SynapseError(
  40. HTTPStatus.BAD_REQUEST,
  41. "Param 'reason' must be a string",
  42. Codes.BAD_JSON,
  43. )
  44. if not isinstance(body["score"], int):
  45. raise SynapseError(
  46. HTTPStatus.BAD_REQUEST,
  47. "Param 'score' must be an integer",
  48. Codes.BAD_JSON,
  49. )
  50. await self.store.add_event_report(
  51. room_id=room_id,
  52. event_id=event_id,
  53. user_id=user_id,
  54. reason=body["reason"],
  55. content=body,
  56. received_ts=self.clock.time_msec(),
  57. )
  58. return 200, {}
  59. def register_servlets(hs, http_server):
  60. ReportEventRestServlet(hs).register(http_server)