Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Easy refactors of the user directory #10789

Merged
merged 6 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions synapse/handlers/state_deltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import logging
from enum import Enum, auto
from typing import TYPE_CHECKING, Optional

if TYPE_CHECKING:
Expand All @@ -21,6 +22,12 @@
logger = logging.getLogger(__name__)


class MatchChange(Enum):
no_change = auto()
now_true = auto()
now_false = auto()


class StateDeltasHandler:
def __init__(self, hs: "HomeServer"):
self.store = hs.get_datastore()
Expand All @@ -31,18 +38,12 @@ async def _get_key_change(
event_id: Optional[str],
key_name: str,
public_value: str,
) -> Optional[bool]:
) -> MatchChange:
"""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`).

Returns:
None if the field in the events either both match `public_value`
or if neither do, i.e. there has been no change.
True if it didn't match `public_value` but now does
False if it did match `public_value` but now doesn't
"""
prev_event = None
event = None
Expand All @@ -54,7 +55,7 @@ async def _get_key_change(

if not event and not prev_event:
logger.debug("Neither event exists: %r %r", prev_event_id, event_id)
return None
return MatchChange.no_change

prev_value = None
value = None
Expand All @@ -68,8 +69,8 @@ async def _get_key_change(
logger.debug("prev_value: %r -> value: %r", prev_value, value)

if value == public_value and prev_value != public_value:
return True
return MatchChange.now_true
elif value != public_value and prev_value == public_value:
return False
return MatchChange.now_false
else:
return None
return MatchChange.no_change
20 changes: 10 additions & 10 deletions synapse/handlers/user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import synapse.metrics
from synapse.api.constants import EventTypes, HistoryVisibility, JoinRules, Membership
from synapse.handlers.state_deltas import StateDeltasHandler
from synapse.handlers.state_deltas import MatchChange, StateDeltasHandler
from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.storage.roommember import ProfileInfo
from synapse.types import JsonDict
Expand Down Expand Up @@ -208,7 +208,7 @@ async def _handle_deltas(self, deltas: List[Dict[str, Any]]) -> None:
public_value=Membership.JOIN,
)

if change is False:
if change is MatchChange.now_false:
# Need to check if the server left the room entirely, if so
# we might need to remove all the users in that room
is_in_room = await self.store.is_host_joined(
Expand All @@ -231,14 +231,14 @@ async def _handle_deltas(self, deltas: List[Dict[str, Any]]) -> None:

is_support = await self.store.is_support_user(state_key)
if not is_support:
if change is None:
if change is MatchChange.no_change:
# Handle any profile changes
await self._handle_profile_change(
state_key, room_id, prev_event_id, event_id
)
continue

if change: # The user joined
if change is MatchChange.now_true: # The user joined
event = await self.store.get_event(event_id, allow_none=True)
# It isn't expected for this event to not exist, but we
# don't want the entire background process to break.
Expand Down Expand Up @@ -275,14 +275,14 @@ async def _handle_room_publicity_change(
logger.debug("Handling change for %s: %s", typ, room_id)

if typ == EventTypes.RoomHistoryVisibility:
change = await self._get_key_change(
publicness = await self._get_key_change(
prev_event_id,
event_id,
key_name="history_visibility",
public_value=HistoryVisibility.WORLD_READABLE,
)
elif typ == EventTypes.JoinRules:
change = await self._get_key_change(
publicness = await self._get_key_change(
prev_event_id,
event_id,
key_name="join_rule",
Expand All @@ -292,7 +292,7 @@ async def _handle_room_publicity_change(
raise Exception("Invalid event type")
# If change is None, no change. True => become world_readable/public,
# False => was world_readable/public
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
if change is None:
if publicness is MatchChange.no_change:
logger.debug("No change")
return

Expand All @@ -302,13 +302,13 @@ async def _handle_room_publicity_change(
room_id
)

logger.debug("Change: %r, is_public: %r", change, is_public)
logger.debug("Change: %r, publicness: %r", publicness, is_public)

if change and not is_public:
if publicness is MatchChange.now_true and not is_public:
# If we became world readable but room isn't currently public then
# we ignore the change
return
elif not change and is_public:
elif publicness is MatchChange.now_false and is_public:
# If we stopped being world readable but are still public,
# ignore the change
return
Expand Down