Automatic Post Pinning and Unpinning, Closes

This commit is contained in:
Neshura 2023-08-03 23:34:23 +02:00
parent 7f717d30b7
commit 8c58088321
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 142 additions and 32 deletions
src/config

View file

@ -1,7 +1,7 @@
use std::{fs::{self, OpenOptions}, path::Path, io::Write, thread::sleep, time};
use lemmy_api_common::{sensitive::Sensitive, post::CreatePost, community::{ListCommunities, ListCommunitiesResponse}};
use lemmy_db_schema::{newtypes::{LanguageId, CommunityId}, ListingType};
use lemmy_db_schema::{newtypes::{LanguageId, CommunityId, PostId}, ListingType};
use serde_derive::{Deserialize, Serialize};
use url::Url;
@ -98,8 +98,8 @@ impl Config {
}
pub(crate) fn check_feeds(&mut self, post_history: &mut Vec<PrevPost>
, community_ids: &CommunitiesVector, auth: &Sensitive<String>) -> Result<Vec<CreatePost>, reqwest::Error> {
let mut post_queue: Vec<CreatePost> = vec![];
, community_ids: &CommunitiesVector, auth: &Sensitive<String>) -> Result<Vec<(CreatePost, (Option<usize>, usize, String))>, reqwest::Error> {
let mut post_queue: Vec<(CreatePost, (Option<usize>, usize, String))> = vec![];
match self.feeds.iter().map(|feed| {
let res = CLIENT
@ -135,18 +135,14 @@ impl Config {
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
auth: auth.clone(),
};
post_queue.push(new_post);
match prev_post_idx {
Some(idx) => {
post_history[idx].title = data.title;
post_history[idx].last_post_url = item.url.clone();
}
None => post_history.push(PrevPost {
id: feed.id,
title: data.title,
last_post_url: item.url.clone(),
}),
}
let prev_data = (
prev_post_idx,
feed.id,
data.title
);
post_queue.push((new_post, prev_data));
}
sleep(time::Duration::from_millis(100)); // Should prevent dos-ing J-Novel servers
return Ok(());
@ -155,7 +151,6 @@ impl Config {
Err(e) => return Err(e)
}
PrevPost::save(&post_history);
return Ok(post_queue);
}
}
@ -184,7 +179,7 @@ pub(crate) enum LemmyCommunities {
aobprepub,
aoblightnovel,
aobmanga,
aobanime
metadiscussions
}
pub_struct!(FeedRedditSettings {
@ -195,6 +190,7 @@ pub_struct!(FeedRedditSettings {
// Posts structs
pub_struct!(PrevPost {
id: usize,
post_id: PostId,
title: String,
last_post_url: String,
});
@ -206,13 +202,13 @@ impl PrevPost {
if Path::new("posts.json").exists() {
let file_contents = match fs::read_to_string("posts.json") {
Ok(data) => data,
Err(e) => panic!("ERROR: secrets.json could not be read:\n\n{:#?}", e),
Err(e) => panic!("ERROR: posts.json could not be read:\n\n{:#?}", e),
};
if file_contents.len() > 0 {
let history_parse: Vec<PrevPost> = match serde_json::from_str(&file_contents) {
Ok(data) => data,
Err(e) => panic!("ERROR: secrets.json could not be parsed:\n\n{:#?}", e),
Err(e) => panic!("ERROR: posts.json could not be parsed:\n\n{:#?}", e),
};
history = history_parse;
}