account_data.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class AccountDataEventSource(object):
  17. def __init__(self, hs):
  18. self.store = hs.get_datastore()
  19. def get_current_key(self, direction='f'):
  20. return self.store.get_max_account_data_stream_id()
  21. @defer.inlineCallbacks
  22. def get_new_events(self, user, from_key, **kwargs):
  23. user_id = user.to_string()
  24. last_stream_id = from_key
  25. current_stream_id = yield self.store.get_max_account_data_stream_id()
  26. results = []
  27. tags = yield self.store.get_updated_tags(user_id, last_stream_id)
  28. for room_id, room_tags in tags.items():
  29. results.append({
  30. "type": "m.tag",
  31. "content": {"tags": room_tags},
  32. "room_id": room_id,
  33. })
  34. account_data, room_account_data = (
  35. yield self.store.get_updated_account_data_for_user(user_id, last_stream_id)
  36. )
  37. for account_data_type, content in account_data.items():
  38. results.append({
  39. "type": account_data_type,
  40. "content": content,
  41. })
  42. for room_id, account_data in room_account_data.items():
  43. for account_data_type, content in account_data.items():
  44. results.append({
  45. "type": account_data_type,
  46. "content": content,
  47. "room_id": room_id,
  48. })
  49. defer.returnValue((results, current_stream_id))
  50. @defer.inlineCallbacks
  51. def get_pagination_rows(self, user, config, key):
  52. defer.returnValue(([], config.to_id))