Skip to content

Commit

Permalink
In Pod provider config, use TypeRegistration to find containers URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
srosset81 committed Jul 2, 2024
1 parent 5e097bf commit 5c33a75
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { MIME_TYPES } = require('@semapps/mime-types');
const { getSlugFromUri } = require('@semapps/ldp');
const { OBJECT_TYPES, ACTIVITY_TYPES, ACTOR_TYPES } = require('../../../constants');
const { OBJECT_TYPES, ACTIVITY_TYPES } = require('../../../constants');

const ObjectService = {
name: 'activitypub.object',
Expand Down Expand Up @@ -52,20 +51,37 @@ const ObjectService = {
context: activity['@context']
});

// Get the first matching container
// TODO: attach to all matching containers
const container = await ctx.call('ldp.registry.getByType', {
type: types,
dataset: this.settings.podProvider ? getSlugFromUri(actorUri) : undefined
});
let container, containerUri;

if (this.settings.podProvider) {
// If this is a Pod provider, find the container with the type-registrations service
for (const type of types) {
const containersUris = await ctx.call('type-registrations.findContainersUris', {
type,
webId: actorUri
});
if (containersUris.length > 0) {
containerUri = containersUris[0];
continue;
}
}
} else {
// Otherwise try to find it with the LdpRegistry
container = await ctx.call('ldp.registry.getByType', { type: types });
if (container) {
containerUri = await ctx.call('ldp.registry.getUri', {
path: container.path,
webId: actorUri
});
}
}

if (!container)
if (!containerUri)
throw new Error(`Cannot create resource of type "${types.join(', ')}", no matching containers were found!`);

const containerUri = await ctx.call('ldp.registry.getUri', { path: container.path, webId: actorUri });

objectUri = await ctx.call(
container.controlledActions?.post || 'ldp.container.post',
container?.controlledActions?.post || 'ldp.container.post',
{
containerUri,
resource: activity.object,
Expand Down
30 changes: 20 additions & 10 deletions src/middleware/packages/sync/services/synchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,27 @@ const SynchronizerService = {
}

if (this.settings.attachToLocalContainers) {
const container = await ctx.call('ldp.registry.getByType', { type });
if (container) {
try {
const containerUri = await ctx.call('ldp.registry.getUri', {
let containerUri;

if (this.settings.podProvider) {
// If this is a Pod provider, try to find the container with the type-registrations service
[containerUri] = await ctx.call('type-registrations.findContainersUris', {
type,
webId: recipientUri
});
} else {
// Otherwise try to find it with the LdpRegistry
const container = await ctx.call('ldp.registry.getByType', { type });
if (container) {
containerUri = await ctx.call('ldp.registry.getUri', {
path: container.path,
webId: recipientUri
});
await ctx.call('ldp.container.attach', { containerUri, resourceUri, webId: recipientUri });
} catch (e) {
this.logger.warn(
`Error when attaching remote resource ${resourceUri} to local container: ${e.message}`
);
}
}

if (containerUri) {
await ctx.call('ldp.container.attach', { containerUri, resourceUri, webId: recipientUri });
} else {
this.logger.warn(
`Cannot attach resource ${resourceUri} of type "${type}", no matching local containers were found`
Expand Down Expand Up @@ -140,7 +148,9 @@ const SynchronizerService = {
webId: recipientUri
});
} catch (e) {
this.logger.warn(`Remote resource ${resourceUri} not deleted as it was not found on local dataset`);
this.logger.warn(
`Remote resource ${resourceUri} not deleted as it was not found on local dataset. Error ${e.message}`
);
}

if (activity.target && this.settings.synchronizeContainers) {
Expand Down

0 comments on commit 5c33a75

Please sign in to comment.