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 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 {
|
|
||||||
|
let promise = match m_promise {
|
||||||
|
Ok(promise) => promise,
|
||||||
|
Err(e) => {
|
||||||
return NextState::Event(EventResponse {
|
return NextState::Event(EventResponse {
|
||||||
id,
|
id,
|
||||||
event: Err(ZappingError::InvalidZapAddress),
|
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, ¬e_target.zap_recipient)?;
|
let address = get_users_zap_address(txn, ndb, ¬e_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(
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user