Skip to content

Commit

Permalink
fix wal decoding of signals, add createdAt field in frontend where ne…
Browse files Browse the repository at this point in the history
…cessary, removed obsolete case in handling of AssetTagsAdded
  • Loading branch information
matthme committed Jan 8, 2025
1 parent 8314b7e commit 5b459dd
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 51 deletions.
2 changes: 1 addition & 1 deletion 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.2",
"version": "0.2.0-alpha.4",
"main": "./dist/index.js",
"module": "./dist/index.js",
"license": "MIT",
Expand Down
17 changes: 10 additions & 7 deletions shared/group-client/src/assetsClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ZomeClient } from '@holochain-open-dev/utils';
import { EntryHash, AppClient } from '@holochain/client';
import { WAL, WalAndTags } from '@theweave/api';
import { WAL, WalRelationAndTags } from '@theweave/api';

import {
AssetRelationAndHash,
Expand All @@ -14,8 +14,8 @@ import { AsyncStatus, Unsubscriber, writable, Writable } from '@holochain-open-d
import { decode, encode } from '@msgpack/msgpack';

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

Expand Down Expand Up @@ -72,6 +72,7 @@ export class WalStore {
wal: v.dst_wal,
tags: v.tags,
relationHash: v.relation_hash,
createdAt: v.created_at,
}));
const linkedFrom = relationsForWal.linked_from.map((v) => ({
wal: v.dst_wal,
Expand Down Expand Up @@ -206,24 +207,26 @@ function walEncodeContext(wal: WAL): WAL {
* @param wal
* @returns
*/
function walDecodeContext(wal: WAL): WAL {
export function walDecodeContext(wal: WAL): WAL {
return {
hrl: wal.hrl,
context: wal.context ? decode(wal.context) : undefined,
};
}

function walsEncodeContext(wals: WAL[]): WAL[] {
export function walsEncodeContext(wals: WAL[]): WAL[] {
return wals.map((wal) => walEncodeContext(wal));
}

function decodeAssetRelationsWALs(
export function decodeAssetRelationsWALs(
relationsWithTags: AssetRelationWithTags[],
): AssetRelationWithTags[] {
return relationsWithTags.map((relationWithTags) => decodeAssetRelationWALs(relationWithTags));
}

function decodeAssetRelationWALs(relationWithTags: AssetRelationWithTags): AssetRelationWithTags {
export function decodeAssetRelationWALs(
relationWithTags: AssetRelationWithTags,
): AssetRelationWithTags {
return {
src_wal: walDecodeContext(relationWithTags.src_wal),
dst_wal: walDecodeContext(relationWithTags.dst_wal),
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@theweave/api": "0.4.0-alpha.3",
"@theweave/elements": "0.4.0-alpha.1",
"@theweave/utils": "0.3.0-alpha.2",
"@theweave/group-client": "0.2.0-alpha.2",
"@theweave/group-client": "0.2.0-alpha.4",
"@lit/context": "^1.0.1",
"@lit-labs/task": "^1.1.2",
"@lit/localize": "^0.12.0",
Expand Down
111 changes: 71 additions & 40 deletions src/renderer/src/groups/group-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ import {
} from '../utils.js';
import { DistributionInfo, TDistributionInfo } from '@theweave/moss-types';
import {
decodeAssetRelationWALs,
GroupRemoteSignal,
PeerStatusClient,
SignalPayloadAssets,
SignalPayloadPeerStatus,
walDecodeContext,
} from '@theweave/group-client';
import { FoyerStore } from './foyer.js';
import { appIdFromAppletHash, deriveToolCompatibilityId, toLowerCaseB64 } from '@theweave/utils';
Expand Down Expand Up @@ -215,11 +217,13 @@ export class GroupStore {
wal: v.dst_wal,
tags: v.tags,
relationHash: v.relation_hash,
createdAt: v.created_at,
}));
const linkedFrom = relationsForWal.linked_from.map((v) => ({
wal: v.dst_wal,
tags: v.tags,
relationHash: v.relation_hash,
createdAt: v.created_at,
}));
const newValue = {
status: 'complete',
Expand Down Expand Up @@ -261,7 +265,7 @@ export class GroupStore {
// Update asset store(s)
switch (signal.type) {
case 'AssetTagsAdded': {
const walStringified = stringifyWal(signal.wal);
const walStringified = stringifyWal(walDecodeContext(signal.wal));
const storeAndSubscribers = this._assetStores[walStringified];
// If there are no subscribers, we can just drop it here
if (!storeAndSubscribers) return;
Expand All @@ -273,7 +277,7 @@ export class GroupStore {
break;
}
case 'AssetTagsRemoved': {
const walStringified = stringifyWal(signal.wal);
const walStringified = stringifyWal(walDecodeContext(signal.wal));
const storeAndSubscribers = this._assetStores[walStringified];
// If there are no subscribers, we can just drop it here
if (!storeAndSubscribers) return;
Expand All @@ -285,9 +289,13 @@ export class GroupStore {
break;
}
case 'AssetRelationCreated': {
const decodedSignal: SignalPayloadAssets = {
type: 'AssetRelationCreated',
relation: decodeAssetRelationWALs(signal.relation),
};
// Add it to the asset store of the srcWal
const srcWalStringified = stringifyWal(signal.relation.src_wal);
const dstWalStringified = stringifyWal(signal.relation.dst_wal);
const srcWalStringified = stringifyWal(walDecodeContext(decodedSignal.relation.src_wal));
const dstWalStringified = stringifyWal(walDecodeContext(decodedSignal.relation.dst_wal));
const srcStoreAndSubscribers = this._assetStores[srcWalStringified];
const dstStoreAndSubscribers = this._assetStores[dstWalStringified];
if (srcStoreAndSubscribers) {
Expand All @@ -299,21 +307,23 @@ export class GroupStore {
if (existingWalAndTagsIdx !== -1) {
const existingWalAndTags = store.value.linkedTo[existingWalAndTagsIdx];
const newTags = Array.from(
new Set([...existingWalAndTags.tags, ...signal.relation.tags]),
new Set([...existingWalAndTags.tags, ...decodedSignal.relation.tags]),
);
// overwrite existing item with the one containing merged tags
store.value.linkedTo[existingWalAndTagsIdx] = {
wal: existingWalAndTags.wal,
relationHash: existingWalAndTags.relationHash,
tags: newTags,
createdAt: existingWalAndTags.createdAt,
};
} else {
store.value.linkedTo = [
...store.value.linkedTo,
{
wal: signal.relation.dst_wal,
relationHash: signal.relation.relation_hash,
tags: signal.relation.tags,
wal: decodedSignal.relation.dst_wal,
relationHash: decodedSignal.relation.relation_hash,
tags: decodedSignal.relation.tags,
createdAt: decodedSignal.relation.created_at,
},
];
}
Expand All @@ -332,21 +342,23 @@ export class GroupStore {
if (existingWalAndTagsIdx !== -1) {
const existingWalAndTags = store.value.linkedFrom[existingWalAndTagsIdx];
const newTags = Array.from(
new Set([...existingWalAndTags.tags, ...signal.relation.tags]),
new Set([...existingWalAndTags.tags, ...decodedSignal.relation.tags]),
);
// overwrite existing item with the one containing merged tags
store.value.linkedFrom[existingWalAndTagsIdx] = {
wal: existingWalAndTags.wal,
relationHash: existingWalAndTags.relationHash,
tags: newTags,
createdAt: existingWalAndTags.createdAt,
};
} else {
store.value.linkedFrom = [
...store.value.linkedFrom,
{
wal: signal.relation.src_wal,
relationHash: signal.relation.relation_hash,
tags: signal.relation.tags,
wal: decodedSignal.relation.src_wal,
relationHash: decodedSignal.relation.relation_hash,
tags: decodedSignal.relation.tags,
createdAt: decodedSignal.relation.created_at,
},
];
}
Expand All @@ -356,9 +368,18 @@ export class GroupStore {
break;
}
case 'AssetRelationRemoved': {
const decodedSignal: SignalPayloadAssets = {
type: 'AssetRelationRemoved',
relation: {
src_wal: walDecodeContext(signal.relation.src_wal),
dst_wal: walDecodeContext(signal.relation.dst_wal),
relation_hash: signal.relation.relation_hash,
created_at: signal.relation.created_at,
},
};
console.log('AssetRelationRemoved: signal: ', signal);
const srcWalStringified = stringifyWal(signal.relation.src_wal);
const dstWalStringified = stringifyWal(signal.relation.dst_wal);
const srcWalStringified = stringifyWal(decodedSignal.relation.src_wal);
const dstWalStringified = stringifyWal(decodedSignal.relation.dst_wal);
const srcStoreAndSubscribers = this._assetStores[srcWalStringified];
const dstStoreAndSubscribers = this._assetStores[dstWalStringified];
if (srcStoreAndSubscribers) {
Expand All @@ -382,8 +403,15 @@ export class GroupStore {
break;
}
case 'RelationTagsAdded': {
const srcWalStringified = stringifyWal(signal.src_wal);
const dstWalStringified = stringifyWal(signal.dst_wal);
const decodedSignal: SignalPayloadAssets = {
type: 'RelationTagsAdded',
relation_hash: signal.relation_hash,
src_wal: walDecodeContext(signal.src_wal),
dst_wal: walDecodeContext(signal.dst_wal),
tags: signal.tags,
};
const srcWalStringified = stringifyWal(decodedSignal.src_wal);
const dstWalStringified = stringifyWal(decodedSignal.dst_wal);
const srcStoreAndSubscribers = this._assetStores[srcWalStringified];
const dstStoreAndSubscribers = this._assetStores[dstWalStringified];

Expand All @@ -396,22 +424,16 @@ export class GroupStore {
);
if (existingWalAndTagsIdx !== -1) {
const existingWalAndTags = store.value.linkedTo[existingWalAndTagsIdx];
const newTags = Array.from(new Set([...existingWalAndTags.tags, ...signal.tags]));
const newTags = Array.from(
new Set([...existingWalAndTags.tags, ...decodedSignal.tags]),
);
// overwrite existing item with the one containing merged tags
store.value.linkedTo[existingWalAndTagsIdx] = {
wal: existingWalAndTags.wal,
relationHash: existingWalAndTags.relationHash,
tags: newTags,
createdAt: existingWalAndTags.createdAt,
};
} else {
store.value.linkedTo = [
...store.value.linkedTo,
{
wal: signal.dst_wal,
relationHash: signal.relation_hash,
tags: signal.tags,
},
];
}
return store;
});
Expand All @@ -427,32 +449,33 @@ export class GroupStore {
);
if (existingWalAndTagsIdx !== -1) {
const existingWalAndTags = store.value.linkedFrom[existingWalAndTagsIdx];
const newTags = Array.from(new Set([...existingWalAndTags.tags, ...signal.tags]));
const newTags = Array.from(
new Set([...existingWalAndTags.tags, ...decodedSignal.tags]),
);
// overwrite existing item with the one containing merged tags
store.value.linkedFrom[existingWalAndTagsIdx] = {
wal: existingWalAndTags.wal,
relationHash: existingWalAndTags.relationHash,
tags: newTags,
createdAt: existingWalAndTags.createdAt,
};
} else {
store.value.linkedFrom = [
...store.value.linkedFrom,
{
wal: signal.src_wal,
relationHash: signal.relation_hash,
tags: signal.tags,
},
];
}
return store;
});
}
break;
}
case 'RelationTagsRemoved': {
const decodedSignal: SignalPayloadAssets = {
type: 'RelationTagsRemoved',
relation_hash: signal.relation_hash,
src_wal: walDecodeContext(signal.src_wal),
dst_wal: walDecodeContext(signal.dst_wal),
tags: signal.tags,
};
console.log('RelationTagsRemoved: signal: ', signal);
const srcWalStringified = stringifyWal(signal.src_wal);
const dstWalStringified = stringifyWal(signal.dst_wal);
const srcWalStringified = stringifyWal(decodedSignal.src_wal);
const dstWalStringified = stringifyWal(decodedSignal.dst_wal);
const srcStoreAndSubscribers = this._assetStores[srcWalStringified];
const dstStoreAndSubscribers = this._assetStores[dstWalStringified];

Expand All @@ -465,12 +488,15 @@ export class GroupStore {
);
if (existingWalAndTagsIdx !== -1) {
const existingWalAndTags = store.value.linkedTo[existingWalAndTagsIdx];
const newTags = existingWalAndTags.tags.filter((tag) => !signal.tags.includes(tag));
const newTags = existingWalAndTags.tags.filter(
(tag) => !decodedSignal.tags.includes(tag),
);
// overwrite existing item with the one containing merged tags
store.value.linkedTo[existingWalAndTagsIdx] = {
wal: existingWalAndTags.wal,
relationHash: existingWalAndTags.relationHash,
tags: newTags,
createdAt: existingWalAndTags.createdAt,
};
}
return store;
Expand All @@ -487,12 +513,15 @@ export class GroupStore {
);
if (existingWalAndTagsIdx !== -1) {
const existingWalAndTags = store.value.linkedFrom[existingWalAndTagsIdx];
const newTags = existingWalAndTags.tags.filter((tag) => !signal.tags.includes(tag));
const newTags = existingWalAndTags.tags.filter(
(tag) => !decodedSignal.tags.includes(tag),
);
// overwrite existing item with the one containing merged tags
store.value.linkedFrom[existingWalAndTagsIdx] = {
wal: existingWalAndTags.wal,
relationHash: existingWalAndTags.relationHash,
tags: newTags,
createdAt: existingWalAndTags.createdAt,
};
}
return store;
Expand Down Expand Up @@ -570,11 +599,13 @@ export class GroupStore {
wal: v.dst_wal,
tags: v.tags,
relationHash: v.relation_hash,
createdAt: v.created_at,
}));
const linkedFrom = relationsForWal.linked_from.map((v) => ({
wal: v.dst_wal,
tags: v.tags,
relationHash: v.relation_hash,
createdAt: v.created_at,
}));
console.log('@subscribe: setting assetstore', {
tags: relationsForWal.tags,
Expand Down
2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@holochain/client": "^0.18.0",
"@holochain/tryorama": "0.17.0-rc.0",
"@holochain-open-dev/utils": "0.400.0",
"@theweave/group-client": "0.2.0-alpha.2",
"@theweave/group-client": "0.2.0-alpha.4",
"@theweave/api": "0.4.0-alpha.3",
"typescript": "^4.9.4",
"vitest": "^0.28.4"
Expand Down
2 changes: 1 addition & 1 deletion wdocker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@sinclair/typebox": "0.33.12",
"@theweave/api": "0.4.0-alpha.3",
"@theweave/utils": "0.3.0-alpha.2",
"@theweave/group-client": "0.2.0-alpha.2",
"@theweave/group-client": "0.2.0-alpha.4",
"bufferutil": "4.0.8",
"cli-table": "0.3.11",
"commander": "11.1.0",
Expand Down

0 comments on commit 5b459dd

Please sign in to comment.