Skip to content

Commit

Permalink
fix: fetching of collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Nov 19, 2024
1 parent 7f8cb95 commit cb57913
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 68 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"@commitlint/config-conventional": "^19.4.1",
"@comunica/config-query-sparql": "^3.2.1",
"@openhps/core": ">=0.7.10",
"@openhps/rdf": ">=0.4.90",
"@openhps/rdf": ">=0.4.92",
"@types/chai": "^4.3.19",
"@types/cookie-session": "^2.0.49",
"@types/express": "^4.17.21",
Expand Down
43 changes: 40 additions & 3 deletions src/common/SolidPropertyService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataService } from '@openhps/core';
import { Property, RDFSerializer, dcterms, rdfs, sosa, ssn, IriString, RDFBuilder, DataFactory } from '@openhps/rdf';
import { Property, RDFSerializer, dcterms, rdfs, sosa, ssn, IriString, RDFBuilder, DataFactory, SerializableThing } from '@openhps/rdf';
import { SolidProfileObject } from './SolidProfileObject';
import { SolidDataDriver, SolidFilterQuery } from './SolidDataDriver';
import { SolidService, SolidSession } from './SolidService';
Expand Down Expand Up @@ -148,7 +148,7 @@ export class SolidPropertyService extends DataService<string, any> {
const nodeURL = new URL(node.id);
nodeURL.hash = '';
const isContainer = !nodeURL.href.endsWith('.ttl');
const datasetURL = `${nodeURL.href}${isContainer ? `${nodeURL.href.endsWith('/') ? '' : '/'}.meta` : ''}`
const datasetURL = `${nodeURL.href}${isContainer ? `${nodeURL.href.endsWith('/') ? '' : '/'}.meta` : ''}`;
this.service.getDatasetStore(session, datasetURL).then(dataset => {
dataset.addQuads(RDFSerializer.serializeToQuads(node));
if (collection) {
Expand All @@ -159,6 +159,30 @@ export class SolidPropertyService extends DataService<string, any> {
});
}

protected fetchTreeNode(session: SolidSession, node: SerializableThing, collection?: IriString): Promise<Node> {
return new Promise((resolve, reject) => {
const nodeURL = new URL(node.id);
nodeURL.hash = '';
const isContainer = !nodeURL.href.endsWith('.ttl');
const datasetURL = `${nodeURL.href}${isContainer ? `${nodeURL.href.endsWith('/') ? '' : '/'}.meta` : ''}`
this.service.getDatasetStore(session, datasetURL).then(dataset => {
const nodeThing = RDFSerializer.quadsToThing(DataFactory.namedNode(node.id), dataset);
if (nodeThing) {
const deserializedNode: Node = RDFSerializer.deserializeFromStore(DataFactory.namedNode(node.id), dataset);
const deserializedCollection: Collection = collection ? RDFSerializer.deserializeFromStore(DataFactory.namedNode(collection), dataset) : undefined;
if (deserializedNode) {
deserializedNode.collection = deserializedCollection;
resolve(deserializedNode);
} else {
reject(new Error('Node could not be deserialized'));
}
} else {
reject(new Error('Node not found'));
}
}).catch(reject);
});
}

/**
* Add an observation to a property
* @param session
Expand Down Expand Up @@ -188,7 +212,20 @@ export class SolidPropertyService extends DataService<string, any> {
// Root node not found
return reject(new Error('Root node not found, but it was in the query result'));
}
let childNode = rootNode.getChildNode(observation.resultTime);
// Child nodes are references and not stored in the dataset
// Fetch each node
for(let relation of rootNode.relations) {
if (!relation.node) {
throw new Error('Node is corrupted. Relation node is missing');
}
const node = await this.fetchTreeNode(session, relation.node, property.id as IriString);
relation.node = node;
}

let childNode = rootNode.getChildNode(observation.resultTime, (node) => {
// Filter false if node has 3 or more children
return node.collection ? node.collection.members.length < 3 : true;
});
if (!childNode) {
// Create node
childNode = new Node();
Expand Down
1 change: 0 additions & 1 deletion src/common/SolidService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import type {
import { StorageUtility } from '@inrupt/solid-client-authn-core';
import { ClientRegistrar } from './ClientRegistrar';
import { SessionManager } from './SessionManager';
import { seeks } from '@openhps/rdf/dist/types/vocab/schema';

class StorageUtilityWrapper extends StorageUtility {
constructor(secureStorage: IStorage, insecureStorage: IStorage) {
Expand Down
Loading

0 comments on commit cb57913

Please sign in to comment.