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

Commit 90da862

Browse files
AndrewFerrH-Shay
authored andcommitted
Use device_one_time_keys_count to match MSC3202 (#14565)
* Use `device_one_time_keys_count` to match MSC3202 Rename the `device_one_time_key_counts` key in responses to `device_one_time_keys_count` to match the name specified by MSC3202. Also change related variable/class names for consistency. Signed-off-by: Andrew Ferrazzutti <[email protected]> * Update changelog.d/14565.misc * Revert name change for `one_time_key_counts` key as this is a different key altogether from `device_one_time_keys_count`, which is used for `/sync` instead of appservice transactions. Signed-off-by: Andrew Ferrazzutti <[email protected]>
1 parent 7cf35fa commit 90da862

File tree

9 files changed

+38
-34
lines changed

9 files changed

+38
-34
lines changed

changelog.d/14565.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In application service transactions that include the experimental `org.matrix.msc3202.device_one_time_key_counts` key, include a duplicate key of `org.matrix.msc3202.device_one_time_keys_count` to match the name proposed by [MSC3202](https://github.com/matrix-org/matrix-spec-proposals/blob/travis/msc/otk-dl-appservice/proposals/3202-encrypted-appservices.md).

synapse/appservice/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232

3333
logger = logging.getLogger(__name__)
3434

35-
# Type for the `device_one_time_key_counts` field in an appservice transaction
35+
# Type for the `device_one_time_keys_count` field in an appservice transaction
3636
# user ID -> {device ID -> {algorithm -> count}}
37-
TransactionOneTimeKeyCounts = Dict[str, Dict[str, Dict[str, int]]]
37+
TransactionOneTimeKeysCount = Dict[str, Dict[str, Dict[str, int]]]
3838

3939
# Type for the `device_unused_fallback_key_types` field in an appservice transaction
4040
# user ID -> {device ID -> [algorithm]}
@@ -376,7 +376,7 @@ def __init__(
376376
events: List[EventBase],
377377
ephemeral: List[JsonDict],
378378
to_device_messages: List[JsonDict],
379-
one_time_key_counts: TransactionOneTimeKeyCounts,
379+
one_time_keys_count: TransactionOneTimeKeysCount,
380380
unused_fallback_keys: TransactionUnusedFallbackKeys,
381381
device_list_summary: DeviceListUpdates,
382382
):
@@ -385,7 +385,7 @@ def __init__(
385385
self.events = events
386386
self.ephemeral = ephemeral
387387
self.to_device_messages = to_device_messages
388-
self.one_time_key_counts = one_time_key_counts
388+
self.one_time_keys_count = one_time_keys_count
389389
self.unused_fallback_keys = unused_fallback_keys
390390
self.device_list_summary = device_list_summary
391391

@@ -402,7 +402,7 @@ async def send(self, as_api: "ApplicationServiceApi") -> bool:
402402
events=self.events,
403403
ephemeral=self.ephemeral,
404404
to_device_messages=self.to_device_messages,
405-
one_time_key_counts=self.one_time_key_counts,
405+
one_time_keys_count=self.one_time_keys_count,
406406
unused_fallback_keys=self.unused_fallback_keys,
407407
device_list_summary=self.device_list_summary,
408408
txn_id=self.id,

synapse/appservice/api.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from synapse.api.errors import CodeMessageException
2424
from synapse.appservice import (
2525
ApplicationService,
26-
TransactionOneTimeKeyCounts,
26+
TransactionOneTimeKeysCount,
2727
TransactionUnusedFallbackKeys,
2828
)
2929
from synapse.events import EventBase
@@ -262,7 +262,7 @@ async def push_bulk(
262262
events: List[EventBase],
263263
ephemeral: List[JsonDict],
264264
to_device_messages: List[JsonDict],
265-
one_time_key_counts: TransactionOneTimeKeyCounts,
265+
one_time_keys_count: TransactionOneTimeKeysCount,
266266
unused_fallback_keys: TransactionUnusedFallbackKeys,
267267
device_list_summary: DeviceListUpdates,
268268
txn_id: Optional[int] = None,
@@ -310,10 +310,13 @@ async def push_bulk(
310310

311311
# TODO: Update to stable prefixes once MSC3202 completes FCP merge
312312
if service.msc3202_transaction_extensions:
313-
if one_time_key_counts:
313+
if one_time_keys_count:
314314
body[
315315
"org.matrix.msc3202.device_one_time_key_counts"
316-
] = one_time_key_counts
316+
] = one_time_keys_count
317+
body[
318+
"org.matrix.msc3202.device_one_time_keys_count"
319+
] = one_time_keys_count
317320
if unused_fallback_keys:
318321
body[
319322
"org.matrix.msc3202.device_unused_fallback_key_types"

synapse/appservice/scheduler.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from synapse.appservice import (
6565
ApplicationService,
6666
ApplicationServiceState,
67-
TransactionOneTimeKeyCounts,
67+
TransactionOneTimeKeysCount,
6868
TransactionUnusedFallbackKeys,
6969
)
7070
from synapse.appservice.api import ApplicationServiceApi
@@ -258,7 +258,7 @@ async def _send_request(self, service: ApplicationService) -> None:
258258
):
259259
return
260260

261-
one_time_key_counts: Optional[TransactionOneTimeKeyCounts] = None
261+
one_time_keys_count: Optional[TransactionOneTimeKeysCount] = None
262262
unused_fallback_keys: Optional[TransactionUnusedFallbackKeys] = None
263263

264264
if (
@@ -269,7 +269,7 @@ async def _send_request(self, service: ApplicationService) -> None:
269269
# for the users which are mentioned in this transaction,
270270
# as well as the appservice's sender.
271271
(
272-
one_time_key_counts,
272+
one_time_keys_count,
273273
unused_fallback_keys,
274274
) = await self._compute_msc3202_otk_counts_and_fallback_keys(
275275
service, events, ephemeral, to_device_messages_to_send
@@ -281,7 +281,7 @@ async def _send_request(self, service: ApplicationService) -> None:
281281
events,
282282
ephemeral,
283283
to_device_messages_to_send,
284-
one_time_key_counts,
284+
one_time_keys_count,
285285
unused_fallback_keys,
286286
device_list_summary,
287287
)
@@ -296,7 +296,7 @@ async def _compute_msc3202_otk_counts_and_fallback_keys(
296296
events: Iterable[EventBase],
297297
ephemerals: Iterable[JsonDict],
298298
to_device_messages: Iterable[JsonDict],
299-
) -> Tuple[TransactionOneTimeKeyCounts, TransactionUnusedFallbackKeys]:
299+
) -> Tuple[TransactionOneTimeKeysCount, TransactionUnusedFallbackKeys]:
300300
"""
301301
Given a list of the events, ephemeral messages and to-device messages,
302302
- first computes a list of application services users that may have
@@ -367,7 +367,7 @@ async def send(
367367
events: List[EventBase],
368368
ephemeral: Optional[List[JsonDict]] = None,
369369
to_device_messages: Optional[List[JsonDict]] = None,
370-
one_time_key_counts: Optional[TransactionOneTimeKeyCounts] = None,
370+
one_time_keys_count: Optional[TransactionOneTimeKeysCount] = None,
371371
unused_fallback_keys: Optional[TransactionUnusedFallbackKeys] = None,
372372
device_list_summary: Optional[DeviceListUpdates] = None,
373373
) -> None:
@@ -380,7 +380,7 @@ async def send(
380380
events: The persistent events to include in the transaction.
381381
ephemeral: The ephemeral events to include in the transaction.
382382
to_device_messages: The to-device messages to include in the transaction.
383-
one_time_key_counts: Counts of remaining one-time keys for relevant
383+
one_time_keys_count: Counts of remaining one-time keys for relevant
384384
appservice devices in the transaction.
385385
unused_fallback_keys: Lists of unused fallback keys for relevant
386386
appservice devices in the transaction.
@@ -397,7 +397,7 @@ async def send(
397397
events=events,
398398
ephemeral=ephemeral or [],
399399
to_device_messages=to_device_messages or [],
400-
one_time_key_counts=one_time_key_counts or {},
400+
one_time_keys_count=one_time_keys_count or {},
401401
unused_fallback_keys=unused_fallback_keys or {},
402402
device_list_summary=device_list_summary or DeviceListUpdates(),
403403
)

synapse/handlers/sync.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,14 +1426,14 @@ async def generate_sync_result(
14261426

14271427
logger.debug("Fetching OTK data")
14281428
device_id = sync_config.device_id
1429-
one_time_key_counts: JsonDict = {}
1429+
one_time_keys_count: JsonDict = {}
14301430
unused_fallback_key_types: List[str] = []
14311431
if device_id:
14321432
# TODO: We should have a way to let clients differentiate between the states of:
14331433
# * no change in OTK count since the provided since token
14341434
# * the server has zero OTKs left for this device
14351435
# Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
1436-
one_time_key_counts = await self.store.count_e2e_one_time_keys(
1436+
one_time_keys_count = await self.store.count_e2e_one_time_keys(
14371437
user_id, device_id
14381438
)
14391439
unused_fallback_key_types = (
@@ -1463,7 +1463,7 @@ async def generate_sync_result(
14631463
archived=sync_result_builder.archived,
14641464
to_device=sync_result_builder.to_device,
14651465
device_lists=device_lists,
1466-
device_one_time_keys_count=one_time_key_counts,
1466+
device_one_time_keys_count=one_time_keys_count,
14671467
device_unused_fallback_key_types=unused_fallback_key_types,
14681468
next_batch=sync_result_builder.now_token,
14691469
)

synapse/storage/databases/main/appservice.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ApplicationService,
2121
ApplicationServiceState,
2222
AppServiceTransaction,
23-
TransactionOneTimeKeyCounts,
23+
TransactionOneTimeKeysCount,
2424
TransactionUnusedFallbackKeys,
2525
)
2626
from synapse.config.appservice import load_appservices
@@ -260,7 +260,7 @@ async def create_appservice_txn(
260260
events: List[EventBase],
261261
ephemeral: List[JsonDict],
262262
to_device_messages: List[JsonDict],
263-
one_time_key_counts: TransactionOneTimeKeyCounts,
263+
one_time_keys_count: TransactionOneTimeKeysCount,
264264
unused_fallback_keys: TransactionUnusedFallbackKeys,
265265
device_list_summary: DeviceListUpdates,
266266
) -> AppServiceTransaction:
@@ -273,7 +273,7 @@ async def create_appservice_txn(
273273
events: A list of persistent events to put in the transaction.
274274
ephemeral: A list of ephemeral events to put in the transaction.
275275
to_device_messages: A list of to-device messages to put in the transaction.
276-
one_time_key_counts: Counts of remaining one-time keys for relevant
276+
one_time_keys_count: Counts of remaining one-time keys for relevant
277277
appservice devices in the transaction.
278278
unused_fallback_keys: Lists of unused fallback keys for relevant
279279
appservice devices in the transaction.
@@ -299,7 +299,7 @@ def _create_appservice_txn(txn: LoggingTransaction) -> AppServiceTransaction:
299299
events=events,
300300
ephemeral=ephemeral,
301301
to_device_messages=to_device_messages,
302-
one_time_key_counts=one_time_key_counts,
302+
one_time_keys_count=one_time_keys_count,
303303
unused_fallback_keys=unused_fallback_keys,
304304
device_list_summary=device_list_summary,
305305
)
@@ -379,7 +379,7 @@ def _get_oldest_unsent_txn(
379379
events=events,
380380
ephemeral=[],
381381
to_device_messages=[],
382-
one_time_key_counts={},
382+
one_time_keys_count={},
383383
unused_fallback_keys={},
384384
device_list_summary=DeviceListUpdates(),
385385
)

synapse/storage/databases/main/end_to_end_keys.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from synapse.api.constants import DeviceKeyAlgorithms
3535
from synapse.appservice import (
36-
TransactionOneTimeKeyCounts,
36+
TransactionOneTimeKeysCount,
3737
TransactionUnusedFallbackKeys,
3838
)
3939
from synapse.logging.opentracing import log_kv, set_tag, trace
@@ -514,7 +514,7 @@ def _count_e2e_one_time_keys(txn: LoggingTransaction) -> Dict[str, int]:
514514

515515
async def count_bulk_e2e_one_time_keys_for_as(
516516
self, user_ids: Collection[str]
517-
) -> TransactionOneTimeKeyCounts:
517+
) -> TransactionOneTimeKeysCount:
518518
"""
519519
Counts, in bulk, the one-time keys for all the users specified.
520520
Intended to be used by application services for populating OTK counts in
@@ -528,7 +528,7 @@ async def count_bulk_e2e_one_time_keys_for_as(
528528

529529
def _count_bulk_e2e_one_time_keys_txn(
530530
txn: LoggingTransaction,
531-
) -> TransactionOneTimeKeyCounts:
531+
) -> TransactionOneTimeKeysCount:
532532
user_in_where_clause, user_parameters = make_in_list_sql_clause(
533533
self.database_engine, "user_id", user_ids
534534
)
@@ -541,7 +541,7 @@ def _count_bulk_e2e_one_time_keys_txn(
541541
"""
542542
txn.execute(sql, user_parameters)
543543

544-
result: TransactionOneTimeKeyCounts = {}
544+
result: TransactionOneTimeKeysCount = {}
545545

546546
for user_id, device_id, algorithm, count in txn:
547547
# We deliberately construct empty dictionaries for

tests/appservice/test_scheduler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_single_service_up_txn_sent(self):
6969
events=events,
7070
ephemeral=[],
7171
to_device_messages=[], # txn made and saved
72-
one_time_key_counts={},
72+
one_time_keys_count={},
7373
unused_fallback_keys={},
7474
device_list_summary=DeviceListUpdates(),
7575
)
@@ -96,7 +96,7 @@ def test_single_service_down(self):
9696
events=events,
9797
ephemeral=[],
9898
to_device_messages=[], # txn made and saved
99-
one_time_key_counts={},
99+
one_time_keys_count={},
100100
unused_fallback_keys={},
101101
device_list_summary=DeviceListUpdates(),
102102
)
@@ -125,7 +125,7 @@ def test_single_service_up_txn_not_sent(self):
125125
events=events,
126126
ephemeral=[],
127127
to_device_messages=[],
128-
one_time_key_counts={},
128+
one_time_keys_count={},
129129
unused_fallback_keys={},
130130
device_list_summary=DeviceListUpdates(),
131131
)

tests/handlers/test_appservice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from synapse.api.constants import EduTypes, EventTypes
2626
from synapse.appservice import (
2727
ApplicationService,
28-
TransactionOneTimeKeyCounts,
28+
TransactionOneTimeKeysCount,
2929
TransactionUnusedFallbackKeys,
3030
)
3131
from synapse.handlers.appservice import ApplicationServicesHandler
@@ -1123,7 +1123,7 @@ def test_application_services_receive_otk_counts_and_fallback_key_usages_with_pd
11231123
# Capture what was sent as an AS transaction.
11241124
self.send_mock.assert_called()
11251125
last_args, _last_kwargs = self.send_mock.call_args
1126-
otks: Optional[TransactionOneTimeKeyCounts] = last_args[self.ARG_OTK_COUNTS]
1126+
otks: Optional[TransactionOneTimeKeysCount] = last_args[self.ARG_OTK_COUNTS]
11271127
unused_fallbacks: Optional[TransactionUnusedFallbackKeys] = last_args[
11281128
self.ARG_FALLBACK_KEYS
11291129
]

0 commit comments

Comments
 (0)