Structured files/directories and nip01, nip02, nip17 implementation
This commit is contained in:
24
src/functions/create_contact.rs
Normal file
24
src/functions/create_contact.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use anyhow::{Context, Result};
|
||||
use nostr_sdk::prelude::*;
|
||||
use crate::functions::get_contacts; // Wir nutzen die Logik von nebenan
|
||||
|
||||
pub async fn create_contact(client: &Client, npub: &str, nickname: Option<String>) -> Result<EventId> {
|
||||
let new_pk = PublicKey::parse(npub).context("Ungültiger npub")?;
|
||||
|
||||
// 1. Liste laden (über die ausgelagerte Funktion)
|
||||
let mut current_contacts = get_contacts::get_contacts(client).await?;
|
||||
|
||||
// 2. Alten Eintrag löschen (Update)
|
||||
current_contacts.retain(|c| c.public_key != new_pk);
|
||||
|
||||
// 3. Neuen Kontakt hinzufügen
|
||||
let mut new_contact = Contact::new(new_pk);
|
||||
new_contact.alias = nickname;
|
||||
current_contacts.push(new_contact);
|
||||
|
||||
// 4. Senden
|
||||
let builder = EventBuilder::contact_list(current_contacts);
|
||||
let output = client.send_event_builder(builder).await?;
|
||||
|
||||
Ok(*output.id())
|
||||
}
|
||||
26
src/functions/get_contacts.rs
Normal file
26
src/functions/get_contacts.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use anyhow::Result;
|
||||
use nostr_sdk::prelude::*;
|
||||
use nostr_sdk::nostr::TagStandard;
|
||||
use crate::nips::nip02;
|
||||
|
||||
pub async fn get_contacts(client: &Client) -> Result<Vec<Contact>> {
|
||||
let event_opt = nip02::get_contact_list_event(client).await?;
|
||||
|
||||
match event_opt {
|
||||
Some(event) => {
|
||||
let mut contacts = Vec::new();
|
||||
for tag in event.tags {
|
||||
if let Some(TagStandard::PublicKey { public_key, relay_url, alias, .. }) = tag.as_standardized() {
|
||||
// Werte kopieren/klonen (Ownership Fix)
|
||||
let mut contact = Contact::new(*public_key);
|
||||
contact.relay_url = relay_url.clone();
|
||||
contact.alias = alias.clone();
|
||||
|
||||
contacts.push(contact);
|
||||
}
|
||||
}
|
||||
Ok(contacts)
|
||||
}
|
||||
None => Ok(Vec::new()),
|
||||
}
|
||||
}
|
||||
8
src/functions/messages.rs
Normal file
8
src/functions/messages.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use anyhow::Result;
|
||||
use nostr_sdk::prelude::*;
|
||||
use crate::nips::nip17;
|
||||
|
||||
pub async fn send_private_message(client: &Client, receiver_pubkey: &str, message: &str) -> Result<EventId> {
|
||||
let receiver = PublicKey::parse(receiver_pubkey)?;
|
||||
nip17::send_dm(client, receiver, message).await
|
||||
}
|
||||
5
src/functions/mod.rs
Normal file
5
src/functions/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod new;
|
||||
pub mod messages;
|
||||
pub mod relays;
|
||||
pub mod get_contacts;
|
||||
pub mod create_contact;
|
||||
8
src/functions/new.rs
Normal file
8
src/functions/new.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use anyhow::Result;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
pub async fn new_client(private_key: &str) -> Result<Client> {
|
||||
let keys = Keys::parse(private_key)?;
|
||||
let client = Client::new(keys);
|
||||
Ok(client)
|
||||
}
|
||||
10
src/functions/relays.rs
Normal file
10
src/functions/relays.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use anyhow::Result;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
pub async fn add_relays(client: &Client, urls: Vec<&str>) -> Result<()> {
|
||||
for url in urls {
|
||||
client.add_relay(url.to_string()).await?;
|
||||
}
|
||||
client.connect().await;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user