Skip to content

Commit

Permalink
merge with latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Nov 15, 2023
1 parent 5be73f8 commit 2768918
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 50 deletions.
85 changes: 37 additions & 48 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,6 @@ where
}
}

/// Build this struct
/// # Arguments
/// * `strat`: the [`IdentityStrategy`] for this client
///
/// # Example
///
/// TODO: Fix this example
/// ```no_run
/// Client::builder()
/// .api_client(api_client)
/// .network(Network::Dev)
/// .build()
/// ```
pub fn builder<Owner: crate::InboxOwner>(
strat: IdentityStrategy<Owner>,
) -> ClientBuilder<ApiClient, Owner> {
ClientBuilder::new(strat)
}

// TODO: Remove this and figure out the correct lifetimes to allow long lived provider
pub fn mls_provider(&self, conn: &'a mut DbConnection) -> XmtpOpenMlsProvider<'a> {
XmtpOpenMlsProvider::new(conn)
Expand Down Expand Up @@ -141,8 +122,9 @@ where
pub async fn register_identity(&self) -> Result<(), ClientError> {
// TODO: Mark key package as last_resort in creation
let mut connection = self.store.conn()?;
let mls_provider = XmtpOpenMlsProvider::new(&mut connection);
let last_resort_kp = self.identity.new_key_package(&mls_provider)?;
let last_resort_kp = self
.identity
.new_key_package(&self.mls_provider(&mut connection))?;
let last_resort_kp_bytes = last_resort_kp.tls_serialize_detached()?;

self.api_client
Expand Down Expand Up @@ -221,32 +203,39 @@ where
pub async fn sync_welcomes(&self) -> Result<Vec<MlsGroup<ApiClient>>, ClientError> {
let welcome_topic = get_welcome_topic(&self.installation_public_key());
let mut conn = self.store.conn()?;
let provider = self.mls_provider();
// TODO: Use the last_message_timestamp_ns field on the TopicRefreshState to only fetch new messages
// Waiting for more atomic update methods
let envelopes = self.api_client.read_topic(&welcome_topic, 0).await?;

let groups: Vec<MlsGroup<ApiClient>> = envelopes
.into_iter()
.filter_map(|envelope| {
// TODO: Wrap in a transaction
let welcome = match extract_welcome(&envelope.message) {
Ok(welcome) => welcome,
Err(err) => {
log::error!("failed to extract welcome: {}", err);
return None;
}
};

// TODO: Update last_message_timestamp_ns on success or non-retryable error
// TODO: Abort if error is retryable
match MlsGroup::create_from_welcome(self, &mut conn, &provider, welcome) {
Ok(mls_group) => Some(mls_group),
Err(err) => {
log::error!("failed to create group from welcome: {}", err);
None
XmtpOpenMlsProvider::transaction(&mut conn, |provider| {
let welcome = match extract_welcome(&envelope.message) {
Ok(welcome) => welcome,
Err(err) => {
log::error!("failed to extract welcome: {}", err);
return Ok::<_, ClientError>(None);
}
};

// TODO: Update last_message_timestamp_ns on success or non-retryable error
// TODO: Abort if error is retryable
match MlsGroup::create_from_welcome(
self,
*provider.conn().borrow_mut(),
&provider,
welcome,
) {
Ok(mls_group) => Ok(Some(mls_group)),
Err(err) => {
log::error!("failed to create group from welcome: {}", err);
Ok(None)
}
}
}
})
.ok()
.flatten()
})
.collect();

Expand Down Expand Up @@ -277,7 +266,7 @@ fn extract_welcome(welcome_bytes: &Vec<u8>) -> Result<Welcome, ClientError> {
mod tests {
use xmtp_cryptography::utils::generate_local_wallet;

use crate::{builder::ClientBuilder, InboxOwner};
use crate::{builder::ClientBuilder, storage::EncryptedMessageStore, InboxOwner};

#[tokio::test]
async fn test_mls_error() {
Expand Down Expand Up @@ -331,16 +320,16 @@ mod tests {

// Manually mark as committed
// TODO: Replace with working synchronization once we can add members end to end
let intents = alice
.store
.find_group_intents(conn, alice_bob_group.group_id.clone(), None, None)
.unwrap();
let intents = EncryptedMessageStore::find_group_intents(
conn,
alice_bob_group.group_id.clone(),
None,
None,
)
.unwrap();
let intent = intents.first().unwrap();
// Set the intent to committed manually
alice
.store
.set_group_intent_committed(conn, intent.id)
.unwrap();
EncryptedMessageStore::set_group_intent_committed(conn, intent.id).unwrap();

alice_bob_group.post_commit(conn).await.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ where
}

pub fn process_messages(&self, envelopes: Vec<Envelope>) -> Result<(), GroupError> {
let provider = self.client.mls_provider();
let mut conn = self.client.store.conn()?;
let provider = self.client.mls_provider(&mut conn);
let mut openmls_group = self.load_mls_group(&provider)?;
let receive_errors: Vec<MessageProcessingError> = envelopes
.into_iter()
Expand Down
3 changes: 2 additions & 1 deletion xmtp_mls/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ mod tests {
#[test]
fn test_key_package_extensions() {
let store = EncryptedMessageStore::new_test();
let provider = XmtpOpenMlsProvider::new(&store);
let mut conn = store.conn().unwrap();
let provider = XmtpOpenMlsProvider::new(&mut conn);
let identity = Identity::new(&store, &provider, &generate_local_wallet()).unwrap();

let new_key_package = identity.new_key_package(&provider).unwrap();
Expand Down

0 comments on commit 2768918

Please sign in to comment.