Skip to content
Snippets Groups Projects
Commit def3411a authored by Éloïs's avatar Éloïs Committed by Pascal Engélibert
Browse files

[feat] gva: idty.isMember

parent 1e90d113
No related branches found
No related tags found
1 merge request!1350[feat] gva: idty.isMember
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::*;
impl DbsReader {
pub fn pubkey_is_member(&self, pubkey: PublicKey) -> KvResult<Option<bool>> {
Ok(self
.0
.gva_identities()
.get(&PubKeyKeyV2(pubkey))?
.map(|identity| identity.is_member))
}
}
#[cfg(test)]
mod tests {
use super::*;
use duniter_gva_db::GvaV1DbWritable;
#[test]
fn test_pubkey_is_member() -> KvResult<()> {
let gva_db = duniter_gva_db::GvaV1Db::<Mem>::open(MemConf::default())?;
let db_reader = create_dbs_reader(unsafe { std::mem::transmute(&gva_db.get_ro_handler()) });
let pk = PublicKey::default();
// Write test data
gva_db
.gva_identities_write()
.upsert(PubKeyKeyV2(pk), GvaIdtyDbV1::default())?;
// Test
assert_eq!(db_reader.pubkey_is_member(pk)?, Some(false));
gva_db.gva_identities_write().upsert(
PubKeyKeyV2(pk),
GvaIdtyDbV1 {
is_member: true,
..Default::default()
},
)?;
assert_eq!(db_reader.pubkey_is_member(pk)?, Some(true));
Ok(())
}
}
......@@ -24,6 +24,7 @@
pub mod current_frame;
pub mod find_inputs;
pub mod idty;
pub mod pagination;
pub mod txs_history;
pub mod uds_of_pubkey;
......
......@@ -14,6 +14,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pub mod block_gva;
pub mod idty_gva;
pub mod tx_gva;
pub mod ud_gva;
......
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#[derive(async_graphql::SimpleObject)]
pub(crate) struct Identity {
pub is_member: bool,
}
......@@ -35,6 +35,7 @@ pub use schema::{build_schema_with_data, get_schema_definition, GvaSchema, GvaSc
use crate::entities::{
block_gva::{Block, BlockMeta},
idty_gva::Identity,
tx_gva::TxGva,
ud_gva::{CurrentUdGva, RevalUdGva, UdGva},
AggregateSum, AmountWithBase, EdgeTx, PeerCardGva, RawTxOrChanges, Sum, TxDirection,
......@@ -147,6 +148,7 @@ mod tests {
txs_mp_db_ro: &TxsMpDb,
pubkey: PublicKey,
) -> KvResult<(Vec<TransactionDocumentV10>, Vec<TransactionDocumentV10>)>;
fn pubkey_is_member(&self, pubkey: PublicKey) -> KvResult<Option<bool>>;
fn unspent_uds_of_pubkey<BcDb: 'static + BcV2DbReadable>(
&self,
bc_db: &BcDb,
......
......@@ -17,6 +17,7 @@ pub mod account_balance;
pub mod current_block;
pub mod current_frame;
pub mod gen_tx;
pub mod idty;
pub mod txs_history;
pub mod uds;
pub mod utxos_of_script;
......@@ -31,6 +32,7 @@ pub struct QueryRoot(
queries::current_block::CurrentBlockQuery,
queries::current_frame::CurrentFrameQuery,
queries::gen_tx::GenTxsQuery,
queries::idty::IdtyQuery,
queries::txs_history::TxsHistoryBlockchainQuery,
queries::txs_history::TxsHistoryMempoolQuery,
queries::uds::UdsQuery,
......
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::*;
#[derive(Default)]
pub(crate) struct IdtyQuery;
#[async_graphql::Object]
impl IdtyQuery {
/// Get identity by public key
async fn idty(
&self,
ctx: &async_graphql::Context<'_>,
#[graphql(desc = "public key")] pubkey: String,
) -> async_graphql::Result<Identity> {
let pubkey = PublicKey::from_base58(&pubkey)?;
let data = ctx.data::<GvaSchemaData>()?;
let dbs_reader = data.dbs_reader();
let pubkey_is_member = data
.dbs_pool
.execute(move |_| dbs_reader.pubkey_is_member(pubkey))
.await??;
Ok(Identity {
is_member: pubkey_is_member.unwrap_or_default(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::*;
#[tokio::test]
async fn test_idty() -> anyhow::Result<()> {
let mut dbs_reader = MockDbsReader::new();
dbs_reader
.expect_pubkey_is_member()
.withf(|s| {
s == &PublicKey::from_base58("DnjL6hYA1k7FavGHbbir79PKQbmzw63d6bsamBBdUULP")
.expect("wrong pubkey")
})
.times(1)
.returning(|_| Ok(Some(true)));
let schema = create_schema(dbs_reader)?;
assert_eq!(
exec_graphql_request(
&schema,
r#"{ idty(pubkey: "DnjL6hYA1k7FavGHbbir79PKQbmzw63d6bsamBBdUULP") {isMember} }"#
)
.await?,
serde_json::json!({
"data": {
"idty": {
"isMember": true
}
}
})
);
Ok(())
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment