[Add] App storage

This commit is contained in:
Alex Emmet 2026-04-14 21:37:51 +02:00
commit 3b218411d0
4 changed files with 279 additions and 44 deletions

View file

@ -42,6 +42,14 @@ pub async fn load_from_tu(username: &str) -> Result<(), ()> {
pub fn add_user(user: UserProfile) {
USERS.lock().unwrap().push(user);
}
pub fn update_user(user: UserProfile) {
let mut users = USERS.lock().unwrap();
if let Some(pos) = users.iter().position(|u| u.user_id == user.user_id) {
users[pos] = user;
}
*UNIQUE.lock().unwrap() = true;
}
pub fn get_user_by_username(username: &str) -> Option<UserProfile> {
USERS
.lock()
@ -111,3 +119,15 @@ pub async fn load_users() -> io::Result<()> {
pub fn set_unique(val: bool) {
*UNIQUE.lock().unwrap() = val;
}
pub fn save_app_data(user_id: i64, app_identifier: &str, data: &str) {
let path = format!("users/{}/apps", user_id);
let name = format!("{}.json", app_identifier);
save_file(&path, &name, data);
}
pub fn load_app_data(user_id: i64, app_identifier: &str) -> String {
let path = format!("users/{}/apps", user_id);
let name = format!("{}.json", app_identifier);
load_file(&path, &name)
}

View file

@ -16,6 +16,7 @@ pub struct UserProfile {
pub reset_token: String,
pub created_at: i64,
pub display_name: Option<String>,
pub trusted_apps: std::collections::HashMap<String, String>,
}
impl UserProfile {
@ -38,17 +39,24 @@ impl UserProfile {
.unwrap()
.as_millis() as i64,
reset_token,
trusted_apps: std::collections::HashMap::new(),
}
}
pub fn to_json(&self) -> JsonValue {
let mut trusted_apps_obj = json::JsonValue::new_object();
for (k, v) in &self.trusted_apps {
trusted_apps_obj[k] = v.clone().into();
}
let mut obj = object! {
"uuid" => self.user_id,
"username" => self.username.clone(),
"public_key" => self.public_key.clone(),
"private_key_hash" => self.private_key_hash.clone(),
"created_at" => self.created_at,
"reset_token" => self.reset_token.clone()
"reset_token" => self.reset_token.clone(),
"trusted_apps" => trusted_apps_obj,
};
if let Some(d) = &self.display_name {
obj["display_name"] = d.clone().into();
@ -82,6 +90,15 @@ impl UserProfile {
let created_at = j["created_at"].as_i64()?;
let display_name = j["display_name"].as_str().map(|s| s.to_string());
let mut trusted_apps = std::collections::HashMap::new();
if j["trusted_apps"].is_object() {
for (key, value) in j["trusted_apps"].entries() {
if let Some(s) = value.as_str() {
trusted_apps.insert(key.to_string(), s.to_string());
}
}
}
let up = UserProfile {
user_id,
username,
@ -90,6 +107,7 @@ impl UserProfile {
private_key_hash,
created_at,
reset_token,
trusted_apps,
};
// TODO: Migrate to Omikron / Wss