notifications.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 twisted.internet import defer
  17. from synapse.events.utils import format_event_for_client_v2_without_room_id
  18. from synapse.http.servlet import RestServlet, parse_integer, parse_string
  19. from ._base import client_patterns
  20. logger = logging.getLogger(__name__)
  21. class NotificationsServlet(RestServlet):
  22. PATTERNS = client_patterns("/notifications$")
  23. def __init__(self, hs):
  24. super(NotificationsServlet, self).__init__()
  25. self.store = hs.get_datastore()
  26. self.auth = hs.get_auth()
  27. self.clock = hs.get_clock()
  28. self._event_serializer = hs.get_event_client_serializer()
  29. @defer.inlineCallbacks
  30. def on_GET(self, request):
  31. requester = yield self.auth.get_user_by_req(request)
  32. user_id = requester.user.to_string()
  33. from_token = parse_string(request, "from", required=False)
  34. limit = parse_integer(request, "limit", default=50)
  35. only = parse_string(request, "only", required=False)
  36. limit = min(limit, 500)
  37. push_actions = yield self.store.get_push_actions_for_user(
  38. user_id, from_token, limit, only_highlight=(only == "highlight")
  39. )
  40. receipts_by_room = yield self.store.get_receipts_for_user_with_orderings(
  41. user_id, "m.read"
  42. )
  43. notif_event_ids = [pa["event_id"] for pa in push_actions]
  44. notif_events = yield self.store.get_events(notif_event_ids)
  45. returned_push_actions = []
  46. next_token = None
  47. for pa in push_actions:
  48. returned_pa = {
  49. "room_id": pa["room_id"],
  50. "profile_tag": pa["profile_tag"],
  51. "actions": pa["actions"],
  52. "ts": pa["received_ts"],
  53. "event": (
  54. yield self._event_serializer.serialize_event(
  55. notif_events[pa["event_id"]],
  56. self.clock.time_msec(),
  57. event_format=format_event_for_client_v2_without_room_id,
  58. )
  59. ),
  60. }
  61. if pa["room_id"] not in receipts_by_room:
  62. returned_pa["read"] = False
  63. else:
  64. receipt = receipts_by_room[pa["room_id"]]
  65. returned_pa["read"] = (
  66. receipt["topological_ordering"],
  67. receipt["stream_ordering"],
  68. ) >= (pa["topological_ordering"], pa["stream_ordering"])
  69. returned_push_actions.append(returned_pa)
  70. next_token = str(pa["stream_ordering"])
  71. return (200, {"notifications": returned_push_actions, "next_token": next_token})
  72. def register_servlets(hs, http_server):
  73. NotificationsServlet(hs).register(http_server)