Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
13 changes: 6 additions & 7 deletions src/cli/get_dm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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!();
Expand Down
2 changes: 0 additions & 2 deletions src/cli/new_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 67 additions & 30 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ pub async fn connect() -> Result<Pool<Sqlite>> {
counterparty_pubkey TEXT,
is_mine BOOLEAN,
buyer_invoice TEXT,
buyer_token INTEGER,
seller_token INTEGER,
request_id INTEGER,
created_at INTEGER,
expires_at INTEGER
Expand All @@ -66,11 +64,68 @@ pub async fn connect() -> Result<Pool<Sqlite>> {
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
Expand Down Expand Up @@ -214,8 +269,6 @@ pub struct Order {
pub counterparty_pubkey: Option<String>,
pub is_mine: Option<bool>,
pub buyer_invoice: Option<String>,
pub buyer_token: Option<u16>,
pub seller_token: Option<u16>,
pub request_id: Option<i64>,
pub created_at: Option<i64>,
pub expires_at: Option<i64>,
Expand Down Expand Up @@ -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,
Expand All @@ -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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"#,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
)
.bind(&order.id)
Expand All @@ -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)
Expand Down Expand Up @@ -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 = ?
"#,
)
Expand All @@ -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?;
Expand Down Expand Up @@ -440,22 +484,15 @@ impl Order {
}

pub async fn get_all_trade_keys(pool: &SqlitePool) -> Result<Vec<String>> {
#[derive(sqlx::FromRow)]
struct TradeKeyRow {
trade_keys: Option<String>,
}

let rows = sqlx::query_as::<_, TradeKeyRow>(
"SELECT DISTINCT trade_keys FROM orders WHERE trade_keys IS NOT NULL"
let trade_keys: Vec<String> = sqlx::query_scalar::<_, Option<String>>(
"SELECT DISTINCT trade_keys FROM orders WHERE trade_keys IS NOT NULL",
)
.fetch_all(pool)
.await?;

let trade_keys: Vec<String> = rows
.into_iter()
.filter_map(|row| row.trade_keys)
.collect();

.await?
.into_iter()
.flatten()
.collect();

Ok(trade_keys)
}

Expand Down