Skip to content

Commit

Permalink
chore: Extract non-order related functions into users module
Browse files Browse the repository at this point in the history
These functions didn't really fit into `order` module.
  • Loading branch information
klochowicz committed Sep 13, 2023
1 parent fff8b28 commit 4f4f527
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mobile/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::trade::order::api::NewOrder;
use crate::trade::order::api::Order;
use crate::trade::position;
use crate::trade::position::api::Position;
use crate::trade::users;
use anyhow::Context;
use anyhow::Result;
use flutter_rust_bridge::frb;
Expand Down Expand Up @@ -326,7 +327,7 @@ pub fn get_seed_phrase() -> SyncReturn<Vec<String>> {
/// Enroll a user in the beta program
#[tokio::main(flavor = "current_thread")]
pub async fn register_beta(email: String) -> Result<()> {
order::register_beta(email).await
users::register_beta(email).await
}

/// Send the Firebase token to the LSP for push notifications
Expand Down
1 change: 1 addition & 0 deletions mobile/native/src/trade/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod order;
pub mod position;
pub mod users;
1 change: 0 additions & 1 deletion mobile/native/src/trade/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,3 @@ impl From<OrderType> for orderbook_commons::OrderType {
}
}
}

61 changes: 61 additions & 0 deletions mobile/native/src/trade/users/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::commons::reqwest_client;
use crate::config;
use crate::ln_dlc;
use anyhow::anyhow;
use anyhow::Context;
use anyhow::Result;
use coordinator_commons::RegisterParams;
use coordinator_commons::TokenUpdateParams;

pub async fn update_fcm_token(fcm_token: String) -> Result<()> {
let token_update = TokenUpdateParams {
pubkey: ln_dlc::get_node_info()?.pubkey.to_string(),
fcm_token,
};

reqwest_client()
.post(format!(
"http://{}/api/fcm_token",
config::get_http_endpoint()
))
.json(&token_update)
.send()
.await
.context("Failed to update FCM token with coordinator")?
.error_for_status()?;
Ok(())
}

/// Enroll the user in the beta program
pub async fn register_beta(email: String) -> Result<()> {
let register = RegisterParams {
pubkey: ln_dlc::get_node_info()?.pubkey,
email: Some(email),
nostr: None,
};

let client = reqwest_client();
let response = client
.post(format!(
"http://{}/api/register",
config::get_http_endpoint()
))
.json(&register)
.send()
.await
.context("Failed to register beta program with coordinator")?;

if !response.status().is_success() {
let response_text = match response.text().await {
Ok(text) => text,
Err(err) => {
format!("could not decode response {err:#}")
}
};
return Err(anyhow!(
"Could not register email with coordinator: {response_text}"
));
}
tracing::info!("Registered into beta program successfully");
Ok(())
}

0 comments on commit 4f4f527

Please sign in to comment.