push_tools.py 2.2 KB

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