feat: basic chat flow
This commit is contained in:
25
src-tauri/Cargo.lock
generated
25
src-tauri/Cargo.lock
generated
@@ -837,6 +837,7 @@ name = "coop"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"border",
|
||||
"futures",
|
||||
"itertools",
|
||||
"keyring",
|
||||
"keyring-search",
|
||||
@@ -2610,8 +2611,7 @@ checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
|
||||
[[package]]
|
||||
name = "nostr"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f08db214560a34bf7c4c1fea09a8461b9412bae58ba06e99ce3177d89fa1e0a6"
|
||||
source = "git+https://github.com/rust-nostr/nostr#9e8fea62f3a8c4ab943291a3f265c0a42b457c77"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"base64 0.21.7",
|
||||
@@ -2640,8 +2640,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-database"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9f6c72d0d0842de637f7fba6e70764f719257d29dad8fc5f7352810b0f117ad"
|
||||
source = "git+https://github.com/rust-nostr/nostr#9e8fea62f3a8c4ab943291a3f265c0a42b457c77"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"flatbuffers",
|
||||
@@ -2655,8 +2654,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-relay-pool"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afa5502a3df456790ca16d90cc688a677117d57ab56b079dcfa091390ac9f202"
|
||||
source = "git+https://github.com/rust-nostr/nostr#9e8fea62f3a8c4ab943291a3f265c0a42b457c77"
|
||||
dependencies = [
|
||||
"async-utility",
|
||||
"async-wsocket",
|
||||
@@ -2671,8 +2669,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-sdk"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b427dceefbbb49a9dd98abb8c4e40d25fdd467e99821aaad88615252bdb915bd"
|
||||
source = "git+https://github.com/rust-nostr/nostr#9e8fea62f3a8c4ab943291a3f265c0a42b457c77"
|
||||
dependencies = [
|
||||
"async-utility",
|
||||
"atomic-destructor",
|
||||
@@ -2692,8 +2689,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-signer"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "665268b316f41cd8fa791be54b6c7935c5a239461708c380a699d6677be9af38"
|
||||
source = "git+https://github.com/rust-nostr/nostr#9e8fea62f3a8c4ab943291a3f265c0a42b457c77"
|
||||
dependencies = [
|
||||
"async-utility",
|
||||
"nostr",
|
||||
@@ -2706,8 +2702,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-sqlite"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31f643ba919864f3a9bb004244c0d5c958646b07fe760823fdc33aae1c8fc0fc"
|
||||
source = "git+https://github.com/rust-nostr/nostr#9e8fea62f3a8c4ab943291a3f265c0a42b457c77"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"nostr",
|
||||
@@ -2721,8 +2716,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-zapper"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69922e74f8eab1f9d287008c0c06acdec87277a2d8f44bd9d38e003422aea0ab"
|
||||
source = "git+https://github.com/rust-nostr/nostr#9e8fea62f3a8c4ab943291a3f265c0a42b457c77"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"nostr",
|
||||
@@ -2852,8 +2846,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nwc"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb2e04b3edb5e9572e95b62842430625f1718e8a4a3596a30aeb04e6734764ea"
|
||||
source = "git+https://github.com/rust-nostr/nostr#9e8fea62f3a8c4ab943291a3f265c0a42b457c77"
|
||||
dependencies = [
|
||||
"async-utility",
|
||||
"nostr",
|
||||
|
||||
@@ -11,7 +11,9 @@ edition = "2021"
|
||||
tauri-build = { version = "2.0.0-beta", features = [] }
|
||||
|
||||
[dependencies]
|
||||
nostr-sdk = { version = "0.33", features = ["sqlite"] }
|
||||
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = [
|
||||
"sqlite",
|
||||
] }
|
||||
tauri = { version = "2.0.0-beta", features = [
|
||||
"tray-icon",
|
||||
"macos-private-api",
|
||||
@@ -34,6 +36,7 @@ keyring = { version = "3", features = [
|
||||
] }
|
||||
keyring-search = "1.2.0"
|
||||
itertools = "0.13.0"
|
||||
futures = "0.3.30"
|
||||
specta = "^2.0.0-rc.12"
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
|
||||
@@ -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