|
|
|
@ -4,7 +4,7 @@ use std::path::{Path, PathBuf}; |
|
|
|
|
use elefren::AppData; |
|
|
|
|
|
|
|
|
|
use crate::error::GroupError; |
|
|
|
|
use crate::store::{DEFAULT_LOCALE_NAME, CommonConfig}; |
|
|
|
|
use crate::store::{CommonConfig, DEFAULT_LOCALE_NAME}; |
|
|
|
|
use crate::tr::TranslationTable; |
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)] |
|
|
|
@ -25,7 +25,7 @@ struct FixedConfig { |
|
|
|
|
_path: PathBuf, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)] |
|
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)] |
|
|
|
|
#[serde(default, deny_unknown_fields)] |
|
|
|
|
struct MutableConfig { |
|
|
|
|
/// Hashtags the group will auto-boost from it's members
|
|
|
|
@ -48,7 +48,7 @@ struct MutableConfig { |
|
|
|
|
_path: PathBuf, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)] |
|
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)] |
|
|
|
|
#[serde(default, deny_unknown_fields)] |
|
|
|
|
struct StateConfig { |
|
|
|
|
/// Last seen notification timestamp (millis)
|
|
|
|
@ -94,33 +94,6 @@ impl Default for FixedConfig { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Default for MutableConfig { |
|
|
|
|
fn default() -> Self { |
|
|
|
|
Self { |
|
|
|
|
group_tags: Default::default(), |
|
|
|
|
admin_users: Default::default(), |
|
|
|
|
member_users: Default::default(), |
|
|
|
|
banned_users: Default::default(), |
|
|
|
|
optout_users: Default::default(), |
|
|
|
|
member_only: false, |
|
|
|
|
banned_servers: Default::default(), |
|
|
|
|
_dirty: false, |
|
|
|
|
_path: PathBuf::default(), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Default for StateConfig { |
|
|
|
|
fn default() -> Self { |
|
|
|
|
Self { |
|
|
|
|
last_notif_ts: 0, |
|
|
|
|
last_status_ts: 0, |
|
|
|
|
_dirty: false, |
|
|
|
|
_path: PathBuf::default(), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
macro_rules! impl_change_tracking { |
|
|
|
|
($struc:ident) => { |
|
|
|
|
impl $struc { |
|
|
|
@ -206,7 +179,7 @@ async fn load_locale_override_file(locale_path: impl AsRef<Path>) -> Result<Opti |
|
|
|
|
let locale_path = locale_path.as_ref(); |
|
|
|
|
if locale_path.is_file() { |
|
|
|
|
let f = tokio::fs::read(&locale_path).await?; |
|
|
|
|
let opt : TranslationTable = json5::from_str(&String::from_utf8_lossy(&f))?; |
|
|
|
|
let opt: TranslationTable = json5::from_str(&String::from_utf8_lossy(&f))?; |
|
|
|
|
Ok(Some(opt)) |
|
|
|
|
} else { |
|
|
|
|
Ok(None) |
|
|
|
@ -259,7 +232,11 @@ impl GroupConfig { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// (re)init using new authorization
|
|
|
|
|
pub(crate) async fn initialize_by_appdata(acct: String, appdata: AppData, group_dir: PathBuf) -> Result<(), GroupError> { |
|
|
|
|
pub(crate) async fn initialize_by_appdata( |
|
|
|
|
acct: String, |
|
|
|
|
appdata: AppData, |
|
|
|
|
group_dir: PathBuf, |
|
|
|
|
) -> Result<(), GroupError> { |
|
|
|
|
if !group_dir.is_dir() { |
|
|
|
|
debug!("Creating group directory"); |
|
|
|
|
tokio::fs::create_dir_all(&group_dir).await?; |
|
|
|
@ -306,12 +283,17 @@ impl GroupConfig { |
|
|
|
|
/* state */ |
|
|
|
|
let state = load_or_create_state_file(state_path).await?; |
|
|
|
|
|
|
|
|
|
let g = GroupConfig { config, control, state, _group_tr: TranslationTable::new() }; |
|
|
|
|
let g = GroupConfig { |
|
|
|
|
config, |
|
|
|
|
control, |
|
|
|
|
state, |
|
|
|
|
_group_tr: TranslationTable::new(), |
|
|
|
|
}; |
|
|
|
|
g.warn_of_bad_config(); |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub(crate) async fn from_dir(group_dir: PathBuf, cc : &CommonConfig) -> Result<Self, GroupError> { |
|
|
|
|
pub(crate) async fn from_dir(group_dir: PathBuf, cc: &CommonConfig) -> Result<Self, GroupError> { |
|
|
|
|
let config_path = group_dir.join("config.json"); |
|
|
|
|
let control_path = group_dir.join("control.json"); |
|
|
|
|
let state_path = group_dir.join("state.json"); |
|
|
|
@ -338,7 +320,12 @@ impl GroupConfig { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let g = GroupConfig { config, control, state, _group_tr: tr }; |
|
|
|
|
let g = GroupConfig { |
|
|
|
|
config, |
|
|
|
|
control, |
|
|
|
|
state, |
|
|
|
|
_group_tr: tr, |
|
|
|
|
}; |
|
|
|
|
g.warn_of_bad_config(); |
|
|
|
|
Ok(g) |
|
|
|
|
} |
|
|
|
@ -458,7 +445,7 @@ impl GroupConfig { |
|
|
|
|
/// Check if the user's server is banned
|
|
|
|
|
fn is_users_server_banned(&self, acct: &str) -> bool { |
|
|
|
|
let server = acct_to_server(acct); |
|
|
|
|
self.is_server_banned(server) |
|
|
|
|
self.is_server_banned(&server) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub(crate) fn can_write(&self, acct: &str) -> bool { |
|
|
|
@ -574,8 +561,8 @@ impl GroupConfig { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn acct_to_server(acct: &str) -> &str { |
|
|
|
|
acct.split('@').nth(1).unwrap_or_default() |
|
|
|
|
fn acct_to_server(acct: &str) -> String { |
|
|
|
|
crate::utils::acct_to_server(acct).unwrap_or_default() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
@ -588,15 +575,15 @@ mod tests { |
|
|
|
|
config: Default::default(), |
|
|
|
|
control: Default::default(), |
|
|
|
|
state: Default::default(), |
|
|
|
|
_group_tr: Default::default() |
|
|
|
|
_group_tr: Default::default(), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn test_acct_to_server() { |
|
|
|
|
assert_eq!("pikachu.rocks", acct_to_server("raichu@pikachu.rocks")); |
|
|
|
|
assert_eq!("pikachu.rocks", acct_to_server("m@pikachu.rocks")); |
|
|
|
|
assert_eq!("", acct_to_server("what")); |
|
|
|
|
assert_eq!("pikachu.rocks".to_string(), acct_to_server("raichu@pikachu.rocks")); |
|
|
|
|
assert_eq!("pikachu.rocks".to_string(), acct_to_server("m@pikachu.rocks")); |
|
|
|
|
assert_eq!("".to_string(), acct_to_server("what")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|