-
Notifications
You must be signed in to change notification settings - Fork 336
feat(crypto): Add new encrypt_and_send_custom_to_device
to the client
#4998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2e746c0
feat(crypto): Add new `encrypt_and_send_custom_to_device` to the client
BillCarsonFr 2254e72
review: Gate feature behind experimental flag
BillCarsonFr 4451f83
review: fix doc + quick renaming
BillCarsonFr d875aa7
refactor: move encrypt_and_send_raw_to_device to encryption mod
BillCarsonFr c2de912
review: get rid of send_to_device_with_config
BillCarsonFr 06ef97f
changelog entry for SDK
BillCarsonFr c23fe55
Merge branch 'main' into valere/crypto/to_device_encrypt_helper
BillCarsonFr 230c958
fixup: missing PR link in changelog
BillCarsonFr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#[cfg(feature = "e2e-encryption")] | ||
use std::ops::Deref; | ||
use std::{ | ||
collections::{btree_map, BTreeMap}, | ||
fmt::{self, Debug}, | ||
|
@@ -37,8 +39,6 @@ use matrix_sdk_base::{ | |
StateStoreDataKey, StateStoreDataValue, SyncOutsideWasm, | ||
}; | ||
use matrix_sdk_common::ttl_cache::TtlCache; | ||
#[cfg(feature = "e2e-encryption")] | ||
use ruma::events::{room::encryption::RoomEncryptionEventContent, InitialStateEvent}; | ||
use ruma::{ | ||
api::{ | ||
client::{ | ||
|
@@ -69,6 +69,15 @@ use ruma::{ | |
DeviceId, OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, | ||
RoomAliasId, RoomId, RoomOrAliasId, ServerName, UInt, UserId, | ||
}; | ||
#[cfg(feature = "e2e-encryption")] | ||
use ruma::{ | ||
events::{ | ||
room::encryption::RoomEncryptionEventContent, AnyToDeviceEventContent, InitialStateEvent, | ||
}, | ||
serde::Raw, | ||
to_device::DeviceIdOrAllDevices, | ||
OwnedUserId, | ||
}; | ||
use serde::de::DeserializeOwned; | ||
use tokio::sync::{broadcast, Mutex, OnceCell, RwLock, RwLockReadGuard}; | ||
use tracing::{debug, error, instrument, trace, warn, Instrument, Span}; | ||
|
@@ -99,7 +108,9 @@ use crate::{ | |
}; | ||
#[cfg(feature = "e2e-encryption")] | ||
use crate::{ | ||
encryption::{Encryption, EncryptionData, EncryptionSettings, VerificationState}, | ||
encryption::{ | ||
identities::Device, Encryption, EncryptionData, EncryptionSettings, VerificationState, | ||
}, | ||
store_locks::CrossProcessStoreLock, | ||
}; | ||
|
||
|
@@ -2513,6 +2524,74 @@ impl Client { | |
let base_room = self.inner.base_client.room_knocked(&response.room_id).await?; | ||
Ok(Room::new(self.clone(), base_room)) | ||
} | ||
|
||
/// Encrypts then send the given content via the `sendToDevice` end-point | ||
BillCarsonFr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// using olm encryption. | ||
BillCarsonFr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// | ||
/// If there are a lot of targets this will be break down by chunks. | ||
BillCarsonFr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// | ||
/// # Returns | ||
/// A list of `ToDeviceRequest` to send out the event, and the list of | ||
/// devices where encryption did not succeed (device excluded or no olm) | ||
BillCarsonFr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
#[cfg(feature = "e2e-encryption")] | ||
pub async fn encrypt_and_send_custom_to_device( | ||
BillCarsonFr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
&self, | ||
targets: Vec<&Device>, | ||
|
||
event_type: &str, | ||
content: Raw<AnyToDeviceEventContent>, | ||
) -> Result<Vec<(OwnedUserId, OwnedDeviceId)>> { | ||
let users = targets.iter().map(|device| device.user_id()); | ||
|
||
// Will claim one-time-key for users that needs it | ||
// TODO: For later optimisation: This will establish missing olm sessions with | ||
// all this users devices, but we just want for some devices. | ||
self.claim_one_time_keys(users).await?; | ||
|
||
let olm = self.olm_machine().await; | ||
let olm = olm.as_ref().expect("Olm machine wasn't started"); | ||
|
||
let (requests, withhelds) = olm | ||
.encrypt_content_for_devices( | ||
targets.into_iter().map(|d| d.deref().clone()).collect(), | ||
event_type, | ||
&content | ||
.deserialize_as::<serde_json::Value>() | ||
.expect("Deserialize as Value will always work"), | ||
) | ||
.await?; | ||
|
||
let mut failures: Vec<(OwnedUserId, OwnedDeviceId)> = Default::default(); | ||
|
||
// Push the withhelds in the failures | ||
withhelds.iter().for_each(|(d, _)| { | ||
failures.push((d.user_id().to_owned(), d.device_id().to_owned())); | ||
}); | ||
|
||
// TODO: parallelize that? it's already grouping 250 devices per chunk. | ||
for request in requests { | ||
let send_result = | ||
self.send_to_device_with_config(&request, RequestConfig::short_retry()).await; | ||
|
||
// If the sending failed we need to collect the failures to report them | ||
if send_result.is_err() { | ||
// Mark the sending as failed | ||
for (user_id, device_map) in request.messages { | ||
for device_id in device_map.keys() { | ||
match device_id { | ||
DeviceIdOrAllDevices::DeviceId(device_id) => { | ||
failures.push((user_id.clone(), device_id.to_owned())); | ||
} | ||
DeviceIdOrAllDevices::AllDevices => { | ||
// Cannot happen in this case | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(failures) | ||
} | ||
} | ||
|
||
/// A weak reference to the inner client, useful when trying to get a handle | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.