Skip to content

Commit

Permalink
Update zomes/hc_zome_trust_atom/src/trust_atom.rs
Browse files Browse the repository at this point in the history
Co-authored-by: Harlan T Wood <[email protected]>
  • Loading branch information
dauphin3 and harlantwood committed Mar 1, 2022
1 parent 6c73464 commit d50eb15
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
43 changes: 28 additions & 15 deletions zomes/hc_zome_trust_atom/src/trust_atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const LINK_TAG_ARROW_REVERSE: [u8; 3] = [226, 134, 169]; // Unicode "↩" // hex
#[hdk_entry(id = "extra", visibility = "public")]
#[derive(Clone)]
pub struct Extra {
pub fields: BTreeMap<String, String>, // extra content
pub content_overflow: Option<String>,
pub extra_fields: Option<BTreeMap<String, String>>, // extra content
}

pub fn create(
Expand All @@ -45,19 +46,31 @@ pub fn create(

let bucket = create_bucket()?;

let extra_entry_hash_string = match extra.clone() {
Some(x) => Some(create_extra(x)?),
None => None,
let overflow: bool = match content.clone() {
Some(c) => if c.len() > 900 {true} else {false}
None => false
};

let chunks = [
let extra_entry_hash_string = match overflow {
true => match extra.clone() {
Some(x) => Some(create_extra(content, Some(x))?),
None => Some(create_extra(content, None)?)
}
false => match extra.clone() {
Some(x) => Some(create_extra(None, Some(x))?),
None => None
}
};

let chunks = vec![
content.clone(),
normalize_value(value.clone())?,
Some(bucket),
extra_entry_hash_string,
];
let forward_link_tag = create_link_tag(&LinkDirection::Forward, &chunks);
let reverse_link_tag = create_link_tag(&LinkDirection::Reverse, &chunks);

let forward_link_tag = create_link_tag(&LinkDirection::Forward, chunks);
let reverse_link_tag = create_link_tag(&LinkDirection::Reverse, chunks);

create_link(agent_address.clone(), target.clone(), forward_link_tag)?;
create_link(target.clone(), agent_address.clone(), reverse_link_tag)?;
Expand All @@ -78,10 +91,10 @@ pub fn create(

fn create_bucket() -> ExternResult<String> {
let bucket_bytes = random_bytes(9)?.into_vec();
Ok(create_bucket_string(&bucket_bytes))
Ok(create_bucket_string(bucket_bytes))
}

fn create_bucket_string(bucket_bytes: &[u8]) -> String {
fn create_bucket_string(bucket_bytes: Vec<u8>) -> String {
let mut bucket = String::new();
for chunk in bucket_bytes {
let val = chunk;
Expand All @@ -90,8 +103,8 @@ fn create_bucket_string(bucket_bytes: &[u8]) -> String {
bucket
}

fn create_extra(input: BTreeMap<String, String>) -> ExternResult<String> {
let entry = Extra { fields: input };
fn create_extra(content_overflow: Option<String>, extra_fields: Option<BTreeMap<String, String>>) -> ExternResult<String> {
let entry = Extra { content_overflow, extra_fields };

create_entry(entry.clone())?;

Expand Down Expand Up @@ -139,7 +152,7 @@ fn normalize_value(value_str: Option<String>) -> ExternResult<Option<String>> {
}
}

fn create_link_tag(link_direction: &LinkDirection, chunk_options: &[Option<String>]) -> LinkTag {
fn create_link_tag(link_direction: &LinkDirection, chunk_options: Vec<Option<String>>) -> LinkTag {
let mut chunks: Vec<String> = vec![];
if let Some(content) = chunk_options[0] {
if content.len() > 900 {
Expand Down Expand Up @@ -264,16 +277,16 @@ pub fn query(
}
(Some(content_full), None, Some(value_starts_with)) => Some(create_link_tag(
&link_direction,
&[Some(content_full), Some(value_starts_with)],
Some(content_full), Some(value_starts_with),
)),
(Some(content_full), None, None) => {
Some(create_link_tag_metal(&link_direction, vec![content_full, UNICODE_NUL_STR.to_string()]))
}
(None, Some(content_starts_with), None) => Some(create_link_tag(
&link_direction,
&[Some(content_starts_with)],
Some(content_starts_with),
)),
(None, None, Some(value_starts_with)) => Some(create_link_tag(&link_direction, &[Some(value_starts_with)])),
(None, None, Some(value_starts_with)) => Some(create_link_tag(&link_direction, Some(value_starts_with))),
(None, None, None) => None,
};
let links = get_links(link_base.clone(), link_tag)?;
Expand Down
7 changes: 4 additions & 3 deletions zomes/hc_zome_trust_atom/tests/trust_atom_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ pub async fn test_get_extra() {
.await;

let mock_entry = Extra {
fields: mock_input.extra.unwrap(),
content_overflow: None,
extra_fields: mock_input.extra
};
let mock_extra_entry_hash: EntryHash = conductor
.call(&cell1.zome("trust_atom"), "calc_extra_hash", mock_entry)
Expand All @@ -428,11 +429,11 @@ pub async fn test_get_extra() {
.await;

let field1 = mock_extra_data
.fields
.extra_fields
.get_key_value(&"extra_stuff".to_string())
.unwrap();
let field2 = mock_extra_data
.fields
.extra_fields
.get_key_value(&"another_thing".to_string())
.unwrap();

Expand Down

0 comments on commit d50eb15

Please sign in to comment.