This commit is contained in:
Bianca Fürstenau 2025-02-10 18:39:06 +01:00
parent 934c704cbf
commit bd40e5c8d8
4 changed files with 47 additions and 48 deletions

View file

@ -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 }
}
}

View file

@ -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<u8>, Vec<u8>, usize);
@ -71,7 +57,6 @@ impl easy::Handler for Collector {
pub fn data_client(file: &str) -> Result<Easy2<Collector>, ()> {
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<Easy2<Collector>, ()> {
}
#[tauri::command]
pub async fn pull_data(state: State<'_, Mutex<DoorState>>) -> Result<String, ()> {
pub async fn pull_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
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<DoorState>>) -> Result<String, ()>
}
#[tauri::command]
pub async fn push_data(state: State<'_, Mutex<DoorState>>) -> Result<String, ()> {
pub async fn push_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
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<DoorState>>) -> Result<String, ()>
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())
}

View file

@ -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<AppState>>,
) -> 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::<i64>(),
store,
state.acc,
acc,
state.id,
Utc::now().timestamp(),
false,
),
@ -86,26 +84,10 @@ async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
#[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::<i64>();
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(())
})

View file

@ -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", {});
}