You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

115 lines
3.8 KiB

  1. # Copyright 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from typing import TYPE_CHECKING, Tuple
  16. from synapse.api.constants import ReceiptTypes
  17. from synapse.events.utils import (
  18. SerializeEventConfig,
  19. format_event_for_client_v2_without_room_id,
  20. )
  21. from synapse.http.server import HttpServer
  22. from synapse.http.servlet import RestServlet, parse_integer, parse_string
  23. from synapse.http.site import SynapseRequest
  24. from synapse.types import JsonDict
  25. from ._base import client_patterns
  26. if TYPE_CHECKING:
  27. from synapse.server import HomeServer
  28. logger = logging.getLogger(__name__)
  29. class NotificationsServlet(RestServlet):
  30. PATTERNS = client_patterns("/notifications$")
  31. CATEGORY = "Client API requests"
  32. def __init__(self, hs: "HomeServer"):
  33. super().__init__()
  34. self.store = hs.get_datastores().main
  35. self.auth = hs.get_auth()
  36. self.clock = hs.get_clock()
  37. self._event_serializer = hs.get_event_client_serializer()
  38. async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
  39. requester = await self.auth.get_user_by_req(request)
  40. user_id = requester.user.to_string()
  41. from_token = parse_string(request, "from", required=False)
  42. limit = parse_integer(request, "limit", default=50)
  43. only = parse_string(request, "only", required=False)
  44. limit = min(limit, 500)
  45. push_actions = await self.store.get_push_actions_for_user(
  46. user_id, from_token, limit, only_highlight=(only == "highlight")
  47. )
  48. receipts_by_room = await self.store.get_receipts_for_user_with_orderings(
  49. user_id,
  50. [
  51. ReceiptTypes.READ,
  52. ReceiptTypes.READ_PRIVATE,
  53. ],
  54. )
  55. notif_event_ids = [pa.event_id for pa in push_actions]
  56. notif_events = await self.store.get_events(notif_event_ids)
  57. returned_push_actions = []
  58. next_token = None
  59. serialize_options = SerializeEventConfig(
  60. event_format=format_event_for_client_v2_without_room_id,
  61. requester=requester,
  62. )
  63. now = self.clock.time_msec()
  64. for pa in push_actions:
  65. returned_pa = {
  66. "room_id": pa.room_id,
  67. "profile_tag": pa.profile_tag,
  68. "actions": pa.actions,
  69. "ts": pa.received_ts,
  70. "event": (
  71. await self._event_serializer.serialize_event(
  72. notif_events[pa.event_id],
  73. now,
  74. config=serialize_options,
  75. )
  76. ),
  77. }
  78. if pa.room_id not in receipts_by_room:
  79. returned_pa["read"] = False
  80. else:
  81. receipt = receipts_by_room[pa.room_id]
  82. returned_pa["read"] = (
  83. receipt["topological_ordering"],
  84. receipt["stream_ordering"],
  85. ) >= (pa.topological_ordering, pa.stream_ordering)
  86. returned_push_actions.append(returned_pa)
  87. next_token = str(pa.stream_ordering)
  88. return 200, {"notifications": returned_push_actions, "next_token": next_token}
  89. def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
  90. NotificationsServlet(hs).register(http_server)