mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-09-19 03:18:46 +00:00
Add banner
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
|
||||
use yaml_rust::{Yaml, YamlLoader};
|
||||
|
||||
use crate::data::group_id::GroupID;
|
||||
use crate::utils::date_utils::time;
|
||||
|
||||
/// Server configuration
|
||||
///
|
||||
@@ -35,6 +37,31 @@ pub struct IndependentPushService {
|
||||
pub public_url: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum BannerNature {
|
||||
Information,
|
||||
Warning,
|
||||
Success,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize)]
|
||||
pub struct Banner {
|
||||
pub enabled: bool,
|
||||
pub expire: Option<u64>,
|
||||
pub nature: BannerNature,
|
||||
pub message: HashMap<String, String>,
|
||||
pub link: Option<String>,
|
||||
}
|
||||
|
||||
impl Banner {
|
||||
pub fn is_visible(&self) -> bool {
|
||||
self.enabled &&
|
||||
self.expire.map(|v| v < 1 || v > time()).unwrap_or(true)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Config {
|
||||
pub port: u64,
|
||||
@@ -54,6 +81,7 @@ pub struct Config {
|
||||
pub database: DatabaseConfig,
|
||||
pub rtc_relay: Option<RtcRelayConfig>,
|
||||
pub admin_url: String,
|
||||
pub banner: Option<Banner>,
|
||||
pub forez_groups: Vec<GroupID>,
|
||||
}
|
||||
|
||||
@@ -140,6 +168,28 @@ impl Config {
|
||||
})
|
||||
};
|
||||
|
||||
let parsed_banner = &parsed["banner"];
|
||||
let banner = match parsed_banner.is_badvalue() {
|
||||
true => None,
|
||||
false => Some(Banner {
|
||||
enabled: Self::yaml_bool(parsed_banner, "enabled"),
|
||||
expire: match Self::yaml_u64(parsed_banner, "expire") {
|
||||
0 => None,
|
||||
v => Some(v)
|
||||
},
|
||||
nature: match Self::yaml_str(parsed_banner, "nature").as_str() {
|
||||
"information" => BannerNature::Information,
|
||||
"warning" => BannerNature::Warning,
|
||||
"success" => BannerNature::Success,
|
||||
v => panic!("Invalid banner nature: {} !", v)
|
||||
},
|
||||
message: parsed_banner["message"].as_hash().unwrap().iter()
|
||||
.map(|(k, v)| (k.as_str().unwrap().to_string(), v.as_str().unwrap().to_string()))
|
||||
.collect(),
|
||||
link: parsed_banner["link"].as_str().map(|s| s.to_string()),
|
||||
})
|
||||
};
|
||||
|
||||
let forez_groups = match &parsed["forez_groups"] {
|
||||
Yaml::BadValue => vec![],
|
||||
_ => {
|
||||
@@ -183,6 +233,8 @@ impl Config {
|
||||
|
||||
admin_url: Self::yaml_str(parsed, "admin-url"),
|
||||
|
||||
banner,
|
||||
|
||||
forez_groups,
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user