diff options
Diffstat (limited to 'src-tauri/src')
-rw-r--r-- | src-tauri/src/main.rs | 4 | ||||
-rw-r--r-- | src-tauri/src/thunderstore/mod.rs | 78 |
2 files changed, 82 insertions, 0 deletions
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 63b30e9e..12fa477a 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -33,6 +33,9 @@ use mod_management::{ mod northstar; use northstar::get_northstar_version_number; +mod thunderstore; +use thunderstore::query_thunderstore_packages_api; + use tauri::Manager; use tauri_plugin_store::PluginBuilder; use tokio::time::sleep; @@ -111,6 +114,7 @@ fn main() { delete_northstar_mod, get_server_player_count, delete_thunderstore_mod, + query_thunderstore_packages_api, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/src/thunderstore/mod.rs b/src-tauri/src/thunderstore/mod.rs new file mode 100644 index 00000000..c07c27ff --- /dev/null +++ b/src-tauri/src/thunderstore/mod.rs @@ -0,0 +1,78 @@ +//! For interacting with Thunderstore API +use app::constants::APP_USER_AGENT; +use serde::{Deserialize, Serialize}; +use std::collections::HashSet; +use ts_rs::TS; + +use crate::mod_management::BLACKLISTED_MODS; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, TS)] +#[ts(export)] +pub struct ThunderstoreMod { + pub name: String, + pub full_name: String, + pub owner: String, + pub package_url: String, + pub date_created: String, + pub date_updated: String, + pub uuid4: String, + pub rating_score: i32, + pub is_pinned: bool, + pub is_deprecated: bool, + pub has_nsfw_content: bool, + pub categories: Vec<String>, + pub versions: Vec<ThunderstoreModVersion>, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, TS)] +#[ts(export)] +pub struct ThunderstoreModVersion { + pub name: String, + pub full_name: String, + pub description: String, + pub icon: String, + pub version_number: String, + pub dependencies: Vec<String>, + pub download_url: String, + pub downloads: i32, + pub date_created: String, + pub website_url: String, + pub is_active: bool, + pub uuid4: String, + pub file_size: i64, +} + +/// Queries Thunderstore packages API +#[tauri::command] +pub async fn query_thunderstore_packages_api() -> Result<Vec<ThunderstoreMod>, String> { + println!("Fetching Thunderstore API"); + + // Fetches + let url = "https://northstar.thunderstore.io/api/v1/package/"; + + let client = reqwest::Client::new(); + let res = client + .get(url) + .header(reqwest::header::USER_AGENT, APP_USER_AGENT) + .send() + .await + .unwrap() + .text() + .await + .unwrap(); + + // Parse response + let parsed_json: Vec<ThunderstoreMod> = match serde_json::from_str(&res) { + Ok(res) => res, + Err(err) => return Err(err.to_string()), + }; + + // Remove some mods from listing + let to_remove_set: HashSet<&str> = BLACKLISTED_MODS.iter().map(|s| s.as_ref()).collect(); + let filtered_packages = parsed_json + .into_iter() + .filter(|package| !to_remove_set.contains(&package.full_name.as_ref())) + .collect::<Vec<ThunderstoreMod>>(); + + Ok(filtered_packages) +} |