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