feat: add global state

This commit is contained in:
2024-11-19 10:10:26 +07:00
parent 4d8a1d44cc
commit fb71f02ef3
4 changed files with 47 additions and 10 deletions

View File

@@ -4,6 +4,10 @@ version = "0.1.0"
edition = "2021"
[dependencies]
gpui.workspace = true
nostr-sdk.workspace = true
dirs.workspace = true
tokio.workspace = true
keyring-search = "1.2.0"
keyring-lib = { version = "1.0.2", features = ["tokio", "derive"] }

View File

@@ -1 +1,39 @@
use gpui::Global;
use keyring_search::{Limit, List, Search};
use nostr_sdk::prelude::*;
use state::get_client;
use std::collections::HashSet;
pub mod state;
pub struct Nostr {
pub client: &'static Client,
pub accounts: HashSet<PublicKey>,
}
// Set Nostr as Global State
impl Global for Nostr {}
impl Nostr {
pub async fn init() -> Self {
// Initialize nostr client
let client = get_client().await;
// Get all accounts
let accounts = Self::get_accounts();
Self { client, accounts }
}
pub fn get_accounts() -> HashSet<PublicKey> {
let search = Search::new().expect("Keyring not working.");
let results = search.by_service("coop");
let list = List::list_credentials(&results, Limit::All);
let accounts: HashSet<PublicKey> = list
.split_whitespace()
.filter(|v| v.starts_with("npub1") && !v.ends_with("coop"))
.filter_map(|i| PublicKey::from_bech32(i).ok())
.collect();
accounts
}
}