parent
bba7fae8b9
commit
47e6cc59c0
7 changed files with 253 additions and 123 deletions
src/bot
174
src/bot/mod.rs
174
src/bot/mod.rs
|
@ -1,10 +1,9 @@
|
|||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::sync::{Arc};
|
||||
use chrono::{DateTime, Duration, Timelike, Utc};
|
||||
use lemmy_db_schema::newtypes::{CommunityId, LanguageId};
|
||||
use lemmy_db_schema::PostFeatureType;
|
||||
use tokio::sync::{RwLock, RwLockWriteGuard};
|
||||
use tokio::sync::{RwLock};
|
||||
use crate::{jnovel, lemmy, Message, SharedData};
|
||||
use crate::config::{Config, PostBody, SeriesConfig};
|
||||
use crate::jnovel::PostInfo;
|
||||
|
@ -12,7 +11,7 @@ use crate::lemmy::{CustomCreatePost, Lemmy};
|
|||
use crate::post_history::SeriesHistory;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub(crate) async fn run(data: Arc<RwLock<SharedData>>){
|
||||
pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
|
||||
let credentials = match lemmy::Credentials::set_credentials() {
|
||||
Ok(creds) => creds,
|
||||
Err(e) => panic!("{}", e.to_string()),
|
||||
|
@ -23,17 +22,20 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>){
|
|||
let mut login_error: bool;
|
||||
let mut communities;
|
||||
{
|
||||
let mut shared_data = data.write().await;
|
||||
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
|
||||
shared_data.config = match Config::load() {
|
||||
write.config = match Config::load() {
|
||||
Ok(data) => data,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
||||
last_reload = Utc::now();
|
||||
}
|
||||
|
||||
lemmy = match lemmy::login(&credentials, shared_data.config.instance.as_str()).await {
|
||||
{
|
||||
let read = data.read().await;
|
||||
lemmy = match lemmy::login(&credentials, read.config.instance.as_str()).await {
|
||||
Ok(data) => data,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
||||
|
@ -49,64 +51,101 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>){
|
|||
sleep(Duration::milliseconds(100).to_std().unwrap()).await;
|
||||
}
|
||||
|
||||
{
|
||||
let mut write = data.write().await;
|
||||
write.start = Utc::now();
|
||||
}
|
||||
|
||||
loop {
|
||||
idle(&data).await;
|
||||
|
||||
let mut shared_data = data.write().await;
|
||||
{
|
||||
let mut write = data.write().await;
|
||||
|
||||
shared_data.start = Utc::now();
|
||||
write.start = Utc::now();
|
||||
|
||||
if shared_data.start - last_reload > Duration::seconds(shared_data.config.config_reload_seconds as i64) {
|
||||
shared_data.config = match Config::load() {
|
||||
Ok(data) => data,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
||||
}
|
||||
|
||||
if login_error {
|
||||
lemmy = match lemmy::login(&credentials, shared_data.config.instance.as_str()).await {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
shared_data.messages.push(Message::Error(format!("{}", e)));
|
||||
continue
|
||||
}
|
||||
};
|
||||
login_error = false;
|
||||
}
|
||||
|
||||
if shared_data.start - last_reload > Duration::seconds(shared_data.config.config_reload_seconds as i64) {
|
||||
communities = match lemmy.get_communities().await {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
login_error = true;
|
||||
shared_data.messages.push(Message::Error(format!("{}", e)));
|
||||
continue
|
||||
}
|
||||
};
|
||||
last_reload = Utc::now();
|
||||
}
|
||||
|
||||
shared_data.post_history = match SeriesHistory::load_history() {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
login_error = true;
|
||||
shared_data.messages.push(Message::Warning(format!("{}", e)));
|
||||
continue
|
||||
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),
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
for series in shared_data.config.series.clone() {
|
||||
match handle_series(&series, &communities, &lemmy, &mut shared_data).await {
|
||||
{
|
||||
let read = data.read().await;
|
||||
if login_error {
|
||||
lemmy = match lemmy::login(&credentials, read.config.instance.as_str()).await {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
drop(read);
|
||||
let mut write = data.write().await;
|
||||
write.messages.push(Message::Error(e.to_string()));
|
||||
continue
|
||||
}
|
||||
};
|
||||
login_error = false;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let read = data.read().await;
|
||||
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) => {
|
||||
login_error = true;
|
||||
drop(read);
|
||||
let mut write = data.write().await;
|
||||
write.messages.push(Message::Error(e.to_string()));
|
||||
continue
|
||||
}
|
||||
};
|
||||
last_reload = Utc::now();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let mut write = data.write().await;
|
||||
write.post_history = match SeriesHistory::load_history() {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
login_error = true;
|
||||
shared_data.messages.push(Message::Warning(format!("{}", e)));
|
||||
write.messages.push(Message::Warning(e.to_string()));
|
||||
continue
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let _ = shared_data.downgrade();
|
||||
{
|
||||
let read = data.read().await;
|
||||
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
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -124,7 +163,7 @@ async fn idle(data: &Arc<RwLock<SharedData>>) {
|
|||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
let mut write = data.write().await;
|
||||
write.messages.push(Message::Error(format!("{}", e)))
|
||||
write.messages.push(Message::Error(e.to_string()))
|
||||
},
|
||||
}
|
||||
};
|
||||
|
@ -134,26 +173,35 @@ async fn idle(data: &Arc<RwLock<SharedData>>) {
|
|||
}
|
||||
}
|
||||
|
||||
async fn handle_series<'a>(
|
||||
async fn handle_series(
|
||||
series: &SeriesConfig,
|
||||
communities: &HashMap<String, CommunityId>,
|
||||
lemmy: &Lemmy,
|
||||
data: &mut RwLockWriteGuard<'a, SharedData>,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
data: &Arc<RwLock<SharedData>>,
|
||||
) -> Result<(), String> {
|
||||
|
||||
let mut post_list = match jnovel::check_feed(series.slug.as_str(), series.parted).await {
|
||||
Ok(data) => data,
|
||||
Err(e) => panic!("{:#?}", e),
|
||||
Err(e) => {
|
||||
return Err(e.to_string());
|
||||
},
|
||||
};
|
||||
|
||||
for (index, post_info) in post_list.clone().iter().enumerate() { // todo .clone() likely not needed
|
||||
let post_part_info = post_info.get_part_info();
|
||||
let post_lemmy_info = post_info.get_lemmy_info();
|
||||
|
||||
if data.post_history.check_for_post(series.slug.as_str(), post_part_info.as_string().as_str(), post_lemmy_info.title.as_str()) {
|
||||
data.messages.push(Message::Info(format!("Skipping '{}' since already posted", post_lemmy_info.title)));
|
||||
|
||||
{
|
||||
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()) {
|
||||
drop(read);
|
||||
let mut write = data.write().await;
|
||||
write.messages.push(Message::Info(format!("Skipping '{}' since already posted", post_lemmy_info.title)));
|
||||
post_list.remove(index);
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
let post_series_config = match post_info {
|
||||
PostInfo::Chapter {..} => {&series.prepub_community},
|
||||
|
@ -200,8 +248,10 @@ async fn handle_series<'a>(
|
|||
lemmy.pin(post_id, PostFeatureType::Local).await?;
|
||||
}
|
||||
|
||||
let mut series_history = data.post_history.get_series(series.slug.as_str());
|
||||
let read = data.read().await;
|
||||
let mut series_history = read.post_history.get_series(series.slug.as_str());
|
||||
let mut part_history = series_history.get_part(post_part_info.as_string().as_str());
|
||||
drop(read);
|
||||
|
||||
match post_info {
|
||||
PostInfo::Chapter {..} => {
|
||||
|
@ -213,8 +263,12 @@ async fn handle_series<'a>(
|
|||
}
|
||||
|
||||
series_history.set_part(post_part_info.as_string().as_str(), part_history);
|
||||
data.post_history.set_series(series.slug.as_str(), series_history);
|
||||
let _ = data.post_history.save_history()?;
|
||||
let mut write = data.write().await;
|
||||
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))
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue