push_tools.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. @defer.inlineCallbacks
  17. def get_badge_count(store, user_id):
  18. invites, joins = yield defer.gatherResults([
  19. store.get_invited_rooms_for_user(user_id),
  20. store.get_rooms_for_user(user_id),
  21. ], consumeErrors=True)
  22. my_receipts_by_room = yield store.get_receipts_for_user(
  23. user_id, "m.read",
  24. )
  25. badge = len(invites)
  26. for r in joins:
  27. if r.room_id in my_receipts_by_room:
  28. last_unread_event_id = my_receipts_by_room[r.room_id]
  29. notifs = yield (
  30. store.get_unread_event_push_actions_by_room_for_user(
  31. r.room_id, user_id, last_unread_event_id
  32. )
  33. )
  34. # return one badge count per conversation, as count per
  35. # message is so noisy as to be almost useless
  36. badge += 1 if notifs["notify_count"] else 0
  37. defer.returnValue(badge)
  38. @defer.inlineCallbacks
  39. def get_context_for_event(store, ev):
  40. name_aliases = yield store.get_room_name_and_aliases(
  41. ev.room_id
  42. )
  43. ctx = {'aliases': name_aliases[1]}
  44. if name_aliases[0] is not None:
  45. ctx['name'] = name_aliases[0]
  46. their_member_events_for_room = yield store.get_current_state(
  47. room_id=ev.room_id,
  48. event_type='m.room.member',
  49. state_key=ev.user_id
  50. )
  51. for mev in their_member_events_for_room:
  52. if mev.content['membership'] == 'join' and 'displayname' in mev.content:
  53. dn = mev.content['displayname']
  54. if dn is not None:
  55. ctx['sender_display_name'] = dn
  56. defer.returnValue(ctx)