UI Utility

This commit is contained in:
Alex Emmet 2025-09-28 18:58:42 +00:00
commit cbd357ce14
14 changed files with 962 additions and 622 deletions

View file

@ -1,4 +1,5 @@
use json::{self, JsonValue};
use crate::util::file_util::save_file;
use json::{self, Array, JsonValue};
use std::fs;
use std::path::Path;
use uuid::Uuid;
@ -7,8 +8,8 @@ pub struct UserCommunityUtil;
impl UserCommunityUtil {
pub fn add_community(storage_owner: Uuid, address: String, title: String, position: String) {
let path = format!("users/{}/communities.json", storage_owner);
let mut communities: JsonValue = Self::load_array(&path);
let file_path = format!("users/{}/", storage_owner);
let mut communities = Self::load_array(&file_path);
let mut community = JsonValue::new_object();
community["title"] = JsonValue::String(title);
@ -16,44 +17,52 @@ impl UserCommunityUtil {
community["position"] = JsonValue::String(position);
communities.push(community);
Self::save_array(&path, communities);
save_file(
&file_path,
"communities.json",
&JsonValue::Array(communities).to_string(),
);
}
pub fn remove_community(storage_owner: Uuid, community_address: String) {
let path = format!("users/{}/communities.json", storage_owner);
let mut communities = Self::load_array(&path);
let file_path = format!("users/{}/", storage_owner);
let communities = Self::load_array(&file_path);
let mut new_array = JsonValue::new_array();
for entry in communities.members() {
if entry["address"].as_str() != Some(&community_address) {
new_array.push(entry.clone()).unwrap();
let filtered: Array = communities
.iter()
.filter(|entry| entry["address"].as_str() != Some(&community_address))
.cloned()
.collect();
save_file(
&file_path,
"communities.json",
&JsonValue::Array(filtered).to_string(),
);
}
pub fn get_communities(storage_owner: Uuid) -> Array {
let file_path = format!("users/{}/communities.json", storage_owner);
Self::load_array(&file_path)
}
fn load_array(file_path: &str) -> Array {
if !Path::new(file_path).exists() {
return Array::new();
}
match fs::read_to_string(file_path) {
Ok(content) => {
let parsed = json::parse(&content);
match parsed {
Ok(JsonValue::Array(arr)) => arr,
_ => Array::new(),
}
}
Err(err) => {
eprintln!("Failed to read file {}: {}", file_path, err);
Array::new()
}
}
Self::save_array(&path, new_array);
}
pub fn get_communities(storage_owner: Uuid) -> JsonValue {
let path = format!("users/{}/communities.json", storage_owner);
Self::load_array(&path)
}
fn load_array(path: &str) -> JsonValue {
if !Path::new(path).exists() {
return JsonValue::new_object();
}
match fs::read_to_string(path) {
Ok(content) => json::parse(&content).unwrap_or_else(|_| JsonValue::new_object()),
Err(_) => JsonValue::new_object(),
}
}
fn save_array(path: &str, arr: JsonValue) {
if let Some(parent) = Path::new(path).parent() {
let _ = fs::create_dir_all(parent);
}
let _ = fs::write(path, arr.pretty(3));
}
}

View file

@ -1,4 +1,5 @@
use crate::auth::auth_connector::AuthConnector;
use crate::gui::log_panel::log_message;
use crate::users::user_manager::UserManager;
use base64::{Engine as _, engine::general_purpose};
use json::{JsonValue, object, stringify};
@ -72,15 +73,14 @@ impl UserProfile {
reset_token,
);
// Migration hook (stubbed, since AuthConnector isnt implemented here)
if j.has_key("migrate")
|| j.has_key("migrating")
|| j.has_key("changing")
|| j.has_key("move")
|| j.has_key("moving")
{
if AuthConnector::migrate_user(&mut up, stringify!("{}", Uuid::new_v4())).await {
println!("[INFO] Migration triggered for {}", up.username);
if AuthConnector::migrate_user(&mut up).await {
log_message(format!("[INFO] Migration triggered for {}", up.username));
UserManager::set_unique(true);
}
}
@ -93,7 +93,6 @@ impl UserProfile {
OsRng.fill(bytes.as_mut());
let new_token = general_purpose::STANDARD.encode(&bytes);
self.reset_token = new_token.clone();
UserManager::save_users().ok();
new_token
}