This commit is contained in:
Neshura 2023-12-30 01:27:11 +01:00
parent 8be74585a0
commit 8c1da63e0c
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
6 changed files with 267 additions and 220 deletions

View file

@ -1,15 +1,15 @@
use std::collections::HashMap;
use std::sync::{Arc};
use crate::config::{Config, PostBody, SeriesConfig};
use crate::jnovel::PostInfo;
use crate::lemmy::Lemmy;
use crate::post_history::SeriesHistory;
use crate::{jnovel, lemmy, write_error, write_info, write_warn, SharedData};
use chrono::{DateTime, Duration, Utc};
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, SharedData, write_error, write_info, write_warn};
use crate::config::{Config, PostBody, SeriesConfig};
use crate::jnovel::PostInfo;
use crate::lemmy::{Lemmy};
use crate::post_history::SeriesHistory;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::sleep;
pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
@ -81,7 +81,7 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
Ok(data) => data,
Err(_) => {
login_error = true;
continue
continue;
}
};
let message = "Communities reloaded".to_owned();
@ -100,9 +100,12 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
let series = read.config.series.clone();
drop(read);
for series in series {
if handle_series(&series, &communities, &lemmy, &data).await.is_err() {
if handle_series(&series, &communities, &lemmy, &data)
.await
.is_err()
{
login_error = true;
continue
continue;
};
}
}
@ -124,7 +127,7 @@ async fn idle(data: &Arc<RwLock<SharedData>>) {
Err(e) => {
let err_msg = format!("{e}");
write_error(err_msg);
},
}
}
};
@ -133,36 +136,34 @@ async fn idle(data: &Arc<RwLock<SharedData>>) {
}
}
async fn handle_series(
series: &SeriesConfig,
communities: &HashMap<String, CommunityId>,
lemmy: &Lemmy,
data: &Arc<RwLock<SharedData>>,
) -> Result<(), ()> {
async fn handle_series(series: &SeriesConfig, communities: &HashMap<String, CommunityId>, lemmy: &Lemmy, data: &Arc<RwLock<SharedData>>) -> Result<(), ()> {
let mut post_list = match jnovel::check_feed(series.slug.as_str(), series.parted).await {
Ok(data) => data,
Err(_) => return Err(()),
};
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
let post_part_info = post_info.get_part_info();
let post_lemmy_info = post_info.get_lemmy_info();
{
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 = format!("Skipping '{}' since already posted", post_lemmy_info.title);
write_info(message);
post_list.remove(index);
continue
continue;
}
}
let post_series_config = match post_info {
PostInfo::Chapter {..} => {&series.prepub_community},
PostInfo::Volume {..} => {&series.volume_community}
PostInfo::Chapter { .. } => &series.prepub_community,
PostInfo::Volume { .. } => &series.volume_community,
};
let community_id = *communities
@ -185,23 +186,45 @@ async fn handle_series(
language_id: Some(LanguageId(37)), // TODO get this id once every few hours per API request, the ordering of IDs suggests that the EN Id might change in the future
};
let info = format!("Posting '{}' to {}", post_lemmy_info.title.as_str(), post_series_config.name.as_str());
let info = format!(
"Posting '{}' to {}",
post_lemmy_info.title.as_str(),
post_series_config.name.as_str()
);
write_info(info);
let post_id = lemmy.post(post_data).await?;
{
let read = data.read().await;
if post_series_config.pin_settings.pin_new_post_community && !read.config.protected_communities.contains(&post_series_config.name) {
let info = format!("Pinning '{}' to {}", post_lemmy_info.title, post_series_config.name.as_str());
if post_series_config.pin_settings.pin_new_post_community
&& !read
.config
.protected_communities
.contains(&post_series_config.name)
{
let info = format!(
"Pinning '{}' to {}",
post_lemmy_info.title,
post_series_config.name.as_str()
);
write_info(info);
let pinned_posts = lemmy.get_community_pinned(community_id).await?;
if !pinned_posts.is_empty() {
let community_pinned_post = &pinned_posts[0];
lemmy.unpin(community_pinned_post.post.id, PostFeatureType::Community).await?;
lemmy
.unpin(community_pinned_post.post.id, PostFeatureType::Community)
.await?;
}
lemmy.pin(post_id, PostFeatureType::Community).await?;
} else if read.config.protected_communities.contains(&post_series_config.name) {
let message = format!("Community '{}' for Series '{}' is protected. Is this intended?", &post_series_config.name, series.slug);
} else if read
.config
.protected_communities
.contains(&post_series_config.name)
{
let message = format!(
"Community '{}' for Series '{}' is protected. Is this intended?",
&post_series_config.name, series.slug
);
write_warn(message);
}
}
@ -213,13 +236,18 @@ async fn handle_series(
let pinned_posts = lemmy.get_local_pinned().await?;
if !pinned_posts.is_empty() {
for pinned_post in pinned_posts {
if read.config.protected_communities.contains(&pinned_post.community.name) {
continue
}
else {
if read
.config
.protected_communities
.contains(&pinned_post.community.name)
{
continue;
} else {
let community_pinned_post = &pinned_post;
lemmy.unpin(community_pinned_post.post.id, PostFeatureType::Local).await?;
break
lemmy
.unpin(community_pinned_post.post.id, PostFeatureType::Local)
.await?;
break;
}
}
}
@ -231,21 +259,16 @@ async fn handle_series(
drop(read);
match post_info {
PostInfo::Chapter {..} => {
part_history.chapter = post_info.get_lemmy_info().title
},
PostInfo::Volume {..} => {
part_history.volume = post_info.get_lemmy_info().title
}
PostInfo::Chapter { .. } => part_history.chapter = post_info.get_lemmy_info().title,
PostInfo::Volume { .. } => part_history.volume = post_info.get_lemmy_info().title,
}
series_history.set_part(post_part_info.as_string().as_str(), part_history);
let mut write = data.write().await;
write.post_history.set_series(series.slug.as_str(), series_history);
write
.post_history
.set_series(series.slug.as_str(), series_history);
write.post_history.save_history();
}
Ok(())
}