diff --git a/src/terms/consent_state.rs b/src/terms/consent_state.rs index fd8546e..88f60df 100644 --- a/src/terms/consent_state.rs +++ b/src/terms/consent_state.rs @@ -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, Option, Option)> { + 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 + } } } diff --git a/src/terms/doc.rs b/src/terms/doc.rs index 4a87603..e2d6ceb 100644 --- a/src/terms/doc.rs +++ b/src/terms/doc.rs @@ -22,6 +22,17 @@ impl Doc { } } + pub fn equals_some(&self, other: &Option) -> 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() } diff --git a/src/terms/terms_checker.rs b/src/terms/terms_checker.rs index e78a267..3334977 100644 --- a/src/terms/terms_checker.rs +++ b/src/terms/terms_checker.rs @@ -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 diff --git a/src/terms/terms_getter.rs b/src/terms/terms_getter.rs index 2439ca7..000e4aa 100644 --- a/src/terms/terms_getter.rs +++ b/src/terms/terms_getter.rs @@ -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 { let body = reqwest::get(format!( "https://legal.tensamin.net/api/text/{}/",