Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max 900 bytes #21

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 51 additions & 17 deletions zomes/trust_atom/src/trust_atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,37 @@ 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 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 = [
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(),
Expand All @@ -65,10 +83,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 @@ -77,8 +95,14 @@ 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(EntryTypes::Extra(entry.clone()))?;

Expand Down Expand Up @@ -124,10 +148,20 @@ 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 {
let mut max_content = content.clone();
max_content.truncate(898); // leave 2 bytes for `…`
max_content.push_str("…");
chunks.push(max_content);
} else {
chunks.push(content);
}
}

for i in 0..chunk_options.len() {
for i in 1..chunk_options.len() {
if let Some(chunk) = chunk_options[i].clone() {
chunks.push(chunk);
}
Expand Down Expand Up @@ -256,16 +290,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)],
)),
(None, Some(content_starts_with), None) => {
Some(create_link_tag(&link_direction, Some(content_starts_with)))
}
(None, None, Some(value_starts_with)) => Some(create_link_tag(
&link_direction,
&[None, Some(value_starts_with)],
Expand Down
Loading
Loading