Skip to content

Commit

Permalink
add created_at to AssetRelation, add client-side logic to encode WAL …
Browse files Browse the repository at this point in the history
…context
  • Loading branch information
matthme committed Jan 8, 2025
1 parent 8918083 commit 8314b7e
Show file tree
Hide file tree
Showing 22 changed files with 167 additions and 75 deletions.
4 changes: 2 additions & 2 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"@holochain-open-dev/stores": "0.400.0",
"@holochain-open-dev/utils": "0.400.0",
"@holochain/client": "^0.18.0",
"@lightningrodlabs/we-rust-utils": "0.400.0",
"@lightningrodlabs/we-rust-utils": "0.400.1",
"@matthme/electron-updater": "6.3.0-alpha.1",
"@msgpack/msgpack": "^2.8.0",
"@sinclair/typebox": "0.33.12",
"@theweave/api": "0.4.0-alpha.2",
"@theweave/api": "0.4.0-alpha.3",
"@theweave/moss-types": "0.3.0-alpha.2",
"@theweave/utils": "0.3.0-alpha.2",
"adm-zip": "0.5.14",
Expand Down
32 changes: 24 additions & 8 deletions dnas/assets/zomes/coordinator/assets/src/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct AssetRelationAndHash {
pub src_wal: WAL,
pub dst_wal: WAL,
pub relation_hash: EntryHash,
pub created_at: Timestamp,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -19,6 +20,7 @@ pub struct AssetRelationWithTags {
pub dst_wal: WAL,
pub tags: Vec<String>,
pub relation_hash: EntryHash,
pub created_at: Timestamp,
}

#[derive(Serialize, Deserialize, SerializedBytes, Debug)]
Expand All @@ -28,6 +30,9 @@ pub struct RelateAssetsInput {
pub tags: Vec<String>,
}

/// Note that the WAL's context is an Option<Vec<u8>> and therefore needs to have been
/// encoded into that format client-side because a WAL in general can be any arbitrary
/// javascript object
#[hdk_extern]
pub fn add_asset_relation(input: RelateAssetsInput) -> ExternResult<AssetRelationWithTags> {
let asset_relation = AssetRelation {
Expand All @@ -37,19 +42,22 @@ pub fn add_asset_relation(input: RelateAssetsInput) -> ExternResult<AssetRelatio

// 1. Create entry and add it to the ALL_ASSET_RELATIONS_ANCHOR if no entry exists yet
let relation_hash = hash_entry(asset_relation.clone())?;
match get(relation_hash.clone(), GetOptions::default()) {
Ok(Some(_r)) => (),
let record = match get(relation_hash.clone(), GetOptions::default()) {
Ok(Some(r)) => r,
_ => {
create_entry(&EntryTypes::AssetRelation(asset_relation.clone()))?;
let action_hash = create_entry(&EntryTypes::AssetRelation(asset_relation.clone()))?;
let path = Path::from(ALL_ASSET_RELATIONS_ANCHOR);
create_link(
path.path_entry_hash()?,
relation_hash.clone(),
LinkTypes::AllAssetRelations,
(),
)?;
get(action_hash, GetOptions::default())?.ok_or(wasm_error!(WasmErrorInner::Guest(
format!("Failed to get the record that was just created.")
)))?
}
}
};

// 2. Add tags to the asset relation entry hash
add_tags_to_asset_relation(AddTagsToAssetRelationInput {
Expand Down Expand Up @@ -78,6 +86,7 @@ pub fn add_asset_relation(input: RelateAssetsInput) -> ExternResult<AssetRelatio
dst_wal: input.dst_wal,
tags: input.tags,
relation_hash,
created_at: record.action().timestamp(),
};

emit_signal(Signal::AssetRelationCreated {
Expand Down Expand Up @@ -225,6 +234,7 @@ pub fn remove_asset_relation(relation_hash: EntryHash) -> ExternResult<()> {
src_wal: asset_relation.src_wal,
dst_wal: asset_relation.dst_wal,
relation_hash,
created_at: asset_relation_record.action().timestamp(),
},
})?;

Expand Down Expand Up @@ -353,7 +363,7 @@ pub fn get_all_asset_relations() -> ExternResult<Vec<AssetRelationAndHash>> {
.map(|target| Ok(GetInput::new(target.into(), GetOptions::default())))
.collect::<ExternResult<Vec<GetInput>>>()?;

let records = HDK.with(|hdk| hdk.borrow().get(get_input))?;
let records: Vec<Option<Record>> = HDK.with(|hdk| hdk.borrow().get(get_input))?;

Ok(records
.into_iter()
Expand All @@ -368,6 +378,7 @@ pub fn get_all_asset_relations() -> ExternResult<Vec<AssetRelationAndHash>> {
src_wal: a.src_wal,
dst_wal: a.dst_wal,
relation_hash: eh.clone(),
created_at: r.action().timestamp(),
}),
None => None,
}
Expand Down Expand Up @@ -407,6 +418,7 @@ pub fn get_outgoing_asset_relations_with_tags(
dst_wal: asset_relation.dst_wal,
tags,
relation_hash: asset_relation.relation_hash,
created_at: asset_relation.created_at,
});
}
Ok(asset_relations_with_tags)
Expand All @@ -425,7 +437,7 @@ pub fn get_outgoing_asset_relations(src_wal: WAL) -> ExternResult<Vec<AssetRelat
.unique() // We filter out duplicate links here
.map(|target| Ok(GetInput::new(target.into(), GetOptions::default())))
.collect::<ExternResult<Vec<GetInput>>>()?;
let records = HDK.with(|hdk| hdk.borrow().get(get_input))?;
let records: Vec<Option<Record>> = HDK.with(|hdk| hdk.borrow().get(get_input))?;
let mut asset_relations: Vec<AssetRelationAndHash> = Vec::new();
for maybe_record in records {
if let Some(record) = maybe_record {
Expand All @@ -449,6 +461,7 @@ pub fn get_outgoing_asset_relations(src_wal: WAL) -> ExternResult<Vec<AssetRelat
"AssetRelation record has no entry hash".into()
)))?
.to_owned(),
created_at: record.action().timestamp(),
};
asset_relations.push(asset_relation_and_hash)
}
Expand Down Expand Up @@ -486,6 +499,7 @@ pub fn get_incoming_asset_relations_with_tags(
dst_wal: asset_relation.dst_wal,
tags,
relation_hash: asset_relation.relation_hash,
created_at: asset_relation.created_at,
});
}
Ok(asset_relations_with_tags)
Expand All @@ -504,7 +518,7 @@ pub fn get_incoming_asset_relations(dst_wal: WAL) -> ExternResult<Vec<AssetRelat
.unique() // We filter out duplicate links here
.map(|target| Ok(GetInput::new(target.into(), GetOptions::default())))
.collect::<ExternResult<Vec<GetInput>>>()?;
let records = HDK.with(|hdk| hdk.borrow().get(get_input))?;
let records: Vec<Option<Record>> = HDK.with(|hdk| hdk.borrow().get(get_input))?;
let mut asset_relations: Vec<AssetRelationAndHash> = Vec::new();
for maybe_record in records {
if let Some(record) = maybe_record {
Expand All @@ -528,6 +542,7 @@ pub fn get_incoming_asset_relations(dst_wal: WAL) -> ExternResult<Vec<AssetRelat
"AssetRelation record has no entry hash".into()
)))?
.to_owned(),
created_at: record.action().timestamp(),
};
asset_relations.push(asset_relation_and_hash)
}
Expand Down Expand Up @@ -559,7 +574,7 @@ pub fn get_asset_relations_for_relationship_tag(
))
})
.collect::<ExternResult<Vec<GetInput>>>()?;
let records = HDK.with(|hdk| hdk.borrow().get(get_input))?;
let records: Vec<Option<Record>> = HDK.with(|hdk| hdk.borrow().get(get_input))?;
let mut asset_relations: Vec<AssetRelationAndHash> = Vec::new();
for maybe_record in records {
if let Some(record) = maybe_record {
Expand All @@ -583,6 +598,7 @@ pub fn get_asset_relations_for_relationship_tag(
"AssetRelation record has no entry hash".into()
)))?
.to_owned(),
created_at: record.action().timestamp(),
};
asset_relations.push(asset_relation_and_hash)
}
Expand Down
2 changes: 1 addition & 1 deletion example/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@holochain-open-dev/stores": "0.400.0",
"@holochain-open-dev/utils": "0.400.0",
"@holochain/client": "^0.18.0",
"@theweave/api": "0.4.0-alpha.2",
"@theweave/api": "0.4.0-alpha.3",
"@theweave/elements": "file:../../libs/elements",
"@theweave/attachments": "file:../../libs/attachments",
"@lit/context": "^1.0.1",
Expand Down
4 changes: 3 additions & 1 deletion example/ui/src/example-applet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export class ExampleApplet extends LitElement {
if (!appInfo) throw new Error('AppInfo is null.');
const dnaHash = (appInfo.cell_info.forum[0] as any)[CellType.Provisioned]
.cell_id[0];
this.weaveClient!.assets.dragAsset({ hrl: [dnaHash, e.detail] });
this.weaveClient!.assets.dragAsset({
hrl: [dnaHash, e.detail],
});
}}
></applet-main>
</profiles-context>
Expand Down
2 changes: 1 addition & 1 deletion iframes/applet-iframe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@holochain-open-dev/utils": "0.400.0",
"@holochain-open-dev/stores": "0.400.0",
"@holochain/client": "^0.18.0",
"@theweave/api": "0.4.0-alpha.2",
"@theweave/api": "0.4.0-alpha.3",
"@theweave/utils": "0.3.0-alpha.2",
"inline-module": "^0.6.1"
},
Expand Down
2 changes: 1 addition & 1 deletion libs/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@theweave/api",
"version": "0.4.0-alpha.2",
"version": "0.4.0-alpha.3",
"main": "./dist/index.js",
"module": "./dist/index.js",
"license": "MIT",
Expand Down
12 changes: 8 additions & 4 deletions libs/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,19 @@ export type GroupPermissionType =
type: 'Ambiguous';
};

export type WalAndTags = {
wal: WAL;
export type WalRelationAndTags = {
relationHash: EntryHash;
/**
* Timestamp of when the asset relation to this WAL has been created
*/
createdAt: number;
wal: WAL;
tags: string[];
};

export type AssetStoreContent = {
linkedTo: WalAndTags[];
linkedFrom: WalAndTags[];
linkedTo: WalRelationAndTags[];
linkedFrom: WalRelationAndTags[];
tags: string[];
};

Expand Down
2 changes: 1 addition & 1 deletion libs/attachments/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@holochain-open-dev/stores": "0.400.0",
"@holochain-open-dev/utils": "0.400.0",
"@holochain/client": "^0.18.0",
"@theweave/api": "0.4.0-alpha.2",
"@theweave/api": "0.4.0-alpha.3",
"@theweave/elements": "0.4.0-alpha.1",
"@lit/context": "^1.0.1",
"@lit/localize": "^0.12.0",
Expand Down
2 changes: 1 addition & 1 deletion libs/elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@holochain-open-dev/stores": "0.400.0",
"@holochain-open-dev/utils": "0.400.0",
"@holochain/client": "^0.18.0",
"@theweave/api": "0.4.0-alpha.2",
"@theweave/api": "0.4.0-alpha.3",
"@lit/context": "^1.0.1",
"@lit/localize": "^0.12.0",
"@mdi/js": "^7.2.0",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@
"@holochain-open-dev/stores": "0.400.0",
"@holochain-open-dev/utils": "0.400.0",
"@holochain/client": "^0.18.0",
"@theweave/api": "0.4.0-alpha.2",
"@theweave/api": "0.4.0-alpha.3",
"@theweave/moss-types": "0.3.0-alpha.2",
"@theweave/utils": "0.3.0-alpha.2",
"@lightningrodlabs/we-rust-utils": "0.400.0-rc.1",
"@lightningrodlabs/we-rust-utils": "0.400.1",
"@matthme/electron-updater": "6.3.0-alpha.1",
"@msgpack/msgpack": "^2.8.0",
"@sinclair/typebox": "0.33.12",
Expand Down
4 changes: 2 additions & 2 deletions shared/group-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@theweave/group-client",
"version": "0.2.0-alpha.1",
"version": "0.2.0-alpha.2",
"main": "./dist/index.js",
"module": "./dist/index.js",
"license": "MIT",
Expand All @@ -20,7 +20,7 @@
"@holochain/client": "^0.18.0",
"@holochain-open-dev/utils": "0.400.0",
"@msgpack/msgpack": "^2.7.2",
"@theweave/api": "0.4.0-alpha.2"
"@theweave/api": "0.4.0-alpha.3"
},
"devDependencies": {
"rimraf": "^3.0.2",
Expand Down
Loading

0 comments on commit 8314b7e

Please sign in to comment.