Skip to content

Commit

Permalink
wip: properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Sep 3, 2024
1 parent 3b18d34 commit 87d926c
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 130 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@
},
"peerDependencies": {
"@openhps/core": ">=0.6.9",
"@openhps/rdf": ">=0.4.48",
"@openhps/rdf": ">=0.4.85",
"reflect-metadata": ">=0.2.1"
},
"devDependencies": {
"@commitlint/cli": "^19.4.1",
"@commitlint/config-conventional": "^19.4.1",
"@comunica/config-query-sparql": "^3.2.1",
"@openhps/core": ">=0.7.10",
"@openhps/rdf": ">=0.4.84",
"@openhps/rdf": ">=0.4.86",
"@types/chai": "^4.3.19",
"@types/cookie-session": "^2.0.49",
"@types/express": "^4.17.21",
Expand Down
36 changes: 24 additions & 12 deletions src/common/SolidPropertyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ export class SolidPropertyService extends DataService<string, any> {
*/
createProperty(session: SolidSession, property: Property): Promise<IriString> {
return new Promise((resolve, reject) => {
const propertyContainer = new URL(property.id);
propertyContainer.hash = '';
// Root dataset for the property
property.id = `${property.id}/property.ttl` as IriString;
this.service
.getDatasetStore(session, session.info.webId)
.then((store) => {
const thing = RDFSerializer.quadsToThing(DataFactory.namedNode(session.info.webId), store);
const builder = RDFBuilder.fromSerialized(thing);
builder.add(ssn.hasProperty, property.id);
builder.add(ssn.hasProperty, `${propertyContainer.href}/property.ttl`);
const changelog = builder.build(true);
let dirty = false;
if (changelog.additions.length > 0) {
Expand All @@ -99,29 +103,36 @@ export class SolidPropertyService extends DataService<string, any> {
}
})
.then(() => {
// Verify if the property container exists
return this.service.getDataset(session, propertyContainer.href);
}).then((dataset) => {
if (dataset) {
resolve(property.id as IriString)
return;
}
// Create a new property container
return this.service.createContainer(session, property.id as IriString);
return this.service.createContainer(session, propertyContainer.href as IriString);
}).then(() => {
// Create a new property dataset
return Promise.all([
this.service.getDatasetStore(session, `${property.id}/root.ttl`),
this.service.getDatasetStore(session, `${property.id}/.meta`),
return Promise.all([
this.service.getDatasetStore(session, `${propertyContainer.href}/property.ttl`),
this.service.getDatasetStore(session, `${propertyContainer.href}/.meta`),
]);
})
.then(([store, meta]) => {
// Add the property
store.addQuads(RDFSerializer.serializeToQuads(property));
// Add the eventstream to the metadata
const eventStreamURL = new URL(`${property.id}/root.ttl`);
const eventStreamURL = new URL(`${propertyContainer.href}/property.ttl`);
eventStreamURL.hash = 'EventStream';
const viewURL = new URL(`${property.id}/root.ttl`);
const viewURL = new URL(`${propertyContainer.href}/property.ttl`);
viewURL.hash = 'root';
const stream = new EventStream(eventStreamURL.href as IriString);
stream.setTimestampPath(sosa.resultTime);
stream.view = new Node(viewURL.href as IriString);
meta.addQuads(RDFSerializer.serializeToQuads(stream));
return this.service.saveDatasetStore(session, `${property.id}/root.ttl`, store)
.then(() => this.service.saveDatasetStore(session, `${property.id}/.meta`, meta));
return this.service.saveDatasetStore(session, `${propertyContainer.href}/property.ttl`, store)
.then(() => this.service.saveDatasetStore(session, `${propertyContainer.href}/.meta`, meta));
})
.then(() => {
resolve(property.id as IriString);
Expand All @@ -134,9 +145,10 @@ export class SolidPropertyService extends DataService<string, any> {
return new Promise((resolve, reject) => {
const nodeURL = new URL(node.id);
nodeURL.hash = '';
this.service.getDatasetStore(session, `${nodeURL.href}.meta`).then(meta => {
const isContainer = !nodeURL.href.endsWith('.ttl');
this.service.getDatasetStore(session, `${nodeURL.href}${isContainer ? '/' : ''}.meta`).then(meta => {
meta.addQuads(RDFSerializer.serializeToQuads(node));
return this.service.saveDatasetStore(session, `${nodeURL.href}.meta`, meta);
return this.service.saveDatasetStore(session, `${nodeURL.href}${isContainer ? '/' : ''}.meta`, meta);
}).then(() => resolve(node)).catch(reject);
});
}
Expand All @@ -151,7 +163,7 @@ export class SolidPropertyService extends DataService<string, any> {
addObservation(session: SolidSession, property: Property, observation: Observation): Promise<void> {
return new Promise((resolve, reject) => {
Promise.all([
this.service.getDatasetStore(session, `${property.id}/root.ttl`),
this.service.getDatasetStore(session, `${property.id}/property.ttl`),
this.service.getDatasetStore(session, `${property.id}/.meta`),
])
.then(async ([store, meta]) => {
Expand Down
49 changes: 45 additions & 4 deletions src/common/SolidService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
deleteContainer,
universalAccess,
getSolidDataset,
getContainedResourceUrlAll,
deleteFile,
} from '@inrupt/solid-client';
import { fetch } from 'cross-fetch';
import {
Expand All @@ -59,6 +61,7 @@ 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 Expand Up @@ -339,6 +342,43 @@ export abstract class SolidService extends RemoteService {
});
}

/**
* Recursively delete a Solid dataset
* @param session Solid session to delete a dataset with
* @param dataset Dataset to delete
*/
async deleteRecursively(session: SolidSession, dataset: SolidDataset & WithResourceInfo): Promise<void>
/**
* Recursively delete a Solid dataset
* @param {SolidSession} session Solid session to delete a dataset with
* @param url URL of the dataset
*/
async deleteRecursively(session: SolidSession, url: IriString): Promise<void>
async deleteRecursively(session: SolidSession, dataset: IriString | SolidDataset & WithResourceInfo): Promise<void> {
if (typeof dataset === 'string') {
const fetchedDataset = await this.getDataset(session, dataset);
return await this.deleteRecursively(session, fetchedDataset as SolidDataset & WithResourceInfo);
}
const containedResourceUrls = getContainedResourceUrlAll(dataset as SolidDataset & WithResourceInfo);
const containedDatasets = await Promise.all(containedResourceUrls.map(async resourceUrl => {
try {
return await getSolidDataset(resourceUrl, session ? { fetch: session.fetch } : undefined);
} catch(e) {
// The Resource might not have been a SolidDataset;
// we can delete it directly:
await deleteFile(resourceUrl, session ? { fetch: session.fetch } : undefined);
return null;
}
}));
await Promise.all(containedDatasets.map(async containedDataset => {
if (containedDataset === null) {
return;
}
return await this.deleteRecursively(session, containedDataset);
}));
return await deleteSolidDataset(dataset, session ? { fetch: session.fetch } : undefined);
}

/**
* Delete a Solid dataset
* @param {SolidSession} session Solid session to get a thing from
Expand Down Expand Up @@ -408,16 +448,17 @@ export abstract class SolidService extends RemoteService {
if (subjectURL.href !== documentURL.href &&
subjectURL.href !== documentURL.href.replace(".meta", "")) {
store.removeQuad(quad);
console.log('Removed quad', quad.subject.value);
}
}
});

const additions = store.additions;
const deletions = store.deletions;

console.log('Additions', additions.map((a) => a.subject.value));
console.log('Deletions', deletions.map((a) => a.subject.value));

if (additions.length === 0 && deletions.length === 0) {
resolve(this.storeToDataset(store));
return;
}

const dummyDataset: SolidDataset & WithChangeLog & Partial<WithResourceInfo> = {
...(this.storeToDataset(store) as SolidDataset),
Expand Down
Loading

0 comments on commit 87d926c

Please sign in to comment.