From fd30b9cccee0540a1c7a344ba0b9545b4ab99dcb Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 13 Feb 2025 19:46:17 -0800 Subject: [PATCH 1/3] add the wasm and node bindings for the new conversation list --- bindings_node/src/conversation.rs | 16 ++++++++++++++ bindings_node/src/conversations.rs | 31 +++++++++++++++++++-------- bindings_wasm/src/conversation.rs | 16 ++++++++++++++ bindings_wasm/src/conversations.rs | 34 ++++++++++++++---------------- 4 files changed, 70 insertions(+), 27 deletions(-) diff --git a/bindings_node/src/conversation.rs b/bindings_node/src/conversation.rs index 83577ceba..71d3c5786 100644 --- a/bindings_node/src/conversation.rs +++ b/bindings_node/src/conversation.rs @@ -105,6 +105,22 @@ pub struct Conversation { created_at_ns: i64, } +#[napi] +pub struct ConversationListItem { + conversation: Conversation, + last_message: Option, +} + +#[napi] +impl ConversationListItem { + pub fn conversation(&self) -> Arc { + Arc::new(self.conversation.clone()) + } + pub fn last_message(&self) -> Option { + self.last_message.clone() + } +} + impl From> for Conversation { fn from(mls_group: MlsGroup) -> Self { Conversation { diff --git a/bindings_node/src/conversations.rs b/bindings_node/src/conversations.rs index 05d2a8756..4dde88358 100644 --- a/bindings_node/src/conversations.rs +++ b/bindings_node/src/conversations.rs @@ -333,20 +333,30 @@ impl Conversations { } #[napi] - pub fn list(&self, opts: Option) -> Result> { - let convo_list: Vec = self - .inner_client - .find_groups(opts.unwrap_or_default().into()) - .map_err(ErrorWrapper::from)? + pub fn list( + &self, + opts: Option, + ) -> Result>> { + let convo_list: Vec> = inner + .list_conversations(opts.into())? .into_iter() - .map(Conversation::from) + .map(|conversation_item| { + Arc::new(ConversationListItem { + conversation: conversation_item.group.into(), + last_message: conversation_item + .last_message + .map(|stored_message| stored_message.into()), + }) + }) .collect(); - Ok(convo_list) } #[napi] - pub fn list_groups(&self, opts: Option) -> Result> { + pub fn list_groups( + &self, + opts: Option, + ) -> Result>> { self.list(Some(ListConversationsOptions { conversation_type: Some(ConversationType::Group), ..opts.unwrap_or_default() @@ -354,7 +364,10 @@ impl Conversations { } #[napi] - pub fn list_dms(&self, opts: Option) -> Result> { + pub fn list_dms( + &self, + opts: Option, + ) -> Result>> { self.list(Some(ListConversationsOptions { conversation_type: Some(ConversationType::Dm), ..opts.unwrap_or_default() diff --git a/bindings_wasm/src/conversation.rs b/bindings_wasm/src/conversation.rs index e2daf7769..7c706c404 100644 --- a/bindings_wasm/src/conversation.rs +++ b/bindings_wasm/src/conversation.rs @@ -114,6 +114,22 @@ pub struct Conversation { created_at_ns: i64, } +#[wasm_bindgen] +pub struct ConversationListItem { + conversation: Conversation, + last_message: Option, +} + +#[wasm_bindgen] +impl ConversationListItem { + pub fn conversation(&self) -> Arc { + Arc::new(self.conversation.clone()) + } + pub fn last_message(&self) -> Option { + self.last_message.clone() + } +} + impl Conversation { pub fn new(inner_client: Arc, group_id: Vec, created_at_ns: i64) -> Self { Self { diff --git a/bindings_wasm/src/conversations.rs b/bindings_wasm/src/conversations.rs index 831730475..6cce9a9fd 100644 --- a/bindings_wasm/src/conversations.rs +++ b/bindings_wasm/src/conversations.rs @@ -384,29 +384,27 @@ impl Conversations { } #[wasm_bindgen] - pub fn list(&self, opts: Option) -> Result { - let convo_list: js_sys::Array = self + pub fn list(&self, opts: Option) -> Result { + let convo_list = Array::new(); + + let conversations = self .inner_client - .find_groups(opts.unwrap_or_default().into()) - .map_err(|e| JsError::new(format!("{}", e).as_str()))? - .into_iter() - .map(|group| { - JsValue::from(Conversation::new( - self.inner_client.clone(), - group.group_id, - group.created_at_ns, - )) - }) - .collect(); + .list_conversations(opts.unwrap_or_default().into()) + .map_err(|e| JsError::new(&e.to_string()))?; + + for conversation_item in conversations { + let item = ConversationListItem::new( + conversation_item.group.into(), + conversation_item.last_message.map(|msg| msg.into()), + ); + convo_list.push(&JsValue::from(item)); + } Ok(convo_list) } #[wasm_bindgen(js_name = listGroups)] - pub fn list_groups( - &self, - opts: Option, - ) -> Result { + pub fn list_groups(&self, opts: Option) -> Result { self.list(Some(ListConversationsOptions { conversation_type: Some(ConversationType::Group), ..opts.unwrap_or_default() @@ -414,7 +412,7 @@ impl Conversations { } #[wasm_bindgen(js_name = listDms)] - pub fn list_dms(&self, opts: Option) -> Result { + pub fn list_dms(&self, opts: Option) -> Result { self.list(Some(ListConversationsOptions { conversation_type: Some(ConversationType::Dm), ..opts.unwrap_or_default() From 1a53c3b4a3d8a89734e50e34b04ea53b21b42e60 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 13 Feb 2025 19:51:22 -0800 Subject: [PATCH 2/3] add consent state filtering for list --- bindings_node/src/conversations.rs | 7 ++++++- bindings_wasm/src/conversations.rs | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/bindings_node/src/conversations.rs b/bindings_node/src/conversations.rs index 4dde88358..3d7127553 100644 --- a/bindings_node/src/conversations.rs +++ b/bindings_node/src/conversations.rs @@ -18,7 +18,10 @@ use crate::conversation::MessageDisappearingSettings; use crate::message::Message; use crate::permissions::{GroupPermissionsOptions, PermissionPolicySet}; use crate::ErrorWrapper; -use crate::{client::RustXmtpClient, conversation::Conversation, streams::StreamCloser}; +use crate::{ + client::RustXmtpClient, consent_state::ConsentState, conversation::Conversation, + streams::StreamCloser, +}; #[napi] #[derive(Debug)] @@ -84,6 +87,7 @@ pub struct ListConversationsOptions { pub created_before_ns: Option, pub limit: Option, pub conversation_type: Option, + pub consent_states: Option>, } impl From for GroupQueryArgs { @@ -98,6 +102,7 @@ impl From for GroupQueryArgs { .maybe_created_after_ns(opts.created_after_ns) .maybe_created_before_ns(opts.created_before_ns) .maybe_limit(opts.limit) + .maybe_consent_states(consent_states.map(|cs| cs.into())) } } diff --git a/bindings_wasm/src/conversations.rs b/bindings_wasm/src/conversations.rs index 6cce9a9fd..bf0a002ad 100644 --- a/bindings_wasm/src/conversations.rs +++ b/bindings_wasm/src/conversations.rs @@ -83,6 +83,8 @@ pub struct ListConversationsOptions { #[wasm_bindgen(js_name = createdBeforeNs)] pub created_before_ns: Option, pub limit: Option, + #[wasm_bindgen(js_name = consentState)] + pub consent_states: Option>, } impl From for GroupQueryArgs { @@ -97,6 +99,7 @@ impl From for GroupQueryArgs { .maybe_created_after_ns(opts.created_after_ns) .maybe_created_before_ns(opts.created_before_ns) .maybe_limit(opts.limit) + .maybe_consent_states(opts.consent_states.map(Into::into)) } } From 7c2819e808ce884e709ee13d113029e6bd3fc8e7 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 13 Feb 2025 19:57:56 -0800 Subject: [PATCH 3/3] add consent filter to syncAll --- bindings_node/src/conversations.rs | 11 ++++++++++- bindings_wasm/src/conversations.rs | 9 +++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/bindings_node/src/conversations.rs b/bindings_node/src/conversations.rs index 3d7127553..45e2e754f 100644 --- a/bindings_node/src/conversations.rs +++ b/bindings_node/src/conversations.rs @@ -322,12 +322,21 @@ impl Conversations { } #[napi] - pub async fn sync_all_conversations(&self) -> Result { + pub async fn sync_all_conversations( + &self, + consent_states: Option>, + ) -> Result { let provider = self .inner_client .mls_provider() .map_err(ErrorWrapper::from)?; + let consents: Option> = + consent_states.map(|states| states.into_iter().map(|state| state.into()).collect()); + let num_groups_synced: usize = inner + .sync_all_welcomes_and_groups(&provider, consents) + .await?; + let num_groups_synced = self .inner_client .sync_all_welcomes_and_groups(&provider, None) diff --git a/bindings_wasm/src/conversations.rs b/bindings_wasm/src/conversations.rs index bf0a002ad..45e53ac41 100644 --- a/bindings_wasm/src/conversations.rs +++ b/bindings_wasm/src/conversations.rs @@ -371,15 +371,20 @@ impl Conversations { } #[wasm_bindgen(js_name = syncAllConversations)] - pub async fn sync_all_conversations(&self) -> Result { + pub async fn sync_all_conversations( + &self, + consent_states: Option>, + ) -> Result { let provider = self .inner_client .mls_provider() .map_err(|e| JsError::new(format!("{}", e).as_str()))?; + let consents: Option> = + consent_states.map(|states| states.into_iter().map(|state| state.into()).collect()); let num_groups_synced = self .inner_client - .sync_all_welcomes_and_groups(&provider, None) + .sync_all_welcomes_and_groups(&provider, consents) .await .map_err(|e| JsError::new(format!("{}", e).as_str()))?;