remove constantly lib use and switch to enums. (#12624)

This commit is contained in:
andrew do 2022-05-04 04:26:11 -07:00 committed by GitHub
parent 873d467976
commit 01e625513a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 38 deletions

1
changelog.d/12624.misc Normal file
View File

@ -0,0 +1 @@
Remove use of constantly library and switch to enums for EventRedactBehaviour. Contributed by @andrewdoh.

View File

@ -244,9 +244,6 @@ ignore_missing_imports = True
[mypy-canonicaljson] [mypy-canonicaljson]
ignore_missing_imports = True ignore_missing_imports = True
[mypy-constantly]
ignore_missing_imports = True
[mypy-ijson.*] [mypy-ijson.*]
ignore_missing_imports = True ignore_missing_imports = True

View File

@ -164,7 +164,7 @@ class EventHandler:
event. event.
""" """
redact_behaviour = ( redact_behaviour = (
EventRedactBehaviour.AS_IS if show_redacted else EventRedactBehaviour.REDACT EventRedactBehaviour.as_is if show_redacted else EventRedactBehaviour.redact
) )
event = await self.store.get_event( event = await self.store.get_event(
event_id, check_room_id=room_id, redact_behaviour=redact_behaviour event_id, check_room_id=room_id, redact_behaviour=redact_behaviour

View File

@ -316,7 +316,7 @@ class FederationHandler:
events_to_check = await self.store.get_events_as_list( events_to_check = await self.store.get_events_as_list(
event_ids_to_check, event_ids_to_check,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
get_prev_content=False, get_prev_content=False,
) )
@ -1494,7 +1494,7 @@ class FederationHandler:
events = await self.store.get_events_as_list( events = await self.store.get_events_as_list(
batch, batch,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
allow_rejected=True, allow_rejected=True,
) )
for event in events: for event in events:

View File

@ -860,7 +860,7 @@ class FederationEventHandler:
evs = await self._store.get_events( evs = await self._store.get_events(
list(state_map.values()), list(state_map.values()),
get_prev_content=False, get_prev_content=False,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
) )
event_map.update(evs) event_map.update(evs)

View File

@ -1407,7 +1407,7 @@ class EventCreationHandler:
original_event = await self.store.get_event( original_event = await self.store.get_event(
event.redacts, event.redacts,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
get_prev_content=False, get_prev_content=False,
allow_rejected=False, allow_rejected=False,
allow_none=True, allow_none=True,
@ -1504,7 +1504,7 @@ class EventCreationHandler:
original_event = await self.store.get_event( original_event = await self.store.get_event(
event.redacts, event.redacts,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
get_prev_content=False, get_prev_content=False,
allow_rejected=False, allow_rejected=False,
allow_none=True, allow_none=True,

View File

@ -800,7 +800,7 @@ class StateResolutionStore:
return self.store.get_events( return self.store.get_events(
event_ids, event_ids,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
get_prev_content=False, get_prev_content=False,
allow_rejected=allow_rejected, allow_rejected=allow_rejected,
) )

View File

@ -14,6 +14,7 @@
import logging import logging
import threading import threading
from enum import Enum, auto
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Any, Any,
@ -30,7 +31,6 @@ from typing import (
) )
import attr import attr
from constantly import NamedConstant, Names
from prometheus_client import Gauge from prometheus_client import Gauge
from typing_extensions import Literal from typing_extensions import Literal
@ -150,14 +150,14 @@ class _EventRow:
outlier: bool outlier: bool
class EventRedactBehaviour(Names): class EventRedactBehaviour(Enum):
""" """
What to do when retrieving a redacted event from the database. What to do when retrieving a redacted event from the database.
""" """
AS_IS = NamedConstant() as_is = auto()
REDACT = NamedConstant() redact = auto()
BLOCK = NamedConstant() block = auto()
class EventsWorkerStore(SQLBaseStore): class EventsWorkerStore(SQLBaseStore):
@ -327,7 +327,7 @@ class EventsWorkerStore(SQLBaseStore):
async def get_event( async def get_event(
self, self,
event_id: str, event_id: str,
redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact,
get_prev_content: bool = ..., get_prev_content: bool = ...,
allow_rejected: bool = ..., allow_rejected: bool = ...,
allow_none: Literal[False] = ..., allow_none: Literal[False] = ...,
@ -339,7 +339,7 @@ class EventsWorkerStore(SQLBaseStore):
async def get_event( async def get_event(
self, self,
event_id: str, event_id: str,
redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact,
get_prev_content: bool = ..., get_prev_content: bool = ...,
allow_rejected: bool = ..., allow_rejected: bool = ...,
allow_none: Literal[True] = ..., allow_none: Literal[True] = ...,
@ -350,7 +350,7 @@ class EventsWorkerStore(SQLBaseStore):
async def get_event( async def get_event(
self, self,
event_id: str, event_id: str,
redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact,
get_prev_content: bool = False, get_prev_content: bool = False,
allow_rejected: bool = False, allow_rejected: bool = False,
allow_none: bool = False, allow_none: bool = False,
@ -362,9 +362,9 @@ class EventsWorkerStore(SQLBaseStore):
event_id: The event_id of the event to fetch event_id: The event_id of the event to fetch
redact_behaviour: Determine what to do with a redacted event. Possible values: redact_behaviour: Determine what to do with a redacted event. Possible values:
* AS_IS - Return the full event body with no redacted content * as_is - Return the full event body with no redacted content
* REDACT - Return the event but with a redacted body * redact - Return the event but with a redacted body
* DISALLOW - Do not return redacted events (behave as per allow_none * block - Do not return redacted events (behave as per allow_none
if the event is redacted) if the event is redacted)
get_prev_content: If True and event is a state event, get_prev_content: If True and event is a state event,
@ -406,7 +406,7 @@ class EventsWorkerStore(SQLBaseStore):
async def get_events( async def get_events(
self, self,
event_ids: Collection[str], event_ids: Collection[str],
redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact,
get_prev_content: bool = False, get_prev_content: bool = False,
allow_rejected: bool = False, allow_rejected: bool = False,
) -> Dict[str, EventBase]: ) -> Dict[str, EventBase]:
@ -417,9 +417,9 @@ class EventsWorkerStore(SQLBaseStore):
redact_behaviour: Determine what to do with a redacted event. Possible redact_behaviour: Determine what to do with a redacted event. Possible
values: values:
* AS_IS - Return the full event body with no redacted content * as_is - Return the full event body with no redacted content
* REDACT - Return the event but with a redacted body * redact - Return the event but with a redacted body
* DISALLOW - Do not return redacted events (omit them from the response) * block - Do not return redacted events (omit them from the response)
get_prev_content: If True and event is a state event, get_prev_content: If True and event is a state event,
include the previous states content in the unsigned field. include the previous states content in the unsigned field.
@ -442,7 +442,7 @@ class EventsWorkerStore(SQLBaseStore):
async def get_events_as_list( async def get_events_as_list(
self, self,
event_ids: Collection[str], event_ids: Collection[str],
redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.REDACT, redact_behaviour: EventRedactBehaviour = EventRedactBehaviour.redact,
get_prev_content: bool = False, get_prev_content: bool = False,
allow_rejected: bool = False, allow_rejected: bool = False,
) -> List[EventBase]: ) -> List[EventBase]:
@ -455,9 +455,9 @@ class EventsWorkerStore(SQLBaseStore):
event_ids: The event_ids of the events to fetch event_ids: The event_ids of the events to fetch
redact_behaviour: Determine what to do with a redacted event. Possible values: redact_behaviour: Determine what to do with a redacted event. Possible values:
* AS_IS - Return the full event body with no redacted content * as_is - Return the full event body with no redacted content
* REDACT - Return the event but with a redacted body * redact - Return the event but with a redacted body
* DISALLOW - Do not return redacted events (omit them from the response) * block - Do not return redacted events (omit them from the response)
get_prev_content: If True and event is a state event, get_prev_content: If True and event is a state event,
include the previous states content in the unsigned field. include the previous states content in the unsigned field.
@ -568,10 +568,10 @@ class EventsWorkerStore(SQLBaseStore):
event = entry.event event = entry.event
if entry.redacted_event: if entry.redacted_event:
if redact_behaviour == EventRedactBehaviour.BLOCK: if redact_behaviour == EventRedactBehaviour.block:
# Skip this event # Skip this event
continue continue
elif redact_behaviour == EventRedactBehaviour.REDACT: elif redact_behaviour == EventRedactBehaviour.redact:
event = entry.redacted_event event = entry.redacted_event
events.append(event) events.append(event)

View File

@ -494,11 +494,11 @@ class SearchStore(SearchBackgroundUpdateStore):
results = list(filter(lambda row: row["room_id"] in room_ids, results)) results = list(filter(lambda row: row["room_id"] in room_ids, results))
# We set redact_behaviour to BLOCK here to prevent redacted events being returned in # We set redact_behaviour to block here to prevent redacted events being returned in
# search results (which is a data leak) # search results (which is a data leak)
events = await self.get_events_as_list( # type: ignore[attr-defined] events = await self.get_events_as_list( # type: ignore[attr-defined]
[r["event_id"] for r in results], [r["event_id"] for r in results],
redact_behaviour=EventRedactBehaviour.BLOCK, redact_behaviour=EventRedactBehaviour.block,
) )
event_map = {ev.event_id: ev for ev in events} event_map = {ev.event_id: ev for ev in events}
@ -652,11 +652,11 @@ class SearchStore(SearchBackgroundUpdateStore):
results = list(filter(lambda row: row["room_id"] in room_ids, results)) results = list(filter(lambda row: row["room_id"] in room_ids, results))
# We set redact_behaviour to BLOCK here to prevent redacted events being returned in # We set redact_behaviour to block here to prevent redacted events being returned in
# search results (which is a data leak) # search results (which is a data leak)
events = await self.get_events_as_list( # type: ignore[attr-defined] events = await self.get_events_as_list( # type: ignore[attr-defined]
[r["event_id"] for r in results], [r["event_id"] for r in results],
redact_behaviour=EventRedactBehaviour.BLOCK, redact_behaviour=EventRedactBehaviour.block,
) )
event_map = {ev.event_id: ev for ev in events} event_map = {ev.event_id: ev for ev in events}

View File

@ -48,7 +48,7 @@ class SignatureWorkerStore(EventsWorkerStore):
""" """
events = await self.get_events( events = await self.get_events(
event_ids, event_ids,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
allow_rejected=True, allow_rejected=True,
) )

View File

@ -943,7 +943,7 @@ class EventsPersistenceStorage:
dropped_events = await self.main_store.get_events( dropped_events = await self.main_store.get_events(
dropped_extrems, dropped_extrems,
allow_rejected=True, allow_rejected=True,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
) )
new_senders = {get_domain_from_id(e.sender) for e, _ in events_context} new_senders = {get_domain_from_id(e.sender) for e, _ in events_context}
@ -974,7 +974,7 @@ class EventsPersistenceStorage:
prev_events = await self.main_store.get_events( prev_events = await self.main_store.get_events(
new_events, new_events,
allow_rejected=True, allow_rejected=True,
redact_behaviour=EventRedactBehaviour.AS_IS, redact_behaviour=EventRedactBehaviour.as_is,
) )
events_to_check = prev_events.values() events_to_check = prev_events.values()