From a22b2c5e4dda170a559a6a36831c055546eea6c4 Mon Sep 17 00:00:00 2001 From: Junko Date: Fri, 24 Nov 2023 15:01:56 +0100 Subject: [PATCH] suggested fixes --- src/main.rs | 194 +++++++++++++++++++++++++++------------------------- 1 file changed, 99 insertions(+), 95 deletions(-) diff --git a/src/main.rs b/src/main.rs index 21b2a5f..bc599b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,21 +13,14 @@ use dirs; const PK_URL: &str = "https://api.pluralkit.me/v2"; const SP_URL: &str = "https://api.apparyllis.com/v1"; -#[derive(Debug)] -#[derive(Serialize)] -#[derive(Deserialize)] -#[derive(Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] struct System { pk_userid: String, sp_userid: String, members: Vec, } -#[derive(Debug)] -#[derive(Serialize)] -#[derive(Deserialize)] -#[derive(Clone)] -#[derive(PartialEq)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] struct Member { pk_id: String, sp_id: String, @@ -35,34 +28,54 @@ struct Member { alias: String } +#[derive(Debug)] +struct Fronters { + sp: Vec, + pk: Vec, +} + +impl Fronters { + fn new(pk: Vec, sp: Vec) -> Self { + Self {sp, pk} + } +} + fn main() { + let helpstring = r#"PluralSync +Made by Jvnko@lavenderfield + +Usage:\npluralsync [COMMAND] [member1] [member2]... + +Commands: +sync - Fetches the system member information from PluralKit and SimplyPlural +set - Sets one or more members to the front +get - Refreshes the current front information +add - Adds a member to the existing front +memberlist - Shows loaded member list"#; + if std::env::args().len() > 1 { let augs: Vec = std::env::args().collect(); let command = &augs[1]; let config_path: String; + match dirs::config_dir() { Some(x) => { - config_path = format!("{}/pluralshit", x.display()); + config_path = format!("{}/pluralsync", x.display()); match command.as_str() { "sync" => { let _ = sync(config_path); }, "set" => { if std::env::args().len() > 2 { - match set_member(config_path, &augs[2..]) { - Ok(_) => (), - Err(e) => println!("{e}"), - } + let _ = set_member(config_path, &augs[2..]).map_err(|e| println!("{e}")); + } else { //set_empty(config_path); } }, "add" => { if std::env::args().len() > 2 { - match add_member(config_path, &augs[2..]) { - Ok(_) => (), - Err(e) => println!("{e}"), - } + let _ = add_member(config_path, &augs[2..]).map_err(|e| println!("{e}")); } else { //add_empty(config_path); } @@ -70,13 +83,13 @@ fn main() { } "get" => get(config_path), "memberlist" => memberlist(config_path), - &_ => println!("PluralSync\nMade by Jvnko@lavenderfield\n\nUsage:\npluralshit [COMMAND] [member1] [member2]...\n\nCommands:\nsync - Fetches the system member information from PluralKit and SimplyPlural\nset - Sets one or more members to the front\nget - Refreshes the current front information\nadd - Adds a member to the existing front\nmemberlist - Shows loaded member list"), + &_ => println!("{}", helpstring), } }, None => println!("Something went wrong") } } else { - println!("PluralSync\nMade by Jvnko@lavenderfield\n\nUsage:\npluralshit [COMMAND] [member1] [member2]...\n\nCommands:\nsync - Fetches the system member information from PluralKit and SimplyPlural\nset - Sets one or more members to the front\nget - Refreshes the current front information\nadd - Adds a member to the existing front\nmemberlist - Shows loaded member list"); + println!("{}", helpstring); } } @@ -171,7 +184,7 @@ fn set_member(config_path: String, tf_members: &[String]) -> Result<(), &'static } else { println!("No members found. Known members:\n--------------------------"); memberlist(config_path); - println!("--------------------------\nIf a member is missing from the system try running \"pluralshit sync\" to refresh the local database"); + println!("--------------------------\nIf a member is missing from the system try running \"pluralsync sync\" to refresh the local database"); } Ok(()) } @@ -197,7 +210,7 @@ fn add_member(config_path: String, tf_members: &[String]) -> Result<(), &'static let fronters = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &system); let mut aux: Vec = Vec::new(); - aux.append(&mut fronters["pk"].clone()); + aux.append(&mut fronters.pk.clone()); aux.append(&mut to_front); to_front = aux; @@ -208,7 +221,7 @@ fn add_member(config_path: String, tf_members: &[String]) -> Result<(), &'static } else { println!("No members found. Known members:\n--------------------------"); memberlist(config_path); - println!("--------------------------\nIf a member is missing from the system try running \"pluralshit sync\" to refresh the local database"); + println!("--------------------------\nIf a member is missing from the system try running \"pluralsync sync\" to refresh the local database"); } Ok(()) } @@ -232,7 +245,7 @@ fn get(config_path: String) { let f = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &sys); let mut names = Vec::new(); - for m in &f["pk"] { + for m in &f.pk { names.push(String::from(&m.name)); } let fronters = names.join(" || "); @@ -277,10 +290,10 @@ fn pk_get_fronters(key: &str, sys: &System) -> Vec { return members; } -fn pk_set_fronters(key: &str, sys: &System, to_front: &Vec, fronters: &HashMap>) { +fn pk_set_fronters(key: &str, sys: &System, to_front: &Vec, fronters: &Fronters) { let url = format!("{}/systems/{}/switches", PK_URL, sys.pk_userid); - if to_front != &fronters["pk"] { + if to_front != &fronters.pk { let mut frontcodes = Vec::new(); for tf in to_front { frontcodes.push(String::from(&tf.pk_id)); @@ -364,73 +377,63 @@ fn sp_get_frontids(key: &str, to_id: &Member) -> String { return String::new(); } -fn sp_set_fronters(key: &str, to_front: &Vec, fronters: &HashMap>) { - if to_front != &fronters["sp"] { - for fronting_member in &fronters["sp"] { - if !to_front.contains(&fronting_member) { - let f_id = sp_get_frontids(&key, &fronting_member); - - let url = format!("{}/frontHistory/{}", SP_URL, f_id); - - #[derive (Serialize)] - #[allow (non_snake_case)] - struct SpRem { - live: bool, - endTime: u64 - } - - let end_time = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).expect("wa").as_secs(); - let rem = SpRem { - live: false, - endTime: end_time * 1000 - }; - - let body = serde_json::to_string(&rem).expect("Error"); - - let _ = http_patch_request(url, key, body); - } - } - - for tf_member in to_front { - if !fronters["sp"].contains(&tf_member) { - let url = format!("{}/frontHistory", SP_URL); - - #[derive (Serialize)] - #[allow (non_snake_case)] - struct SpAdd { - member: String, - custom: bool, - live: bool, - startTime: u64 - } - - let start_time = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).expect("wa").as_secs(); - let rem = SpAdd { - member: String::from(&tf_member.sp_id), - custom: false, - live: true, - startTime: start_time * 1000 - }; - - let body = serde_json::to_string(&rem).expect("Error"); - - let _ = http_post_request_st(url, key, body); - } - } - - /* - let mut frontcodes = Vec::new(); - for tf in to_front { - frontcodes.push(tf.sp_id); - } - let mut body: HashMap<&str, Vec> = HashMap::new(); - body.insert("members", frontcodes); - */ - //let _ = http_post_request(url, key, &body); - } else { +fn sp_set_fronters(key: &str, to_front: &Vec, fronters: &Fronters) { + if to_front == &fronters.sp { println!("Members already fonting"); + return; } + for fronting_member in &fronters.sp { + if !to_front.contains(&fronting_member) { + let f_id = sp_get_frontids(&key, &fronting_member); + + let url = format!("{}/frontHistory/{}", SP_URL, f_id); + + #[derive (Serialize)] + #[allow (non_snake_case)] + struct SpRem { + live: bool, + endTime: u64 + } + + let end_time = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).expect("wa").as_secs(); + let rem = SpRem { + live: false, + endTime: end_time * 1000 + }; + + let body = serde_json::to_string(&rem).expect("Error"); + + let _ = http_patch_request(url, key, body); + } + } + + for tf_member in to_front { + if !fronters.sp.contains(&tf_member) { + let url = format!("{}/frontHistory", SP_URL); + + #[derive (Serialize)] + #[allow (non_snake_case)] + struct SpAdd { + member: String, + custom: bool, + live: bool, + startTime: u64 + } + + let start_time = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).expect("wa").as_secs(); + let rem = SpAdd { + member: String::from(&tf_member.sp_id), + custom: false, + live: true, + startTime: start_time * 1000 + }; + + let body = serde_json::to_string(&rem).expect("Error"); + + let _ = http_post_request_st(url, key, body); + } + } } fn load_json(path: String) -> Value { @@ -491,10 +494,11 @@ fn get_system(config_path: &str) -> System { } -fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> HashMap> { - let mut fronters: HashMap> = HashMap::new(); - fronters.insert(String::from("pk"), pk_get_fronters(pk_key, sys)); - fronters.insert(String::from("sp"), sp_get_fronters(sp_key, sys)); +fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> Fronters { + let fronters = Fronters::new( + pk_get_fronters(pk_key, sys), + sp_get_fronters(sp_key, sys) + ); // if fronters["pk"] != fronters["sp"] { // fronters.insert(String::from("pk"), fronters["sp"].clone()); @@ -520,7 +524,7 @@ async fn http_get_request(url: String, key: &str) -> Result>) -> Result<(), Box> { let client = reqwest::Client::new(); - let _ = client + let _= client .post(url) .json(body) .header(USER_AGENT, "Pluralsync")