diff --git a/Cargo.lock b/Cargo.lock index 2a1a2d6..9f701e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1724,9 +1724,9 @@ dependencies = [ [[package]] name = "mostro-core" -version = "0.6.46" +version = "0.6.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "108a32cdb4e0a8952e30a9cac2335aec5cf19ba5256b71f34a5ce6bdbec724a2" +checksum = "628171bedc33fbc0b5b7c783566047541954155776ff2e41cbbea517657cf9c5" dependencies = [ "argon2", "base64 0.22.1", diff --git a/Cargo.toml b/Cargo.toml index d7dca94..e7f8abb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ uuid = { version = "1.3.0", features = [ dotenvy = "0.15.6" lightning-invoice = { version = "0.33.2", features = ["std"] } reqwest = { version = "0.12.23", features = ["json"] } -mostro-core = "0.6.46" +mostro-core = "0.6.49" lnurl-rs = "0.9.0" pretty_env_logger = "0.5.0" openssl = { version = "0.10.73", features = ["vendored"] } diff --git a/src/cli/get_dm.rs b/src/cli/get_dm.rs index 8432843..9f46943 100644 --- a/src/cli/get_dm.rs +++ b/src/cli/get_dm.rs @@ -21,7 +21,8 @@ pub async fn execute_get_dm( if !admin { for index in 1..=trade_index { let keys = User::get_trade_keys(&pool, index).await?; - let dm_temp = get_direct_messages(client, &keys, *since, from_user, Some(mostro_pubkey)).await; + let dm_temp = + get_direct_messages(client, &keys, *since, from_user, Some(mostro_pubkey)).await; dm.extend(dm_temp); } } else { @@ -32,7 +33,8 @@ pub async fn execute_get_dm( std::process::exit(1); } }; - let dm_temp = get_direct_messages(client, &id_key, *since, from_user, Some(mostro_pubkey)).await; + let dm_temp = + get_direct_messages(client, &id_key, *since, from_user, Some(mostro_pubkey)).await; dm.extend(dm_temp); } @@ -63,13 +65,10 @@ pub async fn execute_get_dm( println!("{text}"); println!(); } - Payload::Dispute(id, token, info) => { + Payload::Dispute(id, info) => { println!("Action: {}", message.action); println!("Dispute id: {}", id); - if token.is_some() { - println!("Dispute token: {}", token.unwrap()); - } - if info.is_some() { + if let Some(info) = info { println!(); println!("Dispute info: {:#?}", info); println!(); diff --git a/src/cli/new_order.rs b/src/cli/new_order.rs index 528d874..26af7b1 100644 --- a/src/cli/new_order.rs +++ b/src/cli/new_order.rs @@ -82,8 +82,6 @@ pub async fn execute_new_order( invoice.as_ref().to_owned().cloned(), Some(0), expires_at, - None, - None, ); // Create new order for mostro diff --git a/src/db.rs b/src/db.rs index 92d1eab..e5f0a8f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -38,8 +38,6 @@ pub async fn connect() -> Result> { counterparty_pubkey TEXT, is_mine BOOLEAN, buyer_invoice TEXT, - buyer_token INTEGER, - seller_token INTEGER, request_id INTEGER, created_at INTEGER, expires_at INTEGER @@ -66,11 +64,68 @@ pub async fn connect() -> Result> { println!("User created with pubkey: {}", user.i0_pubkey); } else { pool = SqlitePool::connect(&db_url).await?; + + // Migration: Drop buyer_token and seller_token columns if they exist + migrate_remove_token_columns(&pool).await?; } Ok(pool) } +async fn migrate_remove_token_columns(pool: &SqlitePool) -> Result<()> { + println!("Checking for legacy token columns..."); + + // Check if buyer_token column exists + let buyer_token_exists = sqlx::query_scalar::<_, i64>( + "SELECT COUNT(*) FROM pragma_table_info('orders') WHERE name = 'buyer_token'", + ) + .fetch_one(pool) + .await?; + + // Check if seller_token column exists + let seller_token_exists = sqlx::query_scalar::<_, i64>( + "SELECT COUNT(*) FROM pragma_table_info('orders') WHERE name = 'seller_token'", + ) + .fetch_one(pool) + .await?; + + // Drop buyer_token column if it exists + if buyer_token_exists > 0 { + println!("Removing legacy buyer_token column..."); + match sqlx::query("ALTER TABLE orders DROP COLUMN buyer_token") + .execute(pool) + .await + { + Ok(_) => println!("Successfully removed buyer_token column"), + Err(e) => { + println!("Warning: Could not remove buyer_token column: {}", e); + // Continue execution - this is not critical + } + } + } + + // Drop seller_token column if it exists + if seller_token_exists > 0 { + println!("Removing legacy seller_token column..."); + match sqlx::query("ALTER TABLE orders DROP COLUMN seller_token") + .execute(pool) + .await + { + Ok(_) => println!("Successfully removed seller_token column"), + Err(e) => { + println!("Warning: Could not remove seller_token column: {}", e); + // Continue execution - this is not critical + } + } + } + + if buyer_token_exists == 0 && seller_token_exists == 0 { + println!("No legacy token columns found - database is up to date"); + } + + Ok(()) +} + #[derive(Debug, Default, Clone, sqlx::FromRow)] pub struct User { /// The user's ID is the identity pubkey @@ -214,8 +269,6 @@ pub struct Order { pub counterparty_pubkey: Option, pub is_mine: Option, pub buyer_invoice: Option, - pub buyer_token: Option, - pub seller_token: Option, pub request_id: Option, pub created_at: Option, pub expires_at: Option, @@ -248,8 +301,6 @@ impl Order { counterparty_pubkey: None, is_mine: Some(true), buyer_invoice: None, - buyer_token: None, - seller_token: None, request_id, created_at: Some(chrono::Utc::now().timestamp()), expires_at: None, @@ -259,9 +310,8 @@ impl Order { r#" INSERT INTO orders (id, kind, status, amount, min_amount, max_amount, fiat_code, fiat_amount, payment_method, premium, trade_keys, - counterparty_pubkey, is_mine, buyer_invoice, buyer_token, seller_token, - request_id, created_at, expires_at) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + counterparty_pubkey, is_mine, buyer_invoice, request_id, created_at, expires_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "#, ) .bind(&order.id) @@ -278,8 +328,6 @@ impl Order { .bind(&order.counterparty_pubkey) .bind(order.is_mine) .bind(&order.buyer_invoice) - .bind(order.buyer_token) - .bind(order.seller_token) .bind(order.request_id) .bind(order.created_at) .bind(order.expires_at) @@ -359,8 +407,7 @@ impl Order { UPDATE orders SET kind = ?, status = ?, amount = ?, fiat_code = ?, min_amount = ?, max_amount = ?, fiat_amount = ?, payment_method = ?, premium = ?, trade_keys = ?, counterparty_pubkey = ?, - is_mine = ?, buyer_invoice = ?, created_at = ?, expires_at = ?, buyer_token = ?, - seller_token = ? + is_mine = ?, buyer_invoice = ?, expires_at = ? WHERE id = ? "#, ) @@ -377,10 +424,7 @@ impl Order { .bind(&self.counterparty_pubkey) .bind(self.is_mine) .bind(&self.buyer_invoice) - .bind(self.created_at) .bind(self.expires_at) - .bind(self.buyer_token) - .bind(self.seller_token) .bind(id) .execute(pool) .await?; @@ -440,22 +484,15 @@ impl Order { } pub async fn get_all_trade_keys(pool: &SqlitePool) -> Result> { - #[derive(sqlx::FromRow)] - struct TradeKeyRow { - trade_keys: Option, - } - - let rows = sqlx::query_as::<_, TradeKeyRow>( - "SELECT DISTINCT trade_keys FROM orders WHERE trade_keys IS NOT NULL" + let trade_keys: Vec = sqlx::query_scalar::<_, Option>( + "SELECT DISTINCT trade_keys FROM orders WHERE trade_keys IS NOT NULL", ) .fetch_all(pool) - .await?; - - let trade_keys: Vec = rows - .into_iter() - .filter_map(|row| row.trade_keys) - .collect(); - + .await? + .into_iter() + .flatten() + .collect(); + Ok(trade_keys) }