Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
Reset profile to default deterministic label if empty label is set
Browse files Browse the repository at this point in the history
  • Loading branch information
izolyomi committed Sep 9, 2019
1 parent 6dd7e5b commit 5aff923
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion prometheus/src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Api for ApiHttpClient {

fn create_profile(&mut self, label: Option<ProfileLabel>) -> Fallible<ProfileId> {
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| {
Expand Down
8 changes: 4 additions & 4 deletions prometheus/src/http/server/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub fn publish(

pub fn create_did(
state: web::Data<Mutex<Context>>,
label: web::Json<Option<ProfileLabel>>,
label: web::Json<ProfileLabel>,
) -> impl Responder {
let mut state = match lock_state(&state) {
Err(e) => return HttpResponse::Conflict().body(e.to_string()),
Expand Down Expand Up @@ -215,15 +215,15 @@ pub fn rename_did(
state: web::Data<Mutex<Context>>,
did: web::Path<String>,
//did: web::Path<ProfileId>,
name: web::Json<ProfileLabel>,
label: web::Json<ProfileLabel>,
) -> 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) => {
Expand Down
29 changes: 18 additions & 11 deletions prometheus/src/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ pub fn list_dids_impl(state: &Context) -> Fallible<Vec<VaultEntry>> {
Ok(entries)
}

pub fn create_dids_impl(state: &mut Context, mut label: Option<String>) -> Fallible<VaultEntry> {
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<VaultEntry> {
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()
Expand All @@ -71,7 +77,7 @@ pub fn create_dids_impl(state: &mut Context, mut label: Option<String>) -> 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(),
})
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit 5aff923

Please sign in to comment.