pub fn query_expressions(perspective: String, attributes: Vec<String>, query_options: QueryOptions, target_type: QueryTarget, 
                        query_type: QueryType, dos: u32, seed: String, resonations: bool) -> ZomeApiResult<JsonString> {
    let index_strings = attributes_to_index_string(attributes)?;
    hdk::debug(format!("Getting expressions with generated query string(s): {:?}", index_strings))?;
    match perspective.as_ref() {
        "random" => {
            let seed = HashString::encode_from_str(&seed, Hash::SHA2256);
            hdk::debug(format!("Seed addresss: {}", seed.to_string()))?;
            let current_bit_prefix = hdk::call(hdk::THIS_INSTANCE, "config", Address::from(hdk::PUBLIC_TOKEN.to_string()), 
                                                        "get_current_bit_prefix", JsonString::from(json!({})))?;
            let current_bit_prefix: ZomeApiResult<u32> = current_bit_prefix.try_into()?;
            let current_bit_prefix = current_bit_prefix?;
            let bit_prefix_bucket_id = utils::helpers::hash_prefix(Address::from(seed), current_bit_prefix); //get and id for bucket to make query from using seed passed into function
            hdk::debug(format!("Making random query with bit prefix: {}", bit_prefix_bucket_id))?;
            let bit_prefix_bucket = hdk::entry_address(&Entry::App("bucket".into(), app_definition::Bucket{id: bit_prefix_bucket_id}.into()))?;
            query_from_address(Some(&bit_prefix_bucket), Some(index_strings), target_type, None, false, Some("bucket_expression_post"))
        },

        "dos" => {
            if dos < 1 || dos > 6 {return Err(ZomeApiError::from("DOS not within bounds 1 -> 6".to_string()))};
            let mut expressions = dos::dos_query(index_strings, query_options, query_type, dos, seed, resonations)?;
            expressions = expressions.into_iter().unique().collect::<Vec<_>>(); //ensure all posts returned are unique
            query_from_address(None, None, target_type, Some(expressions), false, None)
        },

        _ => { //TODO: Add maximum post retrieval here - perhaps dont return over 50 posts - and posts should either be selected randomly or by a pagination query?
            hdk::debug("Making either a group, perspective or collection query")?;
            let current_agent_username = utils::helpers::call_and_get_current_user_username()?;
            let context_address = Address::from(perspective);

            match indexing::run_context_auth(&context_address, &current_agent_username.address){
                Ok(Some(function_definition::ContextAuthResult::Collection(_context_entry))) => {
                    hdk::debug("Making a collection query")?;
                    query_from_address(Some(&context_address), Some(index_strings), target_type, None, false, Some("collection_expression_post"))
                },
                Ok(Some(function_definition::ContextAuthResult::Group(_context_entry))) => {
                    hdk::debug("Making a group query")?;
                    query_from_address(Some(&context_address), Some(index_strings), target_type, None, resonations, Some("group_expression_post"))
                },
                Ok(None) => { 
                    hdk::debug("Making a perspective query")?;
                    let perspective_users = hdk::call(hdk::THIS_INSTANCE, "perspective", Address::from(hdk::PUBLIC_TOKEN.to_string()), 
                                                        "get_perspectives_users", JsonString::from(json!({"perspective": context_address})))?;
                    let perspective_users: ZomeApiResult<Vec<EntryAndAddress<app_definition::UserName>>> = perspective_users.try_into()?;
                    let mut out = vec![];
                
                    for user in perspective_users?{
                        let mut expressions = vec![];
                        for index_string in &index_strings{
                            if resonations == true{
                                expressions.append(&mut utils::helpers::get_links_and_load_type::<app_definition::ExpressionPost>(&user.address, LinkMatch::Exactly("resonation"), LinkMatch::Regex(index_string))?);
                            } else {
                                expressions.append(&mut utils::helpers::get_links_and_load_type::<app_definition::ExpressionPost>(&user.address, LinkMatch::Exactly("expression_post"), LinkMatch::Regex(index_string))?);
                            }
                        };
                        let mut expressions = expressions.into_iter().map(|expression| get_expression_attributes(expression, true)).collect::<Result<Vec<_>,_>>()?;
                        out.append(&mut expressions);
                    };
                    Ok(JsonString::from(out))
                }
                Err(err) => {
                    hdk::debug("Error invalid auth on perspective")?;
                    Err(err)
                }
            }
        }
    }
}
///Function to handle the getting of expression for a given perspective and query point(s)
///for example: perspective: dos & query_points: [2018<timestamp>, holochain<channel>, dht<channel>, eric<channel>]
//TODO: Switch to normal Entry (JsonString as returned from get_entry & get_links) for EntryAndAddress across the whole application
Function to handle the getting of expression for a given perspective and query point(s)
for example: perspective: dos & query_points: [2018, holochain, dht, eric]
TODO: Switch to normal Entry (JsonString as returned from get_entry & get_links) for EntryAndAddress across the whole application
Function to handle the getting of expression for a given perspective and query point ( s ) for example : perspective : dos & query_points : [ 2018 holochain dht eric ] TODO : Switch to normal Entry ( JsonString as returned from get_entry & get_links ) for EntryAndAddress across the whole application
pub fn query_expressions(perspective: String, attributes: Vec<String>, query_options: QueryOptions, target_type: QueryTarget, 
                        query_type: QueryType, dos: u32, seed: String, resonations: bool) -> ZomeApiResult<JsonString> {
    let index_strings = attributes_to_index_string(attributes)?;
    hdk::debug(format!("Getting expressions with generated query string(s): {:?}", index_strings))?;
    match perspective.as_ref() {
        "random" => {
            let seed = HashString::encode_from_str(&seed, Hash::SHA2256);
            hdk::debug(format!("Seed addresss: {}", seed.to_string()))?;
            let current_bit_prefix = hdk::call(hdk::THIS_INSTANCE, "config", Address::from(hdk::PUBLIC_TOKEN.to_string()), 
                                                        "get_current_bit_prefix", JsonString::from(json!({})))?;
            let current_bit_prefix: ZomeApiResult<u32> = current_bit_prefix.try_into()?;
            let current_bit_prefix = current_bit_prefix?;
            let bit_prefix_bucket_id = utils::helpers::hash_prefix(Address::from(seed), current_bit_prefix); //get and id for bucket to make query from using seed passed into function
            hdk::debug(format!("Making random query with bit prefix: {}", bit_prefix_bucket_id))?;
            let bit_prefix_bucket = hdk::entry_address(&Entry::App("bucket".into(), app_definition::Bucket{id: bit_prefix_bucket_id}.into()))?;
            query_from_address(Some(&bit_prefix_bucket), Some(index_strings), target_type, None, false, Some("bucket_expression_post"))
        },
        "dos" => {
            if dos < 1 || dos > 6 {return Err(ZomeApiError::from("DOS not within bounds 1 -> 6".to_string()))};
            let mut expressions = dos::dos_query(index_strings, query_options, query_type, dos, seed, resonations)?;
            expressions = expressions.into_iter().unique().collect::<Vec<_>>(); //ensure all posts returned are unique
            query_from_address(None, None, target_type, Some(expressions), false, None)
        },
        _ => { //TODO: Add maximum post retrieval here - perhaps dont return over 50 posts - and posts should either be selected randomly or by a pagination query?
            hdk::debug("Making either a group, perspective or collection query")?;
            let current_agent_username = utils::helpers::call_and_get_current_user_username()?;
            let context_address = Address::from(perspective);
            match indexing::run_context_auth(&context_address, &current_agent_username.address){
                Ok(Some(function_definition::ContextAuthResult::Collection(_context_entry))) => {
                    hdk::debug("Making a collection query")?;
                    query_from_address(Some(&context_address), Some(index_strings), target_type, None, false, Some("collection_expression_post"))
                },
                Ok(Some(function_definition::ContextAuthResult::Group(_context_entry))) => {
                    hdk::debug("Making a group query")?;
                    query_from_address(Some(&context_address), Some(index_strings), target_type, None, resonations, Some("group_expression_post"))
                },
                Ok(None) => { 
                    hdk::debug("Making a perspective query")?;
                    let perspective_users = hdk::call(hdk::THIS_INSTANCE, "perspective", Address::from(hdk::PUBLIC_TOKEN.to_string()), 
                                                        "get_perspectives_users", JsonString::from(json!({"perspective": context_address})))?;
                    let perspective_users: ZomeApiResult<Vec<EntryAndAddress<app_definition::UserName>>> = perspective_users.try_into()?;
                    let mut out = vec![];
                    for user in perspective_users?{
                        let mut expressions = vec![];
                        for index_string in &index_strings{
                            if resonations == true{
                                expressions.append(&mut utils::helpers::get_links_and_load_type::<app_definition::ExpressionPost>(&user.address, LinkMatch::Exactly("resonation"), LinkMatch::Regex(index_string))?);
                            } else {
                                expressions.append(&mut utils::helpers::get_links_and_load_type::<app_definition::ExpressionPost>(&user.address, LinkMatch::Exactly("expression_post"), LinkMatch::Regex(index_string))?);
                            }
                        };
                        let mut expressions = expressions.into_iter().map(|expression| get_expression_attributes(expression, true)).collect::<Result<Vec<_>,_>>()?;
                        out.append(&mut expressions);
                    };
                    Ok(JsonString::from(out))
                }
                Err(err) => {
                    hdk::debug("Error invalid auth on perspective")?;
                    Err(err)
                }
            }
        }
    }
}
pub fn query_expressions ( perspective : String , attributes : Vec < String > , query_options : QueryOptions , target_type : QueryTarget , query_type : QueryType , dos : u32 , seed : String , resonations : bool ) -> ZomeApiResult < JsonString > { let index_strings = attributes_to_index_string ( attributes ) ? ; hdk :: debug ( format ! ( " " index_strings ) ) ? ; match perspective . as_ref ( ) { " " => { let seed = HashString :: encode_from_str ( & seed , Hash :: SHA2256 ) ; hdk :: debug ( format ! ( " " seed to_string ( ) ) ) ? ; let current_bit_prefix = hdk :: call ( hdk :: THIS_INSTANCE , " " , Address :: from ( hdk :: PUBLIC_TOKEN . to_string ( ) ) , " " , JsonString :: from ( json ! ( { } ) ) ) ? ; let current_bit_prefix : ZomeApiResult < u32 > = current_bit_prefix . try_into ( ) ? ; let current_bit_prefix = current_bit_prefix ? ; let bit_prefix_bucket_id = utils :: helpers :: hash_prefix ( Address :: from ( seed ) , current_bit_prefix ) ; //get and id for bucket to make query from using seed passed into function hdk :: debug ( format ! ( " " bit_prefix_bucket_id ) ) ? ; let bit_prefix_bucket = hdk :: entry_address ( & Entry :: App ( " " . into ( ) , app_definition :: Bucket { id : bit_prefix_bucket_id } . into ( ) ) ) ? ; query_from_address ( Some ( & bit_prefix_bucket ) , Some ( index_strings ) , target_type , None , false , Some ( " " ) ) } , " " => { if dos < 1 || dos > 6 { return Err ( ZomeApiError :: from ( " " . to_string ( ) ) ) } ; let mut expressions = dos :: dos_query ( index_strings , query_options , query_type , dos , seed , resonations ) ? ; expressions = expressions . into_iter ( ) . unique ( ) . collect :: < Vec < _ > > ( ) ; //ensure all posts returned are unique query_from_address ( None , None , target_type , Some ( expressions ) , false , None ) } , _ => { //TODO: Add maximum post retrieval here - perhaps dont return over 50 posts - and posts should either be selected randomly or by a pagination query? hdk :: debug ( " " ) ? ; let current_agent_username = utils :: helpers :: call_and_get_current_user_username ( ) ? ; let context_address = Address :: from ( perspective ) ; match indexing :: run_context_auth ( & context_address , & current_agent_username . address ) { Ok ( Some ( function_definition :: ContextAuthResult :: Collection ( _context_entry ) ) ) => { hdk :: debug ( " " ) ? ; query_from_address ( Some ( & context_address ) , Some ( index_strings ) , target_type , None , false , Some ( " " ) ) } , Ok ( Some ( function_definition :: ContextAuthResult :: Group ( _context_entry ) ) ) => { hdk :: debug ( " " ) ? ; query_from_address ( Some ( & context_address ) , Some ( index_strings ) , target_type , None , resonations , Some ( " " ) ) } , Ok ( None ) => { hdk :: debug ( " " ) ? ; let perspective_users = hdk :: call ( hdk :: THIS_INSTANCE , " " , Address :: from ( hdk :: PUBLIC_TOKEN . to_string ( ) ) , " " , JsonString :: from ( json ! ( { " " context_address } ) ) ) ? ; let perspective_users : ZomeApiResult < Vec < EntryAndAddress < app_definition :: UserName > > > = perspective_users . try_into ( ) ? ; let mut out = vec ! [ ] ; for user in perspective_users ? { let mut expressions = vec ! [ ] ; for index_string in & index_strings { if resonations == true { expressions . append ( & mut utils :: helpers :: get_links_and_load_type :: < app_definition :: ExpressionPost > ( & user . address , LinkMatch :: Exactly ( " " ) , LinkMatch :: Regex ( index_string ) ) ? ) ; } else { expressions . append ( & mut utils :: helpers :: get_links_and_load_type :: < app_definition :: ExpressionPost > ( & user . address , LinkMatch :: Exactly ( " " ) , LinkMatch :: Regex ( index_string ) ) ? ) ; } } ; let mut expressions = expressions . into_iter ( ) . map ( | expression | get_expression_attributes ( expression , true ) ) . collect :: < Result < Vec < _ > , _ > > ( ) ? ; out . append ( & mut expressions ) ; } ; Ok ( JsonString :: from ( out ) ) } Err ( err ) => { hdk :: debug ( " " ) ? ; Err ( err ) } } } } }
Function to handle the getting of expression for a given perspective and query point(s)
for example: perspective: dos & query_points: [2018<timestamp>, holochain<channel>, dht<channel>, eric<channel>]
TODO: Switch to normal Entry (JsonString as returned from get_entry & get_links) for EntryAndAddress across the whole application
Function to handle the getting of expression for a given perspective and query point ( s ) for example : perspective : dos & query_points : [ 2018<timestamp > holochain<channel > dht<channel > eric<channel > ] TODO : Switch to normal Entry ( JsonString as returned from get_entry & get_links ) for EntryAndAddress across the whole application
perspective
String
attributes
Vec<String>
query_options
QueryOptions
target_type
QueryTarget
query_type
QueryType
dos
u32
seed
String
resonations
bool
perspective
String
attributes
Vec<String>
query_options
QueryOptions
target_type
QueryTarget
query_type
QueryType
dos
u32
seed
String
resonations
bool