You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

77 line
2.3 KiB

  1. # Copyright 2018 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from enum import Enum, auto
  16. from typing import TYPE_CHECKING, Optional
  17. if TYPE_CHECKING:
  18. from synapse.server import HomeServer
  19. logger = logging.getLogger(__name__)
  20. class MatchChange(Enum):
  21. no_change = auto()
  22. now_true = auto()
  23. now_false = auto()
  24. class StateDeltasHandler:
  25. def __init__(self, hs: "HomeServer"):
  26. self.store = hs.get_datastores().main
  27. async def _get_key_change(
  28. self,
  29. prev_event_id: Optional[str],
  30. event_id: Optional[str],
  31. key_name: str,
  32. public_value: str,
  33. ) -> MatchChange:
  34. """Given two events check if the `key_name` field in content changed
  35. from not matching `public_value` to doing so.
  36. For example, check if `history_visibility` (`key_name`) changed from
  37. `shared` to `world_readable` (`public_value`).
  38. """
  39. prev_event = None
  40. event = None
  41. if prev_event_id:
  42. prev_event = await self.store.get_event(prev_event_id, allow_none=True)
  43. if event_id:
  44. event = await self.store.get_event(event_id, allow_none=True)
  45. if not event and not prev_event:
  46. logger.debug("Neither event exists: %r %r", prev_event_id, event_id)
  47. return MatchChange.no_change
  48. prev_value = None
  49. value = None
  50. if prev_event:
  51. prev_value = prev_event.content.get(key_name)
  52. if event:
  53. value = event.content.get(key_name)
  54. logger.debug("prev_value: %r -> value: %r", prev_value, value)
  55. if value == public_value and prev_value != public_value:
  56. return MatchChange.now_true
  57. elif value != public_value and prev_value == public_value:
  58. return MatchChange.now_false
  59. else:
  60. return MatchChange.no_change