push_tools.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 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. from synapse.push.presentable_names import calculate_room_name, name_from_member_event
  16. from synapse.storage import Storage
  17. async def get_badge_count(store, user_id):
  18. invites = await store.get_invited_rooms_for_local_user(user_id)
  19. joins = await store.get_rooms_for_user(user_id)
  20. my_receipts_by_room = await store.get_receipts_for_user(user_id, "m.read")
  21. badge = len(invites)
  22. for room_id in joins:
  23. if room_id in my_receipts_by_room:
  24. last_unread_event_id = my_receipts_by_room[room_id]
  25. notifs = await (
  26. store.get_unread_event_push_actions_by_room_for_user(
  27. room_id, user_id, last_unread_event_id
  28. )
  29. )
  30. # return one badge count per conversation, as count per
  31. # message is so noisy as to be almost useless
  32. badge += 1 if notifs["notify_count"] else 0
  33. return badge
  34. async def get_context_for_event(storage: Storage, state_handler, ev, user_id):
  35. ctx = {}
  36. room_state_ids = await storage.state.get_state_ids_for_event(ev.event_id)
  37. # we no longer bother setting room_alias, and make room_name the
  38. # human-readable name instead, be that m.room.name, an alias or
  39. # a list of people in the room
  40. name = await calculate_room_name(
  41. storage.main, room_state_ids, user_id, fallback_to_single_member=False
  42. )
  43. if name:
  44. ctx["name"] = name
  45. sender_state_event_id = room_state_ids[("m.room.member", ev.sender)]
  46. sender_state_event = await storage.main.get_event(sender_state_event_id)
  47. ctx["sender_display_name"] = name_from_member_event(sender_state_event)
  48. return ctx