This commit is contained in:
2024-10-26 17:24:39 +07:00
parent 470dc1c759
commit 83d24351cd
13 changed files with 191 additions and 496 deletions

View File

@@ -1,126 +1,9 @@
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::{
collections::HashSet,
fs::{self, File},
str::FromStr,
};
use tauri::{ipc::Channel, AppHandle, Manager, State};
use tauri_specta::Event as TauriEvent;
use std::fs::{self, File};
use tauri::{ipc::Channel, Manager, State};
use crate::Nostr;
#[derive(Clone, Serialize, Type, TauriEvent)]
pub struct NegentropyEvent {
kind: NegentropyKind,
total_event: i32,
}
#[derive(Clone, Serialize, Deserialize, Type)]
pub enum NegentropyKind {
Profile,
Metadata,
Events,
EventIds,
Global,
Notification,
Others,
}
pub fn sync_all(accounts: Vec<String>, app_handle: AppHandle) {
if accounts.is_empty() {
return;
};
let public_keys: Vec<PublicKey> = accounts
.iter()
.filter_map(|acc| {
if let Ok(pk) = PublicKey::from_str(acc) {
Some(pk)
} else {
None
}
})
.collect();
tauri::async_runtime::spawn(async move {
let state = app_handle.state::<Nostr>();
let client = &state.client;
let bootstrap_relays = state.bootstrap_relays.lock().unwrap().clone();
// NEG: Sync events for all pubkeys in local database
//
if let Ok(events) = client
.database()
.query(vec![Filter::new()
.authors(public_keys)
.kinds(vec![Kind::ContactList, Kind::FollowSet])])
.await
{
let set: HashSet<PublicKey> = events
.iter()
.flat_map(|ev| ev.tags.public_keys().copied())
.collect();
let pubkeys: Vec<PublicKey> = set.into_iter().collect();
for chunk in pubkeys.chunks(500) {
if chunk.is_empty() {
break;
}
let authors = chunk.to_owned();
// NEG: Sync event
//
let events = Filter::new()
.authors(authors.clone())
.kinds(vec![Kind::TextNote, Kind::Repost])
.limit(500);
if let Ok(output) = client
.sync_with(&bootstrap_relays, events, &SyncOptions::default())
.await
{
NegentropyEvent {
kind: NegentropyKind::Events,
total_event: output.received.len() as i32,
}
.emit(&app_handle)
.unwrap();
}
// NEG: Sync metadata
//
let events = Filter::new()
.authors(authors.clone())
.kinds(vec![
Kind::Metadata,
Kind::InterestSet,
Kind::Interests,
Kind::FollowSet,
Kind::EventDeletion,
Kind::Custom(30315),
])
.limit(500);
if let Ok(output) = client
.sync_with(&bootstrap_relays, events, &SyncOptions::default())
.await
{
NegentropyEvent {
kind: NegentropyKind::Metadata,
total_event: output.received.len() as i32,
}
.emit(&app_handle)
.unwrap();
}
}
}
});
}
#[tauri::command]
#[specta::specta]
pub fn is_account_sync(id: String, app_handle: tauri::AppHandle) -> Result<bool, String> {