Skip to content

Commit

Permalink
Add solver logic
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Oct 26, 2023
1 parent d4fa573 commit 99bf6e2
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ uuid = { version = "1.3.0", features = [
"serde",
] }
reqwest = { version = "0.11", features = ["json"] }
mostro-core = "0.3.9"
mostro-core = "0.3.10"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
config = "0.13.3"
Expand Down
3 changes: 2 additions & 1 deletion migrations/20231005195154_users.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CREATE TABLE IF NOT EXISTS users (
id char(36) primary key not null,
pubkey char(64),
pubkey char(64) not null,
is_admin integer not null default 0,
is_solver integer not null default 0,
is_banned integer not null default 0,
category integer not null default 0,
created_at integer not null
);
8 changes: 8 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod add_invoice;
pub mod admin_add_solver;
pub mod admin_cancel;
pub mod admin_settle;
pub mod cancel;
Expand All @@ -11,6 +12,7 @@ pub mod take_buy;
pub mod take_sell;

use crate::app::add_invoice::add_invoice_action;
use crate::app::admin_add_solver::admin_add_solver_action;
use crate::app::admin_cancel::admin_cancel_action;
use crate::app::admin_settle::admin_settle_action;
use crate::app::cancel::cancel_action;
Expand Down Expand Up @@ -114,6 +116,12 @@ pub async fn run(
)
.await?;
}
Action::AdminAddSolver => {
admin_add_solver_action(
msg, &event, &my_keys, &client, &pool,
)
.await?;
}
_ => todo!(),
}
}
Expand Down
55 changes: 55 additions & 0 deletions src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::db::add_user;
use crate::util::send_dm;

use anyhow::Result;
use log::error;
use mostro_core::user::User;
use mostro_core::{Action, Content, Message};
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};

pub async fn admin_add_solver_action(
msg: Message,
event: &Event,
my_keys: &Keys,
client: &Client,
pool: &Pool<Sqlite>,
) -> Result<()> {
let content = if let Some(content) = msg.content {
content
} else {
error!("AdminAddSolver: No pubkey found!");
return Ok(());
};
println!("Content: {:#?}", content);
let npubkey = if let Content::TextMessage(p) = content {
p
} else {
error!("AdminAddSolver: No pubkey found!");
return Ok(());
};
let user = User::new(npubkey, 0, 1, 0, 0);
add_user(&user, pool).await?;

let mostro_pubkey = my_keys.public_key().to_bech32()?;
// Check if the pubkey is Mostro
if event.pubkey.to_bech32()? != mostro_pubkey {
// We create a Message
let message = Message::new(0, None, None, Action::CantDo, None);
let message = message.as_json()?;
send_dm(client, my_keys, &event.pubkey, message).await?;

return Ok(());
}

// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
// update_order_event(pool, client, my_keys, Status::CanceledByAdmin, &order, None).await?;
// We create a Message
// let message = Message::new(0, Some(order.id), None, Action::AdminCancel, None);
// let message = message.as_json()?;
// Message to admin
// send_dm(client, my_keys, &event.pubkey, message.clone()).await?;

Ok(())
}
30 changes: 30 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use mostro_core::dispute::Dispute;
use mostro_core::order::{Kind, NewOrder, Order, Status};
use mostro_core::user::User;
use nostr_sdk::prelude::*;
use sqlx::migrate::MigrateDatabase;
use sqlx::pool::Pool;
Expand All @@ -21,6 +22,35 @@ pub async fn connect() -> Result<Pool<Sqlite>, sqlx::Error> {
Ok(pool)
}

pub async fn add_user(user: &User, pool: &SqlitePool) -> anyhow::Result<User> {
let mut conn = pool.acquire().await?;
let user = sqlx::query_as::<_, User>(
r#"
INSERT INTO users (
id,
pubkey,
is_admin,
is_solver,
is_banned,
category,
created_at
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
RETURNING *
"#,
)
.bind(user.id)
.bind(&user.pubkey)
.bind(user.is_admin)
.bind(user.is_solver)
.bind(user.is_banned)
.bind(user.category)
.bind(user.created_at)
.fetch_one(&mut conn)
.await?;

Ok(user)
}

pub async fn add_dispute(dispute: &Dispute, pool: &SqlitePool) -> anyhow::Result<Dispute> {
let mut conn = pool.acquire().await?;
let dispute = sqlx::query_as::<_, Dispute>(
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ async fn main() -> Result<()> {
let client = util::connect_nostr().await?;
let my_keys = util::get_keys()?;

// println!("pub {}", my_keys.public_key().to_bech32().unwrap());

let subscription = Filter::new()
.pubkey(my_keys.public_key())
.since(Timestamp::now());
Expand Down

0 comments on commit 99bf6e2

Please sign in to comment.