Skip to content

Commit

Permalink
fix handling of published URI
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Mar 3, 2024
1 parent 63093e4 commit 2840850
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 51 deletions.
10 changes: 0 additions & 10 deletions js/src/nanopub.rs
Expand Up @@ -69,12 +69,6 @@ impl Nanopub {
} else {
None
};
let server_url = if let Some(server_url) = server_url {
Some(server_url)
} else {
None
};
// TODO: make shorter
future_to_promise(async move {
match self
.np
Expand Down Expand Up @@ -127,10 +121,6 @@ impl Nanopub {
serde_wasm_bindgen::to_value(&self.np.info).map_err(|e| e.into())
}

pub fn published(&self) -> Result<bool, JsValue> {
Ok(self.np.info.published)
}

#[wasm_bindgen(js_name = toString)]
pub fn to_string(&self) -> String {
self.np.to_string()
Expand Down
12 changes: 11 additions & 1 deletion lib/docs/contributing.md
Expand Up @@ -145,7 +145,7 @@ Build the npm package:
./scripts/run-js.py
```

Start a web server:
Start a web server to access the dev webpage:

```bash
python -m http.server 3000 --directory ./js
Expand All @@ -171,6 +171,16 @@ cargo clippy --all --all-targets --all-features
./scripts/docs-serve.sh
```

### 🎭️ Work on the demo webpage

Start a web server:

```bash
python -m http.server 3000 --directory ./lib/docs
```

Access http://localhost:3000/demo.html

### 📦️ Build and run

All packages at once:
Expand Down
6 changes: 3 additions & 3 deletions lib/docs/demo.html
Expand Up @@ -192,9 +192,9 @@ <h1 class="text-xl font-semibold">✍️ Nanopublication signing playground 🕹
oeditor.setValue(np.get_rdf());
console.log("Published Nanopub:", np.info());
const npUrlElem = document.getElementById('np-url');
const npUrl = (serverUrl) ? np.info().uri : `https://np.test.knowledgepixels.com/${np.info().trusty_hash}`
npUrlElem.textContent = npUrl;
npUrlElem.href = npUrl;
// const npUrl = (serverUrl) ? np.info().uri : `https://np.test.knowledgepixels.com/${np.info().trusty_hash}`
npUrlElem.textContent = np.info().published;
npUrlElem.href = np.info().published;
document.getElementById('success-msg').classList.remove('hidden');

} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/extract.rs
Expand Up @@ -32,7 +32,7 @@ pub struct NpInfo {
pub algo: String,
pub public_key: String,
pub orcid: String,
pub published: bool,
pub published: Option<String>,
}

impl fmt::Display for NpInfo {
Expand Down Expand Up @@ -262,7 +262,7 @@ pub fn extract_np_info(dataset: &LightDataset) -> Result<NpInfo, NpError> {
public_key: pubkey.unwrap_or("".to_string()),
algo: algo.unwrap_or("".to_string()),
orcid: orcid.unwrap_or("".to_string()),
published: false,
published: None,
})
}

Expand Down
38 changes: 13 additions & 25 deletions lib/src/nanopub.rs
Expand Up @@ -54,7 +54,9 @@ impl fmt::Display for Nanopub {
writeln!(f, "Trusty hash: {}", self.info.trusty_hash)?;
writeln!(f, "Signature hash: {}", self.info.signature)?;
writeln!(f, "Public key: {}", self.info.public_key)?;
writeln!(f, "Published: {}", self.info.published)?;
if let Some(published) = &self.info.published {
writeln!(f, "Published: {:?}", published)?;
}
Ok(())
}
}
Expand Down Expand Up @@ -93,7 +95,7 @@ impl Nanopub {
let np_rdf = fetch_np(url).await?;
let dataset: LightDataset = parse_rdf(&np_rdf)?;
let mut np_info = extract_np_info(&dataset)?;
np_info.published = true;
np_info.published = Some(url.to_string());
Ok(Self {
info: np_info,
dataset,
Expand Down Expand Up @@ -359,7 +361,9 @@ impl Nanopub {
server_url: Option<&str>,
) -> Result<Self, NpError> {
self = if let Some(profile) = profile {
println!("Nanopub not signed, signing it before publishing");
println!("Profile provided, signing Nanopub before publishing");
// TODO: handle when the user provide a Profile and a signed Nanopub.
// We should re-sign the Nanopub with the new profile
self.sign(profile)?
} else if self.info.signature.is_empty() {
return Err(NpError(format!(
Expand All @@ -370,45 +374,34 @@ impl Nanopub {
// If the nanopub is already signed we verify it, then publish it
self.check()?
};

// Use test server if None provided
let server_url = if let Some(server_url) = server_url {
if server_url.is_empty() {
TEST_SERVER.to_string()
} else {
server_url.to_string()
}
} else {
// Use test server if None provided
println!(
"No server URL provided, using the test server {}",
TEST_SERVER
);
TEST_SERVER.to_string()
};
// let server_url = server_url.unwrap_or_else(|| {
// println!("No server URL provided, using the test server {}", TEST_SERVER);
// TEST_SERVER
// }).to_string();
let published = publish_np(&server_url, &self.get_rdf()?).await?;
if published {
if TEST_SERVER == server_url {
self.info.uri =
Iri::new_unchecked(format!("{}/{}", server_url, self.info.trusty_hash));
self.info.published = Some(format!("{}{}", server_url, self.info.trusty_hash));
} else {
self.info.published = Some(self.info.uri.to_string());
}
println!(
"\n🎉 Nanopublication published at {}{}{}",
BOLD, self.info.uri, END
"\n🎉 Nanopublication published at {}{:?}{}",
BOLD, self.info.published, END
);
} else {
println!("\n❌ Issue publishing the Nanopublication \n{}", self);
// TODO: when publish fails, should we return a Nanopub struct with published=false, or throw an error?
return Err(NpError(format!(
"Issue publishing the Nanopublication \n{}",
self
)));
}
// self.set_published(published);
self.info.published = true;
Ok(self)
}

Expand Down Expand Up @@ -561,11 +554,6 @@ impl Nanopub {
pub fn get_rdf(&self) -> Result<String, NpError> {
serialize_rdf(&self.dataset, self.info.uri.as_str(), self.info.ns.as_str())
}

/// Sets if the nanopub has been published to the network
pub fn set_published(&mut self, value: bool) {
self.info.published = value;
}
}

/// Bootstrap a base nanopub dataset that can be edited later
Expand Down
12 changes: 6 additions & 6 deletions lib/tests/nanopub_test.rs
Expand Up @@ -22,7 +22,7 @@ async fn publish_nanopub_simple_rsa() -> Result<(), Box<dyn Error>> {
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await?;
println!("{}", np.get_rdf()?);
assert!(np.info.published);
assert!(np.info.published.is_some());
// Values compiled with the nanopub java lib using the exact same RDF
assert_eq!(
np.info.trusty_hash,
Expand All @@ -38,7 +38,7 @@ async fn publish_proteinatlas() -> Result<(), Box<dyn Error>> {
// let np_rdf = fs::read_to_string("./tests/resources/nanopub_test_blank.trig")?;
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await?;
assert!(np.info.published);
assert!(np.info.published.is_some());
Ok(())
}

Expand All @@ -55,7 +55,7 @@ fn sign_nanopub_blank() -> Result<(), Box<dyn Error>> {
println!("{}", profile); // cov
let _pubkey = profile.get_public_key(); // cov
let np = Nanopub::new(&np_rdf)?.sign(&profile)?;
assert!(!np.info.published);
assert!(np.info.published.is_none());
Ok(())
}

Expand Down Expand Up @@ -117,7 +117,7 @@ async fn publish_jsonld() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/resources/nanopub.jsonld")?;
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await?;
assert!(np.info.published);
assert!(np.info.published.is_some());
Ok(())
}

Expand All @@ -133,7 +133,7 @@ async fn publish_np_intro() -> Result<(), Box<dyn Error>> {
.publish(Some(&profile), None)
.await?;
// println!("{}", np);
assert!(np.info.published);
assert!(np.info.published.is_some());
Ok(())
}

Expand Down Expand Up @@ -175,7 +175,7 @@ fn test_get_ns_empty() -> Result<(), Box<dyn Error>> {
async fn fetch_nanopub() -> Result<(), Box<dyn Error>> {
let np_url = "https://w3id.org/np/RAltRkGOtHoj5LcBJZ62AMVOAVc0hnxt45LMaCXgxJ4fw";
let np = Nanopub::fetch(np_url).await?;
assert!(np.info.published);
assert!(np.info.published.is_some());
println!("{}", np);
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions lib/tests/testsuite_test.rs
Expand Up @@ -18,7 +18,7 @@ async fn testsuite_publish_valid_plain() -> Result<(), Box<dyn Error>> {
let np = Nanopub::new(&np_rdf)?
.publish(Some(&get_profile()), None)
.await?;
assert!(np.info.published);
assert!(np.info.published.is_some());
}
}
Ok(())
Expand Down Expand Up @@ -146,7 +146,7 @@ async fn testsuite_publish_transform_signed_simple1() -> Result<(), Box<dyn Erro
let np = Nanopub::new(&np_rdf)?
.publish(Some(&get_profile()), None)
.await?;
assert!(np.info.published);
assert!(np.info.published.is_some());
assert_eq!(
np.info.trusty_hash,
"RALbDbWVnLmLqpNgOsI_AaYfLbEnlOfZy3CoRRLs9XqVk"
Expand All @@ -164,7 +164,7 @@ async fn testsuite_publish_transform_trusty_aida() -> Result<(), Box<dyn Error>>
.publish(Some(&get_profile()), None)
.await?;
// println!("{}", np);
assert!(np.info.published);
assert!(np.info.published.is_some());
// assert_eq!(np.trusty_hash, "RAPpJU5UOB4pavfWyk7FE3WQiam5yBpmIlviAQWtBSC4M");
Ok(())
}
Expand All @@ -175,7 +175,7 @@ async fn testsuite_publish_transform_trusty_simple1() -> Result<(), Box<dyn Erro
let np = Nanopub::new(&np_rdf)?
.publish(Some(&get_profile()), None)
.await?;
assert!(np.info.published);
assert!(np.info.published.is_some());
// assert_eq!(np.trusty_hash, "RAtAU6U_xKTH016Eoiu11SswQkBu1elB_3_BoDJWH3arA");
Ok(())
}

0 comments on commit 2840850

Please sign in to comment.