Skip to content

Commit

Permalink
Merge pull request #1303 from assemblee-virtuelle/persist-only-custom…
Browse files Browse the repository at this point in the history
…-ontologies

OntologiesService: Persist only selected ontologies
  • Loading branch information
srosset81 committed Sep 6, 2024
2 parents eee9724 + 6eabcdf commit 086cdbc
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 273 deletions.
23 changes: 4 additions & 19 deletions src/middleware/packages/activitypub/services/activitypub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const CollectionsRegistryService = require('./subservices/collections-registry')
const ReplyService = require('./subservices/reply');
const SideEffectsService = require('./subservices/side-effects');
const FakeQueueMixin = require('../../mixins/fake-queue');
const { ACTOR_TYPES } = require('../../constants');

const ActivityPubService = {
name: 'activitypub',
Expand All @@ -24,13 +23,7 @@ const ActivityPubService = {
collectionsPath: '/as/collection',
activateTombstones: true,
selectActorData: null,
queueServiceUrl: null,
like: {
attachToActorTypes: null
},
follow: {
attachToActorTypes: null
}
queueServiceUrl: null
},
dependencies: ['api', 'ontologies'],
created() {
Expand All @@ -41,9 +34,7 @@ const ActivityPubService = {
collectionsPath,
selectActorData,
queueServiceUrl,
activateTombstones,
like,
follow
activateTombstones
} = this.settings;

this.broker.createService({
Expand Down Expand Up @@ -139,14 +130,8 @@ const ActivityPubService = {
});
},
async started() {
await this.broker.call('ontologies.register', {
...as,
overwrite: true
});
await this.broker.call('ontologies.register', {
...sec,
overwrite: true
});
await this.broker.call('ontologies.register', as);
await this.broker.call('ontologies.register', sec);
}
};

Expand Down
5 changes: 1 addition & 4 deletions src/middleware/packages/crypto/keys/keys-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ const KeysService = {
},
async started() {
await this.waitForServices('ontologies');
this.broker.call('ontologies.register', {
...sec,
overwrite: true
});
this.broker.call('ontologies.register', sec);

await this.waitForServices('keys.migration');
this.isMigrated = await this.broker.call('keys.migration.isMigrated');
Expand Down
5 changes: 1 addition & 4 deletions src/middleware/packages/ldp/mixins/document-tagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ module.exports = {
}
},
async started() {
await this.broker.call('ontologies.register', {
...dc,
overwrite: true
});
await this.broker.call('ontologies.register', dc);
},
actions: {
async tagCreatedResource(ctx) {
Expand Down
10 changes: 2 additions & 8 deletions src/middleware/packages/ldp/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,9 @@ module.exports = {
}
},
async started() {
await this.broker.call('ontologies.register', {
...ldp,
overwrite: true
});
await this.broker.call('ontologies.register', ldp);
// Used by binaries
await this.broker.call('ontologies.register', {
...semapps,
overwrite: true
});
await this.broker.call('ontologies.register', semapps);
},
actions: {
getBaseUrl() {
Expand Down
25 changes: 7 additions & 18 deletions src/middleware/packages/ontologies/actions/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,14 @@ module.exports = {
let { prefix, namespace, uri } = ctx.params;
let ontology;

if (!prefix && !namespace && !uri) throw new Error('You must provide a prefix, namespace or uri parameter');

if (this.settings.persistRegistry) {
if (prefix) {
ontology = await ctx.call('ontologies.registry.getByPrefix', { prefix });
} else if (namespace) {
ontology = await ctx.call('ontologies.registry.getByNamespace', { namespace });
} else if (uri) {
const ontologies = await ctx.call('ontologies.registry.list');
ontology = ontologies.find(o => uri.startsWith(o.namespace));
}
if (prefix) {
ontology = this.ontologies[prefix] || false;
} else if (namespace) {
ontology = Object.values(this.ontologies).find(o => o.namespace === namespace);
} else if (uri) {
ontology = Object.values(this.ontologies).find(o => uri.startsWith(o.namespace));
} else {
if (prefix) {
ontology = this.ontologies[prefix] || false;
} else if (namespace) {
ontology = Object.values(this.ontologies).find(o => o.namespace === namespace);
} else if (uri) {
ontology = Object.values(this.ontologies).find(o => uri.startsWith(o.namespace));
}
throw new Error('You must provide a prefix, namespace or uri parameter');
}

// Must return null if no results, because the cache JsonSerializer cannot handle undefined values
Expand Down
8 changes: 2 additions & 6 deletions src/middleware/packages/ontologies/actions/list.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module.exports = {
visibility: 'public',
cache: true,
async handler(ctx) {
if (this.settings.persistRegistry) {
return await ctx.call('ontologies.registry.list');
} else {
return Object.values(this.ontologies);
}
handler() {
return Object.values(this.ontologies);
}
};
26 changes: 13 additions & 13 deletions src/middleware/packages/ontologies/actions/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ module.exports = {
optional: true
},
preserveContextUri: { type: 'boolean', default: false },
overwrite: { type: 'boolean', default: false }
persist: { type: 'boolean', default: false }
},
async handler(ctx) {
let { prefix, namespace, owl, jsonldContext, preserveContextUri, overwrite } = ctx.params;

const ontology = await this.actions.get({ prefix }, { parentCtx: ctx });

if (!overwrite && ontology) {
throw new Error(`Cannot register ${prefix} ontology. An ontology with the prefix is already registered`);
}
let { prefix, namespace, owl, jsonldContext, preserveContextUri, persist } = ctx.params;

if (preserveContextUri === true) {
if (!jsonldContext || !arrayOf(jsonldContext).every(context => isURL(context))) {
Expand All @@ -31,14 +25,20 @@ module.exports = {
}
}

if (this.settings.persistRegistry) {
if (persist) {
if (!this.settings.persistRegistry)
throw new Error(`Cannot persist ontology because the persistRegistry setting is false`);
if (owl || jsonldContext) throw new Error(`The owl and jsonldContext params cannot be persisted`);

await ctx.call('ontologies.registry.updateOrCreate', {
prefix,
namespace,
owl,
jsonldContext,
preserveContextUri
namespace
});

this.ontologies[prefix] = {
prefix,
namespace
};
} else {
this.ontologies[prefix] = {
prefix,
Expand Down
14 changes: 7 additions & 7 deletions src/middleware/packages/ontologies/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ module.exports = {
}
},
async started() {
if (!this.settings.persistRegistry) this.ontologies = {};

this.ontologies = {};
await this.registerAll();
},
actions: {
Expand All @@ -42,12 +41,13 @@ module.exports = {
},
methods: {
async registerAll() {
if (this.settings.persistRegistry) await this.broker.waitForServices(['ontologies.registry']);
if (this.settings.persistRegistry) {
await this.broker.waitForServices(['ontologies.registry']);
const persistedOntologies = await this.broker.call('ontologies.registry.list');
this.ontologies = { ...this.ontologies, ...persistedOntologies };
}
for (const ontology of this.settings.ontologies) {
if (!ontology || !ontology.prefix || !ontology.namespace) {
throw new Error(`Cannot register ontologies ! At least one ontology is incorrect.`);
}
await this.actions.register({ ...ontology, overwrite: true });
await this.actions.register(ontology);
}
}
}
Expand Down
39 changes: 6 additions & 33 deletions src/middleware/packages/ontologies/sub-services/registry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const DbService = require('moleculer-db');
const { TripleStoreAdapter } = require('@semapps/triplestore');
const { isURL } = require('../utils');

module.exports = {
name: 'ontologies.registry',
Expand All @@ -13,59 +12,33 @@ module.exports = {
async getByPrefix(ctx) {
const { prefix } = ctx.params;
const [ontology] = await this._find(ctx, { query: { prefix } });
return this.parse(ontology);
return ontology && { prefix: ontology.prefix, namespace: ontology.namespace };
},
async getByNamespace(ctx) {
const { namespace } = ctx.params;
const [ontology] = await this._find(ctx, { query: { namespace } });
return this.parse(ontology);
return ontology && { prefix: ontology.prefix, namespace: ontology.namespace };
},
async list(ctx) {
const ontologies = await this._list(ctx, {});
return ontologies.rows.map(({ prefix, namespace, owl, jsonldContext, preserveContextUri }) => {
return this.parse({ prefix, namespace, owl, jsonldContext, preserveContextUri });
});
return ontologies.rows.map(({ prefix, namespace }) => ({ prefix, namespace }));
},
async updateOrCreate(ctx) {
let { prefix, namespace, owl, jsonldContext, preserveContextUri } = ctx.params;
const { prefix, namespace } = ctx.params;

const ontology = await this.actions.getByPrefix({ prefix }, { parentCtx: ctx });

if (!isURL(jsonldContext)) jsonldContext = JSON.stringify(jsonldContext);

if (ontology) {
await this._update(ctx, {
'@id': ontology['@id'],
namespace,
owl,
jsonldContext,
preserveContextUri
namespace
});
} else {
await this._create(ctx, {
prefix,
namespace,
owl,
jsonldContext,
preserveContextUri
namespace
});
}
}
},
methods: {
parse(ontology) {
// If the jsonldContext is not an URL, it is an object to be parsed
if (ontology?.jsonldContext && !isURL(ontology.jsonldContext)) {
ontology.jsonldContext = JSON.parse(ontology.jsonldContext);
}

if (ontology?.preserveContextUri === 'true') {
ontology.preserveContextUri = true;
} else if (ontology?.preserveContextUri === 'false') {
ontology.preserveContextUri = false;
}

return ontology;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ module.exports = {
async started() {
if (!this.settings.baseUrl) throw new Error(`The baseUrl setting is required`);

await this.broker.call('ontologies.register', {
...notify,
overwrite: true
});
await this.broker.call('ontologies.register', notify);

// Retrieve all active listeners
const results = await this.actions.list({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ module.exports = {
}
},
async started() {
await this.broker.call('ontologies.register', {
...notify,
overwrite: true
});
await this.broker.call('ontologies.register', notify);

await this.broker.call('ldp.link-header.register', { actionName: 'solid-notifications.provider.getLink' });

Expand Down
5 changes: 1 addition & 4 deletions src/middleware/packages/solid/services/pod.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ module.exports = {
async started() {
if (!this.settings.baseUrl) throw new Error('The baseUrl setting of the pod service is required');

await this.broker.call('ontologies.register', {
...pim,
overwrite: true
});
await this.broker.call('ontologies.register', pim);

// Register root container for the Pod (/:username/data/)
// Do not await or we will have a circular dependency with the LdpRegistryService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ module.exports = {
});
},
async started() {
await this.broker.call('ontologies.register', {
...solid,
overwrite: true
});
await this.broker.call('ontologies.register', solid);
},
actions: {
async createAndAttachToWebId(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/packages/triplestore/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const TripleStoreService = {
// Sub-services customization
dataset: {}
},
dependencies: ['jsonld'],
dependencies: ['jsonld.parser'],
async created() {
const { url, user, password, dataset, fusekiBase } = this.settings;
this.subservices = {};
Expand Down
5 changes: 1 addition & 4 deletions src/middleware/packages/void/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ module.exports = {
},
dependencies: ['ldp.registry', 'api', 'triplestore', 'ontologies', 'jsonld'],
async started() {
await this.broker.call('ontologies.register', {
...voidOntology,
overwrite: true
});
await this.broker.call('ontologies.register', voidOntology);
await this.broker.call('api.addRoute', {
route: {
path: '/.well-known/void', // .well-known routes must use the root path
Expand Down
15 changes: 3 additions & 12 deletions src/middleware/packages/webacl/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,8 @@ module.exports = {
await this.broker.call('api.addRoute', { route });
}

await this.broker.call('ontologies.register', {
...acl,
overwrite: true
});
await this.broker.call('ontologies.register', {
...vcard,
overwrite: true
});
await this.broker.call('ontologies.register', {
...rdfs,
overwrite: true
});
await this.broker.call('ontologies.register', acl);
await this.broker.call('ontologies.register', vcard);
await this.broker.call('ontologies.register', rdfs);
}
};
10 changes: 2 additions & 8 deletions src/middleware/packages/webid/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ const WebIdService = {
if (!this.settings.baseUrl) throw new Error('The baseUrl setting is required for webId service.');
},
async started() {
await this.broker.call('ontologies.register', {
...foaf,
overwrite: true
});
await this.broker.call('ontologies.register', {
...schema,
overwrite: true
});
await this.broker.call('ontologies.register', foaf);
await this.broker.call('ontologies.register', schema);
},
actions: {
/**
Expand Down
Loading

0 comments on commit 086cdbc

Please sign in to comment.