push_tools.py 2.3 KB

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