diff --git a/lib/actions/addOwnedBy.js b/lib/actions/addOwnedBy.js index b4c75671..47024dce 100644 --- a/lib/actions/addOwnedBy.js +++ b/lib/actions/addOwnedBy.js @@ -4,10 +4,20 @@ const config = require('../config') const getOwnedBy = require('../query/ownedBy') const retrieveUris = require('../retrieveUris') const getUrisFromReq = require('../getUrisFromReq') +const db = require('../db') -module.exports = function (req, res) { +module.exports = async function (req, res) { const userUri = req.body.user + var userId = '' + if (req.body.user.lastIndexOf('/') >= 0) { + userId = req.body.user.substring(req.body.user.lastIndexOf('/') + 1) + } + + const user = await db.model.User.findOne({ + where: db.sequelize.or({ email: userId }, { username: userId }) + }) + const { graphUri, uri } = getUrisFromReq(req, res) const sharedAdditionQuery = loadTemplate('./sparql/AddToSharedCollection.sparql', { @@ -23,6 +33,12 @@ module.exports = function (req, res) { }) } + if (!user) { + return new Promise(function (resolve, reject) { + reject(new Error('user ' + userId + ' not recognized')) + }) + } + return sparql.updateQuery(sharedAdditionQuery, userUri).then(() => { return retrieveUris(uri, graphUri) }).then((uris) => { diff --git a/lib/actions/removeOwnedBy.js b/lib/actions/removeOwnedBy.js index 9795a27c..253fa4ec 100644 --- a/lib/actions/removeOwnedBy.js +++ b/lib/actions/removeOwnedBy.js @@ -6,7 +6,7 @@ const loadTemplate = require('../loadTemplate') const getOwnedBy = require('../query/ownedBy') module.exports = function (req, res) { - const { graphUri, uri, share } = getUrisFromReq(req, res) + const { graphUri, uri, url } = getUrisFromReq(req, res) return getOwnedBy(uri, graphUri).then((ownedBy) => { if (ownedBy.indexOf(config.get('databasePrefix') + 'user/' + req.user.username) === -1) { @@ -46,7 +46,11 @@ module.exports = function (req, res) { }) .then(() => { // Redirect only once after all promises resolve - res.redirect(share) + if (!req.accepts('text/html')) { + res.status(200).send('Success') + } else { + res.redirect(url) + } }) .catch((err) => { console.error('Error:', err) diff --git a/lib/views/addOwner.js b/lib/views/addOwner.js index 51ef4e2c..9508582d 100644 --- a/lib/views/addOwner.js +++ b/lib/views/addOwner.js @@ -14,9 +14,7 @@ module.exports = function (req, res) { function view (req, res) { const { - graphUri, - uri, - designId + uri } = getUrisFromReq(req, res) db.model.User.findAll().then(users => { @@ -33,14 +31,22 @@ function view (req, res) { function post (req, res) { addOwnedBy(req, res).then(() => { - res.redirect(req.originalUrl.replace('/addOwner', '')) + if (!req.accepts('text/html')) { + res.status(200).send('Success') + } else { + res.redirect(req.originalUrl.replace('/addOwner', '')) + } }, function (err) { - const locals = { - config: config.get(), - section: 'errors', - user: req.user, - errors: [ err ] + if (!req.accepts('text/html')) { + res.status(400).send(err.message) + } else { + const locals = { + config: config.get(), + section: 'errors', + user: req.user, + errors: [ err ] + } + res.send(pug.renderFile('templates/views/errors/errors.jade', locals)) } - res.send(pug.renderFile('templates/views/errors/errors.jade', locals)) }) }