feat: add message form
This commit is contained in:
@@ -3,7 +3,7 @@ use keyring::Entry;
|
||||
use keyring_search::{Limit, List, Search};
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::Serialize;
|
||||
use std::{collections::HashSet, time::Duration};
|
||||
use std::collections::HashSet;
|
||||
use tauri::{Emitter, Manager, State};
|
||||
|
||||
use crate::Nostr;
|
||||
@@ -33,7 +33,7 @@ pub async fn get_profile(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], Some(Duration::from_secs(1))).await {
|
||||
match client.get_events_of(vec![filter], None).await {
|
||||
Ok(events) => {
|
||||
if let Some(event) = events.first() {
|
||||
Ok(Metadata::from_json(&event.content).unwrap_or(Metadata::new()).as_json())
|
||||
@@ -53,6 +53,7 @@ pub async fn login(
|
||||
handle: tauri::AppHandle,
|
||||
) -> Result<String, String> {
|
||||
let client = &state.client;
|
||||
let public_key = PublicKey::parse(&id).map_err(|e| e.to_string())?;
|
||||
let keyring = Entry::new(&id, "nostr_secret").expect("Unexpected.");
|
||||
|
||||
let password = match keyring.get_password() {
|
||||
@@ -60,39 +61,36 @@ 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;
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let window = handle.get_webview_window("main").unwrap();
|
||||
let state = window.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1);
|
||||
|
||||
let public_key = PublicKey::parse(&id_clone).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 url = url.to_string();
|
||||
|
||||
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(&url).await.is_ok() {
|
||||
println!("Adding relay {} ...", url);
|
||||
|
||||
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);
|
||||
}
|
||||
if client.connect_relay(&url).await.is_ok() {
|
||||
println!("Connecting relay {} ...", url);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
@@ -129,7 +127,7 @@ pub async fn login(
|
||||
|
||||
client
|
||||
.handle_notifications(|notification| async {
|
||||
if let RelayPoolNotification::Message { message, .. } = notification {
|
||||
if let RelayPoolNotification::Message { message, relay_url } = notification {
|
||||
if let RelayMessage::Event { event, .. } = message {
|
||||
if event.kind == Kind::GiftWrap {
|
||||
if let Ok(UnwrappedGift { rumor, sender }) =
|
||||
@@ -143,6 +141,27 @@ pub async fn login(
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
} else if let RelayMessage::Auth { challenge } = message {
|
||||
match client.auth(challenge, relay_url.clone()).await {
|
||||
Ok(..) => {
|
||||
println!("Authenticated to {} relay.", relay_url);
|
||||
|
||||
if let Ok(relay) = client.relay(relay_url).await {
|
||||
let opts = RelaySendOptions::new().skip_send_confirmation(true);
|
||||
if let Err(e) = relay.resubscribe(opts).await {
|
||||
println!(
|
||||
"Impossible to resubscribe to '{}': {e}",
|
||||
relay.url()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Can't authenticate to '{relay_url}' relay: {e}");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("relay message: {}", message.as_json());
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
@@ -150,7 +169,6 @@ pub async fn login(
|
||||
.await
|
||||
});
|
||||
|
||||
let public_key = PublicKey::parse(&id).unwrap();
|
||||
let hex = public_key.to_hex();
|
||||
|
||||
Ok(hex)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::{cmp::Reverse, time::Duration};
|
||||
|
||||
use futures::stream::{self, StreamExt};
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
@@ -30,7 +32,9 @@ pub async fn get_chats(state: State<'_, Nostr>) -> Result<Vec<String>, String> {
|
||||
|
||||
let uniqs = rumors
|
||||
.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<_>>();
|
||||
|
||||
@@ -77,3 +81,89 @@ pub async fn get_chat_messages(
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn subscribe_to(id: 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(subscription_id, vec![filter], None).await.is_ok() {
|
||||
println!("Watching ... {}", id)
|
||||
};
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
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 {
|
||||
Ok(events) => {
|
||||
let mut relays = Vec::new();
|
||||
|
||||
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;
|
||||
|
||||
relays.push(relay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(relays)
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn drop_inbox(relays: Vec<String>, state: State<'_, Nostr>) -> Result<(), ()> {
|
||||
let client = &state.client;
|
||||
|
||||
for relay in relays.iter() {
|
||||
let _ = client.disconnect_relay(relay).await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn send_message(
|
||||
to: String,
|
||||
message: String,
|
||||
relays: Vec<String>,
|
||||
state: State<'_, Nostr>,
|
||||
) -> Result<(), String> {
|
||||
let client = &state.client;
|
||||
let receiver = PublicKey::parse(&to).map_err(|e| e.to_string())?;
|
||||
|
||||
match client.send_private_msg_to(relays, receiver, message, None).await {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,10 @@ use tauri_plugin_decorum::WebviewWindowExt;
|
||||
|
||||
use commands::{
|
||||
account::{get_accounts, get_profile, login},
|
||||
chat::{get_chat_messages, get_chats},
|
||||
chat::{
|
||||
drop_inbox, get_chat_messages, get_chats, get_inboxes, send_message, subscribe_to,
|
||||
unsubscribe,
|
||||
},
|
||||
};
|
||||
|
||||
mod commands;
|
||||
@@ -29,8 +32,13 @@ fn main() {
|
||||
login,
|
||||
get_accounts,
|
||||
get_profile,
|
||||
get_inboxes,
|
||||
get_chats,
|
||||
get_chat_messages
|
||||
get_chat_messages,
|
||||
send_message,
|
||||
subscribe_to,
|
||||
unsubscribe,
|
||||
drop_inbox
|
||||
]);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -68,8 +76,8 @@ fn main() {
|
||||
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)));
|
||||
.send_timeout(Some(Duration::from_secs(5)))
|
||||
.connection_timeout(Some(Duration::from_secs(20)));
|
||||
|
||||
// Setup nostr client
|
||||
let client = match database {
|
||||
|
||||
Reference in New Issue
Block a user