Various refactors

This commit is contained in:
Roman Moisieiev 2025-09-11 12:18:06 +01:00
parent e5a40261d9
commit cde3e93565
2 changed files with 38 additions and 66 deletions

View file

@ -67,17 +67,11 @@ impl<'r> FromRow<'r, SqliteRow> for ServerMembersWolves {
} }
fn get_discord_from_row(row: &SqliteRow) -> Option<UserId> { fn get_discord_from_row(row: &SqliteRow) -> Option<UserId> {
match row.try_get("discord") { let x: i64 = row.try_get("discord").ok()?;
Ok(x) => { if x == 0 {
let tmp: i64 = x; return None;
if tmp == 0 {
None
} else {
Some(UserId::from(tmp as u64))
}
}
_ => None,
} }
Some(UserId::from(x as u64))
} }
#[derive(Debug, Clone, Deserialize, Serialize)] #[derive(Debug, Clone, Deserialize, Serialize)]

View file

@ -60,17 +60,11 @@ pub mod get_config_icons {
let config_source = minimal(); let config_source = minimal();
let file_path = format!("{}/open-governance/{}/{}", &config.home, &config_source.source.directory, &config_source.source.file); let file_path = format!("{}/open-governance/{}/{}", &config.home, &config_source.source.directory, &config_source.source.file);
let contents = fs::read_to_string(file_path).unwrap_or_else(|e| { let contents = fs::read_to_string(file_path).map_err(|e| dbg!(e)).unwrap_or_default();
dbg!(e); let festivals = toml::from_str::<ConfigTomlRemote>(&contents)
"".to_string() .map(|config| config.festivals)
}); .map_err(|e| dbg!(e))
let festivals = match toml::from_str::<ConfigTomlRemote>(&contents) { .unwrap_or_default();
Ok(config_festivals) => config_festivals.festivals,
Err(e) => {
dbg!(e);
vec![]
}
};
ConfigToml { ConfigToml {
source: config_source.source, source: config_source.source,
@ -308,16 +302,15 @@ pub mod update_icon {
fn logos_filter(festival_data: &FestivalData, existing: Vec<LogoData>) -> Vec<LogoData> { fn logos_filter(festival_data: &FestivalData, existing: Vec<LogoData>) -> Vec<LogoData> {
let mut filtered: Vec<LogoData> = vec![]; let mut filtered: Vec<LogoData> = vec![];
let allowed_files = vec![".png", ".jpeg", ".gif", ".svg"]; let allowed_extensions = ["png", "jpeg", "gif", "svg"];
'outer: for logo in existing { 'outer: for logo in existing {
let name_lowercase0 = logo.name.to_ascii_lowercase(); let name_lowercase = logo.name.to_ascii_lowercase();
let name_lowercase = name_lowercase0.to_str().unwrap_or_default(); let name_lowercase = name_lowercase.to_str().unwrap_or_default();
let mut allowed = false;
for allowed_type in &allowed_files { let allowed = {
if name_lowercase.ends_with(allowed_type) { let extension = name_lowercase.split('.').next_back().unwrap_or_default();
allowed = true; allowed_extensions.contains(&extension)
} };
}
if !allowed { if !allowed {
continue; continue;
} }
@ -332,13 +325,7 @@ pub mod update_icon {
} }
} else { } else {
// else filter using the excluded ones // else filter using the excluded ones
let mut excluded = false; let excluded = festival_data.exclusions.iter().any(|festival| name_lowercase.contains(festival));
for festival in &festival_data.exclusions {
if name_lowercase.contains(festival) {
excluded = true;
}
}
if !excluded { if !excluded {
filtered.push(logo); filtered.push(logo);
} }
@ -350,38 +337,33 @@ pub mod update_icon {
async fn logo_set(ctx: &Context, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData) { async fn logo_set(ctx: &Context, db: &Pool<Sqlite>, server: &GuildId, logo_selected: &LogoData) {
// add to the database // add to the database
if !logo_set_db(db, logo_selected).await { if logo_set_db(db, logo_selected).await.is_err() {
// something went wrong // something went wrong
return; return;
} }
if let Some(logo_path) = logo_selected.path.to_str() { let Some(logo_path) = logo_selected.path.to_str() else {
match CreateAttachment::path(logo_path).await { return;
Ok(icon) => { };
// assuming a `guild` has already been bound match CreateAttachment::path(logo_path).await {
let builder = EditGuild::new().icon(Some(&icon)); Ok(icon) => {
if let Err(e) = server.edit(ctx, builder).await { // assuming a `guild` has already been bound
dbg!(e); let builder = EditGuild::new().icon(Some(&icon));
} if let Err(e) = server.edit(ctx, builder).await {
}
Err(e) => {
dbg!(e); dbg!(e);
} }
} }
Err(e) => {
dbg!(e);
}
} }
} }
async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData) -> bool { async fn logo_set_db(db: &Pool<Sqlite>, logo_selected: &LogoData) -> Result<(), ()> {
let name = match logo_selected.name.to_str() { let name = logo_selected.name.to_str().ok_or(())?;
None => return false, let path = logo_selected.path.to_str().ok_or(())?;
Some(x) => x,
};
let path = match logo_selected.path.to_str() {
None => return false,
Some(x) => x,
};
match sqlx::query_as::<_, ServerIcons>( sqlx::query_as::<_, ServerIcons>(
" "
INSERT OR REPLACE INTO server_icons (name, date, path) INSERT OR REPLACE INTO server_icons (name, date, path)
VALUES (?1, ?2, ?3) VALUES (?1, ?2, ?3)
@ -392,13 +374,9 @@ pub mod update_icon {
.bind(path) .bind(path)
.fetch_optional(db) .fetch_optional(db)
.await .await
{ .map_err(|e| {
Ok(_) => {} dbg!(e);
Err(e) => { })?;
dbg!(e); Ok(())
return false;
}
}
true
} }
} }