feat: basic chat flow
This commit is contained in:
@@ -2,11 +2,18 @@ use itertools::Itertools;
|
||||
use keyring::Entry;
|
||||
use keyring_search::{Limit, List, Search};
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::{collections::HashSet, str::FromStr};
|
||||
use tauri::{Manager, State};
|
||||
use serde::Serialize;
|
||||
use std::{collections::HashSet, time::Duration};
|
||||
use tauri::{Emitter, Manager, State};
|
||||
|
||||
use crate::Nostr;
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
struct Payload {
|
||||
event: String,
|
||||
sender: String,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn get_accounts() -> Vec<String> {
|
||||
@@ -21,17 +28,20 @@ pub fn get_accounts() -> Vec<String> {
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn get_profile(id: String, state: State<'_, Nostr>) -> Result<String, ()> {
|
||||
pub async fn get_profile(id: String, state: State<'_, Nostr>) -> Result<String, String> {
|
||||
let client = &state.client;
|
||||
let public_key = PublicKey::from_str(&id).unwrap();
|
||||
let public_key = PublicKey::parse(&id).map_err(|e| e.to_string())?;
|
||||
let filter = Filter::new().author(public_key).kind(Kind::Metadata).limit(1);
|
||||
|
||||
let events = client.get_events_of(vec![filter], None).await.unwrap();
|
||||
|
||||
if let Some(event) = events.first() {
|
||||
Ok(Metadata::from_json(&event.content).unwrap().as_json())
|
||||
} else {
|
||||
Ok(Metadata::new().as_json())
|
||||
match client.get_events_of(vec![filter], Some(Duration::from_secs(1))).await {
|
||||
Ok(events) => {
|
||||
if let Some(event) = events.first() {
|
||||
Ok(Metadata::from_json(&event.content).unwrap_or(Metadata::new()).as_json())
|
||||
} else {
|
||||
Ok(Metadata::new().as_json())
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +51,7 @@ pub async fn login(
|
||||
id: String,
|
||||
state: State<'_, Nostr>,
|
||||
handle: tauri::AppHandle,
|
||||
) -> Result<(), String> {
|
||||
) -> Result<String, String> {
|
||||
let client = &state.client;
|
||||
let keyring = Entry::new(&id, "nostr_secret").expect("Unexpected.");
|
||||
|
||||
@@ -50,37 +60,44 @@ pub async fn login(
|
||||
Err(_) => return Err("Cancelled".into()),
|
||||
};
|
||||
|
||||
let id_clone = id.clone();
|
||||
let keys = Keys::parse(password).expect("Secret Key is modified, please check again.");
|
||||
let signer = NostrSigner::Keys(keys);
|
||||
|
||||
// Set signer
|
||||
client.set_signer(Some(signer)).await;
|
||||
|
||||
let public_key = PublicKey::from_str(&id).unwrap();
|
||||
let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1);
|
||||
|
||||
if let Ok(events) = client.get_events_of(vec![inbox], None).await {
|
||||
if let Some(event) = events.into_iter().next() {
|
||||
for tag in &event.tags {
|
||||
if let Some(TagStandard::Relay(url)) = tag.as_standardized() {
|
||||
let relay = url.to_string();
|
||||
let _ = client.add_relay(&relay).await;
|
||||
let _ = client.connect_relay(&relay).await;
|
||||
|
||||
println!("Connecting to {} ...", relay);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let window = handle.get_webview_window("main").unwrap();
|
||||
let state = window.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
|
||||
let incoming = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
|
||||
let public_key = PublicKey::parse(&id_clone).unwrap();
|
||||
let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1);
|
||||
|
||||
if let Ok(report) = client.reconcile(incoming.clone(), NegentropyOptions::default()).await {
|
||||
if let Ok(events) = client.get_events_of(vec![inbox], None).await {
|
||||
if let Some(event) = events.into_iter().next() {
|
||||
for tag in &event.tags {
|
||||
if let Some(TagStandard::Relay(url)) = tag.as_standardized() {
|
||||
let opts = RelayOptions::new().retry_sec(5);
|
||||
let url = url.to_string();
|
||||
|
||||
if client.add_relay_with_opts(&url, opts).await.is_ok() {
|
||||
println!("Adding relay {} ...", url);
|
||||
|
||||
if client.connect_relay(&url).await.is_ok() {
|
||||
println!("Connecting relay {} ...", url);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let old = Filter::new().kind(Kind::GiftWrap).pubkey(public_key).until(Timestamp::now());
|
||||
let new = Filter::new().kind(Kind::GiftWrap).pubkey(public_key).limit(0);
|
||||
|
||||
if let Ok(report) = client.reconcile(old, NegentropyOptions::default()).await {
|
||||
let receives = report.received.clone();
|
||||
let ids = receives.into_iter().collect::<Vec<_>>();
|
||||
|
||||
@@ -104,12 +121,37 @@ pub async fn login(
|
||||
println!("Sync done.")
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if client.subscribe(vec![incoming.limit(0)], None).await.is_ok() {
|
||||
if client.subscribe(vec![new], None).await.is_ok() {
|
||||
println!("Waiting for new message...")
|
||||
}
|
||||
};
|
||||
|
||||
client
|
||||
.handle_notifications(|notification| async {
|
||||
if let RelayPoolNotification::Message { message, .. } = notification {
|
||||
if let RelayMessage::Event { event, .. } = message {
|
||||
if event.kind == Kind::GiftWrap {
|
||||
if let Ok(UnwrappedGift { rumor, sender }) =
|
||||
client.unwrap_gift_wrap(&event).await
|
||||
{
|
||||
window
|
||||
.emit(
|
||||
"event",
|
||||
Payload { event: rumor.as_json(), sender: sender.to_hex() },
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
})
|
||||
.await
|
||||
});
|
||||
|
||||
Ok(())
|
||||
let public_key = PublicKey::parse(&id).unwrap();
|
||||
let hex = public_key.to_hex();
|
||||
|
||||
Ok(hex)
|
||||
}
|
||||
|
||||
79
src-tauri/src/commands/chat.rs
Normal file
79
src-tauri/src/commands/chat.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use futures::stream::{self, StreamExt};
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
use tauri::State;
|
||||
|
||||
use crate::{common::is_target, Nostr};
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn get_chats(state: State<'_, Nostr>) -> Result<Vec<String>, String> {
|
||||
let client = &state.client;
|
||||
let signer = client.signer().await.expect("Unexpected");
|
||||
let public_key = signer.public_key().await.expect("Unexpected");
|
||||
|
||||
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
|
||||
|
||||
match client.database().query(vec![filter], Order::Desc).await {
|
||||
Ok(events) => {
|
||||
let rumors = 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;
|
||||
|
||||
let uniqs = rumors
|
||||
.into_iter()
|
||||
.unique_by(|ev| ev.pubkey)
|
||||
.map(|ev| ev.as_json())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(uniqs)
|
||||
}
|
||||
Err(err) => Err(err.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn get_chat_messages(
|
||||
sender: String,
|
||||
state: State<'_, Nostr>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
let client = &state.client;
|
||||
let database = client.database();
|
||||
let signer = client.signer().await.map_err(|e| e.to_string())?;
|
||||
let receiver_pk = signer.public_key().await.map_err(|e| e.to_string())?;
|
||||
let sender_pk = PublicKey::parse(sender).map_err(|e| e.to_string())?;
|
||||
|
||||
let filter = Filter::new().kind(Kind::GiftWrap).pubkeys(vec![receiver_pk, sender_pk]);
|
||||
|
||||
match database.query(vec![filter], Order::Desc).await {
|
||||
Ok(events) => {
|
||||
let rumors = stream::iter(events)
|
||||
.filter_map(|ev| async move {
|
||||
if let Ok(UnwrappedGift { rumor, sender }) = client.unwrap_gift_wrap(&ev).await
|
||||
{
|
||||
if rumor.kind == Kind::PrivateDirectMessage
|
||||
&& (sender == sender_pk || is_target(&sender_pk, &rumor.tags))
|
||||
{
|
||||
return Some(rumor);
|
||||
}
|
||||
}
|
||||
None
|
||||
})
|
||||
.map(|ev| ev.as_json())
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
Ok(rumors)
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod account;
|
||||
pub mod chat;
|
||||
|
||||
12
src-tauri/src/common.rs
Normal file
12
src-tauri/src/common.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
pub fn is_target(target: &PublicKey, tags: &Vec<Tag>) -> bool {
|
||||
for tag in tags {
|
||||
if let Some(TagStandard::PublicKey { public_key, .. }) = tag.as_standardized() {
|
||||
if public_key == target {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
@@ -2,14 +2,19 @@
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use border::WebviewWindowExt as WebviewWindowExtAlt;
|
||||
use commands::account::{get_accounts, get_profile, login};
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::Serialize;
|
||||
use std::{fs, sync::Mutex};
|
||||
use std::{fs, sync::Mutex, time::Duration};
|
||||
use tauri::Manager;
|
||||
use tauri_plugin_decorum::WebviewWindowExt;
|
||||
|
||||
use commands::{
|
||||
account::{get_accounts, get_profile, login},
|
||||
chat::{get_chat_messages, get_chats},
|
||||
};
|
||||
|
||||
mod commands;
|
||||
mod common;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Nostr {
|
||||
@@ -19,12 +24,13 @@ pub struct Nostr {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut ctx = tauri::generate_context!();
|
||||
let invoke_handler = {
|
||||
let builder = tauri_specta::ts::builder().commands(tauri_specta::collect_commands![
|
||||
get_accounts,
|
||||
login,
|
||||
get_profile
|
||||
get_accounts,
|
||||
get_profile,
|
||||
get_chats,
|
||||
get_chat_messages
|
||||
]);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -39,8 +45,13 @@ fn main() {
|
||||
let main_window = app.get_webview_window("main").unwrap();
|
||||
|
||||
// Set custom decoration
|
||||
#[cfg(target_os = "windows")]
|
||||
main_window.create_overlay_titlebar().unwrap();
|
||||
|
||||
// Set traffic light inset
|
||||
#[cfg(target_os = "macos")]
|
||||
main_window.set_traffic_lights_inset(12.0, 18.0).unwrap();
|
||||
|
||||
// Restore native border
|
||||
#[cfg(target_os = "macos")]
|
||||
main_window.add_border(None);
|
||||
@@ -53,10 +64,17 @@ fn main() {
|
||||
// Setup database
|
||||
let database = SQLiteDatabase::open(dir.join("Coop/coop.db")).await;
|
||||
|
||||
// Config
|
||||
let opts = Options::new()
|
||||
.automatic_authentication(true)
|
||||
.timeout(Duration::from_secs(5))
|
||||
.send_timeout(Some(Duration::from_secs(10)))
|
||||
.connection_timeout(Some(Duration::from_secs(10)));
|
||||
|
||||
// Setup nostr client
|
||||
let client = match database {
|
||||
Ok(db) => ClientBuilder::default().database(db).build(),
|
||||
Err(_) => ClientBuilder::default().build(),
|
||||
Ok(db) => ClientBuilder::default().opts(opts).database(db).build(),
|
||||
Err(_) => ClientBuilder::default().opts(opts).build(),
|
||||
};
|
||||
|
||||
// Add bootstrap relay
|
||||
@@ -79,6 +97,6 @@ fn main() {
|
||||
.plugin(tauri_plugin_decorum::init())
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.invoke_handler(invoke_handler)
|
||||
.run(ctx)
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user