From 9d990816f8739068df8314efe7951bf209f49cb1 Mon Sep 17 00:00:00 2001 From: Chris Myers Date: Sun, 22 Dec 2024 19:09:50 -0700 Subject: [PATCH] Fix issue with remove owner having the wrong link --- lib/actions/removeOwnedBy.js | 44 ++++++++++++++++++------------- templates/layouts/identified.jade | 2 +- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/actions/removeOwnedBy.js b/lib/actions/removeOwnedBy.js index 030fc79d9..9795a27cd 100644 --- a/lib/actions/removeOwnedBy.js +++ b/lib/actions/removeOwnedBy.js @@ -10,16 +10,15 @@ module.exports = function (req, res) { return getOwnedBy(uri, graphUri).then((ownedBy) => { if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) { - res.status(401).send('not authorized to remove an owner') + return res.status(401).send('Not authorized to remove an owner') } - return retrieveUris(uri, graphUri).then(uris => { + return retrieveUris(uri, graphUri).then((uris) => { let chunks = [] let offset = config.get('resolveBatch') for (let i = 0; i < uris.length; i += offset) { let end = i + offset < uris.length ? i + offset : uris.length - chunks.push(uris.slice(i, end)) } @@ -30,22 +29,29 @@ module.exports = function (req, res) { console.log(sharedRemovalQuery) - return sparql.updateQuery(sharedRemovalQuery, req.body.userUri).then(result => { - return Promise.all(chunks.map(chunk => { - let uris = chunk.map(uri => { - return '<' + uri + '> sbh:ownedBy <' + req.body.userUri + '>' - }).join(' . \n') - - const updateQuery = loadTemplate('./sparql/RemoveOwnedBy.sparql', { - uris: uris - }) - console.log(updateQuery) - - return sparql.updateQuery(updateQuery, graphUri).then(() => { - res.redirect(share) - }) - })) - }) + return sparql.updateQuery(sharedRemovalQuery, req.body.userUri) + .then(() => { + return Promise.all( + chunks.map((chunk) => { + let uris = chunk + .map((uri) => `<${uri}> sbh:ownedBy <${req.body.userUri}>`) + .join(' . \n') + + const updateQuery = loadTemplate('./sparql/RemoveOwnedBy.sparql', { uris }) + console.log(updateQuery) + + return sparql.updateQuery(updateQuery, graphUri) + }) + ) + }) + .then(() => { + // Redirect only once after all promises resolve + res.redirect(share) + }) + .catch((err) => { + console.error('Error:', err) + res.status(500).send(`Error removing ownership: ${err.message}`) + }) }) }) } diff --git a/templates/layouts/identified.jade b/templates/layouts/identified.jade index 4aa0c8072..8f6941a76 100644 --- a/templates/layouts/identified.jade +++ b/templates/layouts/identified.jade @@ -225,7 +225,7 @@ block content span.fa.fa-search if(meta.canEdit && annotation.removeOwner) form.form-inline(style="display: inline" method='POST', action=annotation.removeOwner) - input(type='hidden' name='userUri' value=config.databasePrefix + 'user/' + annotation.value) + input(type='hidden' name='userUri' value=config.databasePrefix + annotation.value) button.fakelink(type='submit') |   span.fa.fa-trash