diff --git a/Cargo.toml b/Cargo.toml index 68f8466..818d780 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ crossterm = "0.27.0" color-eyre = "0.6.2" [features] -default = ["avatar"] +default = ["fedi","discord","jlog"] avatar = [] discord = ["avatar"] fedi = ["avatar"] diff --git a/src/api.rs b/src/api.rs index 14a1c62..4f7c3a0 100644 --- a/src/api.rs +++ b/src/api.rs @@ -163,8 +163,8 @@ pub mod sp { let datas: Vec = serde_json::from_str(&result)?; for data in datas { - let sp_f_id = &data["id"].to_string(); - let sp_id = &data["content"]["member"].to_string(); + let sp_f_id = &data["id"].as_str().unwrap().to_string(); + let sp_id = &data["content"]["member"].as_str().unwrap().to_string(); if sp_id.to_string() == to_id.sp_id { return Ok(sp_f_id.to_string()); } @@ -256,12 +256,40 @@ pub mod sp { } } +#[cfg(feature = "fedi")] +pub mod fedi { + + use std::fs; + + use reqwest::header::{USER_AGENT, AUTHORIZATION}; + use reqwest::multipart::{Part, Form}; + + use color_eyre::eyre::{eyre, Result}; + + use crate::configdata::Config; + use crate::api::request::http_request; + + pub fn fedi_request(config: &Config) -> Result<()> { + let client = reqwest::Client::new(); + let form = Form::new().part("avatar", Part::bytes(fs::read(config.avatar_module.avatar_output_path.clone()).unwrap()).file_name("face.png").mime_str("image/png").unwrap()); + let rb = client + .patch(config.fedi_module.instance.clone() + "/api/v1/accounts/update_credentials") + .multipart(form) + .header(USER_AGENT, "Pluralsync") + .header(AUTHORIZATION, format!("Bearer {}", &config.fedi_module.token).as_str()); + + println!("Fedi module finished"); + match http_request(rb) { + Ok(_) => Ok(()), + Err(e) => Err(eyre!("{}", e.to_string())), + } + } +} + pub mod request { use reqwest::RequestBuilder; use reqwest::header::{USER_AGENT, AUTHORIZATION}; - #[cfg(feature = "fedi")] - use reqwest::multipart::{Part, Form}; use color_eyre::eyre::Result; diff --git a/src/app.rs b/src/app.rs index e6e37a0..1227bb9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,6 +2,7 @@ use crate::configdata::*; use crate::systemdata::*; use crate::clap_ps::ForceFrom; +use color_eyre::eyre::OptionExt; use color_eyre::eyre::{eyre, Result}; pub enum CurrentScreen { @@ -94,15 +95,17 @@ impl App { fn add(&mut self, to_front: Vec) -> Result<()> { - let aux_fronters: Fronters = Fronters::get(self, ForceFrom::None)?; + self.get(ForceFrom::None)?; + if let Some(aux_fronters) = &self.fronters { + let mut addfronters: Vec = Vec::new(); + addfronters.append(&mut aux_fronters.pk.clone()); + addfronters.append(&mut to_front.clone()); - let mut addfronters: Vec = Vec::new(); - addfronters.append(&mut aux_fronters.pk.clone()); - addfronters.append(&mut to_front.clone()); + self.fronters = Some(Fronters::set(self, addfronters)?); - self.fronters = Some(Fronters::set(self, addfronters)?); - - Ok(()) + return Ok(()); + } + Err(eyre!("Could not fetch the current front")) } pub fn memberlist(&self) -> Result<()> { @@ -135,9 +138,9 @@ impl App { if to_front.len() == tf_members.len() { if !append { - self.set(to_front)?; + self.set(to_front.clone())?; } else { - self.add(to_front)?; + self.add(to_front.clone())?; } } else { println!("One or more members were not found. Known members:\n--------------------------"); diff --git a/src/systemdata.rs b/src/systemdata.rs index 34eaae5..f2c7f15 100644 --- a/src/systemdata.rs +++ b/src/systemdata.rs @@ -96,7 +96,7 @@ impl System { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Fronters { pub sp: Vec, pub pk: Vec, @@ -120,9 +120,12 @@ impl Fronters { ForceFrom::SP => { fronters.pk = fronters.sp.clone(); } - _ => { + ForceFrom::PK => { fronters.sp = fronters.pk.clone(); } + ForceFrom::None => { + + } } } diff --git a/src/utils.rs b/src/utils.rs index 645054b..f1b1830 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -17,6 +17,9 @@ use { color_eyre::eyre::eyre, }; +#[cfg(feature = "fedi")] +use crate::api::fedi::*; + pub fn load_json(file_path: &str) -> Result { let file_data = fs::read_to_string(&file_path)?; @@ -28,18 +31,18 @@ pub fn load_json(file_path: &str) -> Result { #[cfg(feature = "avatar")] pub fn update_avatars(app: &mut App, #[cfg(feature = "discord")] discord: bool, #[cfg(feature = "fedi")] fedi: bool) -> Result<()>{ - if let Some(cfg) = app.cfg.clone() { + if let Some(mut cfg) = app.cfg.clone() { let names = &app.get_front(ForceFrom::None)?; // TODO CHANGE INTO USING LOCAL FRONT match avatar_module(&cfg, &names) { Ok(_) => { #[cfg(feature = "discord")] { - config.disc_module.enabled |= discord; - discord_module(&config); + cfg.disc_module.enabled |= discord; + discord_module(&cfg)?; } #[cfg(feature = "fedi")] { - config.fedi_module.enabled |= fedi; - fedi_module(&config); + cfg.fedi_module.enabled |= fedi; + fedi_module(&cfg)?; } }, Err(e) => println!("{}", e), @@ -50,7 +53,7 @@ pub fn update_avatars(app: &mut App, #[cfg(feature = "discord")] discord: bool, } #[cfg(feature = "avatar")] -fn avatar_module(config: &Config, names: &Vec) -> Result<(), &'static str>{ +fn avatar_module(config: &Config, names: &Vec) -> Result<()>{ if config.avatar_module.enabled { let mut whitelisted_names: Vec = names.iter().map(|i| i.to_lowercase()).collect(); let blacklist: Vec = config.avatar_module.blacklist.iter().map(|i| i.to_lowercase()).collect(); @@ -78,43 +81,33 @@ fn avatar_module(config: &Config, names: &Vec) -> Result<(), &'static st println!("Avatar module finished"); Ok(()) } else { - Err("Avatar module failed") + Err(eyre!("Avatar module failed")) } } else { - Err("Avatar mode disabled") + Err(eyre!("Avatar mode disabled")) } } #[cfg(feature = "discord")] -fn discord_module(config: &Config) { +fn discord_module(config: &Config) -> Result<()> { if config.disc_module.enabled { if cfg!(target_os = "windows") { - let mut c = Command::new("cmd").args(["/C", format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path).as_str()]).spawn().expect("Discord module error"); - let _ = c.wait().expect("Error"); + let mut c = Command::new("cmd").args(["/C", format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path).as_str()]).spawn()?; + let _ = c.wait()?; } else { - let mut c = Command::new("sh").arg("-c").arg(format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path)).spawn().expect("Discord module error"); - let _ = c.wait().expect("Error"); + let mut c = Command::new("sh").arg("-c").arg(format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path)).spawn()?; + let _ = c.wait()?; } } + Ok(()) } #[cfg(feature = "fedi")] -fn fedi_module(config: &Config) { +fn fedi_module(config: &Config) -> Result<()> { if config.fedi_module.enabled { - - let client = reqwest::Client::new(); - let form = Form::new().part("avatar", Part::bytes(fs::read(config.avatar_module.avatar_output_path.clone()).unwrap()).file_name("face.png").mime_str("image/png").unwrap()); - let rb = client - .patch(config.fedi_module.instance.clone() + "/api/v1/accounts/update_credentials") - .multipart(form) - .header(USER_AGENT, "Pluralsync") - .header(AUTHORIZATION, format!("Bearer {}", &config.fedi_module.token).as_str()); - - println!("Fedi module finished"); - match http_request(rb) { - Ok(_) => (), - Err(e) => println!("{}", e.to_string()), - } + fedi_request(config)?; } + + Ok(()) }