state_deltas.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector 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. import logging
  16. logger = logging.getLogger(__name__)
  17. class StateDeltasHandler(object):
  18. def __init__(self, hs):
  19. self.store = hs.get_datastore()
  20. async def _get_key_change(self, prev_event_id, event_id, key_name, public_value):
  21. """Given two events check if the `key_name` field in content changed
  22. from not matching `public_value` to doing so.
  23. For example, check if `history_visibility` (`key_name`) changed from
  24. `shared` to `world_readable` (`public_value`).
  25. Returns:
  26. None if the field in the events either both match `public_value`
  27. or if neither do, i.e. there has been no change.
  28. True if it didnt match `public_value` but now does
  29. False if it did match `public_value` but now doesn't
  30. """
  31. prev_event = None
  32. event = None
  33. if prev_event_id:
  34. prev_event = await self.store.get_event(prev_event_id, allow_none=True)
  35. if event_id:
  36. event = await self.store.get_event(event_id, allow_none=True)
  37. if not event and not prev_event:
  38. logger.debug("Neither event exists: %r %r", prev_event_id, event_id)
  39. return None
  40. prev_value = None
  41. value = None
  42. if prev_event:
  43. prev_value = prev_event.content.get(key_name)
  44. if event:
  45. value = event.content.get(key_name)
  46. logger.debug("prev_value: %r -> value: %r", prev_value, value)
  47. if value == public_value and prev_value != public_value:
  48. return True
  49. elif value != public_value and prev_value == public_value:
  50. return False
  51. else:
  52. return None