This commit is contained in:
Alex Emmet 2026-04-04 21:43:54 +02:00
commit 71ac8d97fa
12 changed files with 445 additions and 445 deletions

10
iota-terms/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "iota-terms"
version = "0.1.0"
edition = "2024"
[dependencies]
iota-state = { path = "../iota-state" }
json = "*"
reqwest = "0.13.2"

73
iota-terms/src/doc.rs Normal file
View file

@ -0,0 +1,73 @@
use json::{JsonValue, object::Object};
use crate::{terms::terms_getter::Type, util::file_util::load_file};
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(unused)]
pub struct Doc {
version: String,
hash: String,
pub doc_type: Type,
timestamp: u64,
}
#[allow(dead_code)]
impl Doc {
pub fn new(version: String, hash: String, doc_type: Type, timestamp: u64) -> Doc {
Doc {
version,
hash,
doc_type,
timestamp,
}
}
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()
}
pub fn get_hash(&self) -> String {
self.hash.clone()
}
pub fn get_time(&self) -> u64 {
self.timestamp.clone()
}
pub fn get_content(&self) -> String {
load_file(
format!("docs/{}/", self.doc_type.to_str()).as_str(),
format!("{}.md", self.version).as_str(),
)
}
pub fn to_json(&self) -> JsonValue {
let mut json = JsonValue::new_object();
let _ = json.insert("version", self.version.clone());
let _ = json.insert("hash", self.hash.clone());
let _ = json.insert("unix", self.timestamp.clone());
json
}
pub fn from_json(doc_type: Type, json: Object) -> Option<Self> {
let hash = json.get("hash")?.as_str()?.to_string();
let version = json.get("version")?.as_str()?.to_string();
let timestamp = json.get("unix")?.as_u64()?;
Some(Doc {
version,
hash,
doc_type,
timestamp,
})
}
}

2
iota-terms/src/lib.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod doc;
pub mod terms_getter;

105
iota-terms/src/terms_getter.rs Executable file
View file

@ -0,0 +1,105 @@
use json::JsonValue::Object;
use crate::terms::doc::Doc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Type {
EULA,
TOS,
PP,
}
impl Type {
pub fn to_str(&self) -> &str {
match self {
Self::EULA => "eula",
Self::TOS => "tos",
Self::PP => "privacy",
}
}
pub fn to_string(&self) -> String {
match self {
Self::EULA => "End User License Agreement".to_string(),
Self::TOS => "Terms of Service".to_string(),
Self::PP => "Privacy Policy".to_string(),
}
}
}
pub fn get_link(terms_type: Type) -> String {
format!("https://legal.tensamin.net/{}/", terms_type.to_str())
}
pub fn get_newest_link(terms_type: Type) -> String {
format!("https://legal.tensamin.net/{}/newest/", terms_type.to_str())
}
pub async fn get_current_docs() -> Option<(Doc, Doc, Doc)> {
let body = reqwest::get("https://legal.tensamin.net/api/current/")
.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_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/{}/",
terms_type.to_str()
))
.await
.ok()?
.text()
.await
.ok()?;
Some(body)
}