From ed74aaf3a8ffe6737b17e86455326811c9e52f76 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Mon, 22 Feb 2021 14:51:50 -0300 Subject: [PATCH] feat(bindings): add messageCount API (#340) * feat(bindings): add messageCount API * fix: fmt --- .changes/message-count.md | 5 +++++ bindings/nodejs/lib/index.d.ts | 1 + bindings/nodejs/native/src/classes/account/mod.rs | 11 +++++++++++ bindings/nodejs/tests/account.js | 1 + bindings/python/README.md | 4 ++++ bindings/python/native/src/classes/account.rs | 5 +++++ docs/libraries/nodejs/api_reference.md | 4 ++++ 7 files changed, 31 insertions(+) create mode 100644 .changes/message-count.md diff --git a/.changes/message-count.md b/.changes/message-count.md new file mode 100644 index 000000000..9b3287dee --- /dev/null +++ b/.changes/message-count.md @@ -0,0 +1,5 @@ +--- +"nodejs-binding": patch +--- + +Adds a `messageCount` function on the Account class. diff --git a/bindings/nodejs/lib/index.d.ts b/bindings/nodejs/lib/index.d.ts index 2160e34bc..826ee667e 100644 --- a/bindings/nodejs/lib/index.d.ts +++ b/bindings/nodejs/lib/index.d.ts @@ -75,6 +75,7 @@ export declare class Account { index(): number; alias(): string; balance(): AccountBalance; + messageCount(): number; listMessages(count?: number, from?: number, messageType?: MessageType): Message[] listAddresses(unspent?: boolean): Address[] sync(options?: SyncOptions): Promise diff --git a/bindings/nodejs/native/src/classes/account/mod.rs b/bindings/nodejs/native/src/classes/account/mod.rs index bee34049a..6eb234135 100644 --- a/bindings/nodejs/native/src/classes/account/mod.rs +++ b/bindings/nodejs/native/src/classes/account/mod.rs @@ -70,6 +70,17 @@ declare_types! { Ok(neon_serde::to_value(&mut cx, &balance)?.upcast()) } + method messageCount(mut cx) { + let this = cx.this(); + let id = cx.borrow(&this, |r| r.0.clone()); + crate::block_on(async move { + let account_handle = crate::get_account(&id).await; + let account = account_handle.read().await; + let count = account.messages().len(); + Ok(cx.number(count as f64).upcast()) + }) + } + method listMessages(mut cx) { let count = match cx.argument_opt(0) { Some(arg) => arg.downcast::().or_throw(&mut cx)?.value() as usize, diff --git a/bindings/nodejs/tests/account.js b/bindings/nodejs/tests/account.js index e6e38f776..8768ce298 100644 --- a/bindings/nodejs/tests/account.js +++ b/bindings/nodejs/tests/account.js @@ -32,6 +32,7 @@ async function run() { } }) console.log('messages', account.listMessages(0, 0, MessageType.Failed)) + console.log(account.messageCount()) account.setAlias('new alias') const savedAccount = manager.getAccount('new alias') diff --git a/bindings/python/README.md b/bindings/python/README.md index 9740814f4..c42ad3af0 100644 --- a/bindings/python/README.md +++ b/bindings/python/README.md @@ -381,6 +381,10 @@ Updates the account's client options. | ------- | -------------------------------------------- | ---------------------- | ------------------------- | | options | [ClientOptions](#clientoptions) | undefined | The client options to set | +#### message_count(): int + +Returns the number of messages associated with the account. + #### list_messages(count, from, message_type (optional)): list([WalletMessage](#walletmessage)) Get the list of messages of this account. diff --git a/bindings/python/native/src/classes/account.rs b/bindings/python/native/src/classes/account.rs index 7ca9d20ba..d2cd841fd 100644 --- a/bindings/python/native/src/classes/account.rs +++ b/bindings/python/native/src/classes/account.rs @@ -189,6 +189,11 @@ impl AccountHandle { })?) } + /// The number of messages associated with the account. + fn message_count(&self) -> usize { + crate::block_on(async { self.account_handle.read().await.messages().len() }) + } + /// Bridge to [Account#list_messages](struct.Account.html#method.list_messages). /// This method clones the account's messages so when querying a large list of messages /// prefer using the `read` method to access the account instance. diff --git a/docs/libraries/nodejs/api_reference.md b/docs/libraries/nodejs/api_reference.md index d9f702c33..93240eb15 100644 --- a/docs/libraries/nodejs/api_reference.md +++ b/docs/libraries/nodejs/api_reference.md @@ -231,6 +231,10 @@ Returns the account's balance information object. Balance object: { total: number, available: number, incoming: number, outgoing: number } +#### messageCount() + +Returns the number of messages associated with the account. + #### listMessages([count, from, type]) Returns the account's messages.