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 <andrewf@element.io>

* 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 <andrewf@element.io>
This commit is contained in:
Andrew Ferrazzutti 2022-11-28 11:17:29 -05:00 committed by GitHub
parent d56f48038a
commit 1183c372fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 34 deletions

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

@ -0,0 +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).

View File

@ -32,9 +32,9 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Type for the `device_one_time_key_counts` field in an appservice transaction # Type for the `device_one_time_keys_count` field in an appservice transaction
# user ID -> {device ID -> {algorithm -> count}} # user ID -> {device ID -> {algorithm -> count}}
TransactionOneTimeKeyCounts = Dict[str, Dict[str, Dict[str, int]]] TransactionOneTimeKeysCount = Dict[str, Dict[str, Dict[str, int]]]
# Type for the `device_unused_fallback_key_types` field in an appservice transaction # Type for the `device_unused_fallback_key_types` field in an appservice transaction
# user ID -> {device ID -> [algorithm]} # user ID -> {device ID -> [algorithm]}
@ -376,7 +376,7 @@ class AppServiceTransaction:
events: List[EventBase], events: List[EventBase],
ephemeral: List[JsonDict], ephemeral: List[JsonDict],
to_device_messages: List[JsonDict], to_device_messages: List[JsonDict],
one_time_key_counts: TransactionOneTimeKeyCounts, one_time_keys_count: TransactionOneTimeKeysCount,
unused_fallback_keys: TransactionUnusedFallbackKeys, unused_fallback_keys: TransactionUnusedFallbackKeys,
device_list_summary: DeviceListUpdates, device_list_summary: DeviceListUpdates,
): ):
@ -385,7 +385,7 @@ class AppServiceTransaction:
self.events = events self.events = events
self.ephemeral = ephemeral self.ephemeral = ephemeral
self.to_device_messages = to_device_messages self.to_device_messages = to_device_messages
self.one_time_key_counts = one_time_key_counts self.one_time_keys_count = one_time_keys_count
self.unused_fallback_keys = unused_fallback_keys self.unused_fallback_keys = unused_fallback_keys
self.device_list_summary = device_list_summary self.device_list_summary = device_list_summary
@ -402,7 +402,7 @@ class AppServiceTransaction:
events=self.events, events=self.events,
ephemeral=self.ephemeral, ephemeral=self.ephemeral,
to_device_messages=self.to_device_messages, to_device_messages=self.to_device_messages,
one_time_key_counts=self.one_time_key_counts, one_time_keys_count=self.one_time_keys_count,
unused_fallback_keys=self.unused_fallback_keys, unused_fallback_keys=self.unused_fallback_keys,
device_list_summary=self.device_list_summary, device_list_summary=self.device_list_summary,
txn_id=self.id, txn_id=self.id,

View File

@ -23,7 +23,7 @@ from synapse.api.constants import EventTypes, Membership, ThirdPartyEntityKind
from synapse.api.errors import CodeMessageException from synapse.api.errors import CodeMessageException
from synapse.appservice import ( from synapse.appservice import (
ApplicationService, ApplicationService,
TransactionOneTimeKeyCounts, TransactionOneTimeKeysCount,
TransactionUnusedFallbackKeys, TransactionUnusedFallbackKeys,
) )
from synapse.events import EventBase from synapse.events import EventBase
@ -262,7 +262,7 @@ class ApplicationServiceApi(SimpleHttpClient):
events: List[EventBase], events: List[EventBase],
ephemeral: List[JsonDict], ephemeral: List[JsonDict],
to_device_messages: List[JsonDict], to_device_messages: List[JsonDict],
one_time_key_counts: TransactionOneTimeKeyCounts, one_time_keys_count: TransactionOneTimeKeysCount,
unused_fallback_keys: TransactionUnusedFallbackKeys, unused_fallback_keys: TransactionUnusedFallbackKeys,
device_list_summary: DeviceListUpdates, device_list_summary: DeviceListUpdates,
txn_id: Optional[int] = None, txn_id: Optional[int] = None,
@ -310,10 +310,13 @@ class ApplicationServiceApi(SimpleHttpClient):
# TODO: Update to stable prefixes once MSC3202 completes FCP merge # TODO: Update to stable prefixes once MSC3202 completes FCP merge
if service.msc3202_transaction_extensions: if service.msc3202_transaction_extensions:
if one_time_key_counts: if one_time_keys_count:
body[ body[
"org.matrix.msc3202.device_one_time_key_counts" "org.matrix.msc3202.device_one_time_key_counts"
] = one_time_key_counts ] = one_time_keys_count
body[
"org.matrix.msc3202.device_one_time_keys_count"
] = one_time_keys_count
if unused_fallback_keys: if unused_fallback_keys:
body[ body[
"org.matrix.msc3202.device_unused_fallback_key_types" "org.matrix.msc3202.device_unused_fallback_key_types"

View File

@ -64,7 +64,7 @@ from typing import (
from synapse.appservice import ( from synapse.appservice import (
ApplicationService, ApplicationService,
ApplicationServiceState, ApplicationServiceState,
TransactionOneTimeKeyCounts, TransactionOneTimeKeysCount,
TransactionUnusedFallbackKeys, TransactionUnusedFallbackKeys,
) )
from synapse.appservice.api import ApplicationServiceApi from synapse.appservice.api import ApplicationServiceApi
@ -258,7 +258,7 @@ class _ServiceQueuer:
): ):
return return
one_time_key_counts: Optional[TransactionOneTimeKeyCounts] = None one_time_keys_count: Optional[TransactionOneTimeKeysCount] = None
unused_fallback_keys: Optional[TransactionUnusedFallbackKeys] = None unused_fallback_keys: Optional[TransactionUnusedFallbackKeys] = None
if ( if (
@ -269,7 +269,7 @@ class _ServiceQueuer:
# for the users which are mentioned in this transaction, # for the users which are mentioned in this transaction,
# as well as the appservice's sender. # as well as the appservice's sender.
( (
one_time_key_counts, one_time_keys_count,
unused_fallback_keys, unused_fallback_keys,
) = await self._compute_msc3202_otk_counts_and_fallback_keys( ) = await self._compute_msc3202_otk_counts_and_fallback_keys(
service, events, ephemeral, to_device_messages_to_send service, events, ephemeral, to_device_messages_to_send
@ -281,7 +281,7 @@ class _ServiceQueuer:
events, events,
ephemeral, ephemeral,
to_device_messages_to_send, to_device_messages_to_send,
one_time_key_counts, one_time_keys_count,
unused_fallback_keys, unused_fallback_keys,
device_list_summary, device_list_summary,
) )
@ -296,7 +296,7 @@ class _ServiceQueuer:
events: Iterable[EventBase], events: Iterable[EventBase],
ephemerals: Iterable[JsonDict], ephemerals: Iterable[JsonDict],
to_device_messages: Iterable[JsonDict], to_device_messages: Iterable[JsonDict],
) -> Tuple[TransactionOneTimeKeyCounts, TransactionUnusedFallbackKeys]: ) -> Tuple[TransactionOneTimeKeysCount, TransactionUnusedFallbackKeys]:
""" """
Given a list of the events, ephemeral messages and to-device messages, Given a list of the events, ephemeral messages and to-device messages,
- first computes a list of application services users that may have - first computes a list of application services users that may have
@ -367,7 +367,7 @@ class _TransactionController:
events: List[EventBase], events: List[EventBase],
ephemeral: Optional[List[JsonDict]] = None, ephemeral: Optional[List[JsonDict]] = None,
to_device_messages: Optional[List[JsonDict]] = None, to_device_messages: Optional[List[JsonDict]] = None,
one_time_key_counts: Optional[TransactionOneTimeKeyCounts] = None, one_time_keys_count: Optional[TransactionOneTimeKeysCount] = None,
unused_fallback_keys: Optional[TransactionUnusedFallbackKeys] = None, unused_fallback_keys: Optional[TransactionUnusedFallbackKeys] = None,
device_list_summary: Optional[DeviceListUpdates] = None, device_list_summary: Optional[DeviceListUpdates] = None,
) -> None: ) -> None:
@ -380,7 +380,7 @@ class _TransactionController:
events: The persistent events to include in the transaction. events: The persistent events to include in the transaction.
ephemeral: The ephemeral events to include in the transaction. ephemeral: The ephemeral events to include in the transaction.
to_device_messages: The to-device messages to include in the transaction. to_device_messages: The to-device messages to include in the transaction.
one_time_key_counts: Counts of remaining one-time keys for relevant one_time_keys_count: Counts of remaining one-time keys for relevant
appservice devices in the transaction. appservice devices in the transaction.
unused_fallback_keys: Lists of unused fallback keys for relevant unused_fallback_keys: Lists of unused fallback keys for relevant
appservice devices in the transaction. appservice devices in the transaction.
@ -397,7 +397,7 @@ class _TransactionController:
events=events, events=events,
ephemeral=ephemeral or [], ephemeral=ephemeral or [],
to_device_messages=to_device_messages or [], to_device_messages=to_device_messages or [],
one_time_key_counts=one_time_key_counts or {}, one_time_keys_count=one_time_keys_count or {},
unused_fallback_keys=unused_fallback_keys or {}, unused_fallback_keys=unused_fallback_keys or {},
device_list_summary=device_list_summary or DeviceListUpdates(), device_list_summary=device_list_summary or DeviceListUpdates(),
) )

View File

@ -1426,14 +1426,14 @@ class SyncHandler:
logger.debug("Fetching OTK data") logger.debug("Fetching OTK data")
device_id = sync_config.device_id device_id = sync_config.device_id
one_time_key_counts: JsonDict = {} one_time_keys_count: JsonDict = {}
unused_fallback_key_types: List[str] = [] unused_fallback_key_types: List[str] = []
if device_id: if device_id:
# TODO: We should have a way to let clients differentiate between the states of: # TODO: We should have a way to let clients differentiate between the states of:
# * no change in OTK count since the provided since token # * no change in OTK count since the provided since token
# * the server has zero OTKs left for this device # * the server has zero OTKs left for this device
# Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298 # Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
one_time_key_counts = await self.store.count_e2e_one_time_keys( one_time_keys_count = await self.store.count_e2e_one_time_keys(
user_id, device_id user_id, device_id
) )
unused_fallback_key_types = ( unused_fallback_key_types = (
@ -1463,7 +1463,7 @@ class SyncHandler:
archived=sync_result_builder.archived, archived=sync_result_builder.archived,
to_device=sync_result_builder.to_device, to_device=sync_result_builder.to_device,
device_lists=device_lists, device_lists=device_lists,
device_one_time_keys_count=one_time_key_counts, device_one_time_keys_count=one_time_keys_count,
device_unused_fallback_key_types=unused_fallback_key_types, device_unused_fallback_key_types=unused_fallback_key_types,
next_batch=sync_result_builder.now_token, next_batch=sync_result_builder.now_token,
) )

View File

@ -20,7 +20,7 @@ from synapse.appservice import (
ApplicationService, ApplicationService,
ApplicationServiceState, ApplicationServiceState,
AppServiceTransaction, AppServiceTransaction,
TransactionOneTimeKeyCounts, TransactionOneTimeKeysCount,
TransactionUnusedFallbackKeys, TransactionUnusedFallbackKeys,
) )
from synapse.config.appservice import load_appservices from synapse.config.appservice import load_appservices
@ -260,7 +260,7 @@ class ApplicationServiceTransactionWorkerStore(
events: List[EventBase], events: List[EventBase],
ephemeral: List[JsonDict], ephemeral: List[JsonDict],
to_device_messages: List[JsonDict], to_device_messages: List[JsonDict],
one_time_key_counts: TransactionOneTimeKeyCounts, one_time_keys_count: TransactionOneTimeKeysCount,
unused_fallback_keys: TransactionUnusedFallbackKeys, unused_fallback_keys: TransactionUnusedFallbackKeys,
device_list_summary: DeviceListUpdates, device_list_summary: DeviceListUpdates,
) -> AppServiceTransaction: ) -> AppServiceTransaction:
@ -273,7 +273,7 @@ class ApplicationServiceTransactionWorkerStore(
events: A list of persistent events to put in the transaction. events: A list of persistent events to put in the transaction.
ephemeral: A list of ephemeral events to put in the transaction. ephemeral: A list of ephemeral events to put in the transaction.
to_device_messages: A list of to-device messages to put in the transaction. to_device_messages: A list of to-device messages to put in the transaction.
one_time_key_counts: Counts of remaining one-time keys for relevant one_time_keys_count: Counts of remaining one-time keys for relevant
appservice devices in the transaction. appservice devices in the transaction.
unused_fallback_keys: Lists of unused fallback keys for relevant unused_fallback_keys: Lists of unused fallback keys for relevant
appservice devices in the transaction. appservice devices in the transaction.
@ -299,7 +299,7 @@ class ApplicationServiceTransactionWorkerStore(
events=events, events=events,
ephemeral=ephemeral, ephemeral=ephemeral,
to_device_messages=to_device_messages, to_device_messages=to_device_messages,
one_time_key_counts=one_time_key_counts, one_time_keys_count=one_time_keys_count,
unused_fallback_keys=unused_fallback_keys, unused_fallback_keys=unused_fallback_keys,
device_list_summary=device_list_summary, device_list_summary=device_list_summary,
) )
@ -379,7 +379,7 @@ class ApplicationServiceTransactionWorkerStore(
events=events, events=events,
ephemeral=[], ephemeral=[],
to_device_messages=[], to_device_messages=[],
one_time_key_counts={}, one_time_keys_count={},
unused_fallback_keys={}, unused_fallback_keys={},
device_list_summary=DeviceListUpdates(), device_list_summary=DeviceListUpdates(),
) )

View File

@ -33,7 +33,7 @@ from typing_extensions import Literal
from synapse.api.constants import DeviceKeyAlgorithms from synapse.api.constants import DeviceKeyAlgorithms
from synapse.appservice import ( from synapse.appservice import (
TransactionOneTimeKeyCounts, TransactionOneTimeKeysCount,
TransactionUnusedFallbackKeys, TransactionUnusedFallbackKeys,
) )
from synapse.logging.opentracing import log_kv, set_tag, trace from synapse.logging.opentracing import log_kv, set_tag, trace
@ -514,7 +514,7 @@ class EndToEndKeyWorkerStore(EndToEndKeyBackgroundStore, CacheInvalidationWorker
async def count_bulk_e2e_one_time_keys_for_as( async def count_bulk_e2e_one_time_keys_for_as(
self, user_ids: Collection[str] self, user_ids: Collection[str]
) -> TransactionOneTimeKeyCounts: ) -> TransactionOneTimeKeysCount:
""" """
Counts, in bulk, the one-time keys for all the users specified. Counts, in bulk, the one-time keys for all the users specified.
Intended to be used by application services for populating OTK counts in Intended to be used by application services for populating OTK counts in
@ -528,7 +528,7 @@ class EndToEndKeyWorkerStore(EndToEndKeyBackgroundStore, CacheInvalidationWorker
def _count_bulk_e2e_one_time_keys_txn( def _count_bulk_e2e_one_time_keys_txn(
txn: LoggingTransaction, txn: LoggingTransaction,
) -> TransactionOneTimeKeyCounts: ) -> TransactionOneTimeKeysCount:
user_in_where_clause, user_parameters = make_in_list_sql_clause( user_in_where_clause, user_parameters = make_in_list_sql_clause(
self.database_engine, "user_id", user_ids self.database_engine, "user_id", user_ids
) )
@ -541,7 +541,7 @@ class EndToEndKeyWorkerStore(EndToEndKeyBackgroundStore, CacheInvalidationWorker
""" """
txn.execute(sql, user_parameters) txn.execute(sql, user_parameters)
result: TransactionOneTimeKeyCounts = {} result: TransactionOneTimeKeysCount = {}
for user_id, device_id, algorithm, count in txn: for user_id, device_id, algorithm, count in txn:
# We deliberately construct empty dictionaries for # We deliberately construct empty dictionaries for

View File

@ -69,7 +69,7 @@ class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
events=events, events=events,
ephemeral=[], ephemeral=[],
to_device_messages=[], # txn made and saved to_device_messages=[], # txn made and saved
one_time_key_counts={}, one_time_keys_count={},
unused_fallback_keys={}, unused_fallback_keys={},
device_list_summary=DeviceListUpdates(), device_list_summary=DeviceListUpdates(),
) )
@ -96,7 +96,7 @@ class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
events=events, events=events,
ephemeral=[], ephemeral=[],
to_device_messages=[], # txn made and saved to_device_messages=[], # txn made and saved
one_time_key_counts={}, one_time_keys_count={},
unused_fallback_keys={}, unused_fallback_keys={},
device_list_summary=DeviceListUpdates(), device_list_summary=DeviceListUpdates(),
) )
@ -125,7 +125,7 @@ class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
events=events, events=events,
ephemeral=[], ephemeral=[],
to_device_messages=[], to_device_messages=[],
one_time_key_counts={}, one_time_keys_count={},
unused_fallback_keys={}, unused_fallback_keys={},
device_list_summary=DeviceListUpdates(), device_list_summary=DeviceListUpdates(),
) )

View File

@ -25,7 +25,7 @@ import synapse.storage
from synapse.api.constants import EduTypes, EventTypes from synapse.api.constants import EduTypes, EventTypes
from synapse.appservice import ( from synapse.appservice import (
ApplicationService, ApplicationService,
TransactionOneTimeKeyCounts, TransactionOneTimeKeysCount,
TransactionUnusedFallbackKeys, TransactionUnusedFallbackKeys,
) )
from synapse.handlers.appservice import ApplicationServicesHandler from synapse.handlers.appservice import ApplicationServicesHandler
@ -1123,7 +1123,7 @@ class ApplicationServicesHandlerOtkCountsTestCase(unittest.HomeserverTestCase):
# Capture what was sent as an AS transaction. # Capture what was sent as an AS transaction.
self.send_mock.assert_called() self.send_mock.assert_called()
last_args, _last_kwargs = self.send_mock.call_args last_args, _last_kwargs = self.send_mock.call_args
otks: Optional[TransactionOneTimeKeyCounts] = last_args[self.ARG_OTK_COUNTS] otks: Optional[TransactionOneTimeKeysCount] = last_args[self.ARG_OTK_COUNTS]
unused_fallbacks: Optional[TransactionUnusedFallbackKeys] = last_args[ unused_fallbacks: Optional[TransactionUnusedFallbackKeys] = last_args[
self.ARG_FALLBACK_KEYS self.ARG_FALLBACK_KEYS
] ]