wip: refactor
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
use asset::Assets;
|
||||
use components::Root;
|
||||
use constants::{APP_NAME, FAKE_SIG};
|
||||
use dirs::config_dir;
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::{fs, str::FromStr, sync::Arc, time::Duration};
|
||||
use tokio::sync::OnceCell;
|
||||
use std::{
|
||||
fs,
|
||||
str::FromStr,
|
||||
sync::{Arc, OnceLock},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use states::user::UserState;
|
||||
use constants::{APP_NAME, FAKE_SIG};
|
||||
use states::account::AccountState;
|
||||
use ui::app::AppView;
|
||||
|
||||
pub mod asset;
|
||||
@@ -18,77 +22,71 @@ pub mod utils;
|
||||
|
||||
actions!(main_menu, [Quit]);
|
||||
|
||||
pub static CLIENT: OnceCell<Client> = OnceCell::const_new();
|
||||
static CLIENT: OnceLock<Client> = OnceLock::new();
|
||||
|
||||
pub async fn get_client() -> &'static Client {
|
||||
CLIENT
|
||||
.get_or_init(|| async {
|
||||
// Setup app data folder
|
||||
let config_dir = config_dir().expect("Config directory not found");
|
||||
let _ = fs::create_dir_all(config_dir.join("Coop/"));
|
||||
pub fn initialize_client() {
|
||||
// Setup app data folder
|
||||
let config_dir = config_dir().expect("Config directory not found");
|
||||
let _ = fs::create_dir_all(config_dir.join("Coop/"));
|
||||
|
||||
// Setup database
|
||||
let lmdb = NostrLMDB::open(config_dir.join("Coop/nostr"))
|
||||
.expect("Database is NOT initialized");
|
||||
// Setup database
|
||||
let lmdb = NostrLMDB::open(config_dir.join("Coop/nostr")).expect("Database is NOT initialized");
|
||||
|
||||
// Client options
|
||||
let opts = Options::new()
|
||||
.gossip(true)
|
||||
.max_avg_latency(Duration::from_secs(2));
|
||||
// Client options
|
||||
let opts = Options::new()
|
||||
.gossip(true)
|
||||
.max_avg_latency(Duration::from_secs(2));
|
||||
|
||||
// Setup Nostr Client
|
||||
let client = ClientBuilder::default().database(lmdb).opts(opts).build();
|
||||
// Setup Nostr Client
|
||||
let client = ClientBuilder::default().database(lmdb).opts(opts).build();
|
||||
|
||||
// Add some bootstrap relays
|
||||
let _ = client.add_relay("wss://relay.damus.io").await;
|
||||
let _ = client.add_relay("wss://relay.primal.net").await;
|
||||
CLIENT.set(client).expect("Client is already initialized!");
|
||||
}
|
||||
|
||||
let _ = client.add_discovery_relay("wss://directory.yabu.me").await;
|
||||
let _ = client.add_discovery_relay("wss://user.kindpag.es/").await;
|
||||
|
||||
// Connect to all relays
|
||||
client.connect().await;
|
||||
|
||||
// Return client
|
||||
client
|
||||
})
|
||||
.await
|
||||
pub fn get_client() -> &'static Client {
|
||||
CLIENT.get().expect("Client is NOT initialized!")
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// Initialize nostr client
|
||||
let _client = get_client().await;
|
||||
// Initialize client
|
||||
initialize_client();
|
||||
|
||||
// Get client
|
||||
let client = get_client();
|
||||
let mut notifications = client.notifications();
|
||||
|
||||
// Add some bootstrap relays
|
||||
_ = client.add_relay("wss://relay.damus.io/").await;
|
||||
_ = client.add_relay("wss://relay.primal.net/").await;
|
||||
_ = client.add_relay("wss://nos.lol/").await;
|
||||
|
||||
_ = client.add_discovery_relay("wss://user.kindpag.es/").await;
|
||||
_ = client.add_discovery_relay("wss://directory.yabu.me/").await;
|
||||
|
||||
// Connect to all relays
|
||||
_ = client.connect().await;
|
||||
|
||||
App::new()
|
||||
.with_assets(Assets)
|
||||
.with_http_client(Arc::new(reqwest_client::ReqwestClient::new()))
|
||||
.run(move |cx| {
|
||||
AccountState::set_global(cx);
|
||||
|
||||
// Initialize components
|
||||
components::init(cx);
|
||||
|
||||
// Set quit action
|
||||
cx.on_action(quit);
|
||||
|
||||
// Set app state
|
||||
UserState::set_global(cx);
|
||||
|
||||
// Refresh
|
||||
cx.refresh();
|
||||
|
||||
// Handle notifications
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client().await;
|
||||
|
||||
// Generate a fake signature for rumor event.
|
||||
// TODO: Find better way to save unsigned event to database.
|
||||
let fake_sig = Signature::from_str(FAKE_SIG).unwrap();
|
||||
|
||||
client
|
||||
.handle_notifications(|notification| async {
|
||||
cx.background_executor()
|
||||
.spawn({
|
||||
let sig = Signature::from_str(FAKE_SIG).unwrap();
|
||||
async move {
|
||||
while let Ok(notification) = notifications.recv().await {
|
||||
#[allow(clippy::collapsible_match)]
|
||||
if let RelayPoolNotification::Message { message, .. } = notification {
|
||||
if let RelayMessage::Event { event, .. } = message {
|
||||
@@ -96,36 +94,31 @@ async fn main() {
|
||||
if let Ok(UnwrappedGift { rumor, .. }) =
|
||||
client.unwrap_gift_wrap(&event).await
|
||||
{
|
||||
println!("rumor: {}", rumor.as_json());
|
||||
let mut rumor_clone = rumor.clone();
|
||||
|
||||
// Compute event id if not exist
|
||||
rumor_clone.ensure_id();
|
||||
|
||||
let ev = Event::new(
|
||||
rumor_clone.id.expect("System error"),
|
||||
rumor_clone.pubkey,
|
||||
rumor_clone.created_at,
|
||||
rumor_clone.kind,
|
||||
rumor_clone.tags,
|
||||
rumor_clone.content,
|
||||
fake_sig,
|
||||
);
|
||||
if let Some(id) = rumor_clone.id {
|
||||
let ev = Event::new(
|
||||
id,
|
||||
rumor_clone.pubkey,
|
||||
rumor_clone.created_at,
|
||||
rumor_clone.kind,
|
||||
rumor_clone.tags,
|
||||
rumor_clone.content,
|
||||
sig,
|
||||
);
|
||||
|
||||
// Save rumor to database to further query
|
||||
if let Err(e) = client.database().save_event(&ev).await
|
||||
{
|
||||
println!("Error: {}", e)
|
||||
// Save rumor to database to further query
|
||||
_ = client.database().save_event(&ev).await
|
||||
}
|
||||
}
|
||||
} else if event.kind == Kind::Metadata {
|
||||
// TODO: handle metadata
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user