Updated to new FE types

This commit is contained in:
Will Galebach 2024-01-30 12:13:12 +00:00
parent 4bc22973d0
commit 35dab8ed9d
3 changed files with 86 additions and 11 deletions

View File

@ -65,10 +65,7 @@ fn serve_paths(
StatusCode::OK, StatusCode::OK,
None, None,
serde_json::to_vec( serde_json::to_vec(
&state &state.get_downloaded_packages_info()
.downloaded_packages
.iter()
.collect::<Vec<(&PackageId, &PackageState)>>(),
)?, )?,
)); ));
} }
@ -84,7 +81,7 @@ fn serve_paths(
return Ok(( return Ok((
StatusCode::OK, StatusCode::OK,
None, None,
serde_json::to_vec(&state.listed_packages)?, serde_json::to_vec(&state.get_listed_packages_info())?,
)); ));
} }
// GET detail about a specific downloaded app // GET detail about a specific downloaded app
@ -96,7 +93,7 @@ fn serve_paths(
return Err(anyhow::anyhow!("No app ID")); return Err(anyhow::anyhow!("No app ID"));
}; };
match method { 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)?), Some(pkg) => (StatusCode::OK, None, serde_json::to_vec(&pkg)?),
None => ( None => (
StatusCode::NOT_FOUND, StatusCode::NOT_FOUND,
@ -142,7 +139,7 @@ fn serve_paths(
return Err(anyhow::anyhow!("No app ID")); return Err(anyhow::anyhow!("No app ID"));
}; };
match method { 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)?), Some(pkg) => (StatusCode::OK, None, serde_json::to_vec(&pkg)?),
None => ( None => (
StatusCode::NOT_FOUND, StatusCode::NOT_FOUND,

View File

@ -383,7 +383,7 @@ fn handle_receive_download(
)); ));
}; };
if let Some(metadata) = &package_listing.metadata { 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 { if &download_hash != latest_hash {
return Err(anyhow::anyhow!( return Err(anyhow::anyhow!(
"app store: downloaded package is not latest version--rejecting download!" "app store: downloaded package is not latest version--rejecting download!"

View File

@ -63,9 +63,51 @@ pub struct OnchainPackageMetadata {
pub version: Option<String>, pub version: Option<String>,
pub license: Option<String>, pub license: Option<String>,
pub website: Option<String>, pub website: Option<String>,
pub screenshots: Vec<String>, pub screenshots: Option<Vec<String>>,
pub mirrors: Vec<NodeId>, pub mirrors: Option<Vec<NodeId>>,
pub versions: Vec<String>, pub versions: Option<Vec<String>>,
}
/// package information sent to UI
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PackageInfo {
pub owner: Option<String>, // eth address
pub package: String,
pub publisher: NodeId,
pub metadata_hash: Option<String>,
pub metadata: Option<OnchainPackageMetadata>,
pub state: Option<PackageState>,
}
pub fn gen_package_info(id: &PackageId, metadata_hash: Option<String>, 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)] #[derive(Debug, Serialize, Deserialize)]
@ -79,6 +121,7 @@ pub struct RequestedPackage {
/// state of an individual package we have downloaded /// state of an individual package we have downloaded
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PackageState { pub struct PackageState {
/// the node we last downloaded the package from /// 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) /// 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)?) .get(self.package_hashes.get(package_id)?)
} }
pub fn get_package_info(&self, package_id: &PackageId) -> Option<PackageInfo> {
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<PackageInfo> {
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<PackageInfo> {
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( fn get_listing_with_hash_mut(
&mut self, &mut self,
package_hash: &PackageHash, package_hash: &PackageHash,