Some improments and Negentropy (#219)

* feat: adjust default window size

* feat: save window state

* feat: add window state plugin

* feat: add search

* feat: use negentropy for newsfeed

* feat: live feeds

* feat: add search user
This commit is contained in:
雨宮蓮
2024-06-30 14:26:02 +07:00
committed by GitHub
parent 968b1ada94
commit 0fec21b9ce
46 changed files with 5633 additions and 3938 deletions

View File

@@ -10,7 +10,7 @@ use tauri_plugin_notification::NotificationExt;
use crate::nostr::event::RichEvent;
use crate::nostr::utils::parse_event;
use crate::{Nostr, Settings};
use crate::{Nostr, Settings, NEWSFEED_NEG_LIMIT, NOTIFICATION_NEG_LIMIT};
#[derive(Serialize, Type)]
pub struct Account {
@@ -242,8 +242,9 @@ pub async fn load_account(
};
// Get user's contact list
let contacts = client.get_contact_list(None).await.unwrap();
*state.contact_list.lock().unwrap() = contacts;
if let Ok(contacts) = client.get_contact_list(None).await {
*state.contact_list.lock().unwrap() = contacts
};
// Create a subscription for notification
let sub_id = SubscriptionId::new("notification");
@@ -299,8 +300,9 @@ pub async fn load_account(
let window = handle.get_window("main").unwrap();
let state = window.state::<Nostr>();
let client = &state.client;
let contact_list = state.contact_list.lock().unwrap().clone();
let filter = Filter::new()
let notification = Filter::new()
.pubkey(public_key)
.kinds(vec![
Kind::TextNote,
@@ -308,11 +310,39 @@ pub async fn load_account(
Kind::Reaction,
Kind::ZapReceipt,
])
.limit(500);
.limit(NOTIFICATION_NEG_LIMIT);
match client.reconcile(filter, NegentropyOptions::default()).await {
Ok(_) => println!("Sync notification done."),
match client
.reconcile(notification, NegentropyOptions::default())
.await
{
Ok(_) => {
if handle.emit_to(EventTarget::Any, "synced", true).is_err() {
println!("Emit event failed.")
}
}
Err(_) => println!("Sync notification failed."),
};
if !contact_list.is_empty() {
let authors: Vec<PublicKey> = contact_list.into_iter().map(|f| f.public_key).collect();
let newsfeed = Filter::new()
.authors(authors)
.kinds(vec![Kind::TextNote, Kind::Repost])
.limit(NEWSFEED_NEG_LIMIT);
match client
.reconcile(newsfeed, NegentropyOptions::default())
.await
{
Ok(_) => {
if handle.emit_to(EventTarget::Any, "synced", true).is_err() {
println!("Emit event failed.")
}
}
Err(_) => println!("Sync newsfeed failed."),
}
}
});
@@ -324,7 +354,7 @@ pub async fn load_account(
let client = &state.client;
// Handle notifications
if client
client
.handle_notifications(|notification| async {
if let RelayPoolNotification::Message { message, .. } = notification {
if let RelayMessage::Event {
@@ -415,6 +445,24 @@ pub async fn load_account(
{
println!("Emit new notification failed.")
}
} else if id.starts_with("column-") {
let raw = event.as_json();
let parsed = if event.kind == Kind::TextNote {
Some(parse_event(&event.content).await)
} else {
None
};
if app
.emit_to(
EventTarget::window(id),
"new_event",
RichEvent { raw, parsed },
)
.is_err()
{
println!("Emit new notification failed.")
}
} else {
println!("new event: {}", event.as_json())
}
@@ -425,10 +473,6 @@ pub async fn load_account(
Ok(false)
})
.await
.is_ok()
{
print!("Listing for new event...");
}
});
Ok(true)