suggested fixes
This commit is contained in:
parent
2864edcf8b
commit
a22b2c5e4d
194
src/main.rs
194
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<Member>,
|
||||
}
|
||||
|
||||
#[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<Member>,
|
||||
pk: Vec<Member>,
|
||||
}
|
||||
|
||||
impl Fronters {
|
||||
fn new(pk: Vec<Member>, sp: Vec<Member>) -> 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<String> = 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<Member> = 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<Member> {
|
|||
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);
|
||||
|
||||
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<Member>, fronters: &HashMap<String, Vec<Member>>) {
|
||||
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<String>> = HashMap::new();
|
||||
body.insert("members", frontcodes);
|
||||
*/
|
||||
//let _ = http_post_request(url, key, &body);
|
||||
} else {
|
||||
fn sp_set_fronters(key: &str, to_front: &Vec<Member>, 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<String, Vec<Member>> {
|
||||
let mut fronters: HashMap<String, Vec<Member>> = 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<String, Box<dyn std:
|
|||
#[tokio::main]
|
||||
async fn http_post_request(url: String, key: &str, body: &HashMap<&str, Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let _ = client
|
||||
let _= client
|
||||
.post(url)
|
||||
.json(body)
|
||||
.header(USER_AGENT, "Pluralsync")
|
||||
|
|
Loading…
Reference in New Issue