feat: add message form
This commit is contained in:
@@ -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()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user