make get_users_zap_address Result
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -100,7 +100,7 @@ fn process_new_zap_event(
|
||||
};
|
||||
|
||||
let id = zap_ctx.id;
|
||||
let promise = send_note_zap(
|
||||
let m_promise = send_note_zap(
|
||||
ndb,
|
||||
txn,
|
||||
note_target,
|
||||
@@ -112,11 +112,15 @@ fn process_new_zap_event(
|
||||
ctx: zap_ctx,
|
||||
promise,
|
||||
});
|
||||
let Some(promise) = promise else {
|
||||
return NextState::Event(EventResponse {
|
||||
id,
|
||||
event: Err(ZappingError::InvalidZapAddress),
|
||||
});
|
||||
|
||||
let promise = match m_promise {
|
||||
Ok(promise) => promise,
|
||||
Err(e) => {
|
||||
return NextState::Event(EventResponse {
|
||||
id,
|
||||
event: Err(ZappingError::InvoiceFetchFailed(e)),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
NextState::Transition(promise)
|
||||
@@ -129,7 +133,7 @@ fn send_note_zap(
|
||||
msats: u64,
|
||||
nsec: &[u8; 32],
|
||||
relays: Vec<String>,
|
||||
) -> Option<FetchingInvoice> {
|
||||
) -> Result<FetchingInvoice, ZapError> {
|
||||
let address = get_users_zap_address(txn, ndb, ¬e_target.zap_recipient)?;
|
||||
|
||||
let promise = match address {
|
||||
@@ -140,7 +144,7 @@ fn send_note_zap(
|
||||
fetch_invoice_lnurl(s, msats, *nsec, ZapTargetOwned::Note(note_target), relays)
|
||||
}
|
||||
};
|
||||
Some(promise)
|
||||
Ok(promise)
|
||||
}
|
||||
|
||||
fn try_get_promise_response(
|
||||
|
||||
@@ -14,6 +14,8 @@ pub use default_zap::{
|
||||
use enostr::Pubkey;
|
||||
use nostrdb::{Ndb, Transaction};
|
||||
|
||||
use crate::ZapError;
|
||||
|
||||
pub enum ZapAddress {
|
||||
Lud16(String),
|
||||
Lud06(String),
|
||||
@@ -23,15 +25,25 @@ pub fn get_users_zap_address(
|
||||
txn: &Transaction,
|
||||
ndb: &Ndb,
|
||||
receiver: &Pubkey,
|
||||
) -> Option<ZapAddress> {
|
||||
let profile = ndb
|
||||
) -> Result<ZapAddress, ZapError> {
|
||||
let Some(profile) = ndb
|
||||
.get_profile_by_pubkey(txn, receiver.bytes())
|
||||
.ok()?
|
||||
.map_err(|e| ZapError::Ndb(e.to_string()))?
|
||||
.record()
|
||||
.profile()?;
|
||||
.profile()
|
||||
else {
|
||||
return Err(ZapError::Ndb(format!("No profile for {receiver}")));
|
||||
};
|
||||
|
||||
profile
|
||||
let Some(address) = profile
|
||||
.lud06()
|
||||
.map(|l| ZapAddress::Lud06(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)
|
||||
}
|
||||
|
||||
@@ -401,6 +401,10 @@ mod tests {
|
||||
});
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user