Skip to content

Commit

Permalink
Fix issues with adding and removing owners
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmyers committed Dec 23, 2024
1 parent 9d99081 commit 1a384be
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
18 changes: 17 additions & 1 deletion lib/actions/addOwnedBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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) => {
Expand Down
8 changes: 6 additions & 2 deletions lib/actions/removeOwnedBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 16 additions & 10 deletions lib/views/addOwner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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))
})
}

0 comments on commit 1a384be

Please sign in to comment.