claimKeys method
Claims one-time keys for use in pre-key messages.
The request contains the user ID, device ID and algorithm name of the keys that are required. If a key matching these requirements can be found, the response contains it. The returned key is a one-time key if one is available, and otherwise a fallback key.
One-time keys are given out in the order that they were uploaded via
/keys/upload. (All
keys uploaded within a given call to /keys/upload
are considered
equivalent in this regard; no ordering is specified within them.)
Servers must ensure that each one-time key is returned at most once, so when a key has been returned, no other request will ever return the same key.
oneTimeKeys
The keys to be claimed. A map from user ID, to a map from
device ID to algorithm name.
timeout
The time (in milliseconds) to wait when downloading keys from
remote servers. 10 seconds is the recommended default.
Implementation
Future<ClaimKeysResponse> claimKeys(
Map<String, Map<String, String>> oneTimeKeys, {
int? timeout,
}) async {
final requestUri = Uri(path: '_matrix/client/v3/keys/claim');
final request = Request('POST', baseUri!.resolveUri(requestUri));
request.headers['authorization'] = 'Bearer ${bearerToken!}';
request.headers['content-type'] = 'application/json';
request.bodyBytes = utf8.encode(
jsonEncode({
'one_time_keys': oneTimeKeys
.map((k, v) => MapEntry(k, v.map((k, v) => MapEntry(k, v)))),
if (timeout != null) 'timeout': timeout,
}),
);
final response = await httpClient.send(request);
final responseBody = await response.stream.toBytes();
if (response.statusCode != 200) unexpectedResponse(response, responseBody);
final responseString = utf8.decode(responseBody);
final json = jsonDecode(responseString);
return ClaimKeysResponse.fromJson(json as Map<String, Object?>);
}