account_data.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. class AccountDataEventSource(object):
  16. def __init__(self, hs):
  17. self.store = hs.get_datastore()
  18. def get_current_key(self, direction="f"):
  19. return self.store.get_max_account_data_stream_id()
  20. async def get_new_events(self, user, from_key, **kwargs):
  21. user_id = user.to_string()
  22. last_stream_id = from_key
  23. current_stream_id = self.store.get_max_account_data_stream_id()
  24. results = []
  25. tags = await self.store.get_updated_tags(user_id, last_stream_id)
  26. for room_id, room_tags in tags.items():
  27. results.append(
  28. {"type": "m.tag", "content": {"tags": room_tags}, "room_id": room_id}
  29. )
  30. (
  31. account_data,
  32. room_account_data,
  33. ) = await self.store.get_updated_account_data_for_user(user_id, last_stream_id)
  34. for account_data_type, content in account_data.items():
  35. results.append({"type": account_data_type, "content": content})
  36. for room_id, account_data in room_account_data.items():
  37. for account_data_type, content in account_data.items():
  38. results.append(
  39. {"type": account_data_type, "content": content, "room_id": room_id}
  40. )
  41. return results, current_stream_id