make get_users_zap_address Result

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-09-01 16:52:24 -04:00
parent 2882b1c2d9
commit 594072cfb8
3 changed files with 33 additions and 13 deletions

View File

@@ -100,7 +100,7 @@ fn process_new_zap_event(
}; };
let id = zap_ctx.id; let id = zap_ctx.id;
let promise = send_note_zap( let m_promise = send_note_zap(
ndb, ndb,
txn, txn,
note_target, note_target,
@@ -112,11 +112,15 @@ fn process_new_zap_event(
ctx: zap_ctx, ctx: zap_ctx,
promise, promise,
}); });
let Some(promise) = promise else {
return NextState::Event(EventResponse { let promise = match m_promise {
id, Ok(promise) => promise,
event: Err(ZappingError::InvalidZapAddress), Err(e) => {
}); return NextState::Event(EventResponse {
id,
event: Err(ZappingError::InvoiceFetchFailed(e)),
});
}
}; };
NextState::Transition(promise) NextState::Transition(promise)
@@ -129,7 +133,7 @@ fn send_note_zap(
msats: u64, msats: u64,
nsec: &[u8; 32], nsec: &[u8; 32],
relays: Vec<String>, relays: Vec<String>,
) -> Option<FetchingInvoice> { ) -> Result<FetchingInvoice, ZapError> {
let address = get_users_zap_address(txn, ndb, &note_target.zap_recipient)?; let address = get_users_zap_address(txn, ndb, &note_target.zap_recipient)?;
let promise = match address { let promise = match address {
@@ -140,7 +144,7 @@ fn send_note_zap(
fetch_invoice_lnurl(s, msats, *nsec, ZapTargetOwned::Note(note_target), relays) fetch_invoice_lnurl(s, msats, *nsec, ZapTargetOwned::Note(note_target), relays)
} }
}; };
Some(promise) Ok(promise)
} }
fn try_get_promise_response( fn try_get_promise_response(

View File

@@ -14,6 +14,8 @@ pub use default_zap::{
use enostr::Pubkey; use enostr::Pubkey;
use nostrdb::{Ndb, Transaction}; use nostrdb::{Ndb, Transaction};
use crate::ZapError;
pub enum ZapAddress { pub enum ZapAddress {
Lud16(String), Lud16(String),
Lud06(String), Lud06(String),
@@ -23,15 +25,25 @@ pub fn get_users_zap_address(
txn: &Transaction, txn: &Transaction,
ndb: &Ndb, ndb: &Ndb,
receiver: &Pubkey, receiver: &Pubkey,
) -> Option<ZapAddress> { ) -> Result<ZapAddress, ZapError> {
let profile = ndb let Some(profile) = ndb
.get_profile_by_pubkey(txn, receiver.bytes()) .get_profile_by_pubkey(txn, receiver.bytes())
.ok()? .map_err(|e| ZapError::Ndb(e.to_string()))?
.record() .record()
.profile()?; .profile()
else {
return Err(ZapError::Ndb(format!("No profile for {receiver}")));
};
profile let Some(address) = profile
.lud06() .lud06()
.map(|l| ZapAddress::Lud06(l.to_string())) .map(|l| ZapAddress::Lud06(l.to_string()))
.or(profile.lud16().map(|l| ZapAddress::Lud16(l.to_string()))) .or(profile.lud16().map(|l| ZapAddress::Lud16(l.to_string())))
else {
return Err(ZapError::Ndb(format!(
"profile for {receiver} doesn't have lud06 or lud16"
)));
};
Ok(address)
} }

View File

@@ -401,6 +401,10 @@ mod tests {
}); });
assert!(maybe_invoice.is_ok()); assert!(maybe_invoice.is_ok());
let inner = maybe_invoice.unwrap();
assert!(inner.is_ok());
let inner = inner.unwrap().invoice;
assert!(inner.is_ok());
assert!(maybe_invoice.unwrap().unwrap().invoice.starts_with("lnbc")); assert!(maybe_invoice.unwrap().unwrap().invoice.starts_with("lnbc"));
} }