Skip to content
Snippets Groups Projects
indexer.rs 1.18 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;

Hugo Trentesaux's avatar
Hugo Trentesaux committed
#[derive(Clone)]
pub struct Indexer {
	pub gql_client: reqwest::Client,
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	pub gql_url: String,
Hugo Trentesaux's avatar
Hugo Trentesaux committed
impl Indexer {
	pub async fn username_by_pubkey(&self, pubkey: &str) -> Result<Option<String>> {
		Ok(post_graphql::<IdentityNameByPubkey, _>(
			&self.gql_client,
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			&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,
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			self.gql_url.clone(),
			identity_pubkey_by_name::Variables {
				name: username.to_string(),
			},
		)
		.await?
		.data
		.and_then(|data| data.identity_by_pk.map(|idty| idty.pubkey)))
	}