2019-03-25 05:37:08 -04:00
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
2021-09-10 05:54:38 -04:00
|
|
|
from enum import Enum, auto
|
2021-01-26 10:50:21 -05:00
|
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2019-03-25 05:37:08 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-09-10 05:54:38 -04:00
|
|
|
class MatchChange(Enum):
|
|
|
|
no_change = auto()
|
|
|
|
now_true = auto()
|
|
|
|
now_false = auto()
|
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class StateDeltasHandler:
|
2021-01-26 10:50:21 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2019-03-25 05:37:08 -04:00
|
|
|
|
2021-01-26 10:50:21 -05:00
|
|
|
async def _get_key_change(
|
|
|
|
self,
|
|
|
|
prev_event_id: Optional[str],
|
|
|
|
event_id: Optional[str],
|
|
|
|
key_name: str,
|
|
|
|
public_value: str,
|
2021-09-10 05:54:38 -04:00
|
|
|
) -> MatchChange:
|
2019-03-25 05:37:08 -04:00
|
|
|
"""Given two events check if the `key_name` field in content changed
|
|
|
|
from not matching `public_value` to doing so.
|
|
|
|
|
|
|
|
For example, check if `history_visibility` (`key_name`) changed from
|
|
|
|
`shared` to `world_readable` (`public_value`).
|
|
|
|
"""
|
|
|
|
prev_event = None
|
|
|
|
event = None
|
|
|
|
if prev_event_id:
|
2020-06-05 14:42:55 -04:00
|
|
|
prev_event = await self.store.get_event(prev_event_id, allow_none=True)
|
2019-03-25 05:37:08 -04:00
|
|
|
|
|
|
|
if event_id:
|
2020-06-05 14:42:55 -04:00
|
|
|
event = await self.store.get_event(event_id, allow_none=True)
|
2019-03-25 05:37:08 -04:00
|
|
|
|
|
|
|
if not event and not prev_event:
|
|
|
|
logger.debug("Neither event exists: %r %r", prev_event_id, event_id)
|
2021-09-10 05:54:38 -04:00
|
|
|
return MatchChange.no_change
|
2019-03-25 05:37:08 -04:00
|
|
|
|
|
|
|
prev_value = None
|
|
|
|
value = None
|
|
|
|
|
|
|
|
if prev_event:
|
|
|
|
prev_value = prev_event.content.get(key_name)
|
|
|
|
|
|
|
|
if event:
|
|
|
|
value = event.content.get(key_name)
|
|
|
|
|
|
|
|
logger.debug("prev_value: %r -> value: %r", prev_value, value)
|
|
|
|
|
|
|
|
if value == public_value and prev_value != public_value:
|
2021-09-10 05:54:38 -04:00
|
|
|
return MatchChange.now_true
|
2019-03-25 05:37:08 -04:00
|
|
|
elif value != public_value and prev_value == public_value:
|
2021-09-10 05:54:38 -04:00
|
|
|
return MatchChange.now_false
|
2019-03-25 05:37:08 -04:00
|
|
|
else:
|
2021-09-10 05:54:38 -04:00
|
|
|
return MatchChange.no_change
|