[WIP] Terms update support

This commit is contained in:
Alex Emmet 2026-02-06 18:25:24 +01:00
commit 43af786d16
4 changed files with 91 additions and 24 deletions

View file

@ -1,6 +1,10 @@
use crate::{
terms::{doc::Doc, terms_checker, terms_getter::Type},
util::file_util::{load_file, save_file},
terms::{
doc::Doc,
terms_checker,
terms_getter::{Type, get_current_docs, get_newest_docs},
},
util::file_util::load_file,
};
use std::time::{SystemTime, UNIX_EPOCH};
@ -8,21 +12,44 @@ pub struct ConsentManager;
impl ConsentManager {
pub async fn check() -> (bool, bool) {
let file = load_file("", "agreements");
let existing = ConsentState::from_str(&file).sanitize();
let final_state = if existing.accepted_eula {
existing
let file_state = ConsentState::from_str(&file).sanitize();
let accepted_state = if !&file_state.accepted_eula {
terms_checker::run_consent_ui(file_state).await
} else {
let state = terms_checker::run_consent_ui(existing).await.sanitize();
let string = state.clone().to_string().await;
save_file("", "agreements", &string);
state
file_state
};
(
final_state.accepted_eula,
final_state.accepted_pp && final_state.accepted_tos,
)
if let Some((current_eula, current_tos, current_privacy)) = get_current_docs().await {
if current_eula.equals_some(&accepted_state.eula) {
if current_tos.equals_some(&accepted_state.tos)
&& current_privacy.equals_some(&accepted_state.pp)
{
(true, true)
} else {
(true, false)
}
} else {
(false, false)
}
} else {
println!("There was an error while loading our EULA, please retry later!");
(false, false)
}
}
pub async fn check_updates() -> Option<(Option<Doc>, Option<Doc>, Option<Doc>)> {
let file = load_file("", "agreements");
let accepted_state = ConsentState::from_str(&file).sanitize();
if let (
Some((current_eula, current_tos, current_privacy)),
Some((newest_eula, newest_tos, newest_privacy)),
) = (get_current_docs().await, get_newest_docs().await)
{
None
} else {
None
}
}
}

View file

@ -22,6 +22,17 @@ impl Doc {
}
}
pub fn equals_some(&self, other: &Option<Self>) -> bool {
if let Some(other) = other {
self.get_version() == other.get_version() && self.get_hash() == other.get_hash()
} else {
false
}
}
pub fn equals(&self, other: &Self) -> bool {
self.get_version() == other.get_version() && self.get_hash() == other.get_hash()
}
pub fn get_version(&self) -> String {
self.version.clone()
}

View file

@ -13,7 +13,7 @@ use ratatui::{
};
use std::time::Duration;
pub async fn run_consent_ui(consent: ConsentState) -> ConsentState {
pub async fn run_consent_ui(mut consent: ConsentState) -> ConsentState {
let mut terminal = ratatui::init();
let (mut eula, mut tos, mut pp) = (false, false, false);
@ -277,19 +277,19 @@ pub async fn run_consent_ui(consent: ConsentState) -> ConsentState {
match result {
UserChoice::AcceptAll => {
consent.accepted_eula == true;
consent.accepted_tos == true;
consent.accepted_pp == true;
consent.accepted_eula = true;
consent.accepted_tos = true;
consent.accepted_pp = true;
}
UserChoice::AcceptEULA => {
consent.accepted_eula == true;
consent.accepted_tos == true;
consent.accepted_pp == true;
consent.accepted_eula = true;
consent.accepted_tos = true;
consent.accepted_pp = true;
}
UserChoice::Deny => {
consent.accepted_eula == false;
consent.accepted_tos == false;
consent.accepted_pp == false;
consent.accepted_eula = false;
consent.accepted_tos = false;
consent.accepted_pp = false;
}
};
consent

View file

@ -59,6 +59,35 @@ pub async fn get_current_docs() -> Option<(Doc, Doc, Doc)> {
}
}
pub async fn get_newest_docs() -> Option<(Doc, Doc, Doc)> {
let body = reqwest::get("https://legal.tensamin.net/api/newest/")
.await
.ok()?
.text()
.await
.ok()?;
let json = json::parse(&body).ok()?;
if let Object(eula) = &json["eula"] {
if let Object(tos) = &json["tos"] {
if let Object(pp) = &json["pp"] {
Some((
Doc::from_json(Type::EULA, eula.clone())?,
Doc::from_json(Type::TOS, tos.clone())?,
Doc::from_json(Type::PP, pp.clone())?,
))
} else {
None
}
} else {
None
}
} else {
None
}
}
pub async fn get_terms(terms_type: Type) -> Option<String> {
let body = reqwest::get(format!(
"https://legal.tensamin.net/api/text/{}/",