Skip to content

Commit

Permalink
Update node bindings (#1053)
Browse files Browse the repository at this point in the history
* Add inbox_state to client

* Refactor create_group to use create_group_with_members

* Bump node bindings version and update CHANGELOG
  • Loading branch information
rygine authored Sep 11, 2024
1 parent 4a78a26 commit 649912a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
5 changes: 5 additions & 0 deletions bindings_node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# @xmtp/mls-client-bindings-node

## 0.0.11

- Added `inbox_state` to client
- Skip duplicate message processing when streaming

## 0.0.10

- Fixed several group syncing issues
Expand Down
2 changes: 1 addition & 1 deletion bindings_node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xmtp/mls-client-bindings-node",
"version": "0.0.10",
"version": "0.0.11",
"repository": {
"type": "git",
"url": "git+https://[email protected]/xmtp/libxmtp.git",
Expand Down
24 changes: 15 additions & 9 deletions bindings_node/src/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct NapiListConversationsOptions {
}

#[napi(object)]
#[derive(Clone)]
pub struct NapiCreateGroupOptions {
pub permissions: Option<NapiGroupPermissionsOptions>,
pub group_name: Option<String>,
Expand Down Expand Up @@ -77,16 +78,21 @@ impl NapiConversations {
_ => None,
};

let convo = self
.inner_client
.create_group(group_permissions, options.into_group_metadata_options())
.map_err(|e| Error::from_reason(format!("ClientError: {}", e)))?;
if !account_addresses.is_empty() {
convo
.add_members(&self.inner_client, account_addresses)
let metadata_options = options.clone().into_group_metadata_options();

let convo = if account_addresses.is_empty() {
self
.inner_client
.create_group(group_permissions, metadata_options)
.map_err(|e| Error::from_reason(format!("ClientError: {}", e)))?
} else {
self
.inner_client
.create_group_with_members(account_addresses, group_permissions, metadata_options)
.await
.map_err(|e| Error::from_reason(format!("GroupError: {}", e)))?;
}
.map_err(|e| Error::from_reason(format!("ClientError: {}", e)))?
};

let out = NapiGroup::new(
self.inner_client.clone(),
convo.group_id,
Expand Down
41 changes: 40 additions & 1 deletion bindings_node/src/mls_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::ops::Deref;
use std::sync::Arc;
pub use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
use xmtp_cryptography::signature::ed25519_public_key_to_address;
use xmtp_id::associations::generate_inbox_id as xmtp_id_generate_inbox_id;
use xmtp_id::associations::unverified::UnverifiedSignature;
use xmtp_id::associations::{generate_inbox_id as xmtp_id_generate_inbox_id, AssociationState};
use xmtp_id::associations::{AccountId, MemberIdentifier};
use xmtp_mls::api::ApiClientWrapper;
use xmtp_mls::builder::ClientBuilder;
Expand All @@ -18,6 +18,29 @@ use xmtp_mls::Client as MlsClient;

pub type RustXmtpClient = MlsClient<TonicApiClient>;

#[napi(object)]
pub struct NapiInboxState {
pub inbox_id: String,
pub recovery_address: String,
pub installation_ids: Vec<String>,
pub account_addresses: Vec<String>,
}

impl From<AssociationState> for NapiInboxState {
fn from(state: AssociationState) -> Self {
Self {
inbox_id: state.inbox_id().to_string(),
recovery_address: state.recovery_address().to_string(),
installation_ids: state
.installation_ids()
.into_iter()
.map(|id| ed25519_public_key_to_address(id.as_slice()))
.collect(),
account_addresses: state.account_addresses(),
}
}
}

#[napi]
pub struct NapiClient {
inner_client: Arc<RustXmtpClient>,
Expand Down Expand Up @@ -273,4 +296,20 @@ impl NapiClient {

Ok(inbox_id)
}

/**
* Get the client's inbox state.
*
* If `refresh_from_network` is true, the client will go to the network first to refresh the state.
* Otherwise, the state will be read from the local database.
*/
#[napi]
pub async fn inbox_state(&self, refresh_from_network: bool) -> Result<NapiInboxState> {
let state = self
.inner_client
.inbox_state(refresh_from_network)
.await
.map_err(|e| Error::from_reason(format!("{}", e)))?;
Ok(state.into())
}
}
12 changes: 12 additions & 0 deletions bindings_node/test/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ describe('Client', () => {
const inboxId = await client.findInboxIdByAddress(user.account.address)
expect(inboxId).toBe(client.inboxId())
})

it('should return the correct inbox state', async () => {
const user = createUser()
const client = await createRegisteredClient(user)
const inboxState = await client.inboxState(false)
expect(inboxState.inboxId).toBe(client.inboxId())
expect(inboxState.installationIds).toEqual([client.installationId()])
expect(inboxState.accountAddresses).toEqual([
user.account.address.toLowerCase(),
])
expect(inboxState.recoveryAddress).toBe(user.account.address.toLowerCase())
})
})

0 comments on commit 649912a

Please sign in to comment.