From 5aff9234236af5c9789eb27abdac2e16213cbd6a Mon Sep 17 00:00:00 2001 From: Rache Bartmoss Date: Mon, 9 Sep 2019 08:36:44 +0200 Subject: [PATCH] Reset profile to default deterministic label if empty label is set --- prometheus/src/http/client.rs | 2 +- prometheus/src/http/server/status.rs | 8 ++++---- prometheus/src/imp.rs | 29 +++++++++++++++++----------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/prometheus/src/http/client.rs b/prometheus/src/http/client.rs index babbf6d1..a8c56251 100644 --- a/prometheus/src/http/client.rs +++ b/prometheus/src/http/client.rs @@ -124,7 +124,7 @@ impl Api for ApiHttpClient { fn create_profile(&mut self, label: Option) -> Fallible { let url = format!("{}/vault/dids", self.root_url); - let req_fut = HttpClient::new().post(url).send_json(&label).from_err(); + let req_fut = HttpClient::new().post(url).send_json(&label.unwrap_or_default()).from_err(); let fut = req_fut.and_then(|mut response| { // TODO this probably ignores status code, so we should check it properly response.body().from_err().and_then(|body| { diff --git a/prometheus/src/http/server/status.rs b/prometheus/src/http/server/status.rs index 16179e09..de024114 100644 --- a/prometheus/src/http/server/status.rs +++ b/prometheus/src/http/server/status.rs @@ -176,7 +176,7 @@ pub fn publish( pub fn create_did( state: web::Data>, - label: web::Json>, + label: web::Json, ) -> impl Responder { let mut state = match lock_state(&state) { Err(e) => return HttpResponse::Conflict().body(e.to_string()), @@ -215,15 +215,15 @@ pub fn rename_did( state: web::Data>, did: web::Path, //did: web::Path, - name: web::Json, + label: web::Json, ) -> impl Responder { let mut state = match lock_state(&state) { Err(e) => return HttpResponse::Conflict().body(e.to_string()), Ok(state) => state, }; - match rename_did_impl(&mut state, did.clone(), name.clone()) { + match rename_did_impl(&mut state, did.clone(), label.clone()) { Ok(()) => { - debug!("Renamed profile {} to {}", did, name); + debug!("Renamed profile {} to {}", did, label); HttpResponse::Ok().body("") } Err(e) => { diff --git a/prometheus/src/imp.rs b/prometheus/src/imp.rs index 01fe320c..3705666c 100644 --- a/prometheus/src/imp.rs +++ b/prometheus/src/imp.rs @@ -47,15 +47,21 @@ pub fn list_dids_impl(state: &Context) -> Fallible> { Ok(entries) } -pub fn create_dids_impl(state: &mut Context, mut label: Option) -> Fallible { - let did = state.create_profile(label.clone())?; - let did_bytes = did.to_bytes(); - - if label.is_none() { - let hd_label = DeterministicNameGenerator::default().name(&did_bytes); +fn reset_label_if_empty(state: &mut Context, label: &mut String, did: &ProfileId) -> Fallible<()> { + if label.is_empty() || label.find(|c| !char::is_whitespace(c)).is_none() { + let hd_label = DeterministicNameGenerator::default().name(&did.to_bytes()); state.set_profile_label(Some(did.clone()), hd_label.clone())?; - label = Some(hd_label); + *label = hd_label; } + Ok(()) +} + +pub fn create_dids_impl(state: &mut Context, mut label: String) -> Fallible { + debug!("Creating profile with label '{}'", label); + let did = state.create_profile(Some(label.clone()))?; + let did_bytes = did.to_bytes(); + + reset_label_if_empty(state, &mut label, &did)?; let mut avatar_png = Vec::new(); blockies::Ethereum::default() @@ -71,7 +77,7 @@ pub fn create_dids_impl(state: &mut Context, mut label: Option) -> Falli state.save_vault()?; Ok(VaultEntry { id: did.to_string(), - label: label.unwrap_or_default(), + label, avatar: Image { format: metadata.image_format, blob: metadata.image_blob }, state: "TODO".to_owned(), }) @@ -119,10 +125,11 @@ pub fn rename_did_impl( state: &mut Context, did_str: String, //did: ProfileId, - name: ProfileLabel, + mut label: ProfileLabel, ) -> Fallible<()> { - let did = did_opt(did_str)?; - state.set_profile_label(did, name)?; + let did = did_str.parse()?; + reset_label_if_empty(state, &mut label, &did)?; + state.set_profile_label(Some(did), label)?; state.save_vault()?; Ok(()) }