Skip to content

Commit

Permalink
feat(channel): add action to channel server callback
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 5, 2023
1 parent 64d847f commit aaa85fe
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/core/channel_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ impl ChannelRequest {
}
}

#[derive(Debug)]
pub enum CallbackAction {
Accept { private: bool },
Cancel,
}

#[derive(Debug)]
pub enum CallbackResponse {
Error(String),
Expand Down
31 changes: 28 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ pub struct Server<CQ, CC, WQ, WC, PQ, PC> {
impl Default
for Server<
unimplemented::Handler0<core::channel_request::ChannelRequest>,
unimplemented::Handler1<(String, String), core::channel_request::CallbackResponse>,
unimplemented::Handler1<
(String, String, core::channel_request::CallbackAction),
core::channel_request::CallbackResponse,
>,
unimplemented::Handler0<core::withdraw_request::WithdrawRequest>,
unimplemented::Handler1<(String, String), core::withdraw_request::CallbackResponse>,
unimplemented::Handler0<core::pay_request::PayRequest>,
Expand Down Expand Up @@ -86,7 +89,10 @@ where
CQ: 'static + Send + Clone + Fn() -> CQFut,
CQFut: Send + Future<Output = Result<core::channel_request::ChannelRequest, StatusCode>>,

CC: 'static + Send + Clone + Fn((String, String)) -> CCFut,
CC: 'static
+ Send
+ Clone
+ Fn((String, String, core::channel_request::CallbackAction)) -> CCFut,
CCFut: Send + Future<Output = Result<core::channel_request::CallbackResponse, StatusCode>>,

WQ: 'static + Send + Clone + Fn() -> WQFut,
Expand Down Expand Up @@ -123,7 +129,24 @@ where

let k1 = qs.get("k1").ok_or(StatusCode::BAD_REQUEST)?;
let remoteid = qs.get("remoteid").ok_or(StatusCode::BAD_REQUEST)?;
let param = (String::from(*k1), String::from(*remoteid));
let action = qs
.get("cancel")
.filter(|v| **v == "1")
.map(|_| core::channel_request::CallbackAction::Cancel)
.or_else(|| {
qs.get("private").and_then(|v| match *v {
"0" => Some(core::channel_request::CallbackAction::Accept {
private: false,
}),
"1" => Some(core::channel_request::CallbackAction::Accept {
private: true,
}),
_ => None,
})
})
.ok_or(StatusCode::BAD_REQUEST)?;

let param = (String::from(*k1), String::from(*remoteid), action);
cc(param).await.map(|a| a.to_string())
}
}),
Expand All @@ -148,6 +171,7 @@ where

let k1 = qs.get("k1").ok_or(StatusCode::BAD_REQUEST)?;
let pr = qs.get("pr").ok_or(StatusCode::BAD_REQUEST)?;

let param = (String::from(*k1), String::from(*pr));
wc(param).await.map(|a| a.to_string())
}
Expand Down Expand Up @@ -177,6 +201,7 @@ where
.ok_or(StatusCode::BAD_REQUEST)?;

let comment = qs.get("comment").map(|c| String::from(*c));

pc((amount, comment)).await.map(|a| a.to_string())
}
}),
Expand Down
35 changes: 31 additions & 4 deletions tests/lud02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ async fn test() {
})
}
},
|(k1, remoteid)| async move {
|(k1, remoteid, action)| async move {
Ok(if remoteid == "idremoto" {
lnurlkit::core::channel_request::CallbackResponse::Ok
} else {
lnurlkit::core::channel_request::CallbackResponse::Error(k1)
lnurlkit::core::channel_request::CallbackResponse::Error(format!(
"{k1}/{action:?}"
))
})
},
)
Expand Down Expand Up @@ -62,10 +64,35 @@ async fn test() {
lnurlkit::core::channel_request::CallbackResponse::Ok
));

let response = cr.callback_cancel("iderrado").await.expect("callback");
let response = cr
.clone()
.callback_cancel("iderrado")
.await
.expect("callback");

assert!(matches!(
response,
lnurlkit::core::channel_request::CallbackResponse::Error(r) if r == "caum/Cancel"
));

let response = cr
.clone()
.callback_accept("iderrado", true)
.await
.expect("callback");

assert!(matches!(
response,
lnurlkit::core::channel_request::CallbackResponse::Error(r) if r == "caum/Accept { private: true }"
));

let response = cr
.callback_accept("iderrado", false)
.await
.expect("callback");

assert!(matches!(
response,
lnurlkit::core::channel_request::CallbackResponse::Error(r) if r == "caum"
lnurlkit::core::channel_request::CallbackResponse::Error(r) if r == "caum/Accept { private: false }"
));
}

0 comments on commit aaa85fe

Please sign in to comment.