feat: group message by date

This commit is contained in:
reya
2024-07-29 11:04:26 +07:00
parent 9a293c6083
commit a65d5d0c1a
14 changed files with 381 additions and 422 deletions

View File

@@ -211,9 +211,17 @@ pub async fn login(
}
}
let new_message = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let subscription_id = SubscriptionId::new("personal_inbox");
let _ = client.subscribe_with_id(subscription_id, vec![new_message], None).await;
let new_message = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
if client.subscription(&subscription_id).await.is_some() {
// Remove old subscriotion
client.unsubscribe(subscription_id.clone()).await;
// Resubscribe new message for current user
let _ = client.subscribe_with_id(subscription_id, vec![new_message], None).await;
} else {
let _ = client.subscribe_with_id(subscription_id, vec![new_message], None).await;
}
let handle_clone = handle.app_handle().clone();
@@ -223,7 +231,14 @@ pub async fn login(
let client = &state.client;
let sync = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let _ = client.reconcile(sync, NegentropyOptions::default()).await;
if client.reconcile(sync.clone(), NegentropyOptions::default()).await.is_ok() {
handle_clone.emit("synchronized", ()).unwrap();
};
if client.get_events_of(vec![sync], Some(Duration::from_secs(20))).await.is_ok() {
handle_clone.emit("synchronized", ()).unwrap();
};
});
tauri::async_runtime::spawn(async move {

View File

@@ -8,48 +8,28 @@ use crate::{common::is_member, Nostr};
#[tauri::command]
#[specta::specta]
pub async fn get_chats(db_only: bool, state: State<'_, Nostr>) -> Result<Vec<String>, String> {
pub async fn get_chats(state: State<'_, Nostr>) -> Result<Vec<String>, String> {
let client = &state.client;
let signer = client.signer().await.map_err(|e| e.to_string())?;
let public_key = signer.public_key().await.map_err(|e| e.to_string())?;
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let events = match db_only {
true => match client.database().query(vec![filter], Order::Desc).await {
Ok(events) => {
stream::iter(events)
.filter_map(|ev| async move {
if let Ok(UnwrappedGift { rumor, .. }) = client.unwrap_gift_wrap(&ev).await
{
if rumor.kind == Kind::PrivateDirectMessage {
return Some(rumor);
}
let events = match client.database().query(vec![filter], Order::Desc).await {
Ok(events) => {
stream::iter(events)
.filter_map(|ev| async move {
if let Ok(UnwrappedGift { rumor, .. }) = client.unwrap_gift_wrap(&ev).await {
if rumor.kind == Kind::PrivateDirectMessage {
return Some(rumor);
}
None
})
.collect::<Vec<_>>()
.await
}
Err(err) => return Err(err.to_string()),
},
false => match client.get_events_of(vec![filter], Some(Duration::from_secs(12))).await {
Ok(events) => {
stream::iter(events)
.filter_map(|ev| async move {
if let Ok(UnwrappedGift { rumor, .. }) = client.unwrap_gift_wrap(&ev).await
{
if rumor.kind == Kind::PrivateDirectMessage {
return Some(rumor);
}
}
None
})
.collect::<Vec<_>>()
.await
}
Err(err) => return Err(err.to_string()),
},
}
None
})
.collect::<Vec<_>>()
.await
}
Err(err) => return Err(err.to_string()),
};
let uniqs = events

View File

@@ -70,12 +70,11 @@ fn main() {
let client = tauri::async_runtime::block_on(async move {
// Create data folder if not exist
let dir = handle.path().config_dir().expect("Config Directory not found.");
let _ = fs::create_dir_all(dir.join("Coop/"));
let dir = handle.path().app_config_dir().expect("App config directory not found.");
let _ = fs::create_dir_all(dir.clone());
// Setup database
let database =
SQLiteDatabase::open(dir.join("Coop/coop.db")).await.expect("Error.");
let database = SQLiteDatabase::open(dir.join("nostr.db")).await.expect("Error.");
// Setup nostr client
let opts = Options::new()