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

Fetch Group & Message by id & InboxId from address #861

Merged
merged 5 commits into from
Jun 24, 2024
Merged
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
31 changes: 29 additions & 2 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ impl FfiXmtpClient {
})
}

pub fn group(&self, group_id: Vec<u8>) -> Result<FfiGroup, GenericError> {
let convo = self.inner_client.group(group_id)?;
Ok(FfiGroup {
inner_client: self.inner_client.clone(),
group_id: convo.group_id,
created_at_ns: convo.created_at_ns,
})
}

pub async fn can_message(
&self,
account_addresses: Vec<String>,
Expand All @@ -264,6 +273,13 @@ impl FfiXmtpClient {
pub async fn db_reconnect(&self) -> Result<(), GenericError> {
Ok(self.inner_client.reconnect_db()?)
}

pub async fn find_inbox_id(&self, address: String) -> Result<Option<String>, GenericError> {
let inner = self.inner_client.as_ref();

let result = inner.find_inbox_id_from_address(address).await?;
Ok(result)
}
}

#[uniffi::export(async_runtime = "tokio")]
Expand Down Expand Up @@ -524,6 +540,17 @@ impl FfiGroup {
Ok(())
}

pub async fn message(&self, message_id: Vec<u8>) -> Result<FfiMessage, GenericError> {
let group = MlsGroup::new(
self.inner_client.context().clone(),
self.group_id.clone(),
self.created_at_ns,
);

let message = group.message(message_id)?;
Ok(message.into())
}

pub fn find_messages(
&self,
opts: FfiListMessagesOptions,
Expand Down Expand Up @@ -1575,7 +1602,7 @@ mod tests {
.unwrap();
assert_eq!(bo_messages2.len(), second_msg_check);

// TODO: message_callbacks should eventually come through here, why does this
// TODO: message_callbacks should eventually come through here, why does this
// not work?
// tokio::time::sleep(tokio::time::Duration::from_millis(10000)).await;
// assert_eq!(message_callbacks.message_count(), second_msg_check as u32);
Expand Down Expand Up @@ -1860,7 +1887,7 @@ mod tests {

alix_group.send("hello1".as_bytes().to_vec()).await.unwrap();
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;

assert_eq!(group_callbacks.message_count(), 1);
assert_eq!(message_callbacks.message_count(), 1);

Expand Down
8 changes: 8 additions & 0 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ where
self.context.inbox_id()
}

pub async fn find_inbox_id_from_address(
&self,
address: String,
) -> Result<Option<String>, ClientError> {
let mut results = self.api_client.get_inbox_ids(vec![address.clone()]).await?;
Ok(results.remove(&address))
}

/// Get sequence id, may not be consistent with the backend
pub fn inbox_sequence_id(&self, conn: &DbConnection) -> Result<i64, StorageError> {
self.context.inbox_sequence_id(conn)
Expand Down
16 changes: 15 additions & 1 deletion xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use crate::{
group::{GroupMembershipState, Purpose, StoredGroup},
group_intent::{IntentKind, NewGroupIntent},
group_message::{DeliveryStatus, GroupMessageKind, StoredGroupMessage},
sql_key_store,
sql_key_store, StorageError,
},
utils::{id::calculate_message_id, time::now_ns},
xmtp_openmls_provider::XmtpOpenMlsProvider,
Expand Down Expand Up @@ -487,6 +487,20 @@ impl MlsGroup {
Ok(messages)
}

/// Look up a message by its ID
/// Returns a [`StoredGroupMessage`] if the message exists, or an error if it does not
pub fn message(&self, message_id: Vec<u8>) -> Result<StoredGroupMessage, GroupError> {
let conn = self.context.store.conn()?;
let message = conn.get_group_message(&message_id)?;
match message {
Some(message) => Ok(message),
None => Err(GroupError::Storage(StorageError::NotFound(format!(
"message {}",
hex::encode(message_id)
)))),
}
}

/**
* Add members to the group by account address
*
Expand Down
Loading