From 080b0bf4d88db31f057d9320d88c8255e8691d6d Mon Sep 17 00:00:00 2001 From: Neshura Date: Mon, 18 Dec 2023 11:23:47 +0100 Subject: [PATCH] New protected communitites feature --- src/bot/mod.rs | 25 +++++++++++++++++++++---- src/config/mod.rs | 2 ++ src/main.rs | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/bot/mod.rs b/src/bot/mod.rs index c5a1c47..e6c92b5 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -236,7 +236,8 @@ async fn handle_series( let post_id = lemmy.post(post_data).await?; - if post_series_config.pin_settings.pin_new_post_community { + 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 pinned_posts = lemmy.get_community_pinned(community_id).await?; if !pinned_posts.is_empty() { let community_pinned_post = &pinned_posts[0]; @@ -244,17 +245,33 @@ 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 read = data.read().await; if post_series_config.pin_settings.pin_new_post_local { let pinned_posts = lemmy.get_local_pinned().await?; if !pinned_posts.is_empty() { - let community_pinned_post = &pinned_posts[0]; - lemmy.unpin(community_pinned_post.post.id, PostFeatureType::Local).await?; + for pinned_post in pinned_posts { + 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.pin(post_id, PostFeatureType::Local).await?; } - 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); diff --git a/src/config/mod.rs b/src/config/mod.rs index f960166..d592611 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -6,6 +6,7 @@ pub(crate) struct Config { pub(crate) instance: String, pub(crate) status_post_url: Option, pub(crate) config_reload_seconds: u32, + pub(crate) protected_communities: Vec, pub(crate) series: Vec, } @@ -34,6 +35,7 @@ impl Default for Config { instance: "".to_string(), status_post_url: None, config_reload_seconds: 21600, + protected_communities: vec![], series: vec![] } } diff --git a/src/main.rs b/src/main.rs index 3bdb4d8..b7c164c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ impl SharedData { instance: "".to_string(), status_post_url: None, config_reload_seconds: 0, + protected_communities: vec![], series: vec![], }, post_history: SeriesHistory {