Skip to content

Commit

Permalink
refactor: sync based on interval not window event
Browse files Browse the repository at this point in the history
  • Loading branch information
reyamir committed Nov 4, 2024
1 parent efd3c83 commit bd1f2b8
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 112 deletions.
16 changes: 8 additions & 8 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added src-tauri/icons/tray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
200 changes: 96 additions & 104 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub mod common;
pub struct Nostr {
client: Client,
queue: RwLock<HashSet<PublicKey>>,
is_syncing: RwLock<bool>,
settings: RwLock<Settings>,
}

Expand Down Expand Up @@ -68,7 +67,7 @@ pub const QUEUE_DELAY: u64 = 300;
pub const NOTIFICATION_SUB_ID: &str = "lume_notification";

fn main() {
tracing_subscriber::fmt::init();
// tracing_subscriber::fmt::init();

let builder = Builder::<tauri::Wry>::new().commands(collect_commands![
get_relays,
Expand Down Expand Up @@ -231,110 +230,9 @@ fn main() {
app.manage(Nostr {
client,
queue: RwLock::new(HashSet::new()),
is_syncing: RwLock::new(false),
settings: RwLock::new(Settings::default()),
});

// Trigger some actions for window events
main_window.on_window_event(move |event| match event {
tauri::WindowEvent::Focused(focused) => {
if !focused {
let handle = handle_clone_event.clone();

tauri::async_runtime::spawn(async move {
let state = handle.state::<Nostr>();
let client = &state.client;

if *state.is_syncing.read().await {
return;
}

let mut is_syncing = state.is_syncing.write().await;

// Mark sync in progress
*is_syncing = true;

let opts = SyncOptions::default();
let accounts = get_all_accounts();

if !accounts.is_empty() {
let public_keys: Vec<PublicKey> = accounts
.iter()
.filter_map(|acc| PublicKey::from_str(acc).ok())
.collect();

let filter = Filter::new().pubkeys(public_keys).kinds(vec![
Kind::TextNote,
Kind::Repost,
Kind::Reaction,
Kind::ZapReceipt,
]);

if let Ok(output) = client.sync(filter, &opts).await {
println!("Received: {}", output.received.len())
}
}

let filter = Filter::new().kinds(vec![
Kind::TextNote,
Kind::Repost,
Kind::ContactList,
Kind::FollowSet,
]);

// Get all public keys in database
if let Ok(events) = client.database().query(vec![filter]).await {
let public_keys: HashSet<PublicKey> = events
.iter()
.flat_map(|ev| ev.tags.public_keys().copied())
.collect();
let pk_vec: Vec<PublicKey> = public_keys.into_iter().collect();

for chunk in pk_vec.chunks(500) {
if chunk.is_empty() {
return;
}

let authors = chunk.to_owned();

let filter = Filter::new()
.authors(authors.clone())
.kinds(vec![
Kind::Metadata,
Kind::FollowSet,
Kind::Interests,
Kind::InterestSet,
])
.limit(1000);

if let Ok(output) = client.sync(filter, &opts).await {
println!("Received: {}", output.received.len())
}

let filter = Filter::new()
.authors(authors)
.kinds(vec![
Kind::TextNote,
Kind::Repost,
Kind::EventDeletion,
])
.limit(500);

if let Ok(output) = client.sync(filter, &opts).await {
println!("Received: {}", output.received.len())
}
}
}

// Mark sync is done
*is_syncing = false;
});
}
}
tauri::WindowEvent::Moved(_size) => {}
_ => {}
});

// Listen for request metadata
app.listen_any("request_metadata", move |event| {
let payload = event.payload();
Expand Down Expand Up @@ -379,7 +277,101 @@ fn main() {
});
});

// Run notification thread
// Run a thread for negentropy
tauri::async_runtime::spawn(async move {
let state = handle_clone_event.state::<Nostr>();
let client = &state.client;

// Use default sync options
let opts = SyncOptions::default();

// Set interval
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(600));

loop {
interval.tick().await;

let accounts = get_all_accounts();
let public_keys: Vec<PublicKey> = accounts
.iter()
.filter_map(|acc| PublicKey::from_str(acc).ok())
.collect();

if !public_keys.is_empty() {
// Create filter for notification
//
let filter = Filter::new().pubkeys(public_keys.clone()).kinds(vec![
Kind::TextNote,
Kind::Repost,
Kind::Reaction,
Kind::ZapReceipt,
]);

// Sync notification
//
if let Ok(output) = client.sync(filter, &opts).await {
println!("Received: {}", output.received.len())
}

// Create filter for contact list
//
let filter = Filter::new()
.authors(public_keys)
.kinds(vec![Kind::ContactList, Kind::FollowSet]);

// Sync events for contact list
//
if let Ok(events) = client.database().query(vec![filter]).await {
// Get unique public keys
let public_keys: HashSet<PublicKey> = events
.iter()
.flat_map(|ev| ev.tags.public_keys().copied())
.collect();

// Convert to vector
let public_keys: Vec<PublicKey> = public_keys.into_iter().collect();

for chunk in public_keys.chunks(1000) {
if chunk.is_empty() {
return;
}

let authors = chunk.to_owned();

// Create filter for metadata
//
let filter = Filter::new().authors(authors.clone()).kinds(vec![
Kind::Metadata,
Kind::FollowSet,
Kind::Interests,
Kind::InterestSet,
]);

// Sync metadata
//
if let Ok(output) = client.sync(filter, &opts).await {
println!("Received: {}", output.received.len())
}

// Create filter for text note
//
let filter = Filter::new()
.authors(authors)
.kinds(vec![Kind::TextNote, Kind::Repost, Kind::EventDeletion])
.limit(100);

// Sync text note
//
if let Ok(output) = client.sync(filter, &opts).await {
println!("Received: {}", output.received.len())
}
}
}
}
}
});

// Run a thread for handle notification
tauri::async_runtime::spawn(async move {
let state = handle_clone.state::<Nostr>();
let client = &state.client;
Expand Down
7 changes: 7 additions & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
"$RESOURCE/*"
]
}
},
"trayIcon": {
"id": "main",
"iconAsTemplate": true,
"menuOnLeftClick": true,
"tooltip": "Lume",
"iconPath": "./icons/tray.png"
}
},
"bundle": {
Expand Down
Binary file added tray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bd1f2b8

Please sign in to comment.