use std::sync::atomic::{AtomicBool, Ordering};
use super::Kind;
pub const CONSENT_REQUIRED: &str = "realtime_consent_required";
static ALLOW_ALWAYS: AtomicBool = AtomicBool::new(false);
pub fn allowed() -> bool {
check().is_ok()
}
pub fn check() -> Result<(), String> {
if !crate::db::session_is_live() {
return Err("Vector is still connecting.".into());
}
match super::preference() {
None => Err("Vector is still connecting.".into()),
Some(Kind::Clearnet) => Ok(()),
Some(k) => {
if ALLOW_ALWAYS.load(Ordering::Acquire) || super::prefs::current().consent() == Some(k) {
Ok(())
} else {
Err(CONSENT_REQUIRED.into())
}
}
}
}
pub fn grant() {
if let Some(k) = super::preference().filter(|k| *k != Kind::Clearnet) {
super::prefs::live().set_consent(Some(k));
super::host::notify(super::host::TransportEvent::Changed);
}
}
const DIRECT_KEY: &str = "realtime_direct";
pub fn direct() -> bool {
crate::db::get_sql_setting(DIRECT_KEY.to_string()).ok().flatten().is_some_and(|v| v == "1")
}
pub async fn set_direct(on: bool) -> Result<(), String> {
crate::db::set_sql_setting(DIRECT_KEY.to_string(), if on { "1" } else { "0" }.to_string())?;
#[cfg(feature = "xdc")]
crate::xdc::mesh::retire_live_if_idle().await;
Ok(())
}
pub fn allow_always(on: bool) {
ALLOW_ALWAYS.store(on, Ordering::Release);
}