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

Commit

Permalink
Add constants for device messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed May 26, 2022
1 parent cb481e0 commit 063d0dd
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 25 deletions.
4 changes: 4 additions & 0 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ class EduTypes:
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
2 changes: 1 addition & 1 deletion synapse/federation/sender/per_destination_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,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
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
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
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
18 changes: 10 additions & 8 deletions tests/federation/test_federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,10 @@ def test_upload_signatures(self):

# expect signing key update edu
self.assertEqual(len(self.edus), 2)
self.assertEqual(self.edus.pop(0)["edu_type"], "m.signing_key_update")
self.assertEqual(self.edus.pop(0)["edu_type"], "org.matrix.signing_key_update")
self.assertEqual(self.edus.pop(0)["edu_type"], EduTypes.SIGNING_KEY_UPDATE)
self.assertEqual(
self.edus.pop(0)["edu_type"], EduTypes.UNSTABLE_SIGNING_KEY_UPDATE
)

# sign the devices
d1_json = build_device_dict(u1, "D1", device1_signing_key)
Expand All @@ -348,7 +350,7 @@ def test_upload_signatures(self):
self.assertEqual(len(self.edus), 2)
stream_id = None # FIXME: there is a discontinuity in the stream IDs: see #7142
for edu in self.edus:
self.assertEqual(edu["edu_type"], "m.device_list_update")
self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
c = edu["content"]
if stream_id is not None:
self.assertEqual(c["prev_id"], [stream_id])
Expand Down Expand Up @@ -388,7 +390,7 @@ def test_delete_devices(self):
# expect three edus, in an unknown order
self.assertEqual(len(self.edus), 3)
for edu in self.edus:
self.assertEqual(edu["edu_type"], "m.device_list_update")
self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
c = edu["content"]
self.assertGreaterEqual(
c.items(),
Expand Down Expand Up @@ -435,7 +437,7 @@ def test_unreachable_server(self):
self.assertEqual(len(self.edus), 3)
stream_id = None
for edu in self.edus:
self.assertEqual(edu["edu_type"], "m.device_list_update")
self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
c = edu["content"]
self.assertEqual(c["prev_id"], [stream_id] if stream_id is not None else [])
if stream_id is not None:
Expand Down Expand Up @@ -487,7 +489,7 @@ def test_prune_outbound_device_pokes1(self):
# there should be a single update for this user.
self.assertEqual(len(self.edus), 1)
edu = self.edus.pop(0)
self.assertEqual(edu["edu_type"], "m.device_list_update")
self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
c = edu["content"]

# synapse uses an empty prev_id list to indicate "needs a full resync".
Expand Down Expand Up @@ -544,7 +546,7 @@ def test_prune_outbound_device_pokes2(self):
# ... and we should get a single update for this user.
self.assertEqual(len(self.edus), 1)
edu = self.edus.pop(0)
self.assertEqual(edu["edu_type"], "m.device_list_update")
self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
c = edu["content"]

# synapse uses an empty prev_id list to indicate "needs a full resync".
Expand All @@ -560,7 +562,7 @@ def check_device_update_edu(
"""Check that the given EDU is an update for the given device
Returns the stream_id.
"""
self.assertEqual(edu["edu_type"], "m.device_list_update")
self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
content = edu["content"]

expected = {
Expand Down
4 changes: 3 additions & 1 deletion tests/federation/transport/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from synapse.api.constants import EduTypes

from tests import unittest
from tests.unittest import DEBUG, override_config

Expand Down Expand Up @@ -50,7 +52,7 @@ def test_edu_debugging_doesnt_explode(self):
"/_matrix/federation/v1/send/txn_id_1234/",
content={
"edus": [
{"edu_type": "m.device_list_update", "content": {"foo": "bar"}}
{"edu_type": EduTypes.DEVICE_LIST_UPDATE, "content": {"foo": "bar"}}
],
"pdus": [],
},
Expand Down
5 changes: 3 additions & 2 deletions tests/rest/client/test_sendtodevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from synapse.api.constants import EduTypes
from synapse.rest import admin
from synapse.rest.client import login, sendtodevice, sync

Expand Down Expand Up @@ -139,7 +140,7 @@ def test_remote_room_key_request(self) -> None:
for i in range(3):
self.get_success(
federation_registry.on_edu(
"m.direct_to_device",
EduTypes.DIRECT_TO_DEVICE,
"remote_server",
{
"sender": "@user:remote_server",
Expand Down Expand Up @@ -172,7 +173,7 @@ def test_remote_room_key_request(self) -> None:
# and we can send more messages
self.get_success(
federation_registry.on_edu(
"m.direct_to_device",
EduTypes.DIRECT_TO_DEVICE,
"remote_server",
{
"sender": "@user:remote_server",
Expand Down
7 changes: 5 additions & 2 deletions tests/storage/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import synapse.api.errors
from synapse.api.constants import EduTypes

from tests.unittest import HomeserverTestCase

Expand Down Expand Up @@ -266,10 +267,12 @@ def test_get_device_updates_by_remote_cross_signing_key_updates(
# (This is a temporary arrangement for backwards compatibility!)
self.assertEqual(len(device_updates), 2, device_updates)
self.assertEqual(
device_updates[0][0], "m.signing_key_update", device_updates[0]
device_updates[0][0], EduTypes.SIGNING_KEY_UPDATE, device_updates[0]
)
self.assertEqual(
device_updates[1][0], "org.matrix.signing_key_update", device_updates[1]
device_updates[1][0],
EduTypes.UNSTABLE_SIGNING_KEY_UPDATE,
device_updates[1],
)

# Check there are no more device updates left.
Expand Down

0 comments on commit 063d0dd

Please sign in to comment.