Skip to content
Snippets Groups Projects
Select Git revision
  • 1d41394759c6eb18268dc22cfc9cee9dabaeef6f
  • master default protected
  • chrome-manifest-v3
  • feature/migrate-cordova-13
  • feat/improve-network-scan
  • feat/force-migration-check
  • develop
  • feature/encrypted_comment
  • feature/android_api_19
  • gitlab_migration_1
  • rml8
  • v1.7.15-rc1
  • v1.7.14
  • v1.7.13
  • v1.7.12
  • v1.7.11
  • v1.7.10
  • v1.7.9
  • v1.7.8
  • v1.7.7
  • v1.7.6
  • v1.7.5
  • v1.7.4
  • v1.7.3
  • v1.7.2
  • v1.7.1
  • v1.7.0
  • v1.7.0-rc2
  • v1.7.0-rc1
  • v1.6.12
  • v1.6.11
31 results

app.js

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(),
    		})
    	}
    }