Skip to content
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

Conversation and sync list updates for Node & Wasm Bindings #1629

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bindings_node/src/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ pub struct Conversation {
created_at_ns: i64,
}

#[napi]
pub struct ConversationListItem {
conversation: Conversation,
last_message: Option<Message>,
}

#[napi]
impl ConversationListItem {
pub fn conversation(&self) -> Arc<Conversation> {
Arc::new(self.conversation.clone())
}
pub fn last_message(&self) -> Option<Message> {
self.last_message.clone()
}
}

impl From<MlsGroup<RustXmtpClient>> for Conversation {
fn from(mls_group: MlsGroup<RustXmtpClient>) -> Self {
Conversation {
Expand Down
49 changes: 38 additions & 11 deletions bindings_node/src/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -84,6 +87,7 @@ pub struct ListConversationsOptions {
pub created_before_ns: Option<i64>,
pub limit: Option<i64>,
pub conversation_type: Option<ConversationType>,
pub consent_states: Option<Vec<ConsentState>>,
}

impl From<ListConversationsOptions> for GroupQueryArgs {
Expand All @@ -98,6 +102,7 @@ impl From<ListConversationsOptions> 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()))
}
}

Expand Down Expand Up @@ -317,12 +322,21 @@ impl Conversations {
}

#[napi]
pub async fn sync_all_conversations(&self) -> Result<usize> {
pub async fn sync_all_conversations(
&self,
consent_states: Option<Vec<ConsentState>>,
) -> Result<usize> {
let provider = self
.inner_client
.mls_provider()
.map_err(ErrorWrapper::from)?;

let consents: Option<Vec<ConsentState>> =
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)
Expand All @@ -333,28 +347,41 @@ impl Conversations {
}

#[napi]
pub fn list(&self, opts: Option<ListConversationsOptions>) -> Result<Vec<Conversation>> {
let convo_list: Vec<Conversation> = self
.inner_client
.find_groups(opts.unwrap_or_default().into())
.map_err(ErrorWrapper::from)?
pub fn list(
&self,
opts: Option<ListConversationsOptions>,
) -> Result<Vec<Arc<ConversationListItem>>> {
let convo_list: Vec<Arc<ConversationListItem>> = 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<ListConversationsOptions>) -> Result<Vec<Conversation>> {
pub fn list_groups(
&self,
opts: Option<ListConversationsOptions>,
) -> Result<Vec<Arc<ConversationListItem>>> {
self.list(Some(ListConversationsOptions {
conversation_type: Some(ConversationType::Group),
..opts.unwrap_or_default()
}))
}

#[napi]
pub fn list_dms(&self, opts: Option<ListConversationsOptions>) -> Result<Vec<Conversation>> {
pub fn list_dms(
&self,
opts: Option<ListConversationsOptions>,
) -> Result<Vec<Arc<ConversationListItem>>> {
self.list(Some(ListConversationsOptions {
conversation_type: Some(ConversationType::Dm),
..opts.unwrap_or_default()
Expand Down
16 changes: 16 additions & 0 deletions bindings_wasm/src/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ pub struct Conversation {
created_at_ns: i64,
}

#[wasm_bindgen]
pub struct ConversationListItem {
conversation: Conversation,
last_message: Option<Message>,
}

#[wasm_bindgen]
impl ConversationListItem {
pub fn conversation(&self) -> Arc<Conversation> {
Arc::new(self.conversation.clone())
}
pub fn last_message(&self) -> Option<Message> {
self.last_message.clone()
}
}

impl Conversation {
pub fn new(inner_client: Arc<RustXmtpClient>, group_id: Vec<u8>, created_at_ns: i64) -> Self {
Self {
Expand Down
46 changes: 26 additions & 20 deletions bindings_wasm/src/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub struct ListConversationsOptions {
#[wasm_bindgen(js_name = createdBeforeNs)]
pub created_before_ns: Option<i64>,
pub limit: Option<i64>,
#[wasm_bindgen(js_name = consentState)]
pub consent_states: Option<Vec<ConsentState>>,
}

impl From<ListConversationsOptions> for GroupQueryArgs {
Expand All @@ -97,6 +99,7 @@ impl From<ListConversationsOptions> 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))
}
}

Expand Down Expand Up @@ -368,53 +371,56 @@ impl Conversations {
}

#[wasm_bindgen(js_name = syncAllConversations)]
pub async fn sync_all_conversations(&self) -> Result<usize, JsError> {
pub async fn sync_all_conversations(
&self,
consent_states: Option<Vec<ConsentState>>,
) -> Result<usize, JsError> {
let provider = self
.inner_client
.mls_provider()
.map_err(|e| JsError::new(format!("{}", e).as_str()))?;

let consents: Option<Vec<ConsentState>> =
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()))?;

Ok(num_groups_synced)
}

#[wasm_bindgen]
pub fn list(&self, opts: Option<ListConversationsOptions>) -> Result<js_sys::Array, JsError> {
let convo_list: js_sys::Array = self
pub fn list(&self, opts: Option<ListConversationsOptions>) -> Result<Array, JsError> {
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<ListConversationsOptions>,
) -> Result<js_sys::Array, JsError> {
pub fn list_groups(&self, opts: Option<ListConversationsOptions>) -> Result<Array, JsError> {
self.list(Some(ListConversationsOptions {
conversation_type: Some(ConversationType::Group),
..opts.unwrap_or_default()
}))
}

#[wasm_bindgen(js_name = listDms)]
pub fn list_dms(&self, opts: Option<ListConversationsOptions>) -> Result<js_sys::Array, JsError> {
pub fn list_dms(&self, opts: Option<ListConversationsOptions>) -> Result<Array, JsError> {
self.list(Some(ListConversationsOptions {
conversation_type: Some(ConversationType::Dm),
..opts.unwrap_or_default()
Expand Down
Loading