blacklisting members from avatar module

This commit is contained in:
Junko 2024-01-20 21:21:07 +01:00
parent a8072bc530
commit 15d9ea5aab
3 changed files with 38 additions and 14 deletions

View File

@ -16,7 +16,8 @@ On the first run, if it does not exist, PluralSync will create a configuration d
"avatar_module": {
"enabled": false,
"avatar_folder": "// Folder to grab profile pictures, they follow they member1member2...memberX.png format",
"avatar_output_path": "// Path for the copied selected avatar"
"avatar_output_path": "// Path for the copied selected avatar",
"blacklist": "// Array of members to not include in the avatar module"
},
"disc_module": {
"enabled": false,

View File

@ -18,6 +18,7 @@ pub struct AvatarModule {
pub enabled: bool,
pub avatar_folder: String,
pub avatar_output_path: String,
pub blacklist: Vec<String>,
}
#[cfg(feature = "discord")]

View File

@ -36,7 +36,8 @@ const EXAMPLE_JSON: &str = r#"{
"avatar_module": {
"enabled": false,
"avatar_folder": "// Folder to grab profile pictures, they follow they member1member2...memberX.png format",
"avatar_output_path": "// Path for the copied selected avatar"
"avatar_output_path": "// Path for the copied selected avatar",
"blacklist": "// Array of members to not include in the avatar module"
},
"disc_module": {
"enabled": false,
@ -433,25 +434,39 @@ fn update_avatars(config_path: String, discord: bool, fedi: bool) -> Result<(),
let names = get(config_path, ForceFrom::None).unwrap();
#[cfg(feature = "avatar")]
avatar_module(&config, &names);
#[cfg(feature = "discord")] {
config.disc_module.enabled |= discord;
discord_module(&config);
}
#[cfg(feature = "fedi")] {
config.fedi_module.enabled |= fedi;
fedi_module(&config);
#[cfg(feature = "avatar")]
match avatar_module(&config, &names) {
Ok(_) => {
#[cfg(feature = "discord")] {
config.disc_module.enabled |= discord;
discord_module(&config);
}
#[cfg(feature = "fedi")] {
config.fedi_module.enabled |= fedi;
fedi_module(&config);
}
},
Err(e) => println!("{}", e),
}
Ok(())
}
#[cfg(feature = "avatar")]
fn avatar_module(config: &Config, names: &Vec<String>) {
fn avatar_module(config: &Config, names: &Vec<String>) -> Result<(), &'static str>{
if config.avatar_module.enabled {
let avatarnames = names.join("").to_lowercase() + ".png";
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();
for name in names {
if blacklist.contains(&name.to_lowercase()) {
let index = whitelisted_names.iter().position(|x| *x == name.to_lowercase()).unwrap();
whitelisted_names.remove(index);
}
}
let avatarnames = whitelisted_names.join("").to_lowercase() + ".png";
if Path::new(&config.avatar_module.avatar_output_path).exists() {
let _ = remove_file(&config.avatar_module.avatar_output_path);
@ -463,7 +478,14 @@ fn avatar_module(config: &Config, names: &Vec<String>) {
} else {
Command::new("sh").arg("-c").arg(format!("cp {}/{} {}", &config.avatar_module.avatar_folder, avatarnames, &config.avatar_module.avatar_output_path)).output().expect("Avatar module error");
}
println!("Avatar module finished");
if Path::new(&config.avatar_module.avatar_output_path).exists() {
println!("Avatar module finished");
return Ok(())
} else {
return Err("Avatar module failed")
}
} else {
Err("Avatar mode disabled")
}
}