Skip to content
Snippets Groups Projects
Select Git revision
  • 17d2f4d1d45b43152e0b274d68518629075fbe92
  • master default protected
  • elois-ci-refactor protected
  • gtest
  • hugo/gtest
  • json-output
  • nostr
  • 48-error-base-58-requirement-is-violated
  • no-rename
  • hugo/tx-comments
  • poka/dev
  • hugo/dev
  • tuxmain/mail
  • test-gtest
  • 0.4.3-gtest-RC1
  • 0.4.3-RC2
  • 0.4.3-RC1
  • 0.4.2
  • 0.4.1
  • 0.4.0
  • 0.3.0
  • 0.2.17
  • 0.2.16
  • 0.2.15
  • 0.2.14
  • 0.2.13
  • 0.2.12
  • 0.2.10
  • 0.2.9
  • 0.2.8
  • 0.2.7
  • 0.2.6
  • 0.2.5
33 results

indexer.rs

Blame
  • indexer.rs 6.68 KiB
    mod queries;
    
    use crate::*;
    use comfy_table::*;
    use comfy_table::{ContentArrangement, Table};
    use graphql_client::reqwest::post_graphql;
    use graphql_client::GraphQLQuery;
    use queries::*;
    // use sp_core::Bytes;
    
    #[derive(Clone, Debug)]
    pub struct Indexer {
    	pub gql_client: reqwest::Client,
    	pub gql_url: String,
    }
    
    impl Indexer {
    	/// graphql query without error management
    	async fn query<T: GraphQLQuery>(
    		&self,
    		var: <T as GraphQLQuery>::Variables,
    	) -> <T as GraphQLQuery>::ResponseData {
    		post_graphql::<T, _>(&self.gql_client, &self.gql_url, var)
    			.await
    			.expect("indexer connexion error")
    			.data
    			.expect("indexer error")
    	}
    
    	/// index → name
    	pub async fn username_by_index(&self, index: u32) -> Option<String> {
    		self.query::<IdentityNameByIndex>(identity_name_by_index::Variables {
    			index: index.into(),
    		})
    		.await
    		.identity
    		.pop()
    		.map(|idty| idty.name)
    	}
    
    	/// index → name (multiple)
    	pub async fn names_by_indexes(&self, indexes: &[IdtyId]) -> Vec<(IdtyId, String)> {
    		self.query::<NamesByIndexes>(names_by_indexes::Variables {
    			indexes: indexes.iter().map(|i| *i as i64).collect(),
    		})
    		.await
    		.identity
    		.into_iter()
    		.map(|idty| (idty.index as IdtyId, idty.name))
    		.collect()
    	}
    
    	/// pubkey → name
    	pub async fn username_by_pubkey(&self, pubkey: &str) -> Option<String> {
    		self.query::<IdentityNameByPubkey>(identity_name_by_pubkey::Variables {
    			pubkey: pubkey.to_string(),
    		})
    		.await
    		.identity
    		.pop()
    		.map(|idty| idty.name)
    	}
    
    	/// pubkey → was name
    	pub async fn wasname_by_pubkey(&self, pubkey: &str) -> Option<String> {
    		self.query::<WasIdentityNameByPubkey>(was_identity_name_by_pubkey::Variables {
    			pubkey: pubkey.to_string(),
    		})
    		.await
    		.account_by_pk