Removes the TUI in favor of event based logging #15

Merged
Neshura merged 17 commits from remove_tui into main 2023-12-30 00:28:47 +00:00
Showing only changes of commit e4e1767083 - Show all commits

View file

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