|
@@ -3,6 +3,7 @@
|
|
//! This crate will storage events into a database. It will also build index to
|
|
//! This crate will storage events into a database. It will also build index to
|
|
//! find events by their tags, kind and references.
|
|
//! find events by their tags, kind and references.
|
|
use super::*;
|
|
use super::*;
|
|
|
|
+use futures::{StreamExt, TryStreamExt};
|
|
use nostr_rs_types::types::{Addr, Event, Filter, Kind};
|
|
use nostr_rs_types::types::{Addr, Event, Filter, Kind};
|
|
use std::{
|
|
use std::{
|
|
fs::File,
|
|
fs::File,
|
|
@@ -47,7 +48,7 @@ where
|
|
.try_into()
|
|
.try_into()
|
|
.expect("pk");
|
|
.expect("pk");
|
|
|
|
|
|
- let vec = db
|
|
|
|
|
|
+ let vec: Vec<Event> = db
|
|
.get_by_filter(Filter {
|
|
.get_by_filter(Filter {
|
|
authors: vec![pk],
|
|
authors: vec![pk],
|
|
limit: 10,
|
|
limit: 10,
|
|
@@ -55,7 +56,8 @@ where
|
|
})
|
|
})
|
|
.await
|
|
.await
|
|
.expect("set of results")
|
|
.expect("set of results")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
|
|
|
|
let dates = vec.iter().map(|e| e.created_at()).collect::<Vec<_>>();
|
|
let dates = vec.iter().map(|e| e.created_at()).collect::<Vec<_>>();
|
|
@@ -72,7 +74,7 @@ where
|
|
{
|
|
{
|
|
setup_db(db).await;
|
|
setup_db(db).await;
|
|
|
|
|
|
- let related_events = db
|
|
|
|
|
|
+ let related_events: Vec<Event> = db
|
|
.get_by_filter(Filter {
|
|
.get_by_filter(Filter {
|
|
references_to_event: vec![
|
|
references_to_event: vec![
|
|
"f513f1422ee5dbf30f57118b6cc34e788746e589a9b07be767664a164c57b9b1"
|
|
"f513f1422ee5dbf30f57118b6cc34e788746e589a9b07be767664a164c57b9b1"
|
|
@@ -88,7 +90,8 @@ where
|
|
})
|
|
})
|
|
.await
|
|
.await
|
|
.expect("valid")
|
|
.expect("valid")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
assert_eq!(related_events.len(), 1);
|
|
assert_eq!(related_events.len(), 1);
|
|
}
|
|
}
|
|
@@ -99,7 +102,7 @@ where
|
|
{
|
|
{
|
|
setup_db(db);
|
|
setup_db(db);
|
|
|
|
|
|
- let related_events = db
|
|
|
|
|
|
+ let related_events: Vec<Event> = db
|
|
.get_by_filter(Filter {
|
|
.get_by_filter(Filter {
|
|
references_to_event: vec![
|
|
references_to_event: vec![
|
|
"42224859763652914db53052103f0b744df79dfc4efef7e950fc0802fc3df3c5"
|
|
"42224859763652914db53052103f0b744df79dfc4efef7e950fc0802fc3df3c5"
|
|
@@ -115,7 +118,8 @@ where
|
|
})
|
|
})
|
|
.await
|
|
.await
|
|
.expect("valid")
|
|
.expect("valid")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
assert_eq!(related_events.len(), 0);
|
|
assert_eq!(related_events.len(), 0);
|
|
}
|
|
}
|
|
@@ -126,7 +130,7 @@ where
|
|
{
|
|
{
|
|
setup_db(db).await;
|
|
setup_db(db).await;
|
|
|
|
|
|
- let related_events = db
|
|
|
|
|
|
+ let related_events: Vec<Event> = db
|
|
.get_by_filter(Filter {
|
|
.get_by_filter(Filter {
|
|
kinds: vec![Kind::Reaction, Kind::ShortTextNote],
|
|
kinds: vec![Kind::Reaction, Kind::ShortTextNote],
|
|
references_to_event: vec![
|
|
references_to_event: vec![
|
|
@@ -138,7 +142,8 @@ where
|
|
})
|
|
})
|
|
.await
|
|
.await
|
|
.expect("valid")
|
|
.expect("valid")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
assert_eq!(related_events.len(), 3);
|
|
assert_eq!(related_events.len(), 3);
|
|
}
|
|
}
|
|
@@ -153,26 +158,28 @@ where
|
|
.try_into()
|
|
.try_into()
|
|
.expect("pk");
|
|
.expect("pk");
|
|
|
|
|
|
- let events = db
|
|
|
|
|
|
+ let events: Vec<Event> = db
|
|
.get_by_filter(Filter {
|
|
.get_by_filter(Filter {
|
|
ids: vec![id.clone()],
|
|
ids: vec![id.clone()],
|
|
..Default::default()
|
|
..Default::default()
|
|
})
|
|
})
|
|
.await
|
|
.await
|
|
.expect("events")
|
|
.expect("events")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
|
|
|
|
assert_eq!(events.len(), 1);
|
|
assert_eq!(events.len(), 1);
|
|
|
|
|
|
- let related_events = db
|
|
|
|
|
|
+ let related_events: Vec<Event> = db
|
|
.get_by_filter(Filter {
|
|
.get_by_filter(Filter {
|
|
references_to_event: vec![id],
|
|
references_to_event: vec![id],
|
|
..Default::default()
|
|
..Default::default()
|
|
})
|
|
})
|
|
.await
|
|
.await
|
|
.expect("valid")
|
|
.expect("valid")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
assert_eq!(related_events.len(), 2_538);
|
|
assert_eq!(related_events.len(), 2_538);
|
|
|
|
|
|
@@ -200,11 +207,12 @@ where
|
|
],
|
|
],
|
|
..Default::default()
|
|
..Default::default()
|
|
};
|
|
};
|
|
- let records = db
|
|
|
|
|
|
+ let records: Vec<Event> = db
|
|
.get_by_filter(query)
|
|
.get_by_filter(query)
|
|
.await
|
|
.await
|
|
.expect("valid")
|
|
.expect("valid")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
assert_eq!(records.len(), 27);
|
|
assert_eq!(records.len(), 27);
|
|
}
|
|
}
|
|
@@ -222,11 +230,12 @@ where
|
|
],
|
|
],
|
|
..Default::default()
|
|
..Default::default()
|
|
};
|
|
};
|
|
- let records = db
|
|
|
|
|
|
+ let records: Vec<Event> = db
|
|
.get_by_filter(query)
|
|
.get_by_filter(query)
|
|
.await
|
|
.await
|
|
.expect("valid")
|
|
.expect("valid")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
assert_eq!(records.len(), 3);
|
|
assert_eq!(records.len(), 3);
|
|
}
|
|
}
|
|
@@ -245,11 +254,12 @@ where
|
|
kinds: vec![Kind::ShortTextNote, Kind::Reaction],
|
|
kinds: vec![Kind::ShortTextNote, Kind::Reaction],
|
|
..Default::default()
|
|
..Default::default()
|
|
};
|
|
};
|
|
- let records = db
|
|
|
|
|
|
+ let records: Vec<Event> = db
|
|
.get_by_filter(query)
|
|
.get_by_filter(query)
|
|
.await
|
|
.await
|
|
.expect("iterator")
|
|
.expect("iterator")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
assert_eq!(records.len(), 2);
|
|
assert_eq!(records.len(), 2);
|
|
}
|
|
}
|
|
@@ -263,12 +273,14 @@ where
|
|
kinds: vec![Kind::ShortTextNote],
|
|
kinds: vec![Kind::ShortTextNote],
|
|
..Default::default()
|
|
..Default::default()
|
|
};
|
|
};
|
|
- let records = db
|
|
|
|
|
|
+ let records: Vec<Event> = db
|
|
.get_by_filter(query)
|
|
.get_by_filter(query)
|
|
.await
|
|
.await
|
|
.expect("valid")
|
|
.expect("valid")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
|
|
+
|
|
assert_eq!(records.len(), 1_511);
|
|
assert_eq!(records.len(), 1_511);
|
|
records
|
|
records
|
|
.iter()
|
|
.iter()
|
|
@@ -282,27 +294,30 @@ where
|
|
{
|
|
{
|
|
setup_db(db).await;
|
|
setup_db(db).await;
|
|
|
|
|
|
- let ids = db
|
|
|
|
|
|
+ let events_from_filter: Vec<Event> = db
|
|
.get_by_filter(Filter {
|
|
.get_by_filter(Filter {
|
|
limit: 10,
|
|
limit: 10,
|
|
..Default::default()
|
|
..Default::default()
|
|
})
|
|
})
|
|
.await
|
|
.await
|
|
.expect("valid")
|
|
.expect("valid")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
|
|
|
|
- for event in ids.iter() {
|
|
|
|
|
|
+ for event in events_from_filter.iter() {
|
|
db.set_local_event(event).await.expect("valid");
|
|
db.set_local_event(event).await.expect("valid");
|
|
}
|
|
}
|
|
|
|
|
|
- assert_eq!(10, ids.len());
|
|
|
|
|
|
+ assert_eq!(10, events_from_filter.len());
|
|
|
|
|
|
- let records = db
|
|
|
|
|
|
+ let records: Vec<Event> = db
|
|
.get_local_events(None)
|
|
.get_local_events(None)
|
|
.await
|
|
.await
|
|
.expect("valid iterator")
|
|
.expect("valid iterator")
|
|
- .collect::<Result<Vec<_>, _>>()
|
|
|
|
|
|
+ .try_collect()
|
|
|
|
+ .await
|
|
.expect("valid");
|
|
.expect("valid");
|
|
|
|
+
|
|
assert_eq!(10, records.len())
|
|
assert_eq!(10, records.len())
|
|
}
|
|
}
|