feat: add negentropy sync to settings

This commit is contained in:
2024-11-11 10:02:19 +07:00
parent c93edde7d2
commit c5d06a2492
12 changed files with 388 additions and 142 deletions

View File

@@ -2,4 +2,5 @@ pub mod account;
pub mod event;
pub mod metadata;
pub mod relay;
pub mod sync;
pub mod window;

View File

@@ -0,0 +1,71 @@
use nostr_sdk::prelude::*;
use std::collections::HashSet;
use tauri::State;
use crate::Nostr;
#[tauri::command]
#[specta::specta]
pub async fn sync_all(
state: State<'_, Nostr>,
reader: tauri::ipc::Channel<f64>,
) -> Result<(), String> {
let client = &state.client;
// Create a filter for get all public keys
let filter = Filter::new().kinds(vec![
Kind::TextNote,
Kind::Repost,
Kind::FollowSet,
Kind::ContactList,
Kind::MuteList,
]);
let events = client
.database()
.query(vec![filter])
.await
.map_err(|err| err.to_string())?;
let public_keys: Vec<PublicKey> = events
.iter()
.flat_map(|ev| ev.tags.public_keys().copied())
.collect::<HashSet<_>>()
.into_iter()
.collect();
let (tx, mut rx) = SyncProgress::channel();
let opts = SyncOptions::default().progress(tx);
tauri::async_runtime::spawn(async move {
while rx.changed().await.is_ok() {
let progress = *rx.borrow_and_update();
if progress.total > 0 {
reader.send(progress.percentage() * 100.0).unwrap();
}
}
});
for chunk in public_keys.chunks(200) {
let authors = chunk.to_owned();
let filter = Filter::new().authors(authors).kinds(vec![
Kind::Metadata,
Kind::ContactList,
Kind::FollowSet,
Kind::Interests,
Kind::InterestSet,
Kind::EventDeletion,
Kind::TextNote,
Kind::Repost,
Kind::Comment,
]);
let _ = client
.sync(filter, &opts)
.await
.map_err(|err| err.to_string())?;
}
Ok(())
}

View File

@@ -5,7 +5,7 @@
#[cfg(target_os = "macos")]
use border::WebviewWindowExt as BorderWebviewWindowExt;
use commands::{account::*, event::*, metadata::*, relay::*, window::*};
use commands::{account::*, event::*, metadata::*, relay::*, sync::*, window::*};
use common::{get_all_accounts, parse_event};
use nostr_sdk::prelude::{Profile as DatabaseProfile, *};
use serde::{Deserialize, Serialize};
@@ -76,6 +76,7 @@ fn main() {
tracing_subscriber::fmt::init();
let builder = Builder::<tauri::Wry>::new().commands(collect_commands![
sync_all,
get_all_relays,
get_all_relay_lists,
is_relay_connected,
@@ -365,6 +366,8 @@ fn main() {
// Set interval
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(600));
// Skip the first tick
interval.tick().await;
loop {
interval.tick().await;