Fix compilation without online feature

This commit is contained in:
NGnius (Graham) 2024-02-24 22:52:13 -05:00
parent a5e4ce29a6
commit 479cbf22aa
2 changed files with 47 additions and 1 deletions

View file

@ -1,5 +1,7 @@
use std::sync::mpsc::{self, Sender}; use std::sync::mpsc::{self, Sender};
use std::sync::{Arc, Mutex, RwLock}; #[cfg(feature = "online")]
use std::sync::RwLock;
use std::sync::{Arc, Mutex};
use usdpl_back::core::serdes::Primitive; use usdpl_back::core::serdes::Primitive;
use usdpl_back::AsyncCallable; use usdpl_back::AsyncCallable;
@ -8,7 +10,9 @@ use serde::{Deserialize, Serialize};
use super::handler::{ApiMessage, GeneralMessage}; use super::handler::{ApiMessage, GeneralMessage};
#[cfg(feature = "online")]
const BASE_URL_FALLBACK: &'static str = "https://powertools.ngni.us"; const BASE_URL_FALLBACK: &'static str = "https://powertools.ngni.us";
#[cfg(feature = "online")]
static BASE_URL: RwLock<Option<String>> = RwLock::new(None); static BASE_URL: RwLock<Option<String>> = RwLock::new(None);
const MAX_CACHE_DURATION: std::time::Duration = const MAX_CACHE_DURATION: std::time::Duration =
@ -23,12 +27,14 @@ struct CachedData<T> {
type StoreCache = type StoreCache =
std::collections::HashMap<u32, CachedData<Vec<community_settings_core::v1::Metadata>>>; std::collections::HashMap<u32, CachedData<Vec<community_settings_core::v1::Metadata>>>;
#[cfg(feature = "online")]
pub fn set_base_url(base_url: String) { pub fn set_base_url(base_url: String) {
*BASE_URL *BASE_URL
.write() .write()
.expect("Failed to acquire write lock for store base url") = Some(base_url); .expect("Failed to acquire write lock for store base url") = Some(base_url);
} }
#[cfg(feature = "online")]
fn get_base_url() -> String { fn get_base_url() -> String {
BASE_URL BASE_URL
.read() .read()
@ -37,22 +43,27 @@ fn get_base_url() -> String {
.unwrap_or_else(|| BASE_URL_FALLBACK.to_owned()) .unwrap_or_else(|| BASE_URL_FALLBACK.to_owned())
} }
#[cfg(feature = "online")]
fn url_search_by_app_id(steam_app_id: u32) -> String { fn url_search_by_app_id(steam_app_id: u32) -> String {
format!("{}/api/setting/by_app_id/{}", get_base_url(), steam_app_id) format!("{}/api/setting/by_app_id/{}", get_base_url(), steam_app_id)
} }
#[cfg(feature = "online")]
fn url_download_config_by_id(id: u128) -> String { fn url_download_config_by_id(id: u128) -> String {
format!("{}/api/setting/by_id/{}", get_base_url(), id) format!("{}/api/setting/by_id/{}", get_base_url(), id)
} }
#[cfg(feature = "online")]
fn url_upload_config() -> String { fn url_upload_config() -> String {
format!("{}/api/setting", get_base_url()) format!("{}/api/setting", get_base_url())
} }
#[cfg(feature = "online")]
fn cache_path() -> std::path::PathBuf { fn cache_path() -> std::path::PathBuf {
crate::utility::settings_dir().join(crate::consts::WEB_SETTINGS_CACHE) crate::utility::settings_dir().join(crate::consts::WEB_SETTINGS_CACHE)
} }
#[cfg(feature = "online")]
fn load_cache() -> StoreCache { fn load_cache() -> StoreCache {
let path = cache_path(); let path = cache_path();
let file = match std::fs::File::open(&path) { let file = match std::fs::File::open(&path) {
@ -72,6 +83,12 @@ fn load_cache() -> StoreCache {
} }
} }
#[cfg(not(feature = "online"))]
fn load_cache() -> StoreCache {
StoreCache::default()
}
#[cfg(feature = "online")]
fn save_cache(cache: &StoreCache) { fn save_cache(cache: &StoreCache) {
let path = cache_path(); let path = cache_path();
let file = match std::fs::File::create(&path) { let file = match std::fs::File::create(&path) {
@ -89,6 +106,9 @@ fn save_cache(cache: &StoreCache) {
} }
} }
#[cfg(not(feature = "online"))]
fn save_cache(_cache: &StoreCache) {}
fn get_maybe_cached(steam_app_id: u32) -> Vec<community_settings_core::v1::Metadata> { fn get_maybe_cached(steam_app_id: u32) -> Vec<community_settings_core::v1::Metadata> {
let mut cache = load_cache(); let mut cache = load_cache();
let data = if let Some(cached_result) = cache.get(&steam_app_id) { let data = if let Some(cached_result) = cache.get(&steam_app_id) {
@ -137,6 +157,7 @@ fn get_maybe_cached(steam_app_id: u32) -> Vec<community_settings_core::v1::Metad
} }
} }
#[cfg(feature = "online")]
fn search_by_app_id_online( fn search_by_app_id_online(
steam_app_id: u32, steam_app_id: u32,
) -> std::io::Result<Vec<community_settings_core::v1::Metadata>> { ) -> std::io::Result<Vec<community_settings_core::v1::Metadata>> {
@ -163,6 +184,13 @@ fn search_by_app_id_online(
} }
} }
#[cfg(not(feature = "online"))]
fn search_by_app_id_online(
_steam_app_id: u32,
) -> std::io::Result<Vec<community_settings_core::v1::Metadata>> {
Ok(Vec::with_capacity(0))
}
/// Get search results web method /// Get search results web method
pub fn search_by_app_id() -> impl AsyncCallable { pub fn search_by_app_id() -> impl AsyncCallable {
let getter = move || { let getter = move || {
@ -248,6 +276,7 @@ fn web_config_to_settings_json(
} }
} }
#[cfg(feature = "online")]
fn download_config(id: u128) -> std::io::Result<community_settings_core::v1::Metadata> { fn download_config(id: u128) -> std::io::Result<community_settings_core::v1::Metadata> {
let req_url = url_download_config_by_id(id); let req_url = url_download_config_by_id(id);
let response = ureq::get(&req_url).call().map_err(|e| { let response = ureq::get(&req_url).call().map_err(|e| {
@ -257,6 +286,14 @@ fn download_config(id: u128) -> std::io::Result<community_settings_core::v1::Met
response.into_json() response.into_json()
} }
#[cfg(not(feature = "online"))]
fn download_config(_id: u128) -> std::io::Result<community_settings_core::v1::Metadata> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"Online functionality not included in this build",
))
}
pub fn upload_settings( pub fn upload_settings(
id: u64, id: u64,
user_id: String, user_id: String,
@ -345,6 +382,7 @@ fn settings_to_web_config(
} }
} }
#[cfg(feature = "online")]
fn upload_config(config: community_settings_core::v1::Metadata) -> std::io::Result<()> { fn upload_config(config: community_settings_core::v1::Metadata) -> std::io::Result<()> {
let req_url = url_upload_config(); let req_url = url_upload_config();
ureq::post(&req_url) ureq::post(&req_url)
@ -356,6 +394,11 @@ fn upload_config(config: community_settings_core::v1::Metadata) -> std::io::Resu
.map(|_| ()) .map(|_| ())
} }
#[cfg(not(feature = "online"))]
fn upload_config(_config: community_settings_core::v1::Metadata) -> std::io::Result<()> {
Ok(())
}
/// Download config web method /// Download config web method
pub fn download_new_config(sender: Sender<ApiMessage>) -> impl AsyncCallable { pub fn download_new_config(sender: Sender<ApiMessage>) -> impl AsyncCallable {
let sender = Arc::new(Mutex::new(sender)); // Sender is not Sync; this is required for safety let sender = Arc::new(Mutex::new(sender)); // Sender is not Sync; this is required for safety

View file

@ -8,10 +8,13 @@ pub const DEFAULT_SETTINGS_NAME: &str = "Main";
pub const DEFAULT_SETTINGS_VARIANT_NAME: &str = "Primary"; pub const DEFAULT_SETTINGS_VARIANT_NAME: &str = "Primary";
pub const LIMITS_FILE: &str = "limits_cache.ron"; pub const LIMITS_FILE: &str = "limits_cache.ron";
#[cfg(feature = "online")]
pub const LIMITS_REFRESH_PERIOD: std::time::Duration = std::time::Duration::from_secs(60 * 60 * 24); // 1 day pub const LIMITS_REFRESH_PERIOD: std::time::Duration = std::time::Duration::from_secs(60 * 60 * 24); // 1 day
#[cfg(feature = "online")]
pub const LIMITS_STARTUP_WAIT: std::time::Duration = std::time::Duration::from_secs(60); // 1 minute pub const LIMITS_STARTUP_WAIT: std::time::Duration = std::time::Duration::from_secs(60); // 1 minute
pub const LIMITS_OVERRIDE_FILE: &str = "limits_override.ron"; pub const LIMITS_OVERRIDE_FILE: &str = "limits_override.ron";
#[cfg(feature = "online")]
pub const WEB_SETTINGS_CACHE: &str = "store_cache.ron"; pub const WEB_SETTINGS_CACHE: &str = "store_cache.ron";
pub const MESSAGE_SEEN_ID_FILE: &str = "seen_message.bin"; pub const MESSAGE_SEEN_ID_FILE: &str = "seen_message.bin";