feat: improve performance (#234)
* feat: use negentropy as much as possible * update * update
This commit is contained in:
@@ -3,7 +3,12 @@ use keyring_search::{Limit, List, Search};
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::{collections::HashSet, str::FromStr, time::Duration};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs::{self, File},
|
||||
str::FromStr,
|
||||
time::Duration,
|
||||
};
|
||||
use tauri::{Emitter, Manager, State};
|
||||
|
||||
use crate::{
|
||||
@@ -133,7 +138,7 @@ pub async fn connect_account(uri: String, state: State<'_, Nostr>) -> Result<Str
|
||||
let remote_user = bunker_uri.signer_public_key().unwrap();
|
||||
let remote_npub = remote_user.to_bech32().unwrap();
|
||||
|
||||
match Nip46Signer::new(bunker_uri, app_keys, Duration::from_secs(120), None).await {
|
||||
match Nip46Signer::new(bunker_uri, app_keys, Duration::from_secs(120), None) {
|
||||
Ok(signer) => {
|
||||
let mut url = Url::parse(&uri).unwrap();
|
||||
let query: Vec<(String, String)> = url
|
||||
@@ -204,6 +209,17 @@ pub fn delete_account(id: String) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn is_account_sync(id: String, handle: tauri::AppHandle) -> bool {
|
||||
let config_dir = handle
|
||||
.path()
|
||||
.app_config_dir()
|
||||
.expect("Error: app config directory not found.");
|
||||
|
||||
fs::metadata(config_dir.join(id)).is_ok()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn login(
|
||||
@@ -244,7 +260,7 @@ pub async fn login(
|
||||
let public_key = uri.signer_public_key().unwrap().to_bech32().unwrap();
|
||||
let app_keys = Keys::from_str(&account.password).map_err(|e| e.to_string())?;
|
||||
|
||||
match Nip46Signer::new(uri, app_keys, Duration::from_secs(120), None).await {
|
||||
match Nip46Signer::new(uri, app_keys, Duration::from_secs(120), None) {
|
||||
Ok(signer) => {
|
||||
// Update signer
|
||||
client.set_signer(Some(signer.into())).await;
|
||||
@@ -259,6 +275,11 @@ pub async fn login(
|
||||
init_nip65(client, &public_key).await;
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let config_dir = handle
|
||||
.path()
|
||||
.app_config_dir()
|
||||
.expect("Error: app config directory not found.");
|
||||
|
||||
let state = handle.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
|
||||
@@ -298,64 +319,85 @@ pub async fn login(
|
||||
state.settings.lock().await.clone_from(&settings);
|
||||
};
|
||||
|
||||
let contact_list = client
|
||||
.get_contact_list(Some(Duration::from_secs(5)))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Get user's contact list
|
||||
if let Ok(contacts) = client.get_contact_list(Some(Duration::from_secs(5))).await {
|
||||
state.contact_list.lock().await.clone_from(&contacts);
|
||||
if !contact_list.is_empty() {
|
||||
let authors: Vec<PublicKey> = contact_list.iter().map(|f| f.public_key).collect();
|
||||
|
||||
if !contacts.is_empty() {
|
||||
let pubkeys: Vec<PublicKey> = contacts.iter().map(|f| f.public_key).collect();
|
||||
let metadata = Filter::new()
|
||||
.authors(authors.clone())
|
||||
.kind(Kind::Metadata)
|
||||
.limit(authors.len());
|
||||
|
||||
let newsfeed = Filter::new()
|
||||
.authors(pubkeys.clone())
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.limit(NEWSFEED_NEG_LIMIT);
|
||||
if let Ok(report) = client
|
||||
.reconcile(metadata, NegentropyOptions::default())
|
||||
.await
|
||||
{
|
||||
println!("received [metadata]: {}", report.received.len())
|
||||
}
|
||||
|
||||
if client
|
||||
.reconcile(newsfeed, NegentropyOptions::default())
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
handle.emit("synchronized", ()).unwrap();
|
||||
}
|
||||
let newsfeed = Filter::new()
|
||||
.authors(authors.clone())
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.limit(NEWSFEED_NEG_LIMIT);
|
||||
|
||||
if client
|
||||
.reconcile(newsfeed, NegentropyOptions::default())
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
// Save state
|
||||
let _ = File::create(config_dir.join(public_key.to_bech32().unwrap()));
|
||||
// Update frontend
|
||||
handle.emit("synchronized", ()).unwrap();
|
||||
}
|
||||
|
||||
let contacts = Filter::new()
|
||||
.authors(authors.clone())
|
||||
.kind(Kind::ContactList)
|
||||
.limit(authors.len() * 1000);
|
||||
|
||||
if let Ok(report) = client
|
||||
.reconcile(contacts, NegentropyOptions::default())
|
||||
.await
|
||||
{
|
||||
println!("received [contact list]: {}", report.received.len())
|
||||
}
|
||||
|
||||
for author in authors.into_iter() {
|
||||
let filter = Filter::new()
|
||||
.authors(pubkeys.clone())
|
||||
.author(author)
|
||||
.kind(Kind::ContactList)
|
||||
.limit(4000);
|
||||
.limit(1);
|
||||
|
||||
if client
|
||||
.reconcile(filter, NegentropyOptions::default())
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
for pubkey in pubkeys.into_iter() {
|
||||
let mut list: Vec<PublicKey> = Vec::new();
|
||||
let f = Filter::new()
|
||||
.author(pubkey)
|
||||
.kind(Kind::ContactList)
|
||||
.limit(1);
|
||||
let mut circles = state.circles.lock().await;
|
||||
let mut list: Vec<PublicKey> = Vec::new();
|
||||
|
||||
if let Ok(events) = client.database().query(vec![f]).await {
|
||||
if let Some(event) = events.into_iter().next() {
|
||||
for tag in event.tags.into_iter() {
|
||||
if let Some(TagStandard::PublicKey {
|
||||
public_key,
|
||||
uppercase: false,
|
||||
..
|
||||
}) = tag.to_standardized()
|
||||
{
|
||||
list.push(public_key)
|
||||
}
|
||||
}
|
||||
|
||||
if !list.is_empty() {
|
||||
state.circles.lock().await.insert(pubkey, list);
|
||||
};
|
||||
if let Ok(events) = client.database().query(vec![filter]).await {
|
||||
if let Some(event) = events.into_iter().next() {
|
||||
for tag in event.tags.into_iter() {
|
||||
if let Some(TagStandard::PublicKey {
|
||||
public_key,
|
||||
uppercase: false,
|
||||
..
|
||||
}) = tag.to_standardized()
|
||||
{
|
||||
list.push(public_key)
|
||||
}
|
||||
}
|
||||
|
||||
if !list.is_empty() {
|
||||
circles.insert(author, list);
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
} else {
|
||||
handle.emit("synchronized", ()).unwrap();
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@@ -28,13 +28,7 @@ pub async fn get_event(id: String, state: State<'_, Nostr>) -> Result<RichEvent,
|
||||
let event_id = EventId::parse(&id).map_err(|err| err.to_string())?;
|
||||
let filter = Filter::new().id(event_id);
|
||||
|
||||
match client
|
||||
.get_events_of(
|
||||
vec![filter],
|
||||
EventSource::both(Some(Duration::from_secs(5))),
|
||||
)
|
||||
.await
|
||||
{
|
||||
match client.database().query(vec![filter.clone()]).await {
|
||||
Ok(events) => {
|
||||
if let Some(event) = events.first() {
|
||||
let raw = event.as_json();
|
||||
@@ -46,7 +40,29 @@ pub async fn get_event(id: String, state: State<'_, Nostr>) -> Result<RichEvent,
|
||||
|
||||
Ok(RichEvent { raw, parsed })
|
||||
} else {
|
||||
Err("Cannot found this event with current relay list".into())
|
||||
match client
|
||||
.get_events_of(
|
||||
vec![filter],
|
||||
EventSource::relays(Some(Duration::from_secs(5))),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(events) => {
|
||||
if let Some(event) = events.first() {
|
||||
let raw = event.as_json();
|
||||
let parsed = if event.kind == Kind::TextNote {
|
||||
Some(parse_event(&event.content).await)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(RichEvent { raw, parsed })
|
||||
} else {
|
||||
Err("Cannot found this event with current relay list".into())
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err.to_string()),
|
||||
@@ -191,15 +207,12 @@ pub async fn get_events_by(
|
||||
let author = PublicKey::parse(&public_key).map_err(|err| err.to_string())?;
|
||||
|
||||
let filter = Filter::new()
|
||||
.kinds(vec![Kind::TextNote])
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.author(author)
|
||||
.limit(limit as usize);
|
||||
|
||||
match client
|
||||
.get_events_of(
|
||||
vec![filter],
|
||||
EventSource::both(Some(Duration::from_secs(5))),
|
||||
)
|
||||
.get_events_of(vec![filter], EventSource::Database)
|
||||
.await
|
||||
{
|
||||
Ok(events) => {
|
||||
@@ -224,17 +237,11 @@ pub async fn get_events_by(
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn get_events_from_contacts(
|
||||
pub async fn get_local_events(
|
||||
until: Option<&str>,
|
||||
state: State<'_, Nostr>,
|
||||
) -> Result<Vec<RichEvent>, String> {
|
||||
let client = &state.client;
|
||||
let contact_list = state.contact_list.lock().await;
|
||||
let authors: Vec<PublicKey> = contact_list.iter().map(|f| f.public_key).collect();
|
||||
|
||||
if authors.is_empty() {
|
||||
return Err("Contact List is empty.".into());
|
||||
}
|
||||
|
||||
let as_of = match until {
|
||||
Some(until) => Timestamp::from_str(until).map_err(|err| err.to_string())?,
|
||||
@@ -244,8 +251,7 @@ pub async fn get_events_from_contacts(
|
||||
let filter = Filter::new()
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.limit(FETCH_LIMIT)
|
||||
.until(as_of)
|
||||
.authors(authors);
|
||||
.until(as_of);
|
||||
|
||||
match client.database().query(vec![filter]).await {
|
||||
Ok(events) => {
|
||||
@@ -295,7 +301,7 @@ pub async fn get_group_events(
|
||||
|
||||
let filter = Filter::new()
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.limit(FETCH_LIMIT)
|
||||
.limit(20)
|
||||
.until(as_of)
|
||||
.authors(authors);
|
||||
|
||||
@@ -341,7 +347,7 @@ pub async fn get_global_events(
|
||||
|
||||
let filter = Filter::new()
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.limit(FETCH_LIMIT)
|
||||
.limit(20)
|
||||
.until(as_of);
|
||||
|
||||
match client
|
||||
@@ -385,7 +391,7 @@ pub async fn get_hashtag_events(
|
||||
};
|
||||
let filter = Filter::new()
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.limit(FETCH_LIMIT)
|
||||
.limit(20)
|
||||
.until(as_of)
|
||||
.hashtags(hashtags);
|
||||
|
||||
|
||||
@@ -46,10 +46,7 @@ pub async fn get_profile(id: Option<String>, state: State<'_, Nostr>) -> Result<
|
||||
.kind(Kind::Metadata)
|
||||
.limit(1);
|
||||
|
||||
match client
|
||||
.get_events_of(vec![filter], EventSource::Database)
|
||||
.await
|
||||
{
|
||||
match client.database().query(vec![filter.clone()]).await {
|
||||
Ok(events) => {
|
||||
if let Some(event) = events.first() {
|
||||
if let Ok(metadata) = Metadata::from_json(&event.content) {
|
||||
@@ -58,7 +55,26 @@ pub async fn get_profile(id: Option<String>, state: State<'_, Nostr>) -> Result<
|
||||
Err("Parse metadata failed".into())
|
||||
}
|
||||
} else {
|
||||
Ok(Metadata::new().as_json())
|
||||
match client
|
||||
.get_events_of(
|
||||
vec![filter],
|
||||
EventSource::relays(Some(Duration::from_secs(5))),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(events) => {
|
||||
if let Some(event) = events.first() {
|
||||
if let Ok(metadata) = Metadata::from_json(&event.content) {
|
||||
Ok(metadata.as_json())
|
||||
} else {
|
||||
Err("Parse metadata failed".into())
|
||||
}
|
||||
} else {
|
||||
Ok(Metadata::new().as_json())
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
@@ -80,9 +96,6 @@ pub async fn set_contact_list(
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Update local state
|
||||
state.contact_list.lock().await.clone_from(&contact_list);
|
||||
|
||||
match client.set_contact_list(contact_list).await {
|
||||
Ok(_) => Ok(true),
|
||||
Err(err) => Err(err.to_string()),
|
||||
@@ -94,18 +107,13 @@ pub async fn set_contact_list(
|
||||
pub async fn get_contact_list(state: State<'_, Nostr>) -> Result<Vec<String>, String> {
|
||||
let client = &state.client;
|
||||
|
||||
match client.get_contact_list(Some(Duration::from_secs(10))).await {
|
||||
match client.get_contact_list(Some(Duration::from_secs(5))).await {
|
||||
Ok(contact_list) => {
|
||||
if !contact_list.is_empty() {
|
||||
let list = contact_list
|
||||
.into_iter()
|
||||
.map(|f| f.public_key.to_hex())
|
||||
.collect();
|
||||
|
||||
Ok(list)
|
||||
} else {
|
||||
Err("Empty.".into())
|
||||
}
|
||||
let list = contact_list
|
||||
.into_iter()
|
||||
.map(|f| f.public_key.to_hex())
|
||||
.collect();
|
||||
Ok(list)
|
||||
}
|
||||
Err(err) => Err(err.to_string()),
|
||||
}
|
||||
@@ -144,16 +152,14 @@ pub async fn set_profile(profile: Profile, state: State<'_, Nostr>) -> Result<St
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn is_contact_list_empty(state: State<'_, Nostr>) -> Result<bool, ()> {
|
||||
Ok(state.contact_list.lock().await.is_empty())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn check_contact(hex: String, state: State<'_, Nostr>) -> Result<bool, String> {
|
||||
let contact_list = state.contact_list.lock().await;
|
||||
let client = &state.client;
|
||||
let contact_list = client
|
||||
.get_contact_list(Some(Duration::from_secs(5)))
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
match PublicKey::parse(&hex) {
|
||||
Ok(public_key) => match contact_list.iter().position(|x| x.public_key == public_key) {
|
||||
@@ -190,9 +196,6 @@ pub async fn toggle_contact(
|
||||
}
|
||||
}
|
||||
|
||||
// Update local state
|
||||
state.contact_list.lock().await.clone_from(&contact_list);
|
||||
|
||||
// Publish
|
||||
match client.set_contact_list(contact_list).await {
|
||||
Ok(event_id) => Ok(event_id.to_string()),
|
||||
|
||||
Reference in New Issue
Block a user