Skip to content

Commit

Permalink
add documentations
Browse files Browse the repository at this point in the history
  • Loading branch information
Soushi888 committed Feb 16, 2024
1 parent 8de76e2 commit daba648
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ use profiles_integrity::*;

use crate::{error, get_latest_link};

/// Creates an individual profile entry in the DHT.
///
/// # Arguments
///
/// * `individual_profile` - The data structure representing the individual profile to be created.
///
/// # Returns
///
/// * `ExternResult<Record>` - A result containing the record of the newly created individual profile.
///
/// # Errors
///
/// This function will return an error if there is a problem creating the entry or linking it to the appropriate paths.
#[hdk_extern]
pub fn create_individual_profile(individual_profile: IndividualProfile) -> ExternResult<Record> {
let mut individual_profile = individual_profile;
Expand Down Expand Up @@ -31,6 +44,19 @@ pub fn create_individual_profile(individual_profile: IndividualProfile) -> Exter
Ok(record)
}

/// Retrieves the current agent's individual profile from the DHT.
///
/// # Arguments
///
/// * `()` - No arguments are required for this function.
///
/// # Returns
///
/// * `ExternResult<Option<Record>>` - A result containing an optional record of the agent's individual profile.
///
/// # Errors
///
/// This function will return an error if there is a problem retrieving the profile or its updates.
#[hdk_extern]
fn get_my_profile(_: ()) -> ExternResult<Option<Record>> {
let pubkey = agent_info()?.agent_initial_pubkey;
Expand Down Expand Up @@ -59,6 +85,19 @@ fn get_my_profile(_: ()) -> ExternResult<Option<Record>> {
}
}

/// Retrieves an individual profile from the DHT using its action hash.
///
/// # Arguments
///
/// * `individual_profile_hash` - The `ActionHash` of the individual profile to retrieve.
///
/// # Returns
///
/// * `ExternResult<Option<Record>>` - A result containing an optional record of the retrieved individual profile.
///
/// # Errors
///
/// This function will return an error if there is a problem retrieving the profile or its updates.
#[hdk_extern]
pub fn get_individual_profile(individual_profile_hash: ActionHash) -> ExternResult<Option<Record>> {
let links = get_links(
Expand All @@ -77,6 +116,19 @@ pub fn get_individual_profile(individual_profile_hash: ActionHash) -> ExternResu
get(latest_individual_profile_hash, GetOptions::default())
}

/// Retrieves all individual profiles from the DHT.
///
/// # Arguments
///
/// * `()` - No arguments are required for this function.
///
/// # Returns
///
/// * `ExternResult<Vec<Record>>` - A result containing a vector of records of all individual profiles.
///
/// # Errors
///
/// This function will return an error if there is a problem retrieving the profiles or their links.
#[hdk_extern]
pub fn get_all_individual_profiles(_: ()) -> ExternResult<Vec<Record>> {
let mut individual_profiles = Vec::new();
Expand All @@ -96,12 +148,28 @@ pub fn get_all_individual_profiles(_: ()) -> ExternResult<Vec<Record>> {
Ok(individual_profiles)
}

/// Input structure for updating an individual profile.
///
/// Contains the hash of the existing profile and the new profile data.
#[derive(Serialize, Deserialize, Debug)]
pub struct UpdateIndividualProfileInput {
pub individual_profile_hash: ActionHash,
pub updated_individual_profile: IndividualProfile,
}

/// Updates an individual profile in the DHT.
///
/// # Arguments
///
/// * `input` - The input structure containing the hash of the existing profile and the new profile data.
///
/// # Returns
///
/// * `ExternResult<Record>` - A result containing the record of the updated individual profile.
///
/// # Errors
///
/// This function will return an error if there is a problem updating the profile, such as attempting to update another agent's profile or if the profile does not exist.
#[hdk_extern]
pub fn update_individual_profile(input: UpdateIndividualProfileInput) -> ExternResult<Record> {
let individual_profile_hash = input.individual_profile_hash;
Expand All @@ -115,9 +183,9 @@ pub fn update_individual_profile(input: UpdateIndividualProfileInput) -> ExternR
None => return Err(error("Could not find the related IndividualProfile")),
};

let author = record.action().author();
let author = record.action().author().to_owned();

if *author != agent_info()?.agent_initial_pubkey {
if author != agent_info()?.agent_initial_pubkey {
return Err(error("Can not update a profile of a different agent"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
use hdi::prelude::*;
use image::io::Reader as ImageReader;

/// Represents an individual profile Entry with various attributes such as name, nickname, bio, etc.
#[hdk_entry_helper]
#[derive(Clone, PartialEq)]
pub struct IndividualProfile {
/// The full name of the individual.
pub name: String,
/// A shorter version of the individual's name, often used for display purposes.
pub nickname: String,
/// A brief biography about the individual.
pub bio: String,
/// An optional serialized image representing the individual's profile picture.
pub profile_picture: Option<SerializedBytes>,
/// The type of individual, either 'advocate' or 'developer'.
pub individual_type: String,
/// A list of skills associated with the individual.
pub skills: Vec<String>,
/// The individual's email address.
pub email: String,
/// An optional phone number for the individual.
pub phone: Option<String>,
/// The time zone in which the individual resides.
pub time_zone: String,
/// The location where the individual is based.
pub location: String,
/// The timestamp indicating when the individual's profile was created.
pub created_at: Timestamp,
}

Expand Down
34 changes: 22 additions & 12 deletions ui/src/lib/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ export const HOLOCHAIN_APP_ID = 'mewsfeed';
export const IS_LAUNCHER = import.meta.env.VITE_IS_LAUNCHER;
export const IS_HOLO_HOSTED = import.meta.env.VITE_IS_HOLO_HOSTED;

/**
* Sets up the Holochain client, connects to the websocket, and authorizes the client if necessary.
*
* @return {Promise<AppAgentWebsocket>} The connected Holochain client.
*/
export const setupHolochain = async () => {
try {
// console.log(import.meta.env);
// const client = await AppAgentWebsocket.connect(
// IS_LAUNCHER ? `ws://UNUSED` : `ws://localhost:${import.meta.env.VITE_HC_PORT}`,
// HOLOCHAIN_APP_ID,
// 60000
// );
// if (typeof window === 'object' && !('__HC_LAUNCHER_ENV__' in window)) {
// const appInfo = await client.appInfo();
// await authorizeClient(appInfo);
// }
// return client;
console.log(import.meta.env);
const client = await AppAgentWebsocket.connect(
IS_LAUNCHER ? `ws://UNUSED` : `ws://localhost:${import.meta.env.VITE_HC_PORT}`,
HOLOCHAIN_APP_ID,
60000
);
if (typeof window === 'object' && !('__HC_LAUNCHER_ENV__' in window)) {
const appInfo = await client.appInfo();
await authorizeClient(appInfo);
}
return client;
} catch (e) {
console.log('Holochain client setup error', e);
throw e;
}
};

// set up zome call signing when run outside of launcher
/**
* Authorizes the client for making zome calls to the Holochain app.
*
* @param {AppInfo} appInfo - information about the Holochain app
* @return {Promise<void>} a Promise that resolves when the client is authorized
*/
export const authorizeClient = async (appInfo: AppInfo) => {
if (typeof window === 'object' && !('__HC_LAUNCHER_ENV__' in window)) {
if (!(CellType.Provisioned in appInfo.cell_info.mewsfeed[0])) {
Expand Down
14 changes: 1 addition & 13 deletions ui/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import { AppAgentWebsocket, type AppAgentClient } from '@holochain/client';
import * as path from 'path';
import { setContext } from 'svelte';
import { clientContext } from '../contexts';
import { setupHolochain } from '$lib/utils/client';

export const load: PageLoad = async () => {
setupHolochain();
// const hAppPath = new URL(`ws://localhost:35899`);
// const client = await AppAgentWebsocket.connect(hAppPath, 'requests-and-offers');
// setContext(clientContext, {
// getClient: () => client
// });
// return client;
// setupHolochain();
};

0 comments on commit daba648

Please sign in to comment.