Skip to content
Snippets Groups Projects
indexer.rs 1.35 KiB
Newer Older
pub use graphql_client::{reqwest::post_graphql, GraphQLQuery};

use anyhow::Result;

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "res/indexer-schema.json",
    query_path = "res/indexer-queries.graphql"
)]
pub struct IdentityNameByPubkey;

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "res/indexer-schema.json",
    query_path = "res/indexer-queries.graphql"
)]
pub struct IdentityPubkeyByName;

pub struct Indexer<'a> {
    pub gql_client: reqwest::Client,
    pub gql_url: &'a str,
}

impl<'a> Indexer<'a> {
    pub async fn username_by_pubkey(&self, pubkey: &str) -> Result<Option<String>> {
        Ok(post_graphql::<IdentityNameByPubkey, _>(
            &self.gql_client,
            self.gql_url,
            identity_name_by_pubkey::Variables {
                pubkey: pubkey.to_string(),
            },
        )
        .await?
        .data
        .and_then(|data| data.identity.into_iter().next().map(|idty| idty.name)))
    }

    pub async fn pubkey_by_username(&self, username: &str) -> Result<Option<String>> {
        Ok(post_graphql::<IdentityPubkeyByName, _>(
            &self.gql_client,
            self.gql_url,
            identity_pubkey_by_name::Variables {
                name: username.to_string(),
            },
        )
        .await?
        .data
        .and_then(|data| data.identity_by_pk.map(|idty| idty.pubkey)))
    }
}