suggested fixes

This commit is contained in:
Junko 2023-11-24 15:01:56 +01:00
parent 2864edcf8b
commit a22b2c5e4d
1 changed files with 99 additions and 95 deletions

View File

@ -13,21 +13,14 @@ use dirs;
const PK_URL: &str = "https://api.pluralkit.me/v2"; const PK_URL: &str = "https://api.pluralkit.me/v2";
const SP_URL: &str = "https://api.apparyllis.com/v1"; const SP_URL: &str = "https://api.apparyllis.com/v1";
#[derive(Debug)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Serialize)]
#[derive(Deserialize)]
#[derive(Clone)]
struct System { struct System {
pk_userid: String, pk_userid: String,
sp_userid: String, sp_userid: String,
members: Vec<Member>, members: Vec<Member>,
} }
#[derive(Debug)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[derive(Serialize)]
#[derive(Deserialize)]
#[derive(Clone)]
#[derive(PartialEq)]
struct Member { struct Member {
pk_id: String, pk_id: String,
sp_id: String, sp_id: String,
@ -35,34 +28,54 @@ struct Member {
alias: String alias: String
} }
#[derive(Debug)]
struct Fronters {
sp: Vec<Member>,
pk: Vec<Member>,
}
impl Fronters {
fn new(pk: Vec<Member>, sp: Vec<Member>) -> Self {
Self {sp, pk}
}
}
fn main() { 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 { if std::env::args().len() > 1 {
let augs: Vec<String> = std::env::args().collect(); let augs: Vec<String> = std::env::args().collect();
let command = &augs[1]; let command = &augs[1];
let config_path: String; let config_path: String;
match dirs::config_dir() { match dirs::config_dir() {
Some(x) => { Some(x) => {
config_path = format!("{}/pluralshit", x.display()); config_path = format!("{}/pluralsync", x.display());
match command.as_str() { match command.as_str() {
"sync" => { "sync" => {
let _ = sync(config_path); let _ = sync(config_path);
}, },
"set" => { "set" => {
if std::env::args().len() > 2 { if std::env::args().len() > 2 {
match set_member(config_path, &augs[2..]) { let _ = set_member(config_path, &augs[2..]).map_err(|e| println!("{e}"));
Ok(_) => (),
Err(e) => println!("{e}"),
}
} else { } else {
//set_empty(config_path); //set_empty(config_path);
} }
}, },
"add" => { "add" => {
if std::env::args().len() > 2 { if std::env::args().len() > 2 {
match add_member(config_path, &augs[2..]) { let _ = add_member(config_path, &augs[2..]).map_err(|e| println!("{e}"));
Ok(_) => (),
Err(e) => println!("{e}"),
}
} else { } else {
//add_empty(config_path); //add_empty(config_path);
} }
@ -70,13 +83,13 @@ fn main() {
} }
"get" => get(config_path), "get" => get(config_path),
"memberlist" => memberlist(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") None => println!("Something went wrong")
} }
} else { } 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 { } else {
println!("No members found. Known members:\n--------------------------"); println!("No members found. Known members:\n--------------------------");
memberlist(config_path); 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(()) 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 fronters = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &system);
let mut aux: Vec<Member> = Vec::new(); let mut aux: Vec<Member> = Vec::new();
aux.append(&mut fronters["pk"].clone()); aux.append(&mut fronters.pk.clone());
aux.append(&mut to_front); aux.append(&mut to_front);
to_front = aux; to_front = aux;
@ -208,7 +221,7 @@ fn add_member(config_path: String, tf_members: &[String]) -> Result<(), &'static
} else { } else {
println!("No members found. Known members:\n--------------------------"); println!("No members found. Known members:\n--------------------------");
memberlist(config_path); 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(()) 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 f = get_fronters(&config["pk_key"].as_str().unwrap(), &config["sp_key"].as_str().unwrap(), &sys);
let mut names = Vec::new(); let mut names = Vec::new();
for m in &f["pk"] { for m in &f.pk {
names.push(String::from(&m.name)); names.push(String::from(&m.name));
} }
let fronters = names.join(" || "); let fronters = names.join(" || ");
@ -277,10 +290,10 @@ fn pk_get_fronters(key: &str, sys: &System) -> Vec<Member> {
return members; return members;
} }
fn pk_set_fronters(key: &str, sys: &System, to_front: &Vec<Member>, fronters: &HashMap<String, Vec<Member>>) { fn pk_set_fronters(key: &str, sys: &System, to_front: &Vec<Member>, fronters: &Fronters) {
let url = format!("{}/systems/{}/switches", PK_URL, sys.pk_userid); 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(); let mut frontcodes = Vec::new();
for tf in to_front { for tf in to_front {
frontcodes.push(String::from(&tf.pk_id)); frontcodes.push(String::from(&tf.pk_id));
@ -364,9 +377,13 @@ fn sp_get_frontids(key: &str, to_id: &Member) -> String {
return String::new(); return String::new();
} }
fn sp_set_fronters(key: &str, to_front: &Vec<Member>, fronters: &HashMap<String, Vec<Member>>) { fn sp_set_fronters(key: &str, to_front: &Vec<Member>, fronters: &Fronters) {
if to_front != &fronters["sp"] { if to_front == &fronters.sp {
for fronting_member in &fronters["sp"] { println!("Members already fonting");
return;
}
for fronting_member in &fronters.sp {
if !to_front.contains(&fronting_member) { if !to_front.contains(&fronting_member) {
let f_id = sp_get_frontids(&key, &fronting_member); let f_id = sp_get_frontids(&key, &fronting_member);
@ -392,7 +409,7 @@ fn sp_set_fronters(key: &str, to_front: &Vec<Member>, fronters: &HashMap<String,
} }
for tf_member in to_front { for tf_member in to_front {
if !fronters["sp"].contains(&tf_member) { if !fronters.sp.contains(&tf_member) {
let url = format!("{}/frontHistory", SP_URL); let url = format!("{}/frontHistory", SP_URL);
#[derive (Serialize)] #[derive (Serialize)]
@ -417,20 +434,6 @@ fn sp_set_fronters(key: &str, to_front: &Vec<Member>, fronters: &HashMap<String,
let _ = http_post_request_st(url, key, body); 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<String>> = HashMap::new();
body.insert("members", frontcodes);
*/
//let _ = http_post_request(url, key, &body);
} else {
println!("Members already fonting");
}
} }
fn load_json(path: String) -> Value { 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<String, Vec<Member>> { fn get_fronters(pk_key: &str, sp_key: &str, sys: &System) -> Fronters {
let mut fronters: HashMap<String, Vec<Member>> = HashMap::new(); let fronters = Fronters::new(
fronters.insert(String::from("pk"), pk_get_fronters(pk_key, sys)); pk_get_fronters(pk_key, sys),
fronters.insert(String::from("sp"), sp_get_fronters(sp_key, sys)); sp_get_fronters(sp_key, sys)
);
// if fronters["pk"] != fronters["sp"] { // if fronters["pk"] != fronters["sp"] {
// fronters.insert(String::from("pk"), fronters["sp"].clone()); // fronters.insert(String::from("pk"), fronters["sp"].clone());