[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::{ use crate::{
terms::{doc::Doc, terms_checker, terms_getter::Type}, terms::{
util::file_util::{load_file, save_file}, doc::Doc,
terms_checker,
terms_getter::{Type, get_current_docs, get_newest_docs},
},
util::file_util::load_file,
}; };
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
@ -8,21 +12,44 @@ pub struct ConsentManager;
impl ConsentManager { impl ConsentManager {
pub async fn check() -> (bool, bool) { pub async fn check() -> (bool, bool) {
let file = load_file("", "agreements"); let file = load_file("", "agreements");
let existing = ConsentState::from_str(&file).sanitize(); let file_state = ConsentState::from_str(&file).sanitize();
let accepted_state = if !&file_state.accepted_eula {
let final_state = if existing.accepted_eula { terms_checker::run_consent_ui(file_state).await
existing
} else { } else {
let state = terms_checker::run_consent_ui(existing).await.sanitize(); file_state
let string = state.clone().to_string().await;
save_file("", "agreements", &string);
state
}; };
( if let Some((current_eula, current_tos, current_privacy)) = get_current_docs().await {
final_state.accepted_eula, if current_eula.equals_some(&accepted_state.eula) {
final_state.accepted_pp && final_state.accepted_tos, 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 { pub fn get_version(&self) -> String {
self.version.clone() self.version.clone()
} }

View file

@ -13,7 +13,7 @@ use ratatui::{
}; };
use std::time::Duration; 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 terminal = ratatui::init();
let (mut eula, mut tos, mut pp) = (false, false, false); 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 { match result {
UserChoice::AcceptAll => { UserChoice::AcceptAll => {
consent.accepted_eula == true; consent.accepted_eula = true;
consent.accepted_tos == true; consent.accepted_tos = true;
consent.accepted_pp == true; consent.accepted_pp = true;
} }
UserChoice::AcceptEULA => { UserChoice::AcceptEULA => {
consent.accepted_eula == true; consent.accepted_eula = true;
consent.accepted_tos == true; consent.accepted_tos = true;
consent.accepted_pp == true; consent.accepted_pp = true;
} }
UserChoice::Deny => { UserChoice::Deny => {
consent.accepted_eula == false; consent.accepted_eula = false;
consent.accepted_tos == false; consent.accepted_tos = false;
consent.accepted_pp == false; consent.accepted_pp = false;
} }
}; };
consent 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> { pub async fn get_terms(terms_type: Type) -> Option<String> {
let body = reqwest::get(format!( let body = reqwest::get(format!(
"https://legal.tensamin.net/api/text/{}/", "https://legal.tensamin.net/api/text/{}/",