From 35dab8ed9d435b6fc22c3838a0fcb03c57f3a169 Mon Sep 17 00:00:00 2001 From: Will Galebach Date: Tue, 30 Jan 2024 12:13:12 +0000 Subject: [PATCH] Updated to new FE types --- modules/app_store/app_store/src/http_api.rs | 11 +-- modules/app_store/app_store/src/lib.rs | 2 +- modules/app_store/app_store/src/types.rs | 84 ++++++++++++++++++++- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/modules/app_store/app_store/src/http_api.rs b/modules/app_store/app_store/src/http_api.rs index e3a5653b..399bb7b7 100644 --- a/modules/app_store/app_store/src/http_api.rs +++ b/modules/app_store/app_store/src/http_api.rs @@ -65,10 +65,7 @@ fn serve_paths( StatusCode::OK, None, serde_json::to_vec( - &state - .downloaded_packages - .iter() - .collect::>(), + &state.get_downloaded_packages_info() )?, )); } @@ -84,7 +81,7 @@ fn serve_paths( return Ok(( StatusCode::OK, None, - serde_json::to_vec(&state.listed_packages)?, + serde_json::to_vec(&state.get_listed_packages_info())?, )); } // GET detail about a specific downloaded app @@ -96,7 +93,7 @@ fn serve_paths( return Err(anyhow::anyhow!("No app ID")); }; match method { - Method::GET => Ok(match state.get_downloaded_package(&package_id) { + Method::GET => Ok(match state.get_package_info(&package_id) { Some(pkg) => (StatusCode::OK, None, serde_json::to_vec(&pkg)?), None => ( StatusCode::NOT_FOUND, @@ -142,7 +139,7 @@ fn serve_paths( return Err(anyhow::anyhow!("No app ID")); }; match method { - Method::GET => Ok(match state.get_listing(&package_id) { + Method::GET => Ok(match state.get_package_info(&package_id) { Some(pkg) => (StatusCode::OK, None, serde_json::to_vec(&pkg)?), None => ( StatusCode::NOT_FOUND, diff --git a/modules/app_store/app_store/src/lib.rs b/modules/app_store/app_store/src/lib.rs index 5438ae0a..a02ed86e 100644 --- a/modules/app_store/app_store/src/lib.rs +++ b/modules/app_store/app_store/src/lib.rs @@ -383,7 +383,7 @@ fn handle_receive_download( )); }; if let Some(metadata) = &package_listing.metadata { - if let Some(latest_hash) = metadata.versions.first() { + if let Some(latest_hash) = metadata.versions.clone().unwrap_or(vec![]).first() { if &download_hash != latest_hash { return Err(anyhow::anyhow!( "app store: downloaded package is not latest version--rejecting download!" diff --git a/modules/app_store/app_store/src/types.rs b/modules/app_store/app_store/src/types.rs index 186c75f9..b200b5d4 100644 --- a/modules/app_store/app_store/src/types.rs +++ b/modules/app_store/app_store/src/types.rs @@ -63,9 +63,51 @@ pub struct OnchainPackageMetadata { pub version: Option, pub license: Option, pub website: Option, - pub screenshots: Vec, - pub mirrors: Vec, - pub versions: Vec, + pub screenshots: Option>, + pub mirrors: Option>, + pub versions: Option>, +} + +/// package information sent to UI +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PackageInfo { + pub owner: Option, // eth address + pub package: String, + pub publisher: NodeId, + pub metadata_hash: Option, + + pub metadata: Option, + pub state: Option, +} + +pub fn gen_package_info(id: &PackageId, metadata_hash: Option, listing: Option<&PackageListing>, state: Option<&PackageState>) -> PackageInfo { + let owner = match listing { + Some(listing) => Some(listing.owner.clone()), + None => None, + }; + let metadata = match listing { + Some(listing) => listing.metadata.clone(), + None => match state { + Some(state) => state.metadata.clone(), + None => None, + } + }; + + let state = state.cloned().map(|state| { + let mut state = state; + state.metadata = None; + state + }); + + PackageInfo { + owner, + package: id.package().to_string(), + publisher: id.publisher().to_string(), + metadata_hash, + metadata, + state, + } } #[derive(Debug, Serialize, Deserialize)] @@ -79,6 +121,7 @@ pub struct RequestedPackage { /// state of an individual package we have downloaded #[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct PackageState { /// the node we last downloaded the package from /// this is "us" if we don't know the source (usually cause it's a local install) @@ -142,6 +185,41 @@ impl State { .get(self.package_hashes.get(package_id)?) } + pub fn get_package_info(&self, package_id: &PackageId) -> Option { + let hash = self.package_hashes.get(package_id)?; + + let listing = self.listed_packages.get(hash); + + let state = self.downloaded_packages.get(package_id); + + Some(gen_package_info(package_id, Some(hash.to_string()), listing, state)) + } + + pub fn get_downloaded_packages_info(&self) -> Vec { + self.downloaded_packages + .iter() + .map(|(package_id, state)| { + let hash = self.package_hashes.get(package_id); + let listing = match hash { + Some(hash) => self.listed_packages.get(hash), + None => None, + }; + gen_package_info(package_id, hash.cloned(), listing, Some(state)) + }) + .collect() + } + + pub fn get_listed_packages_info(&self) -> Vec { + self.listed_packages + .iter() + .map(|(hash, listing)| { + let package_id = PackageId::new(&listing.name, &listing.publisher); + let state = self.downloaded_packages.get(&package_id); + gen_package_info(&package_id, Some(hash.to_string()), Some(listing), state) + }) + .collect() + } + fn get_listing_with_hash_mut( &mut self, package_hash: &PackageHash,