Skip to content
Snippets Groups Projects
Select Git revision
  • 020be31e5665117ba35109393f0eb7d4f1affd95
  • master default protected
  • json-output
  • nostr
  • 48-error-base-58-requirement-is-violated
  • no-rename
  • hugo/tx-comments
  • poka/dev
  • hugo/dev
  • tuxmain/mail
  • 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
  • 0.2.4
  • 0.2.3
30 results

cache.rs

Blame
  • cache.rs 1.18 KiB
    use crate::indexer::*;
    use crate::*;
    
    use std::collections::{hash_map, HashMap};
    
    pub struct IdentityCache {
    	client: Client,
    	identities: HashMap<u32, String>,
    	indexer: Option<Indexer>,
    }
    
    impl IdentityCache {
    	pub fn new(client: Client, indexer: Option<Indexer>) -> Self {
    		Self {
    			client,
    			identities: HashMap::new(),
    			indexer,
    		}
    	}
    
    	pub async fn fetch_identity(
    		&mut self,
    		identity_id: u32,
    		parent_hash: sp_core::H256,
    	) -> anyhow::Result<String> {
    		Ok(match self.identities.entry(identity_id) {
    			hash_map::Entry::Occupied(entry) => entry.get().clone(),
    			hash_map::Entry::Vacant(entry) => entry
    				.insert({
    					let pubkey = self
    						.client
    						.storage()
    						.fetch(
    							&runtime::storage().identity().identities(identity_id),
    							Some(parent_hash),
    						)
    						.await?
    						.ok_or_else(|| anyhow!("Identity {} not found", identity_id))?
    						.owner_key
    						.to_string();
    					format!(
    						"“ {} ”",
    						if let Some(indexer) = &self.indexer {
    							if let Ok(Some(username)) = indexer.username_by_pubkey(&pubkey).await {
    								username
    							} else {
    								pubkey
    							}
    						} else {
    							pubkey
    						}
    					)
    				})
    				.clone(),
    		})
    	}
    }