|
| 1 | +// Copyright (c) 2022-2023 Yuki Kishimoto |
| 2 | +// Copyright (c) 2023-2024 Rust Nostr Developers |
| 3 | +// Distributed under the MIT software license |
| 4 | + |
| 5 | +use nostr_redb::NostrRedb; |
| 6 | +use nostr_sdk::prelude::*; |
| 7 | + |
| 8 | +#[tokio::main] |
| 9 | +async fn main() -> Result<()> { |
| 10 | + tracing_subscriber::fmt::init(); |
| 11 | + |
| 12 | + let keys = Keys::parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")?; |
| 13 | + |
| 14 | + let database = NostrRedb::persistent("./db/nostr-redb")?; |
| 15 | + let client: Client = ClientBuilder::default() |
| 16 | + .signer(keys.clone()) |
| 17 | + .database(database) |
| 18 | + .build(); |
| 19 | + |
| 20 | + client.add_relay("wss://relay.damus.io").await?; |
| 21 | + client.add_relay("wss://nostr.wine").await?; |
| 22 | + client.add_relay("wss://nostr.oxtr.dev").await?; |
| 23 | + |
| 24 | + client.connect().await; |
| 25 | + |
| 26 | + // Publish a text note |
| 27 | + let builder = EventBuilder::text_note("Hello world"); |
| 28 | + client.send_event_builder(builder).await?; |
| 29 | + |
| 30 | + // Negentropy sync |
| 31 | + let filter = Filter::new().author(keys.public_key()); |
| 32 | + let (tx, mut rx) = SyncProgress::channel(); |
| 33 | + let opts = SyncOptions::default().progress(tx); |
| 34 | + |
| 35 | + tokio::spawn(async move { |
| 36 | + while rx.changed().await.is_ok() { |
| 37 | + let progress = *rx.borrow_and_update(); |
| 38 | + if progress.total > 0 { |
| 39 | + println!("{:.2}%", progress.percentage() * 100.0); |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + let output = client.sync(filter, &opts).await?; |
| 44 | + |
| 45 | + println!("Local: {}", output.local.len()); |
| 46 | + println!("Remote: {}", output.remote.len()); |
| 47 | + println!("Sent: {}", output.sent.len()); |
| 48 | + println!("Received: {}", output.received.len()); |
| 49 | + println!("Failures:"); |
| 50 | + for (url, map) in output.send_failures.iter() { |
| 51 | + println!("* '{url}':"); |
| 52 | + for (id, e) in map.iter() { |
| 53 | + println!(" - {id}: {e}"); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Query events from database |
| 58 | + let filter = Filter::new().author(keys.public_key()).limit(10); |
| 59 | + let events = client.database().query(vec![filter]).await?; |
| 60 | + println!("Events: {events:?}"); |
| 61 | + |
| 62 | + Ok(()) |
| 63 | +} |
0 commit comments