refactor
This commit is contained in:
59
src-tauri/Cargo.lock
generated
59
src-tauri/Cargo.lock
generated
@@ -593,7 +593,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "border"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#bb267e4d34b34fc01a94a60a14579fb6cdd2a256"
|
||||
source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#4ca90fd2a56565af26aeb5d0518d2e85d80a35b0"
|
||||
dependencies = [
|
||||
"cocoa",
|
||||
"color",
|
||||
@@ -868,7 +868,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "color"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#bb267e4d34b34fc01a94a60a14579fb6cdd2a256"
|
||||
source = "git+https://github.com/ahkohd/tauri-toolkit?branch=v2#4ca90fd2a56565af26aeb5d0518d2e85d80a35b0"
|
||||
dependencies = [
|
||||
"cocoa",
|
||||
"tauri",
|
||||
@@ -2874,7 +2874,7 @@ checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
|
||||
[[package]]
|
||||
name = "nostr"
|
||||
version = "0.33.0"
|
||||
source = "git+https://github.com/rust-nostr/nostr#66d4b5905c8722952644d914ca480b2c33fa1395"
|
||||
source = "git+https://github.com/rust-nostr/nostr#52b4f439c55033a871f2d00aa916160bcd35f6ff"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"base64 0.21.7",
|
||||
@@ -2903,7 +2903,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-database"
|
||||
version = "0.33.0"
|
||||
source = "git+https://github.com/rust-nostr/nostr#66d4b5905c8722952644d914ca480b2c33fa1395"
|
||||
source = "git+https://github.com/rust-nostr/nostr#52b4f439c55033a871f2d00aa916160bcd35f6ff"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"flatbuffers",
|
||||
@@ -2917,7 +2917,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-relay-pool"
|
||||
version = "0.33.0"
|
||||
source = "git+https://github.com/rust-nostr/nostr#66d4b5905c8722952644d914ca480b2c33fa1395"
|
||||
source = "git+https://github.com/rust-nostr/nostr#52b4f439c55033a871f2d00aa916160bcd35f6ff"
|
||||
dependencies = [
|
||||
"async-utility",
|
||||
"async-wsocket",
|
||||
@@ -2932,7 +2932,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-sdk"
|
||||
version = "0.33.0"
|
||||
source = "git+https://github.com/rust-nostr/nostr#66d4b5905c8722952644d914ca480b2c33fa1395"
|
||||
source = "git+https://github.com/rust-nostr/nostr#52b4f439c55033a871f2d00aa916160bcd35f6ff"
|
||||
dependencies = [
|
||||
"async-utility",
|
||||
"atomic-destructor",
|
||||
@@ -2952,7 +2952,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-signer"
|
||||
version = "0.33.0"
|
||||
source = "git+https://github.com/rust-nostr/nostr#66d4b5905c8722952644d914ca480b2c33fa1395"
|
||||
source = "git+https://github.com/rust-nostr/nostr#52b4f439c55033a871f2d00aa916160bcd35f6ff"
|
||||
dependencies = [
|
||||
"async-utility",
|
||||
"nostr",
|
||||
@@ -2965,7 +2965,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-sqlite"
|
||||
version = "0.33.0"
|
||||
source = "git+https://github.com/rust-nostr/nostr#66d4b5905c8722952644d914ca480b2c33fa1395"
|
||||
source = "git+https://github.com/rust-nostr/nostr#52b4f439c55033a871f2d00aa916160bcd35f6ff"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"nostr",
|
||||
@@ -2979,7 +2979,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nostr-zapper"
|
||||
version = "0.33.0"
|
||||
source = "git+https://github.com/rust-nostr/nostr#66d4b5905c8722952644d914ca480b2c33fa1395"
|
||||
source = "git+https://github.com/rust-nostr/nostr#52b4f439c55033a871f2d00aa916160bcd35f6ff"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"nostr",
|
||||
@@ -3099,7 +3099,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "nwc"
|
||||
version = "0.33.0"
|
||||
source = "git+https://github.com/rust-nostr/nostr#66d4b5905c8722952644d914ca480b2c33fa1395"
|
||||
source = "git+https://github.com/rust-nostr/nostr#52b4f439c55033a871f2d00aa916160bcd35f6ff"
|
||||
dependencies = [
|
||||
"async-utility",
|
||||
"nostr",
|
||||
@@ -4719,6 +4719,28 @@ version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.26.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
|
||||
dependencies = [
|
||||
"strum_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum_macros"
|
||||
version = "0.26.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
|
||||
dependencies = [
|
||||
"heck 0.5.0",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn 2.0.72",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.6.1"
|
||||
@@ -5095,12 +5117,17 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-prevent-default"
|
||||
version = "0.1.12"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38be0ac8fcc5fa03422409fc506015b01dc29cc8a3a72572c68d41e6f10f4491"
|
||||
checksum = "c76ff4be3c049bac253390c06f26c805a74b58722f8be97f99550213036df949"
|
||||
dependencies = [
|
||||
"bitflags 2.6.0",
|
||||
"itertools 0.13.0",
|
||||
"serde",
|
||||
"strum",
|
||||
"tauri",
|
||||
"tauri-plugin",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5357,9 +5384,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.39.1"
|
||||
version = "1.39.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d040ac2b29ab03b09d4129c2f5bbd012a3ac2f79d38ff506a4bf8dd34b0eac8a"
|
||||
checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"bytes",
|
||||
@@ -5407,9 +5434,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tokio-socks"
|
||||
version = "0.5.1"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51165dfa029d2a65969413a6cc96f354b86b464498702f174a4efa13608fd8c0"
|
||||
checksum = "0d4770b8024672c1101b3f6733eab95b18007dbe0847a8afe341fcf79e06043f"
|
||||
dependencies = [
|
||||
"either",
|
||||
"futures-util",
|
||||
|
||||
@@ -23,7 +23,7 @@ tauri-specta = { git = "https://github.com/reyamir/tauri-specta", branch = "feat
|
||||
"typescript",
|
||||
] }
|
||||
tauri-plugin-devtools = "2.0.0-beta"
|
||||
tauri-plugin-prevent-default = "0.1"
|
||||
tauri-plugin-prevent-default = "0.2"
|
||||
tauri-plugin-os = "2.0.0-beta"
|
||||
tauri-plugin-clipboard-manager = "2.0.0-beta"
|
||||
tauri-plugin-dialog = "2.0.0-beta"
|
||||
|
||||
@@ -1,29 +1,30 @@
|
||||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "default",
|
||||
"description": "Capability for the main window",
|
||||
"windows": [
|
||||
"main"
|
||||
],
|
||||
"permissions": [
|
||||
"path:default",
|
||||
"event:default",
|
||||
"window:default",
|
||||
"app:default",
|
||||
"image:default",
|
||||
"resources:default",
|
||||
"menu:default",
|
||||
"tray:default",
|
||||
"shell:allow-open",
|
||||
"dialog:default",
|
||||
"dialog:allow-open",
|
||||
"window:allow-close",
|
||||
"window:allow-center",
|
||||
"window:allow-minimize",
|
||||
"window:allow-maximize",
|
||||
"window:allow-set-size",
|
||||
"window:allow-set-focus",
|
||||
"window:allow-start-dragging",
|
||||
"decorum:allow-show-snap-overlay"
|
||||
]
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "default",
|
||||
"description": "Capability for the main window",
|
||||
"windows": [
|
||||
"main"
|
||||
],
|
||||
"permissions": [
|
||||
"path:default",
|
||||
"event:default",
|
||||
"window:default",
|
||||
"app:default",
|
||||
"image:default",
|
||||
"resources:default",
|
||||
"menu:default",
|
||||
"tray:default",
|
||||
"shell:allow-open",
|
||||
"dialog:default",
|
||||
"dialog:allow-open",
|
||||
"window:allow-close",
|
||||
"window:allow-center",
|
||||
"window:allow-minimize",
|
||||
"window:allow-maximize",
|
||||
"window:allow-set-size",
|
||||
"window:allow-set-focus",
|
||||
"window:allow-start-dragging",
|
||||
"decorum:allow-show-snap-overlay",
|
||||
"prevent-default:default"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ pub async fn get_metadata(id: String, state: State<'_, Nostr>) -> Result<String,
|
||||
let public_key = PublicKey::parse(&id).map_err(|e| e.to_string())?;
|
||||
let filter = Filter::new().author(public_key).kind(Kind::Metadata).limit(1);
|
||||
|
||||
match client.get_events_of(vec![filter], None).await {
|
||||
match client.get_events_of(vec![filter], Some(Duration::from_secs(2))).await {
|
||||
Ok(events) => {
|
||||
if let Some(event) = events.first() {
|
||||
Ok(Metadata::from_json(&event.content).unwrap_or(Metadata::new()).as_json())
|
||||
@@ -149,7 +149,6 @@ pub async fn login(
|
||||
state: State<'_, Nostr>,
|
||||
handle: tauri::AppHandle,
|
||||
) -> Result<String, String> {
|
||||
let app = handle.app_handle().clone();
|
||||
let client = &state.client;
|
||||
let public_key = PublicKey::parse(&id).map_err(|e| e.to_string())?;
|
||||
let hex = public_key.to_hex();
|
||||
@@ -187,62 +186,65 @@ pub async fn login(
|
||||
}
|
||||
|
||||
let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1);
|
||||
let old = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
|
||||
let new = Filter::new().kind(Kind::GiftWrap).pubkey(public_key).limit(0);
|
||||
|
||||
let mut relays = Vec::new();
|
||||
|
||||
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(relay)) = tag.as_standardized() {
|
||||
let url = relay.to_string();
|
||||
if client.add_relay(&url).await.is_ok() {
|
||||
relays.push(url)
|
||||
let urls = event
|
||||
.tags()
|
||||
.iter()
|
||||
.filter_map(|tag| {
|
||||
if let Some(TagStandard::Relay(relay)) = tag.as_standardized() {
|
||||
Some(relay.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for url in urls.iter() {
|
||||
let _ = client.add_relay(url).await;
|
||||
let _ = client.connect_relay(url).await;
|
||||
}
|
||||
|
||||
let mut inbox_relays = state.inbox_relays.lock().await;
|
||||
inbox_relays.insert(public_key, urls);
|
||||
}
|
||||
}
|
||||
|
||||
if client.reconcile_with(relays.clone(), old, NegentropyOptions::default()).await.is_ok() {
|
||||
handle.emit("synchronized", ()).unwrap();
|
||||
};
|
||||
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;
|
||||
|
||||
if client.subscribe_to(relays, vec![new], None).await.is_ok() {
|
||||
println!("Waiting for new message...")
|
||||
};
|
||||
let handle_clone = handle.app_handle().clone();
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let window = app.get_webview_window("main").expect("Window is terminated.");
|
||||
let window = handle_clone.get_webview_window("main").expect("Window is terminated.");
|
||||
let state = window.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
|
||||
// Workaround for https://github.com/rust-nostr/nostr/issues/509
|
||||
// TODO: remove
|
||||
let _ = client.get_events_of(vec![Filter::new().kind(Kind::TextNote).limit(0)], None).await;
|
||||
let sync = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
|
||||
let _ = client.reconcile(sync, NegentropyOptions::default()).await;
|
||||
});
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let window = handle.get_webview_window("main").expect("Window is terminated.");
|
||||
let state = window.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
|
||||
client
|
||||
.handle_notifications(|notification| async {
|
||||
if let RelayPoolNotification::Message { message, relay_url } = notification {
|
||||
if let RelayMessage::Event { event, .. } = message {
|
||||
if event.kind == Kind::GiftWrap {
|
||||
match client.unwrap_gift_wrap(&event).await {
|
||||
Ok(UnwrappedGift { rumor, sender }) => {
|
||||
let payload =
|
||||
Payload { event: rumor.as_json(), sender: sender.to_hex() };
|
||||
|
||||
if window.emit("event", payload).is_err() {
|
||||
println!("Failed")
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Unwrapped Error: {} from {}", e, relay_url)
|
||||
}
|
||||
if let RelayPoolNotification::Event { event, .. } = notification {
|
||||
if event.kind == Kind::GiftWrap {
|
||||
if let Ok(UnwrappedGift { rumor, sender }) =
|
||||
client.unwrap_gift_wrap(&event).await
|
||||
{
|
||||
if let Err(e) = window.emit(
|
||||
"event",
|
||||
Payload { event: rumor.as_json(), sender: sender.to_hex() },
|
||||
) {
|
||||
println!("emit failed: {}", e)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("message: {}", message.as_json())
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
|
||||
@@ -1,47 +1,66 @@
|
||||
use std::{cmp::Reverse, time::Duration};
|
||||
|
||||
use futures::stream::{self, StreamExt};
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::{cmp::Reverse, time::Duration};
|
||||
use tauri::State;
|
||||
|
||||
use crate::{common::is_target, Nostr};
|
||||
use crate::{common::is_member, Nostr};
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn get_chats(state: State<'_, Nostr>) -> Result<Vec<String>, String> {
|
||||
pub async fn get_chats(db_only: bool, 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 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);
|
||||
|
||||
match client.get_events_of(vec![filter], None).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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
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()),
|
||||
},
|
||||
};
|
||||
|
||||
let uniqs = rumors
|
||||
.into_iter()
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||
.filter(|ev| ev.pubkey != public_key)
|
||||
.unique_by(|ev| ev.pubkey)
|
||||
.map(|ev| ev.as_json())
|
||||
.collect::<Vec<_>>();
|
||||
let uniqs = events
|
||||
.into_iter()
|
||||
.filter(|ev| ev.pubkey != public_key)
|
||||
.unique_by(|ev| ev.pubkey)
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||
.map(|ev| ev.as_json())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(uniqs)
|
||||
}
|
||||
Err(err) => Err(err.to_string()),
|
||||
}
|
||||
Ok(uniqs)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -49,20 +68,21 @@ pub async fn get_chats(state: State<'_, Nostr>) -> Result<Vec<String>, String> {
|
||||
pub async fn get_chat_messages(id: String, state: State<'_, Nostr>) -> Result<Vec<String>, String> {
|
||||
let client = &state.client;
|
||||
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(id).map_err(|e| e.to_string())?;
|
||||
|
||||
let filter = Filter::new().kind(Kind::GiftWrap).pubkeys(vec![sender_pk, receiver_pk]);
|
||||
let filter = Filter::new().kind(Kind::GiftWrap).pubkeys(vec![receiver_pk, sender_pk]);
|
||||
|
||||
match client.get_events_of(vec![filter], None).await {
|
||||
let rumors = match client.get_events_of(vec![filter], Some(Duration::from_secs(10))).await {
|
||||
Ok(events) => {
|
||||
let rumors = stream::iter(events)
|
||||
stream::iter(events)
|
||||
.filter_map(|ev| async move {
|
||||
if let Ok(UnwrappedGift { rumor, sender }) = client.unwrap_gift_wrap(&ev).await
|
||||
{
|
||||
let is_target = is_target(&sender_pk, &rumor.tags);
|
||||
let is_member = sender == sender_pk;
|
||||
if rumor.kind == Kind::PrivateDirectMessage && (is_member || is_target) {
|
||||
let groups = vec![&receiver_pk, &sender_pk];
|
||||
|
||||
if groups.contains(&&sender) && is_member(groups, &rumor.tags) {
|
||||
Some(rumor.as_json())
|
||||
} else {
|
||||
None
|
||||
@@ -72,44 +92,12 @@ pub async fn get_chat_messages(id: String, state: State<'_, Nostr>) -> Result<Ve
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
Ok(rumors)
|
||||
.await
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn subscribe_to(
|
||||
id: String,
|
||||
relays: Vec<String>,
|
||||
state: State<'_, Nostr>,
|
||||
) -> Result<(), String> {
|
||||
let client = &state.client;
|
||||
let public_key = PublicKey::parse(&id).map_err(|e| e.to_string())?;
|
||||
|
||||
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key).limit(0);
|
||||
let subscription_id = SubscriptionId::new(&id[..6]);
|
||||
|
||||
if client.subscribe_with_id_to(relays, subscription_id, vec![filter], None).await.is_ok() {
|
||||
println!("Watching ... {}", id)
|
||||
Err(e) => return Err(e.to_string()),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn unsubscribe(id: String, state: State<'_, Nostr>) -> Result<(), ()> {
|
||||
let client = &state.client;
|
||||
let subscription_id = SubscriptionId::new(&id[..6]);
|
||||
|
||||
client.unsubscribe(subscription_id).await;
|
||||
println!("Unwatching ... {}", id);
|
||||
|
||||
Ok(())
|
||||
Ok(rumors)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -117,6 +105,7 @@ pub async fn unsubscribe(id: String, state: State<'_, Nostr>) -> Result<(), ()>
|
||||
pub async fn get_inboxes(id: String, state: State<'_, Nostr>) -> Result<Vec<String>, String> {
|
||||
let client = &state.client;
|
||||
let public_key = PublicKey::parse(&id).map_err(|e| e.to_string())?;
|
||||
|
||||
let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1);
|
||||
|
||||
match client.get_events_of(vec![inbox], Some(Duration::from_secs(2))).await {
|
||||
@@ -125,12 +114,17 @@ pub async fn get_inboxes(id: String, state: State<'_, Nostr>) -> Result<Vec<Stri
|
||||
|
||||
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;
|
||||
relays.push(relay);
|
||||
if let Some(TagStandard::Relay(relay)) = tag.as_standardized() {
|
||||
let url = relay.to_string();
|
||||
let _ = client.add_relay(&url).await;
|
||||
let _ = client.connect_relay(&url).await;
|
||||
|
||||
relays.push(url)
|
||||
}
|
||||
}
|
||||
|
||||
let mut inbox_relays = state.inbox_relays.lock().await;
|
||||
inbox_relays.insert(public_key, relays.clone());
|
||||
}
|
||||
|
||||
Ok(relays)
|
||||
@@ -144,15 +138,39 @@ pub async fn get_inboxes(id: String, state: State<'_, Nostr>) -> Result<Vec<Stri
|
||||
pub async fn send_message(
|
||||
to: String,
|
||||
message: String,
|
||||
relays: Vec<String>,
|
||||
state: State<'_, Nostr>,
|
||||
) -> Result<(), 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 receiver = PublicKey::parse(&to).map_err(|e| e.to_string())?;
|
||||
|
||||
match client.send_private_msg_to(relays, receiver, message, None).await {
|
||||
Ok(output) => {
|
||||
println!("send to: {}", output.success.iter().join(", "));
|
||||
// TODO: Add support reply_to
|
||||
let rumor = EventBuilder::private_msg_rumor(receiver, message, None);
|
||||
|
||||
// Get inbox relays
|
||||
let relays = state.inbox_relays.lock().await;
|
||||
|
||||
let outbox = relays.get(&receiver);
|
||||
let inbox = relays.get(&public_key);
|
||||
|
||||
let outbox_urls = match outbox {
|
||||
Some(relays) => relays,
|
||||
None => return Err("User's didn't have inbox relays to receive message.".into()),
|
||||
};
|
||||
|
||||
let inbox_urls = match inbox {
|
||||
Some(relays) => relays,
|
||||
None => return Err("User's didn't have inbox relays to receive message.".into()),
|
||||
};
|
||||
|
||||
match client.gift_wrap_to(outbox_urls, receiver, rumor.clone(), None).await {
|
||||
Ok(_) => {
|
||||
if let Err(e) = client.gift_wrap_to(inbox_urls, public_key, rumor, None).await {
|
||||
return Err(e.to_string());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
pub fn is_target(target: &PublicKey, tags: &Vec<Tag>) -> bool {
|
||||
pub fn is_member(groups: Vec<&PublicKey>, tags: &Vec<Tag>) -> bool {
|
||||
for tag in tags {
|
||||
if let Some(TagStandard::PublicKey { public_key, .. }) = tag.as_standardized() {
|
||||
if public_key == target {
|
||||
if groups.contains(&public_key) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
|
||||
use border::WebviewWindowExt as WebviewWindowExtAlt;
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::Serialize;
|
||||
use std::{fs, sync::Mutex, time::Duration};
|
||||
use tauri::Manager;
|
||||
use std::{collections::HashMap, fs, time::Duration};
|
||||
use tauri::{async_runtime::Mutex, Manager};
|
||||
use tauri_plugin_decorum::WebviewWindowExt;
|
||||
|
||||
use commands::{account::*, chat::*};
|
||||
@@ -13,11 +12,9 @@ use commands::{account::*, chat::*};
|
||||
mod commands;
|
||||
mod common;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Nostr {
|
||||
#[serde(skip_serializing)]
|
||||
client: Client,
|
||||
contact_list: Mutex<Vec<Contact>>,
|
||||
inbox_relays: Mutex<HashMap<PublicKey, Vec<String>>>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -29,12 +26,10 @@ fn main() {
|
||||
connect_account,
|
||||
get_accounts,
|
||||
get_metadata,
|
||||
get_inboxes,
|
||||
get_chats,
|
||||
get_chat_messages,
|
||||
get_inboxes,
|
||||
send_message,
|
||||
subscribe_to,
|
||||
unsubscribe,
|
||||
]);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -50,6 +45,8 @@ fn main() {
|
||||
|
||||
builder
|
||||
.setup(|app| {
|
||||
let handle = app.handle();
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
let main_window = app.get_webview_window("main").unwrap();
|
||||
|
||||
@@ -69,35 +66,35 @@ fn main() {
|
||||
#[cfg(target_os = "macos")]
|
||||
main_window.add_border(None);
|
||||
|
||||
tauri::async_runtime::block_on(async move {
|
||||
let client = tauri::async_runtime::block_on(async move {
|
||||
// Create data folder if not exist
|
||||
let dir = app.path().config_dir().expect("Config Directory not found.");
|
||||
let dir = handle.path().config_dir().expect("Config Directory not found.");
|
||||
let _ = fs::create_dir_all(dir.join("Coop/"));
|
||||
|
||||
// Setup database
|
||||
let database = SQLiteDatabase::open(dir.join("Coop/coop.db")).await;
|
||||
|
||||
// Config
|
||||
let opts = Options::new()
|
||||
.autoconnect(true)
|
||||
.timeout(Duration::from_secs(5))
|
||||
.send_timeout(Some(Duration::from_secs(5)))
|
||||
.connection_timeout(Some(Duration::from_secs(20)));
|
||||
let database =
|
||||
SQLiteDatabase::open(dir.join("Coop/coop.db")).await.expect("Error.");
|
||||
|
||||
// Setup nostr client
|
||||
let client = match database {
|
||||
Ok(db) => ClientBuilder::default().opts(opts).database(db).build(),
|
||||
Err(_) => ClientBuilder::default().opts(opts).build(),
|
||||
};
|
||||
let opts = Options::new()
|
||||
.timeout(Duration::from_secs(30))
|
||||
.send_timeout(Some(Duration::from_secs(5)))
|
||||
.connection_timeout(Some(Duration::from_secs(5)));
|
||||
|
||||
let client = ClientBuilder::default().opts(opts).database(database).build();
|
||||
|
||||
// Add bootstrap relay
|
||||
let _ =
|
||||
client.add_relays(["wss://relay.damus.io/", "wss://relay.nostr.net/"]).await;
|
||||
let _ = client.add_relays(["wss://relay.damus.io", "wss://relay.nostr.net"]).await;
|
||||
|
||||
// Create global state
|
||||
app.handle().manage(Nostr { client, contact_list: Mutex::new(vec![]) })
|
||||
// Connect
|
||||
client.connect().await;
|
||||
|
||||
client
|
||||
});
|
||||
|
||||
// Create global state
|
||||
app.manage(Nostr { client, inbox_relays: Mutex::new(HashMap::new()) });
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.enable_macos_default_menu(false)
|
||||
|
||||
Reference in New Issue
Block a user