Skip to content

Commit 83aa8aa

Browse files
committed
fix(api): re-enable payment notifications over websockets
also fix review comments
1 parent 26734f9 commit 83aa8aa

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/interledger-api/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ warp = { version = "0.2", default-features = false }
3434
secrecy = { version = "0.6", default-features = false, features = ["serde"] }
3535
lazy_static = "1.4.0"
3636
async-trait = "0.1.22"
37-
mime = "0.3.16"
3837

3938
[dev-dependencies]
4039
tokio = { version = "0.2.9", features = ["rt-core", "macros"] }

crates/interledger-api/src/http_retry.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ impl Client {
2828
}
2929
}
3030

31-
pub async fn create_engine_account<T: Display + Copy + Unpin>(
31+
pub async fn create_engine_account<T: Display>(
3232
&self,
3333
engine_url: Url,
3434
id: T,
3535
) -> Result<StatusCode, reqwest::Error> {
3636
let mut se_url = engine_url.clone();
37+
let id: String = id.to_string();
3738
se_url
3839
.path_segments_mut()
3940
.expect("Invalid settlement engine URL")
@@ -56,7 +57,7 @@ impl Client {
5657
move || {
5758
client
5859
.post(se_url.as_ref())
59-
.json(&json!({"id" : id.to_string()}))
60+
.json(&json!({ "id": id }))
6061
.send()
6162
.map_ok(move |response| response.status())
6263
},

crates/interledger-api/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub trait NodeStore: AddressStore + Clone + Send + Sync + 'static {
101101
/// Sets the static routes for routing
102102
async fn set_static_routes<R>(&self, routes: R) -> Result<(), ()>
103103
where
104+
// The 'async_trait lifetime is used after recommendation here:
105+
// https://github.com/dtolnay/async-trait/issues/8#issuecomment-514812245
104106
R: IntoIterator<Item = (String, Uuid)> + Send + 'async_trait;
105107

106108
/// Sets a single static route
@@ -113,6 +115,8 @@ pub trait NodeStore: AddressStore + Clone + Send + Sync + 'static {
113115
/// Sets the default settlement engines to be used for the provided asset codes
114116
async fn set_settlement_engines(
115117
&self,
118+
// The 'async_trait lifetime is used after recommendation here:
119+
// https://github.com/dtolnay/async-trait/issues/8#issuecomment-514812245
116120
asset_to_url_map: impl IntoIterator<Item = (String, Url)> + Send + 'async_trait,
117121
) -> Result<(), ()>;
118122

crates/interledger-api/src/routes/accounts.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{http_retry::Client, number_or_string, AccountDetails, AccountSettings, NodeStore};
22
use bytes::Bytes;
3-
use futures::{future::join_all, TryFutureExt};
3+
use futures::{future::join_all, Future, FutureExt, StreamExt, TryFutureExt};
44
use interledger_btp::{connect_to_service_account, BtpAccount, BtpOutgoingService};
55
use interledger_ccp::{CcpRoutingAccount, Mode, RouteControlRequest, RoutingRelation};
66
use interledger_http::{deserialize_json, error::*, HttpAccount, HttpStore};
@@ -13,7 +13,7 @@ use interledger_service::{
1313
use interledger_service_util::{BalanceStore, ExchangeRateStore};
1414
use interledger_settlement::core::types::SettlementAccount;
1515
use interledger_spsp::{pay, SpspResponder};
16-
use interledger_stream::StreamNotificationsStore;
16+
use interledger_stream::{PaymentNotification, StreamNotificationsStore};
1717
use log::{debug, error, trace};
1818
use secrecy::{ExposeSecret, SecretString};
1919
use serde::{Deserialize, Serialize};
@@ -345,20 +345,9 @@ where
345345
.and(warp::path::end())
346346
.and(warp::ws())
347347
.and(with_store.clone())
348-
.map(|_id: Uuid, ws: warp::ws::Ws, _store: S| {
349-
ws.on_upgrade(move |_ws: warp::ws::WebSocket| {
350-
async {
351-
// TODO: Implement this.
352-
unimplemented!()
353-
// let (tx, rx) = futures::channel::mpsc::unbounded::<PaymentNotification>();
354-
// store.add_payment_notification_subscription(id, tx);
355-
// rx.map_err(|_| -> warp::Error { unreachable!("unbounded rx never errors") })
356-
// .map(|notification| {
357-
// warp::ws::Message::text(serde_json::to_string(&notification).unwrap())
358-
// })
359-
// .map(|_| ())
360-
// .map_err(|err| error!("Error forwarding notifications to websocket: {:?}", err))
361-
}
348+
.map(|id: Uuid, ws: warp::ws::Ws, store: S| {
349+
ws.on_upgrade(move |ws: warp::ws::WebSocket| {
350+
notify_user(ws, id, store).map(|result| result.unwrap())
362351
})
363352
})
364353
.boxed();
@@ -474,11 +463,37 @@ where
474463
.or(get_account)
475464
.or(get_account_balance)
476465
.or(put_account_settings)
477-
.or(incoming_payment_notifications) // Commented out until tungenstite ws support is added
466+
.or(incoming_payment_notifications)
478467
.or(post_payments)
479468
.boxed()
480469
}
481470

471+
fn notify_user(
472+
socket: warp::ws::WebSocket,
473+
id: Uuid,
474+
store: impl StreamNotificationsStore,
475+
) -> impl Future<Output = Result<(), ()>> {
476+
let (tx, rx) = futures::channel::mpsc::unbounded::<PaymentNotification>();
477+
// the client is now subscribed
478+
store.add_payment_notification_subscription(id, tx);
479+
480+
// Anytime something is written to tx, it will reach rx
481+
// and get converted to a warp::ws::Message
482+
let rx = rx.map(|notification: PaymentNotification| {
483+
let msg = warp::ws::Message::text(serde_json::to_string(&notification).unwrap());
484+
Ok(msg)
485+
});
486+
487+
// Then it gets forwarded to the client
488+
rx.forward(socket)
489+
.map(|result| {
490+
if let Err(e) = result {
491+
eprintln!("websocket send error: {}", e);
492+
}
493+
})
494+
.then(futures::future::ok)
495+
}
496+
482497
async fn get_address_from_parent_and_update_routes<O, A, S>(
483498
mut service: O,
484499
parent: A,

0 commit comments

Comments
 (0)