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

Additional constants for EDU types #12884

Merged
merged 5 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changelog.d/12884.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use constants for EDU types.
8 changes: 7 additions & 1 deletion synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ class DeviceKeyAlgorithms:


class EduTypes:
Presence: Final = "m.presence"
PRESENCE: Final = "m.presence"
TYPING: Final = "m.typing"
RECEIPT: Final = "m.receipt"
DEVICE_LIST_UPDATE: Final = "m.device_list_update"
SIGNING_KEY_UPDATE: Final = "m.signing_key_update"
UNSTABLE_SIGNING_KEY_UPDATE: Final = "org.matrix.signing_key_update"
DIRECT_TO_DEVICE: Final = "m.direct_to_device"


class RejectedReason:
Expand Down
4 changes: 2 additions & 2 deletions synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import jsonschema
from jsonschema import FormatChecker

from synapse.api.constants import EventContentFields
from synapse.api.constants import EduTypes, EventContentFields
from synapse.api.errors import SynapseError
from synapse.api.presence import UserPresenceState
from synapse.events import EventBase
Expand Down Expand Up @@ -347,7 +347,7 @@ def _check(self, event: FilterEvent) -> bool:
user_id = event.user_id
field_matchers = {
"senders": lambda v: user_id == v,
"types": lambda v: "m.presence" == v,
"types": lambda v: EduTypes.PRESENCE == v,
}
return self._check_fields(field_matchers)
else:
Expand Down
2 changes: 1 addition & 1 deletion synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ def register_instances_for_edu(
self._edu_type_to_instance[edu_type] = instance_names

async def on_edu(self, edu_type: str, origin: str, content: dict) -> None:
if not self.config.server.use_presence and edu_type == EduTypes.Presence:
if not self.config.server.use_presence and edu_type == EduTypes.PRESENCE:
return

# Check if we have a handler on this instance
Expand Down
7 changes: 4 additions & 3 deletions synapse/federation/sender/per_destination_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import attr
from prometheus_client import Counter

from synapse.api.constants import EduTypes
from synapse.api.errors import (
FederationDeniedError,
HttpResponseException,
Expand Down Expand Up @@ -542,7 +543,7 @@ def _get_rr_edus(self, force_flush: bool) -> Iterable[Edu]:
edu = Edu(
origin=self._server_name,
destination=self._destination,
edu_type="m.receipt",
edu_type=EduTypes.RECEIPT,
content=self._pending_rrs,
)
self._pending_rrs = {}
Expand Down Expand Up @@ -592,7 +593,7 @@ async def _get_to_device_message_edus(self, limit: int) -> Tuple[List[Edu], int]
Edu(
origin=self._server_name,
destination=self._destination,
edu_type="m.direct_to_device",
edu_type=EduTypes.DIRECT_TO_DEVICE,
content=content,
)
for content in contents
Expand Down Expand Up @@ -670,7 +671,7 @@ async def __aenter__(self) -> Tuple[List[EventBase], List[Edu]]:
Edu(
origin=self.queue._server_name,
destination=self.queue._destination,
edu_type="m.presence",
edu_type=EduTypes.PRESENCE,
content={
"push": [
format_user_presence_state(
Expand Down
6 changes: 5 additions & 1 deletion synapse/federation/sender/transaction_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from prometheus_client import Gauge

from synapse.api.constants import EduTypes
from synapse.api.errors import HttpResponseException
from synapse.events import EventBase
from synapse.federation.persistence import TransactionActions
Expand Down Expand Up @@ -126,7 +127,10 @@ async def send_new_transaction(
len(edus),
)
if issue_8631_logger.isEnabledFor(logging.DEBUG):
DEVICE_UPDATE_EDUS = {"m.device_list_update", "m.signing_key_update"}
DEVICE_UPDATE_EDUS = {
EduTypes.DEVICE_LIST_UPDATE,
EduTypes.SIGNING_KEY_UPDATE,
}
device_list_updates = [
edu.content for edu in edus if edu.edu_type in DEVICE_UPDATE_EDUS
]
Expand Down
6 changes: 5 additions & 1 deletion synapse/federation/transport/server/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from matrix_common.versionstring import get_distribution_version_string
from typing_extensions import Literal

from synapse.api.constants import EduTypes
from synapse.api.errors import Codes, SynapseError
from synapse.api.room_versions import RoomVersions
from synapse.api.urls import FEDERATION_UNSTABLE_PREFIX, FEDERATION_V2_PREFIX
Expand Down Expand Up @@ -108,7 +109,10 @@ async def on_PUT(
)

if issue_8631_logger.isEnabledFor(logging.DEBUG):
DEVICE_UPDATE_EDUS = ["m.device_list_update", "m.signing_key_update"]
DEVICE_UPDATE_EDUS = [
EduTypes.DEVICE_LIST_UPDATE,
EduTypes.SIGNING_KEY_UPDATE,
]
device_list_updates = [
edu.get("content", {})
for edu in transaction_data.get("edus", [])
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from twisted.internet import defer

import synapse
from synapse.api.constants import EventTypes
from synapse.api.constants import EduTypes, EventTypes
from synapse.appservice import ApplicationService
from synapse.events import EventBase
from synapse.handlers.presence import format_user_presence_state
Expand Down Expand Up @@ -503,7 +503,7 @@ async def _handle_presence(
time_now = self.clock.time_msec()
events.extend(
{
"type": "m.presence",
"type": EduTypes.PRESENCE,
"sender": event.user_id,
"content": format_user_presence_state(
event, time_now, include_user_id=False
Expand Down
5 changes: 3 additions & 2 deletions synapse/handlers/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)

from synapse.api import errors
from synapse.api.constants import EventTypes
from synapse.api.constants import EduTypes, EventTypes
from synapse.api.errors import (
Codes,
FederationDeniedError,
Expand Down Expand Up @@ -279,7 +279,8 @@ def __init__(self, hs: "HomeServer"):
federation_registry = hs.get_federation_registry()

federation_registry.register_edu_handler(
"m.device_list_update", self.device_list_updater.incoming_device_list_update
EduTypes.DEVICE_LIST_UPDATE,
self.device_list_updater.incoming_device_list_update,
)

hs.get_distributor().observe("user_left_room", self.user_left_room)
Expand Down
6 changes: 3 additions & 3 deletions synapse/handlers/devicemessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging
from typing import TYPE_CHECKING, Any, Dict

from synapse.api.constants import ToDeviceEventTypes
from synapse.api.constants import EduTypes, ToDeviceEventTypes
from synapse.api.errors import SynapseError
from synapse.api.ratelimiting import Ratelimiter
from synapse.logging.context import run_in_background
Expand Down Expand Up @@ -59,11 +59,11 @@ def __init__(self, hs: "HomeServer"):
# to the appropriate worker.
if hs.get_instance_name() in hs.config.worker.writers.to_device:
hs.get_federation_registry().register_edu_handler(
"m.direct_to_device", self.on_direct_to_device_edu
EduTypes.DIRECT_TO_DEVICE, self.on_direct_to_device_edu
)
else:
hs.get_federation_registry().register_instances_for_edu(
"m.direct_to_device",
EduTypes.DIRECT_TO_DEVICE,
hs.config.worker.writers.to_device,
)

Expand Down
5 changes: 3 additions & 2 deletions synapse/handlers/e2e_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from twisted.internet import defer

from synapse.api.constants import EduTypes
from synapse.api.errors import CodeMessageException, Codes, NotFoundError, SynapseError
from synapse.logging.context import make_deferred_yieldable, run_in_background
from synapse.logging.opentracing import log_kv, set_tag, tag_args, trace
Expand Down Expand Up @@ -66,13 +67,13 @@ def __init__(self, hs: "HomeServer"):
# Only register this edu handler on master as it requires writing
# device updates to the db
federation_registry.register_edu_handler(
"m.signing_key_update",
EduTypes.SIGNING_KEY_UPDATE,
self._edu_updater.incoming_signing_key_update,
)
# also handle the unstable version
# FIXME: remove this when enough servers have upgraded
federation_registry.register_edu_handler(
"org.matrix.signing_key_update",
EduTypes.UNSTABLE_SIGNING_KEY_UPDATE,
self._edu_updater.incoming_signing_key_update,
)

Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async def get_stream(
states = await presence_handler.get_states(users)
to_add.extend(
{
"type": EduTypes.Presence,
"type": EduTypes.PRESENCE,
"content": format_user_presence_state(state, time_now),
}
for state in states
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/initial_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ async def handle_room(event: RoomsForUser) -> None:
"rooms": rooms_ret,
"presence": [
{
"type": "m.presence",
"type": EduTypes.PRESENCE,
"content": format_user_presence_state(event, now),
}
for event in presence
Expand Down Expand Up @@ -439,7 +439,7 @@ async def get_presence() -> List[JsonDict]:

return [
{
"type": EduTypes.Presence,
"type": EduTypes.PRESENCE,
"content": format_user_presence_state(s, time_now),
}
for s in states
Expand Down
8 changes: 5 additions & 3 deletions synapse/handlers/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from typing_extensions import ContextManager

import synapse.metrics
from synapse.api.constants import EventTypes, Membership, PresenceState
from synapse.api.constants import EduTypes, EventTypes, Membership, PresenceState
from synapse.api.errors import SynapseError
from synapse.api.presence import UserPresenceState
from synapse.appservice import ApplicationService
Expand Down Expand Up @@ -394,7 +394,7 @@ def __init__(self, hs: "HomeServer"):

# Route presence EDUs to the right worker
hs.get_federation_registry().register_instances_for_edu(
"m.presence",
EduTypes.PRESENCE,
hs.config.worker.writers.presence,
)

Expand Down Expand Up @@ -649,7 +649,9 @@ def __init__(self, hs: "HomeServer"):

federation_registry = hs.get_federation_registry()

federation_registry.register_edu_handler("m.presence", self.incoming_presence)
federation_registry.register_edu_handler(
EduTypes.PRESENCE, self.incoming_presence
)

LaterGauge(
"synapse_handlers_presence_user_to_current_state_size",
Expand Down
6 changes: 3 additions & 3 deletions synapse/handlers/receipts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import logging
from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple

from synapse.api.constants import ReceiptTypes
from synapse.api.constants import EduTypes, ReceiptTypes
from synapse.appservice import ApplicationService
from synapse.streams import EventSource
from synapse.types import (
Expand Down Expand Up @@ -52,11 +52,11 @@ def __init__(self, hs: "HomeServer"):
# to the appropriate worker.
if hs.get_instance_name() in hs.config.worker.writers.receipts:
hs.get_federation_registry().register_edu_handler(
"m.receipt", self._received_remote_receipt
EduTypes.RECEIPT, self._received_remote_receipt
)
else:
hs.get_federation_registry().register_instances_for_edu(
"m.receipt",
EduTypes.RECEIPT,
hs.config.worker.writers.receipts,
)

Expand Down
11 changes: 7 additions & 4 deletions synapse/handlers/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import attr

from synapse.api.constants import EduTypes
from synapse.api.errors import AuthError, ShadowBanError, SynapseError
from synapse.appservice import ApplicationService
from synapse.metrics.background_process_metrics import (
Expand Down Expand Up @@ -68,7 +69,7 @@ def __init__(self, hs: "HomeServer"):

if hs.get_instance_name() not in hs.config.worker.writers.typing:
hs.get_federation_registry().register_instances_for_edu(
"m.typing",
EduTypes.TYPING,
hs.config.worker.writers.typing,
)

Expand Down Expand Up @@ -143,7 +144,7 @@ async def _push_remote(self, member: RoomMember, typing: bool) -> None:
logger.debug("sending typing update to %s", domain)
self.federation.build_and_send_edu(
destination=domain,
edu_type="m.typing",
edu_type=EduTypes.TYPING,
content={
"room_id": member.room_id,
"user_id": member.user_id,
Expand Down Expand Up @@ -218,7 +219,9 @@ def __init__(self, hs: "HomeServer"):

self.hs = hs

hs.get_federation_registry().register_edu_handler("m.typing", self._recv_edu)
hs.get_federation_registry().register_edu_handler(
EduTypes.TYPING, self._recv_edu
)

hs.get_distributor().observe("user_left_room", self.user_left_room)

Expand Down Expand Up @@ -458,7 +461,7 @@ def __init__(self, hs: "HomeServer"):
def _make_event_for(self, room_id: str) -> JsonDict:
typing = self.get_typing_handler()._room_typing[room_id]
return {
"type": "m.typing",
"type": EduTypes.TYPING,
"room_id": room_id,
"content": {"user_ids": list(typing)},
}
Expand Down
4 changes: 2 additions & 2 deletions synapse/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from twisted.internet import defer

from synapse.api.constants import EventTypes, HistoryVisibility, Membership
from synapse.api.constants import EduTypes, EventTypes, HistoryVisibility, Membership
from synapse.api.errors import AuthError
from synapse.events import EventBase
from synapse.handlers.presence import format_user_presence_state
Expand Down Expand Up @@ -632,7 +632,7 @@ async def check_for_updates(
now = self.clock.time_msec()
new_events[:] = [
{
"type": "m.presence",
"type": EduTypes.PRESENCE,
"content": format_user_presence_state(event, now),
}
for event in new_events
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from collections import defaultdict
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union

from synapse.api.constants import Membership, PresenceState
from synapse.api.constants import EduTypes, Membership, PresenceState
from synapse.api.errors import Codes, StoreError, SynapseError
from synapse.api.filtering import FilterCollection
from synapse.api.presence import UserPresenceState
Expand Down Expand Up @@ -305,7 +305,7 @@ def encode_presence(events: List[UserPresenceState], time_now: int) -> JsonDict:
return {
"events": [
{
"type": "m.presence",
"type": EduTypes.PRESENCE,
"sender": event.user_id,
"content": format_user_presence_state(
event, time_now, include_user_id=False
Expand Down
5 changes: 3 additions & 2 deletions synapse/storage/databases/main/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
cast,
)

from synapse.api.constants import EduTypes
from synapse.api.errors import Codes, StoreError
from synapse.logging.opentracing import (
get_active_span_text_map,
Expand Down Expand Up @@ -419,7 +420,7 @@ async def get_device_updates_by_remote(
# Add the updated cross-signing keys to the results list
for user_id, result in cross_signing_keys_by_user.items():
result["user_id"] = user_id
results.append(("m.signing_key_update", result))
results.append((EduTypes.SIGNING_KEY_UPDATE, result))
# also send the unstable version
# FIXME: remove this when enough servers have upgraded
# and remove the length budgeting above.
Expand Down Expand Up @@ -545,7 +546,7 @@ async def _get_device_update_edus_by_remote(
else:
result["deleted"] = True

results.append(("m.device_list_update", result))
results.append((EduTypes.DEVICE_LIST_UPDATE, result))

return results

Expand Down
Loading