Skip to content

Commit 26b5868

Browse files
committed
feat: integrate nostrdb relay indexing
- Upgrade `nostrdb` to v0.6.1 with relay metadata support - Switch to `nostr::RelayUrl` for typed relay URLs - Use `process_event_with()` to pass relay info during ingestion - Update `Relay`, `RelayPool`, and unknown ID logic accordingly This enables richer indexing with relay provenance in events. Signed-off-by: William Casarin <[email protected]>
1 parent a7f34a9 commit 26b5868

File tree

12 files changed

+52
-25
lines changed

12 files changed

+52
-25
lines changed

Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ indexmap = "2.6.0"
3333
log = "0.4.17"
3434
nostr = { version = "0.37.0", default-features = false, features = ["std", "nip49"] }
3535
mio = { version = "1.0.3", features = ["os-poll", "net"] }
36-
nostrdb = { git = "https://github.com/damus-io/nostrdb-rs", rev = "22fa1cc57ee1b4ce10e726ab284bc124461c8871" }
37-
#nostrdb = "0.5.2"
36+
#nostrdb = { git = "https://github.com/damus-io/nostrdb-rs", rev = "ecae493712505f91488bea0b53aa5a7b50f44ab2" }
37+
nostrdb = "0.6.1"
3838
notedeck = { path = "crates/notedeck" }
3939
notedeck_chrome = { path = "crates/notedeck_chrome" }
4040
notedeck_columns = { path = "crates/notedeck_columns" }

crates/enostr/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub enum Error {
2525
#[error("invalid public key")]
2626
InvalidPublicKey,
2727

28+
#[error("invalid relay url")]
29+
InvalidRelayUrl,
30+
2831
// Secp(secp256k1::Error),
2932
#[error("json error: {0}")]
3033
Json(#[from] serde_json::Error),

crates/enostr/src/relay/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub fn setup_multicast_relay(
149149
}
150150

151151
pub struct Relay {
152-
pub url: String,
152+
pub url: nostr::RelayUrl,
153153
pub status: RelayStatus,
154154
pub sender: WsSender,
155155
pub receiver: WsReceiver,
@@ -180,9 +180,10 @@ impl PartialEq for Relay {
180180
impl Eq for Relay {}
181181

182182
impl Relay {
183-
pub fn new(url: String, wakeup: impl Fn() + Send + Sync + 'static) -> Result<Self> {
183+
pub fn new(url: nostr::RelayUrl, wakeup: impl Fn() + Send + Sync + 'static) -> Result<Self> {
184184
let status = RelayStatus::Connecting;
185-
let (sender, receiver) = ewebsock::connect_with_wakeup(&url, Options::default(), wakeup)?;
185+
let (sender, receiver) =
186+
ewebsock::connect_with_wakeup(url.as_str(), Options::default(), wakeup)?;
186187

187188
Ok(Self {
188189
url,
@@ -210,7 +211,7 @@ impl Relay {
210211

211212
pub fn connect(&mut self, wakeup: impl Fn() + Send + Sync + 'static) -> Result<()> {
212213
let (sender, receiver) =
213-
ewebsock::connect_with_wakeup(&self.url, Options::default(), wakeup)?;
214+
ewebsock::connect_with_wakeup(self.url.as_str(), Options::default(), wakeup)?;
214215
self.status = RelayStatus::Connecting;
215216
self.sender = sender;
216217
self.receiver = receiver;

crates/enostr/src/relay/pool.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::relay::{setup_multicast_relay, MulticastRelay, Relay, RelayStatus};
2-
use crate::{ClientMessage, Result};
2+
use crate::{ClientMessage, Error, Result};
33
use nostrdb::Filter;
44

55
use std::collections::BTreeSet;
@@ -50,7 +50,7 @@ pub struct WebsocketRelay {
5050
impl PoolRelay {
5151
pub fn url(&self) -> &str {
5252
match self {
53-
Self::Websocket(wsr) => &wsr.relay.url,
53+
Self::Websocket(wsr) => wsr.relay.url.as_str(),
5454
Self::Multicast(_wsr) => "multicast",
5555
}
5656
}
@@ -315,7 +315,10 @@ impl RelayPool {
315315
if self.has(&url) {
316316
return Ok(());
317317
}
318-
let relay = Relay::new(url, wakeup)?;
318+
let relay = Relay::new(
319+
nostr::RelayUrl::parse(url).map_err(|_| Error::InvalidRelayUrl)?,
320+
wakeup,
321+
)?;
319322
let pool_relay = PoolRelay::websocket(relay);
320323

321324
self.relays.push(pool_relay);

crates/enostr/src/relay/subs_debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ fn calculate_error_size(error: &Error) -> usize {
229229
| Error::InvalidBech32
230230
| Error::InvalidByteSize
231231
| Error::InvalidSignature
232+
| Error::InvalidRelayUrl
232233
| Error::Io(_)
233234
| Error::InvalidPublicKey => mem::size_of_val(error), // No heap usage
234235

crates/notedeck/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ strum = { workspace = true }
1212
strum_macros = { workspace = true }
1313
dirs = { workspace = true }
1414
enostr = { workspace = true }
15+
nostr = { workspace = true }
1516
egui = { workspace = true }
1617
eframe = { workspace = true }
1718
image = { workspace = true }

crates/notedeck/src/unknowns.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
};
66

77
use enostr::{Filter, NoteId, Pubkey};
8+
use nostr::RelayUrl;
89
use nostrdb::{BlockType, Mention, Ndb, Note, NoteKey, Transaction};
910
use std::collections::{HashMap, HashSet};
1011
use std::time::{Duration, Instant};
@@ -78,8 +79,6 @@ impl SingleUnkIdAction {
7879
}
7980
}
8081

81-
type RelayUrl = String;
82-
8382
/// Unknown Id searcher
8483
#[derive(Default, Debug)]
8584
pub struct UnknownIds {
@@ -300,15 +299,15 @@ pub fn get_unknown_note_ids<'a>(
300299
let id = UnknownId::Pubkey(Pubkey::new(*nprofile.pubkey()));
301300
let relays = nprofile
302301
.relays_iter()
303-
.map(String::from)
302+
.filter_map(|s| RelayUrl::parse(s).ok())
304303
.collect::<HashSet<RelayUrl>>();
305304
ids.entry(id).or_default().extend(relays);
306305
}
307306
}
308307
Mention::Event(ev) => {
309308
let relays = ev
310309
.relays_iter()
311-
.map(String::from)
310+
.filter_map(|s| RelayUrl::parse(s).ok())
312311
.collect::<HashSet<RelayUrl>>();
313312
match ndb.get_note_by_id(txn, ev.id()) {
314313
Err(_) => {

crates/notedeck_columns/src/app.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,23 @@ fn process_message(damus: &mut Damus, ctx: &mut AppContext<'_>, relay: &str, msg
299299
match relay {
300300
PoolRelay::Websocket(_) => {
301301
//info!("processing event {}", event);
302-
if let Err(err) = ctx.ndb.process_event(ev) {
302+
if let Err(err) = ctx.ndb.process_event_with(
303+
ev,
304+
nostrdb::IngestMetadata::new()
305+
.client(false)
306+
.relay(relay.url()),
307+
) {
303308
error!("error processing event {ev}: {err}");
304309
}
305310
}
306311
PoolRelay::Multicast(_) => {
307312
// multicast events are client events
308-
if let Err(err) = ctx.ndb.process_client_event(ev) {
313+
if let Err(err) = ctx.ndb.process_event_with(
314+
ev,
315+
nostrdb::IngestMetadata::new()
316+
.client(true)
317+
.relay(relay.url()),
318+
) {
309319
error!("error processing multicast event {ev}: {err}");
310320
}
311321
}

crates/notedeck_columns/src/profile.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ impl ProfileAction {
119119
ProfileAction::SaveChanges(changes) => {
120120
let raw_msg = format!("[\"EVENT\",{}]", changes.to_note().json().unwrap());
121121

122-
let _ = ndb.process_client_event(raw_msg.as_str());
122+
let _ = ndb.process_event_with(
123+
raw_msg.as_str(),
124+
nostrdb::IngestMetadata::new().client(true),
125+
);
123126
let _ = state_map.remove_entry(&changes.kp.pubkey);
124127

125128
info!("sending {}", raw_msg);

0 commit comments

Comments
 (0)