diff --git a/src-tauri/src/app_state.rs b/src-tauri/src/app_state.rs new file mode 100644 index 0000000..04e60e3 --- /dev/null +++ b/src-tauri/src/app_state.rs @@ -0,0 +1,35 @@ +use rand::prelude::*; +use tokio::sync::Mutex; +use rusqlite::Connection; +use ring_compat::signature::ed25519::SigningKey; + +pub struct AppState { + pub db: Connection, + pub last_sync: i64, + pub id: u64, + pub key: SigningKey, +} + +impl AppState { + pub fn new() -> Self { + let db = Connection::open_in_memory() + .expect("Failed to create DB."); + db.execute( + "CREATE TABLE swap ( + rand INTEGER, + store INTEGER, + account INTEGER, + submitter INTEGER, + time INTEGER, + cancellation BOOL + )", + (), + ) + .unwrap(); + let mut rng = rand::thread_rng(); + let last_sync = i64::MIN; + let id = rng.gen(); + let key = SigningKey::generate(&mut rng); + AppState { db, last_sync, id, key } + } +} diff --git a/src-tauri/src/data_door.rs b/src-tauri/src/data_door.rs index c382c00..78987c1 100644 --- a/src-tauri/src/data_door.rs +++ b/src-tauri/src/data_door.rs @@ -5,21 +5,7 @@ use ring_compat::signature::ed25519::SigningKey; use tauri::{Manager, State}; use tokio::sync::Mutex; -pub struct DoorState { - last_sync: i64, - id: u64, - key: SigningKey, -} - -impl DoorState { - pub fn new() -> Self { - let mut rng = rand::thread_rng(); - let last_sync = i64::MIN; - let id = rng.gen(); - let key = SigningKey::generate(&mut rng); - DoorState { last_sync, id, key } - } -} +use crate::app_state::AppState; #[derive(Debug)] struct Collector(Vec, Vec, usize); @@ -71,7 +57,6 @@ impl easy::Handler for Collector { pub fn data_client(file: &str) -> Result, ()> { let mut client = Easy2::new(Collector(Vec::new(), Vec::new(), 0)); let url = format!("https://cloud.seebruecke.org/public.php/webdav/data/{}", file); - println!("{}", url); client.url(&url) .map_err(|_| ())?; client.username(include_str!("cloud_user.txt")).map_err(|_| ())?; @@ -82,7 +67,7 @@ pub fn data_client(file: &str) -> Result, ()> { } #[tauri::command] -pub async fn pull_data(state: State<'_, Mutex>) -> Result { +pub async fn pull_data(state: State<'_, Mutex>) -> Result { let mut client = data_client("")?; client.custom_request("PROPFIND").map_err(|_| ())?; client.perform().map_err(|_| ())?; @@ -91,7 +76,7 @@ pub async fn pull_data(state: State<'_, Mutex>) -> Result } #[tauri::command] -pub async fn push_data(state: State<'_, Mutex>) -> Result { +pub async fn push_data(state: State<'_, Mutex>) -> Result { let state = state.lock().await; let key_file = format!("{:016X}.key", state.id); let mut client = data_client(&key_file)?; @@ -100,10 +85,7 @@ pub async fn push_data(state: State<'_, Mutex>) -> Result client.get_mut().1.extend_from_slice(v_key.as_ref()); client.in_filesize(v_key.as_ref().len() as u64); client.upload(true); - println!("{:?}", client); - let perf = client.perform(); - println!("{:?}", perf); - println!("{:?}", client); + let perf = client.perform().map_err(|_| ())?; let content = &client.get_ref().0; Ok(String::from_utf8_lossy(content).to_string()) } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 1a5fa31..e4dd814 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -5,13 +5,9 @@ use tauri::{Manager, State}; use tokio::sync::Mutex; mod data_door; +mod app_state; -use data_door::DoorState; - -struct AppState { - db: Connection, - acc: i64, -} +use app_state::AppState; #[derive(Clone, Copy)] enum Store { @@ -55,17 +51,19 @@ impl ToSql for Store { #[tauri::command] async fn swap( store: &str, + acc: i64, state: State<'_, Mutex>, ) -> Result<(), ()> { let state = state.lock().await; let mut rng = rand::thread_rng(); let store: Store = store.try_into()?; state.db.execute( - "INSERT INTO swap VALUES (?1, ?2, ?3, ?4, ?5)", + "INSERT INTO swap VALUES (?1, ?2, ?3, ?4, ?5, ?6)", ( rng.gen::(), store, - state.acc, + acc, + state.id, Utc::now().timestamp(), false, ), @@ -86,26 +84,10 @@ async fn count(state: State<'_, Mutex>) -> Result { #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { - let mut rng = rand::thread_rng(); - let db = Connection::open_in_memory().expect("Failed to create DB."); - let acc = rng.gen::(); - db.execute( - "CREATE TABLE swap ( - rand INTEGER, - store INTEGER, - account INTEGER, - time INTEGER, - cancellation BOOL - )", - (), - ) - .unwrap(); - let state = AppState { db, acc }; - let door = DoorState::new(); + let state = AppState::new(); tauri::Builder::default() .plugin(tauri_plugin_fs::init()) .setup(|app| { - app.manage(Mutex::new(door)); app.manage(Mutex::new(state)); Ok(()) }) diff --git a/src/main.js b/src/main.js index 56d7aef..6f69bc4 100644 --- a/src/main.js +++ b/src/main.js @@ -4,7 +4,7 @@ let cntMsgEl; let pullMsgEl; async function swap(s) { - await invoke("swap", { store: s }); + await invoke("swap", { store: s, acc: 1 }); cntMsgEl.textContent = await invoke("count", {}); }