2023-12-30 01:27:11 +01:00
|
|
|
use crate::config::PostBody::Description;
|
2023-12-30 00:22:21 +01:00
|
|
|
use lemmy_api_common::sensitive::Sensitive;
|
2023-06-19 00:26:50 +02:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
|
2023-12-17 18:02:51 +01:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
2023-09-18 21:00:55 +02:00
|
|
|
pub(crate) struct Config {
|
2023-12-17 14:43:00 +01:00
|
|
|
pub(crate) instance: String,
|
2023-12-30 00:22:21 +01:00
|
|
|
username: String,
|
|
|
|
password: String,
|
2023-12-17 14:43:00 +01:00
|
|
|
pub(crate) status_post_url: Option<String>,
|
|
|
|
pub(crate) config_reload_seconds: u32,
|
2023-12-18 11:23:47 +01:00
|
|
|
pub(crate) protected_communities: Vec<String>,
|
2023-12-17 14:43:00 +01:00
|
|
|
pub(crate) series: Vec<SeriesConfig>,
|
2023-09-18 21:00:55 +02:00
|
|
|
}
|
2023-06-19 00:26:50 +02:00
|
|
|
|
|
|
|
impl Config {
|
2023-12-29 22:35:16 +01:00
|
|
|
pub(crate) fn load() -> Self {
|
2023-12-29 14:35:07 +01:00
|
|
|
let cfg: Self = match confy::load(env!("CARGO_PKG_NAME"), "config") {
|
2023-12-17 22:29:18 +01:00
|
|
|
Ok(data) => data,
|
2023-12-29 14:35:07 +01:00
|
|
|
Err(e) => panic!("config.toml not found: {e}"),
|
2023-12-17 22:29:18 +01:00
|
|
|
};
|
2023-12-29 14:35:07 +01:00
|
|
|
|
2023-12-17 01:42:41 +01:00
|
|
|
if cfg.instance.is_empty() {
|
2023-12-29 14:35:07 +01:00
|
|
|
panic!("bot instance not set!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.username.is_empty() {
|
|
|
|
panic!("bot username not set!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.password.is_empty() {
|
|
|
|
panic!("bot password not provided!")
|
2023-09-18 21:00:55 +02:00
|
|
|
}
|
2023-12-17 14:43:21 +01:00
|
|
|
|
|
|
|
cfg.series.iter().for_each(|series| {
|
|
|
|
if series.prepub_community.post_body == Description {
|
|
|
|
panic!("'Description' type Post Body only supported for Volumes!")
|
|
|
|
}
|
|
|
|
});
|
2023-12-29 22:35:16 +01:00
|
|
|
cfg
|
2023-06-22 22:08:10 +02:00
|
|
|
}
|
2023-12-30 00:22:21 +01:00
|
|
|
|
|
|
|
pub(crate) fn get_username(&self) -> Sensitive<String> {
|
|
|
|
Sensitive::new(self.username.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_password(&self) -> Sensitive<String> {
|
|
|
|
Sensitive::new(self.password.clone())
|
|
|
|
}
|
2023-06-19 00:26:50 +02:00
|
|
|
}
|
|
|
|
|
2023-12-17 01:42:41 +01:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Config {
|
2023-12-29 14:34:33 +01:00
|
|
|
instance: "".to_owned(),
|
2023-12-29 14:35:07 +01:00
|
|
|
username: "".to_owned(),
|
|
|
|
password: "".to_owned(),
|
2023-12-17 01:42:41 +01:00
|
|
|
status_post_url: None,
|
|
|
|
config_reload_seconds: 21600,
|
2023-12-18 11:23:47 +01:00
|
|
|
protected_communities: vec![],
|
2023-12-30 01:27:11 +01:00
|
|
|
series: vec![],
|
2023-06-19 00:26:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-17 01:42:41 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub(crate) struct SeriesConfig {
|
2023-12-17 14:43:00 +01:00
|
|
|
pub(crate) slug: String,
|
|
|
|
pub(crate) parted: bool,
|
|
|
|
pub(crate) prepub_community: PostConfig,
|
|
|
|
pub(crate) volume_community: PostConfig,
|
2023-12-17 01:42:41 +01:00
|
|
|
}
|
2023-06-22 22:08:10 +02:00
|
|
|
|
2023-12-17 01:42:41 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub(crate) struct PostConfig {
|
2023-12-17 20:17:00 +01:00
|
|
|
pub(crate) name: String,
|
2023-12-17 14:43:00 +01:00
|
|
|
pub(crate) pin_settings: PinConfig,
|
|
|
|
pub(crate) post_body: PostBody,
|
2023-12-17 01:42:41 +01:00
|
|
|
}
|
2023-06-22 22:08:10 +02:00
|
|
|
|
2023-12-17 01:42:41 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
pub(crate) struct PinConfig {
|
2023-12-17 14:43:00 +01:00
|
|
|
pub(crate) pin_new_post_local: bool,
|
|
|
|
pub(crate) pin_new_post_community: bool,
|
2023-12-17 01:42:41 +01:00
|
|
|
}
|
2023-06-22 22:08:10 +02:00
|
|
|
|
2023-12-17 14:43:00 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
2023-12-17 01:42:41 +01:00
|
|
|
#[serde(tag = "body_type", content = "body_content")]
|
|
|
|
pub(crate) enum PostBody {
|
|
|
|
None,
|
|
|
|
Description,
|
|
|
|
Custom(String),
|
2023-06-22 22:08:10 +02:00
|
|
|
}
|