diff --git a/src/bot/mod.rs b/src/bot.rs similarity index 66% rename from src/bot/mod.rs rename to src/bot.rs index 50577d8..4577889 100644 --- a/src/bot/mod.rs +++ b/src/bot.rs @@ -5,7 +5,7 @@ use lemmy_api_common::post::CreatePost; use lemmy_db_schema::newtypes::{CommunityId, LanguageId}; use lemmy_db_schema::PostFeatureType; use tokio::sync::{RwLock}; -use crate::{jnovel, lemmy, Message, SharedData}; +use crate::{jnovel, lemmy, SharedData, write_error, write_info, write_warn}; use crate::config::{Config, PostBody, SeriesConfig}; use crate::jnovel::PostInfo; use crate::lemmy::{Lemmy}; @@ -17,35 +17,26 @@ pub(crate) async fn run(data: Arc>) { let mut lemmy: Lemmy; let mut login_error: bool; let mut communities; - let mut credentials; { let mut write = data.write().await; // Errors during bot init are likely unrecoverable and therefore should panic the bot // Does not really matter since the bot will get restarted anyway but this way the uptime url logs a downtime - write.config = match Config::load() { - Ok(data) => data, - Err(e) => panic!("{}", e), - }; + write.config = Config::load(); last_reload = Utc::now(); } { let read = data.read().await; - credentials = lemmy::Credentials::set_credentials(&read.config); - } - - { - let read = data.read().await; - lemmy = match lemmy::login(&credentials, read.config.instance.as_str()).await { + lemmy = match lemmy::login(&read.config).await { Ok(data) => data, - Err(e) => panic!("{}", e), + Err(_) => panic!(), }; login_error = false; communities = match lemmy.get_communities().await { Ok(data) => data, - Err(e) => panic!("{}", e), + Err(_) => panic!(), }; } @@ -67,36 +58,18 @@ pub(crate) async fn run(data: Arc>) { write.start = Utc::now(); if write.start - last_reload >= Duration::seconds(write.config.config_reload_seconds as i64) { - write.config = match Config::load() { - Ok(data) => data, - Err(e) => panic!("{}", e), - }; - let message = Message::Info("Config reloaded".to_owned()); - if !write.messages.contains(&message) { - write.messages.push(message); - }; - } - } - - { - let read = data.read().await; - - if read.start - last_reload >= Duration::seconds(read.config.config_reload_seconds as i64) { - credentials = lemmy::Credentials::set_credentials(&read.config); + write.config = Config::load(); + let message = "Config reloaded".to_owned(); + write_info(message); } } { let read = data.read().await; if login_error { - lemmy = match lemmy::login(&credentials, read.config.instance.as_str()).await { + lemmy = match lemmy::login(&read.config).await { Ok(data) => data, - Err(e) => { - drop(read); - let mut write = data.write().await; - write.messages.push(Message::Error(e.to_string())); - continue - } + Err(_) => continue, }; login_error = false; } @@ -107,20 +80,13 @@ pub(crate) async fn run(data: Arc>) { if read.start - last_reload >= Duration::seconds(read.config.config_reload_seconds as i64) { communities = match lemmy.get_communities().await { Ok(data) => data, - Err(e) => { + Err(_) => { login_error = true; - drop(read); - let mut write = data.write().await; - write.messages.push(Message::Error(e.to_string())); continue } }; - let message = Message::Info("Communities reloaded".to_owned()); - if !read.messages.contains(&message) { - drop(read); - let mut write = data.write().await; - write.messages.push(message); - }; + let message = "Communities reloaded".to_owned(); + write_info(message); last_reload = Utc::now(); } } @@ -129,10 +95,7 @@ pub(crate) async fn run(data: Arc>) { let mut write = data.write().await; write.post_history = match SeriesHistory::load_history() { Ok(data) => data, - Err(e) => { - write.messages.push(Message::Warning(e.to_string())); - continue - } + Err(_) => continue, }; } @@ -141,32 +104,13 @@ pub(crate) async fn run(data: Arc>) { let series = read.config.series.clone(); drop(read); for series in series { - match handle_series(&series, &communities, &lemmy, &data).await { - Ok(data) => data, - Err(e) => { - login_error = true; - let mut write = data.write().await; - write.messages.push(Message::Warning(e.to_string())); - continue - } + if handle_series(&series, &communities, &lemmy, &data).await.is_err() { + login_error = true; + continue }; } } - let read = data.read().await; - if read.messages.len() > 15 { - let mut list = read.messages.clone(); - drop(read); - list.reverse(); - while list.len() > 15 { - list.pop(); - } - list.reverse(); - let mut write = data.write().await; - write.messages = list - } - - idle(&data).await; } } @@ -182,8 +126,8 @@ async fn idle(data: &Arc>) { match reqwest::get(status_url).await { Ok(_) => {} Err(e) => { - let mut write = data.write().await; - write.messages.push(Message::Error(e.to_string())) + let err_msg = format!("{e}"); + write_error(err_msg); }, } }; @@ -198,13 +142,11 @@ async fn handle_series( communities: &HashMap, lemmy: &Lemmy, data: &Arc>, -) -> Result<(), String> { +) -> Result<(), ()> { let mut post_list = match jnovel::check_feed(series.slug.as_str(), series.parted).await { Ok(data) => data, - Err(e) => { - return Err(e.to_string()); - }, + Err(_) => return Err(()), }; for (index, post_info) in post_list.clone().iter().enumerate() { // todo .clone() likely not needed @@ -215,12 +157,8 @@ async fn handle_series( { let read = data.read().await; if read.post_history.check_for_post(series.slug.as_str(), post_part_info.as_string().as_str(), post_lemmy_info.title.as_str()) { - let message = Message::Info(format!("Skipping '{}' since already posted", post_lemmy_info.title)); - if !read.messages.contains(&message) { - drop(read); - let mut write = data.write().await; - write.messages.push(message); - }; + let message = format!("Skipping '{}' since already posted", post_lemmy_info.title); + write_info(message); post_list.remove(index); continue } @@ -263,12 +201,8 @@ async fn handle_series( } lemmy.pin(post_id, PostFeatureType::Community).await?; } else if read.config.protected_communities.contains(&post_series_config.name) { - let message = Message::Warning(format!("Community '{}' for Series '{}' is protected. Is this intended?", &post_series_config.name, series.slug)); - if !read.messages.contains(&message) { - drop(read); - let mut write = data.write().await; - write.messages.push(message); - } + let message = format!("Community '{}' for Series '{}' is protected. Is this intended?", &post_series_config.name, series.slug); + write_warn(message); } } @@ -308,7 +242,11 @@ async fn handle_series( write.post_history.set_series(series.slug.as_str(), series_history); let _ = match write.post_history.save_history() { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; } Ok(())