|
@@ -1,25 +1,19 @@
|
|
|
-use std::{
|
|
|
- collections::{HashMap, HashSet},
|
|
|
- ops::Deref,
|
|
|
-};
|
|
|
-
|
|
|
use futures::future::join_all;
|
|
|
use nostr_rs_client::Pool;
|
|
|
use nostr_rs_relayer::Relayer;
|
|
|
use nostr_rs_storage_base::Storage;
|
|
|
use nostr_rs_types::{
|
|
|
- types::{content::profile::Profile, Content, Event, Filter, Id, Kind, Tag},
|
|
|
+ types::{content::profile::Profile, tag::TagType, Content, Event, Filter, Id},
|
|
|
Request,
|
|
|
};
|
|
|
use serde::Serialize;
|
|
|
+use std::{
|
|
|
+ collections::{HashMap, HashSet},
|
|
|
+ ops::Deref,
|
|
|
+};
|
|
|
use tokio::{net::TcpListener, task::JoinHandle};
|
|
|
use url::Url;
|
|
|
|
|
|
-//
|
|
|
-// g
|
|
|
-use tokio::fs::OpenOptions;
|
|
|
-use tokio::io::AsyncWriteExt;
|
|
|
-
|
|
|
pub struct Stoppable(Option<Vec<JoinHandle<()>>>);
|
|
|
|
|
|
impl From<Vec<JoinHandle<()>>> for Stoppable {
|
|
@@ -107,7 +101,7 @@ impl<T: Storage + Send + Sync + 'static> PersonalRelayer<T> {
|
|
|
local_connection
|
|
|
.send(Request::Request(
|
|
|
vec![Filter {
|
|
|
- authors: self.accounts.keys().map(|x| x.clone()).collect::<Vec<_>>(),
|
|
|
+ authors: self.accounts.keys().cloned().collect::<Vec<_>>(),
|
|
|
//kinds: vec![Kind::Metadata, Kind::ShortTextNote, Kind::Contacts],
|
|
|
..Default::default()
|
|
|
}]
|
|
@@ -121,10 +115,10 @@ impl<T: Storage + Send + Sync + 'static> PersonalRelayer<T> {
|
|
|
if let Some(event) = res.as_event() {
|
|
|
match event.content() {
|
|
|
Content::Metadata(profile) => {
|
|
|
- self.accounts.get_mut(&event.author()).map(|account| {
|
|
|
+ if let Some(account) = self.accounts.get_mut(event.author()) {
|
|
|
account.profile = Some(profile.clone());
|
|
|
account.content.push(event.deref().clone());
|
|
|
- });
|
|
|
+ }
|
|
|
}
|
|
|
Content::Contacts(_) => {
|
|
|
let current = event.author().clone();
|
|
@@ -139,70 +133,53 @@ impl<T: Storage + Send + Sync + 'static> PersonalRelayer<T> {
|
|
|
let mut ids = vec![];
|
|
|
|
|
|
for tag in event.tags() {
|
|
|
- match tag {
|
|
|
- Tag::PubKey(pub_key, relayer_url, _) => {
|
|
|
- let followed = self
|
|
|
- .accounts
|
|
|
- .entry(pub_key.clone())
|
|
|
- .or_insert(Contact::new(
|
|
|
- pub_key.clone(),
|
|
|
- Some(current.clone()),
|
|
|
- ));
|
|
|
-
|
|
|
- if let Some((Some(relayer_url), _)) = relayer_url {
|
|
|
- let _ = relayer
|
|
|
- .connect_to_relayer(relayer_url.clone())
|
|
|
- .await;
|
|
|
- }
|
|
|
-
|
|
|
- current_user.following.insert(pub_key.clone());
|
|
|
- followed.followed_by.insert(current.clone());
|
|
|
- ids.push(pub_key.clone());
|
|
|
+ if let TagType::PubKey(pub_key, relayer_url, _) =
|
|
|
+ tag.deref()
|
|
|
+ {
|
|
|
+ let followed = self
|
|
|
+ .accounts
|
|
|
+ .entry(pub_key.clone())
|
|
|
+ .or_insert(Contact::new(
|
|
|
+ pub_key.clone(),
|
|
|
+ Some(current.clone()),
|
|
|
+ ));
|
|
|
+
|
|
|
+ if let Some(relayer_url) = relayer_url {
|
|
|
+ let _ = relayer
|
|
|
+ .connect_to_relayer(relayer_url.clone())
|
|
|
+ .await;
|
|
|
}
|
|
|
- _ => {}
|
|
|
+
|
|
|
+ current_user.following.insert(pub_key.clone());
|
|
|
+ followed.followed_by.insert(current.clone());
|
|
|
+ ids.push(pub_key.clone());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
self.accounts.insert(current, current_user);
|
|
|
|
|
|
- let _ = local_connection
|
|
|
- .send(Request::Request(
|
|
|
- Filter {
|
|
|
- authors: ids,
|
|
|
- kinds: vec![
|
|
|
- Kind::Metadata,
|
|
|
- Kind::ShortTextNote,
|
|
|
- Kind::Contacts,
|
|
|
- ],
|
|
|
- ..Default::default()
|
|
|
- }
|
|
|
- .into(),
|
|
|
- ))
|
|
|
- .await;
|
|
|
+ for authors in ids.chunks(20).collect::<Vec<_>>().into_iter() {
|
|
|
+ let _ = local_connection
|
|
|
+ .send(Request::Request(
|
|
|
+ Filter {
|
|
|
+ authors: authors.to_vec(),
|
|
|
+ ..Default::default()
|
|
|
+ }
|
|
|
+ .into(),
|
|
|
+ ))
|
|
|
+ .await;
|
|
|
+ }
|
|
|
}
|
|
|
Content::ShortTextNote(_) => {
|
|
|
- self.accounts.get_mut(&event.author()).map(|account| {
|
|
|
+ if let Some(account) = self.accounts.get_mut(event.author()) {
|
|
|
account.content.push(event.deref().clone());
|
|
|
- });
|
|
|
+ }
|
|
|
}
|
|
|
_ => {}
|
|
|
}
|
|
|
} else {
|
|
|
println!("Not an event: {:?}", res);
|
|
|
}
|
|
|
-
|
|
|
- let mut file = OpenOptions::new()
|
|
|
- .write(true)
|
|
|
- .create(true)
|
|
|
- .open("example.txt")
|
|
|
- .await
|
|
|
- .expect("Failed to open file");
|
|
|
-
|
|
|
- file.write_all(&serde_json::to_vec_pretty(&self.accounts).unwrap())
|
|
|
- .await
|
|
|
- .expect("Failed to write to file");
|
|
|
-
|
|
|
- file.flush().await.expect("Failed to flush file");
|
|
|
}
|
|
|
}
|
|
|
}),
|