Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat: dynamically fetch tos and policy" #357

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions phira/locales/en-US/common.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ chart-unstable = Unstable
list-empty = Nothing here

tos-and-policy = Terms of Service and Privacy Policy
tos-and-policy-desc = You must read and agree to our Terms of Service and Privacy Policy before using Phira online services provided by TeamFlos.
tos-and-policy-desc = You must read (by clicking here) and agree to our Terms of Service and Privacy Policy before using Phira online services provided by TeamFlos.
tos-deny = Deny
tos-accept = Accept
fetch-tos-policy-failed = Failed to fetch Terms of Service and Privacy Policy
warn-deny-tos-policy = Accept to use online service

open-in-web = Open in Web

Expand Down
4 changes: 1 addition & 3 deletions phira/locales/zh-CN/common.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ chart-unstable = 未上架
list-empty = 空空如也

tos-and-policy = 《服务条款》和《隐私政策》
tos-and-policy-desc = 在使用由 TeamFlos 提供的 Phira 线上服务部分之前,你必须阅读并同意我们的《服务条款》和《隐私政策》。
tos-and-policy-desc = 在使用由 TeamFlos 提供的 Phira 线上服务部分之前,你必须 *点击此处* 阅读并同意我们的《服务条款》和《隐私政策》。
tos-deny = 拒绝
tos-accept = 同意
fetch-tos-policy-failed = 获取服务条款和隐私政策内容失败
warn-deny-tos-policy = 需接受条款才可使用线上服务

open-in-web = 在网站中打开

Expand Down
9 changes: 0 additions & 9 deletions phira/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pub struct Client;

// const API_URL: &str = "http://localhost:2924";
const API_URL: &str = "https://api.phira.cn";
const WEB_URL: &str = "https://phira.moe";

pub fn basic_client_builder() -> ClientBuilder {
let mut builder = reqwest::ClientBuilder::new();
Expand Down Expand Up @@ -214,14 +213,6 @@ impl Client {
.await?;
Ok(resp.id)
}
pub async fn get_tos_and_pp(language_id: &str) -> Result<String> {
let resp = recv_raw(CLIENT.load().request(Method::GET, WEB_URL.to_owned() + "/tos_and_pp_plaintext_"+ language_id)).await?.text().await?;
Ok(resp)
}
pub async fn get_tos_and_pp_version() -> Result<String> {
let resp= recv_raw(CLIENT.load().request(Method::GET, WEB_URL.to_owned() + "/tos_policy_version")).await?.text().await?;
Ok(resp)
}
}

#[must_use]
Expand Down
8 changes: 0 additions & 8 deletions phira/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::{
ops::DerefMut,
path::Path,
};
use tracing::debug;

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -83,9 +82,7 @@ pub struct Data {
pub respacks: Vec<String>,
pub respack_id: usize,
pub accept_invalid_cert: bool,
// for compatibility
pub read_tos_and_policy: bool,
pub read_tos_and_policy_version: Option<String>,
pub ignored_version: Option<semver::Version>,
pub character: Option<Character>,
}
Expand Down Expand Up @@ -158,11 +155,6 @@ impl Data {
*res_pack_path = "chart.zip".to_owned();
}
}
if self.read_tos_and_policy {
debug!("migrating from old version");
self.read_tos_and_policy_version = Some("61c95573d1d4e9092fe64e18b4125af48ed73110c395c178543f1f305f5899bd".to_owned());
self.read_tos_and_policy = false;
}
self.config.init();
Ok(())
}
Expand Down
36 changes: 1 addition & 35 deletions phira/src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::{
client::{Client, LoginParams, User, UserManager},
get_data_mut,
page::Fader,
save_data,
scene::{check_read_tos_and_policy, JUST_ACCEPTED_TOS},
save_data, scene::check_read_tos_and_policy,
};
use anyhow::Result;
use macroquad::prelude::*;
Expand Down Expand Up @@ -62,12 +61,6 @@ pub struct Login {
in_reg: bool,

task: Option<(&'static str, Task<Result<Option<User>>>)>,
after_accept_tos: Option<NextAction>,
}

enum NextAction {
Login,
Register,
}

impl Login {
Expand Down Expand Up @@ -99,7 +92,6 @@ impl Login {
in_reg: false,

task: None,
after_accept_tos: None,
}
}

Expand Down Expand Up @@ -172,7 +164,6 @@ impl Login {
}
if self.btn_reg.touch(touch, t) {
if !check_read_tos_and_policy() {
self.after_accept_tos = Some(NextAction::Register);
return true;
}
if let Some(error) = self.register() {
Expand All @@ -182,7 +173,6 @@ impl Login {
}
if self.btn_login.touch(touch, t) {
if !check_read_tos_and_policy() {
self.after_accept_tos = Some(NextAction::Login);
return true;
}
let email = self.t_email.clone();
Expand Down Expand Up @@ -222,30 +212,6 @@ impl Login {
*tmp = text;
}
}
let just_accepted = *JUST_ACCEPTED_TOS.lock().unwrap();
if just_accepted {
match self.after_accept_tos {
Some(NextAction::Login) => {
let email = self.t_email.clone();
let pwd = self.t_pwd.clone();
self.start("login", async move {
Client::login(LoginParams::Password {
email: &email,
password: &pwd,
})
.await?;
Ok(Some(Client::get_me().await?))
});
}
Some(NextAction::Register) => {
if let Some(error) = self.register() {
show_message(error).error();
}
}
None => ()
}
self.after_accept_tos = None;
}
if let Some((action, task)) = &mut self.task {
if let Some(res) = task.take() {
match res {
Expand Down
3 changes: 1 addition & 2 deletions phira/src/page/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
icons::Icons,
login::Login,
save_data,
scene::{load_tos_and_policy, ProfileScene},
scene::ProfileScene,
sync_data,
threed::ThreeD,
};
Expand Down Expand Up @@ -355,7 +355,6 @@ impl Page for HomePage {
self.sf.enter(s.t);
self.need_back = false;
}
load_tos_and_policy();
self.fetch_has_new();
Ok(())
}
Expand Down
120 changes: 29 additions & 91 deletions phira/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,28 @@ mod song;
pub use song::{Downloading, SongScene, RECORD_ID};

mod unlock;
use tracing::{debug, warn};
pub use unlock::UnlockScene;

mod profile;
pub use profile::ProfileScene;

use crate::{
client::{Client, UserManager},
data::LocalChart,
dir, get_data, get_data_mut,
page::Fader,
save_data, ttl,
};
use crate::{client::UserManager, data::LocalChart, dir, get_data, get_data_mut, page::Fader, save_data};
use anyhow::{bail, Context, Result};
use async_trait::async_trait;
use once_cell::sync::{Lazy, OnceCell};
use once_cell::sync::Lazy;
use prpr::{
config::Mods,
core::{BOLD_FONT, PGR_FONT},
ext::{semi_white, unzip_into, RectExt, SafeTexture},
ext::{open_url, semi_white, unzip_into, RectExt, SafeTexture},
fs::{self, FileSystem},
info::ChartInfo,
scene::{show_error, show_message},
task::Task,
ui::{Dialog, RectButton, Scroll, Scroller, Ui},
};
use std::{
any::Any,
cell::RefCell,
fs::File,
io::{BufReader, Write},
ops::DerefMut,
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, Ordering},
Expand All @@ -62,10 +52,7 @@ thread_local! {
}

pub static ASSET_CHART_INFO: Lazy<Mutex<Option<ChartInfo>>> = Lazy::new(Mutex::default);
pub static TOS_AND_POLICY: OnceCell<String> = OnceCell::new();
pub static TOS_AND_POLICY_VERSION: OnceCell<String> = OnceCell::new();
pub static LOAD_TOS_TASK: Lazy<Mutex<Option<Task<Result<Option<(String, /* version */ String)>>>>>> = Lazy::new(Mutex::default);
pub static JUST_ACCEPTED_TOS: Lazy<Mutex<bool>> = Lazy::new(Mutex::default);

#[derive(Clone)]
pub struct AssetsChartFileSystem(pub String, pub String);

Expand Down Expand Up @@ -130,85 +117,36 @@ pub fn confirm_dialog(title: impl Into<String>, content: impl Into<String>, res:
}

pub fn check_read_tos_and_policy() -> bool {
if get_data().read_tos_and_policy_version.is_some() {
if get_data().read_tos_and_policy {
return true;
}
let binding = LOAD_TOS_TASK.lock();
let mut tos_task = binding.unwrap();
if let Some(task) = &mut *tos_task {
if let Some(result) = task.take() {
match result {
Ok(Some((string, ver))) => {
let _ = TOS_AND_POLICY.set(string);
let _ = TOS_AND_POLICY_VERSION.set(ver);
}
Ok(None) => {
warn!("should be unreachable");
}
Err(e) => {
show_error(e.context(ttl!("fetch-tos-policy-failed")));
*tos_task = None;
return false;
}

let mut opened = false;
Dialog::plain(ttl!("tos-and-policy"), ttl!("tos-and-policy-desc"))
.buttons(vec![ttl!("tos-deny").into_owned(), ttl!("tos-accept").into_owned()])
.listener(move |pos| match pos {
-2 => {
opened = true;
open_url("https://phira.moe/terms-of-use").unwrap();
true
}
*tos_task = None;
}
}
drop(tos_task);
if let (Some(tos_policy), Some(version)) = (TOS_AND_POLICY.get(), TOS_AND_POLICY_VERSION.get()) {
Dialog::plain(ttl!("tos-and-policy"), ttl!("tos-and-policy-desc") + "\n\n" + tos_policy.as_str())
.buttons(vec![ttl!("tos-deny").into_owned(), ttl!("tos-accept").into_owned()])
.listener(move |pos| match pos {
-2 | -1 => true,
0 => {
show_message(ttl!("warn-deny-tos-policy")).warn();
false
}
1 => {
get_data_mut().read_tos_and_policy_version = Some(version.clone());
let _ = save_data();
*JUST_ACCEPTED_TOS.lock().unwrap() = true;
false
-1 => true,
0 => false,
1 => {
if !opened {
opened = true;
open_url("https://phira.moe/terms-of-use").unwrap();
return true;
}
_ => unreachable!(),
})
.show();
} else {
warn!("loading data to read because `check_..` was called, this would result a delay and shouldn't happen");
load_tos_and_policy();
}
false
}

pub fn load_tos_and_policy() {
if TOS_AND_POLICY.get().is_some() || TOS_AND_POLICY_VERSION.get().is_some() {
return;
}
*LOAD_TOS_TASK.lock().unwrap() = Some(Task::new(async {
debug!("checking tos and policy update...");
let new_version = Client::get_tos_and_pp_version().await?;
if let Some(current) = &get_data().read_tos_and_policy_version {
if current != &new_version {
// updated, reset read
debug!("has update, new version {new_version}");
get_data_mut().read_tos_and_policy_version = None;
} else {
// no need to download, skipping
return Ok(None);
get_data_mut().read_tos_and_policy = true;
let _ = save_data();
false
}
}
debug!("loading tos and policy");
let mut lang = get_data().language.clone().unwrap_or("en-us".to_owned()).to_ascii_lowercase();
if lang == "zh-tw" {
"zh-cn".clone_into(&mut lang);
}
if lang != "zh-cn" {
"en-us".clone_into(&mut lang);
}
let res = Client::get_tos_and_pp(&lang).await?;
debug!("loaded");
Ok(Some((res, new_version)))
}));
_ => unreachable!(),
})
.show();

false
}

#[inline]
Expand Down