Skip to content

Commit

Permalink
working on incoming messages with trade-index
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Dec 5, 2024
1 parent 9cde2c9 commit 29776c4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
18 changes: 8 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use std::sync::Arc;
use tokio::sync::Mutex;
use mostro_core::user::User;
use sqlx_crud::Crud;

use crate::db::is_user_present;
/// Helper function to log warning messages for action errors
fn warning_msg(action: &Action, e: anyhow::Error) {
tracing::warn!("Error in {} with context {}", action, e);
Expand Down Expand Up @@ -147,29 +147,27 @@ pub async fn run(
if event.rumor.created_at.as_u64() < since_time {
continue;
}



// Parse and process the message
let message = Message::from_json(&event.rumor.content);
match message {
Ok(msg) => {
if msg.get_inner_message_kind().verify() {
let is_trade_index = msg.get_inner_message_kind().trade_index.is_some;
let message_kind = msg.get_inner_message_kind();
// Function to search if user is yet present in db
match db::is_new_user(event.sender){
match is_user_present(&pool, event.sender.to_string()).await{
Ok(user) => {
let next_trade_index = user.trade_index + 1;
if is_trade_index == next_trade_index {
user.trade_index = next_trade_index;
if user.trade_index < message_kind.get_trade_index() {
user.trade_index = message_kind.get_trade_index();
if msg.get_inner_message_kind().verify_content_signature(event.sender){
if let Ok(user) = user.update(pool).await{
if let Ok(user) = user.update(&pool).await{
tracing::info!("Update user trade index");
}
}
}
},
Err(_) => {
let new_user = User::new{ pubkey: event.sender, trade_index: next_trade_index, ..Default::default()};
let new_user = User::new{ pubkey: event.sender, trade_index: message_kind.get_trade_index(), ..Default::default()};
if let Ok(user) = new_user.update(pool).await{
tracing::info!("Added new user for rate");
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub async fn order_action(
// Get request id
let request_id = msg.get_inner_message_kind().request_id;

let pubkey_0 = event.sender
let pubkey_0 = event.sender;

if let Some(order) = msg.get_inner_message_kind().get_order() {
let mostro_settings = Settings::get_mostro();
Expand Down
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub async fn find_solver_pubkey(pool: &SqlitePool, solver_npub: String) -> anyho
Ok(user)
}

pub async fn is_new_user(pool: &SqlitePool, npub: String) -> anyhow::Result<User> {
pub async fn is_user_present(pool: &SqlitePool, npub: String) -> anyhow::Result<User> {
let user = sqlx::query_as::<_, User>(
r#"
SELECT *
Expand Down

0 comments on commit 29776c4

Please sign in to comment.