Skip to content

Commit

Permalink
Fix object is deleted before the Create or Update activity is sent
Browse files Browse the repository at this point in the history
  • Loading branch information
srosset81 committed Jan 17, 2025
1 parent 6085349 commit 2200dcc
Showing 1 changed file with 53 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,54 +68,66 @@ const setRightsHandler = {
// Give read rights to the activity's recipients.
await addReadRights({
ctx,
resourceUri: activityUri,
recipientUris: newRecipients,
skipObjectsWatcher: true,
resourceUri: activityUri,
anon: activityIsPublic
});

// If Create activity, also give rights to the created object.
if (hasType(activity, ACTIVITY_TYPES.CREATE)) {
await addReadRights({
ctx,
resourceUri: objectUri,
recipientUri: newRecipients,
skipObjectsWatcher: true
});
// If Create or Update activity, also give rights to the created object.
if (hasType(activity, ACTIVITY_TYPES.CREATE) || hasType(activity, ACTIVITY_TYPES.UPDATE)) {
try {
await addReadRights({
ctx,
resourceUri: objectUri,
recipientUri: newRecipients,
skipObjectsWatcher: true,
anon: activityIsPublic
});
} catch (e) {
if (e.code === 404) {
// Ignore cases when the object is deleted before the Create or Update activity have been sent
this.logger.warn(`Could not add read rights on object ${objectUri} because it does not exist anymore.`);
} else {
throw e;
}
}
}
// If Update activity, we need to remove read rights of former recipients, and give read rights to new recipients.
else if (hasType(activity, ACTIVITY_TYPES.UPDATE)) {
// const rights = await ctx.call('webacl.resource.getRights', { resourceUri: objectUri });
// const wasPublic = rights?.anon?.read;
const previousRecipients = await ctx.call('webacl.resource.getUsersWithReadRights', {
resourceUri: objectUri
});

// We add rights to all new recipients.
// ~~*And* if the object was private before, we send out a `Create` activity instead,
// by not skipping the objectWatcher that handles this.~~
const addedRecipients = newRecipients.filter(r => !previousRecipients.includes(r));
await addReadRights({
ctx,
resourceUri: objectUri,
recipientUris: addedRecipients,
skipObjectsWatcher: true, // !wasPublic, // TODO: Maybe this should be false...
anon: activityIsPublic
});

// We remove rights from all actors that aren't recipients anymore.
// *And* we do not skip the object-watcher middleware, to send `Delete` activities
// to actors that cannot see the object anymore.
// const removedRecipients = previousRecipients.filter(r => !newRecipients.includes(r));
// if (removedRecipients.length > 0 || !activityIsPublic)
// await removeReadRights({
// ctx,
// resourceUri: objectUri,
// recipientUris: removedRecipients,
// skipObjectsWatcher: false,
// anon: !activityIsPublic
// });
}
// TODO Decide if we keep this special handling of Update activities that can have unexpected results
// If Update activity, we need to remove read rights of former recipients, and give read rights to new recipients.
// else if (hasType(activity, ACTIVITY_TYPES.UPDATE)) {
// // const rights = await ctx.call('webacl.resource.getRights', { resourceUri: objectUri });
// // const wasPublic = rights?.anon?.read;
// const previousRecipients = await ctx.call('webacl.resource.getUsersWithReadRights', {
// resourceUri: objectUri
// });
//
// // We add rights to all new recipients.
// // ~~*And* if the object was private before, we send out a `Create` activity instead,
// // by not skipping the objectWatcher that handles this.~~
// const addedRecipients = newRecipients.filter(r => !previousRecipients.includes(r));
// await addReadRights({
// ctx,
// resourceUri: objectUri,
// recipientUris: addedRecipients,
// skipObjectsWatcher: true, // !wasPublic, // TODO: Maybe this should be false...
// anon: activityIsPublic
// });
//
// // We remove rights from all actors that aren't recipients anymore.
// // *And* we do not skip the object-watcher middleware, to send `Delete` activities
// // to actors that cannot see the object anymore.
// const removedRecipients = previousRecipients.filter(r => !newRecipients.includes(r));
// if (removedRecipients.length > 0 || !activityIsPublic)
// await removeReadRights({
// ctx,
// resourceUri: objectUri,
// recipientUris: removedRecipients,
// skipObjectsWatcher: false,
// anon: !activityIsPublic
// });
// }
}
};

Expand Down

0 comments on commit 2200dcc

Please sign in to comment.