fixies for silly mistakes a silly girl shouldnt make

This commit is contained in:
Junko 2024-02-16 12:50:04 +01:00
parent c1788330f5
commit 87b2d41689
5 changed files with 71 additions and 44 deletions

View File

@ -18,7 +18,7 @@ crossterm = "0.27.0"
color-eyre = "0.6.2" color-eyre = "0.6.2"
[features] [features]
default = ["avatar"] default = ["fedi","discord","jlog"]
avatar = [] avatar = []
discord = ["avatar"] discord = ["avatar"]
fedi = ["avatar"] fedi = ["avatar"]

View File

@ -163,8 +163,8 @@ pub mod sp {
let datas: Vec<Value> = serde_json::from_str(&result)?; let datas: Vec<Value> = serde_json::from_str(&result)?;
for data in datas { for data in datas {
let sp_f_id = &data["id"].to_string(); let sp_f_id = &data["id"].as_str().unwrap().to_string();
let sp_id = &data["content"]["member"].to_string(); let sp_id = &data["content"]["member"].as_str().unwrap().to_string();
if sp_id.to_string() == to_id.sp_id { if sp_id.to_string() == to_id.sp_id {
return Ok(sp_f_id.to_string()); 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 { pub mod request {
use reqwest::RequestBuilder; use reqwest::RequestBuilder;
use reqwest::header::{USER_AGENT, AUTHORIZATION}; use reqwest::header::{USER_AGENT, AUTHORIZATION};
#[cfg(feature = "fedi")]
use reqwest::multipart::{Part, Form};
use color_eyre::eyre::Result; use color_eyre::eyre::Result;

View File

@ -2,6 +2,7 @@ use crate::configdata::*;
use crate::systemdata::*; use crate::systemdata::*;
use crate::clap_ps::ForceFrom; use crate::clap_ps::ForceFrom;
use color_eyre::eyre::OptionExt;
use color_eyre::eyre::{eyre, Result}; use color_eyre::eyre::{eyre, Result};
pub enum CurrentScreen { pub enum CurrentScreen {
@ -94,15 +95,17 @@ impl App {
fn add(&mut self, to_front: Vec<Member>) -> Result<()> { fn add(&mut self, to_front: Vec<Member>) -> 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<Member> = Vec::new(); let mut addfronters: Vec<Member> = Vec::new();
addfronters.append(&mut aux_fronters.pk.clone()); addfronters.append(&mut aux_fronters.pk.clone());
addfronters.append(&mut to_front.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<()> { pub fn memberlist(&self) -> Result<()> {
@ -135,9 +138,9 @@ impl App {
if to_front.len() == tf_members.len() { if to_front.len() == tf_members.len() {
if !append { if !append {
self.set(to_front)?; self.set(to_front.clone())?;
} else { } else {
self.add(to_front)?; self.add(to_front.clone())?;
} }
} else { } else {
println!("One or more members were not found. Known members:\n--------------------------"); println!("One or more members were not found. Known members:\n--------------------------");

View File

@ -96,7 +96,7 @@ impl System {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Fronters { pub struct Fronters {
pub sp: Vec<Member>, pub sp: Vec<Member>,
pub pk: Vec<Member>, pub pk: Vec<Member>,
@ -120,9 +120,12 @@ impl Fronters {
ForceFrom::SP => { ForceFrom::SP => {
fronters.pk = fronters.sp.clone(); fronters.pk = fronters.sp.clone();
} }
_ => { ForceFrom::PK => {
fronters.sp = fronters.pk.clone(); fronters.sp = fronters.pk.clone();
} }
ForceFrom::None => {
}
} }
} }

View File

@ -17,6 +17,9 @@ use {
color_eyre::eyre::eyre, color_eyre::eyre::eyre,
}; };
#[cfg(feature = "fedi")]
use crate::api::fedi::*;
pub fn load_json(file_path: &str) -> Result<Value> { pub fn load_json(file_path: &str) -> Result<Value> {
let file_data = fs::read_to_string(&file_path)?; let file_data = fs::read_to_string(&file_path)?;
@ -28,18 +31,18 @@ pub fn load_json(file_path: &str) -> Result<Value> {
#[cfg(feature = "avatar")] #[cfg(feature = "avatar")]
pub fn update_avatars(app: &mut App, #[cfg(feature = "discord")] discord: bool, #[cfg(feature = "fedi")] fedi: bool) -> Result<()>{ 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 let names = &app.get_front(ForceFrom::None)?; // TODO CHANGE INTO USING LOCAL FRONT
match avatar_module(&cfg, &names) { match avatar_module(&cfg, &names) {
Ok(_) => { Ok(_) => {
#[cfg(feature = "discord")] { #[cfg(feature = "discord")] {
config.disc_module.enabled |= discord; cfg.disc_module.enabled |= discord;
discord_module(&config); discord_module(&cfg)?;
} }
#[cfg(feature = "fedi")] { #[cfg(feature = "fedi")] {
config.fedi_module.enabled |= fedi; cfg.fedi_module.enabled |= fedi;
fedi_module(&config); fedi_module(&cfg)?;
} }
}, },
Err(e) => println!("{}", e), Err(e) => println!("{}", e),
@ -50,7 +53,7 @@ pub fn update_avatars(app: &mut App, #[cfg(feature = "discord")] discord: bool,
} }
#[cfg(feature = "avatar")] #[cfg(feature = "avatar")]
fn avatar_module(config: &Config, names: &Vec<String>) -> Result<(), &'static str>{ fn avatar_module(config: &Config, names: &Vec<String>) -> Result<()>{
if config.avatar_module.enabled { if config.avatar_module.enabled {
let mut whitelisted_names: Vec<String> = names.iter().map(|i| i.to_lowercase()).collect(); let mut whitelisted_names: Vec<String> = names.iter().map(|i| i.to_lowercase()).collect();
let blacklist: Vec<String> = config.avatar_module.blacklist.iter().map(|i| i.to_lowercase()).collect(); let blacklist: Vec<String> = config.avatar_module.blacklist.iter().map(|i| i.to_lowercase()).collect();
@ -78,43 +81,33 @@ fn avatar_module(config: &Config, names: &Vec<String>) -> Result<(), &'static st
println!("Avatar module finished"); println!("Avatar module finished");
Ok(()) Ok(())
} else { } else {
Err("Avatar module failed") Err(eyre!("Avatar module failed"))
} }
} else { } else {
Err("Avatar mode disabled") Err(eyre!("Avatar mode disabled"))
} }
} }
#[cfg(feature = "discord")] #[cfg(feature = "discord")]
fn discord_module(config: &Config) { fn discord_module(config: &Config) -> Result<()> {
if config.disc_module.enabled { if config.disc_module.enabled {
if cfg!(target_os = "windows") { 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 mut c = Command::new("cmd").args(["/C", format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path).as_str()]).spawn()?;
let _ = c.wait().expect("Error"); let _ = c.wait()?;
} else { } 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 mut c = Command::new("sh").arg("-c").arg(format!("{} {}", &config.disc_module.python_path, &config.disc_module.script_path)).spawn()?;
let _ = c.wait().expect("Error"); let _ = c.wait()?;
} }
} }
Ok(())
} }
#[cfg(feature = "fedi")] #[cfg(feature = "fedi")]
fn fedi_module(config: &Config) { fn fedi_module(config: &Config) -> Result<()> {
if config.fedi_module.enabled { if config.fedi_module.enabled {
fedi_request(config)?;
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()),
}
} }
Ok(())
} }