diff options
Diffstat (limited to 'bucky/app')
| -rw-r--r-- | bucky/app/bucky.js | 53 | ||||
| -rw-r--r-- | bucky/app/router.js | 8 |
2 files changed, 50 insertions, 11 deletions
diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js index 9d8d0a7..15487e2 100644 --- a/bucky/app/bucky.js +++ b/bucky/app/bucky.js @@ -180,7 +180,38 @@ var bucky = module.exports = { res.thread.set('lastmodified', util.now()) res.thread.save().then( () => next() ) }, - + destroyThread: function (req, res, next) { + console.log(">>> destroying thread", res.thread.get('id')) + var commentPromises = res.comments.map((comment) => { + return comment.destroy() + }) + var s3client = upload.client() + var rmPromises = res.files.map((file) => { + return new Promise ((resolve, reject) => { + var thread_id = file.get('thread') + if (! thread_id || ! file.filename) { + console.log("weird malformed file?", file) + return resolve() + } + var filePath = '/bucky/' + thread_id + '/' + file.get('filename') + s3client.deleteFile(filePath, function(err, res){ + // check `err`, then do `res.pipe(..)` or `res.resume()` or whatever. + resolve() + }) + }) + }) + var filePromises = res.files.map((file) => { + return file.destroy() + }) + var threadPromise = res.thread.destroy() + var promises = [ threadPromise ].concat(commentPromises).concat(filePromises) + Promise.all(promises).then( () => { + next() + }).catch( () => { + res.sendStatus(500) + }) + }, + /* KEYWORDS */ ensureKeyword: function (req, res, next){ @@ -248,12 +279,6 @@ var bucky = module.exports = { } }) }, - checkCommentPrivacy: function(req, res, next) { - if (req.user.get('ulevel') !== 3 && req.user.get('username') !== res.comment.get('username')) { - return res.sendStatus(500) - } - next() - }, createOptionalComment: function(req, res, next){ if (! req.body.comment || ! req.body.comment.length) { return next() @@ -363,6 +388,20 @@ var bucky = module.exports = { }) }, + /* PRIVACY */ + checkThreadPrivacy: function(req, res, next) { + if (req.user.get('ulevel') !== 3 && req.user.get('username') !== res.thread.get('username')) { + return res.sendStatus(500) + } + next() + }, + checkCommentPrivacy: function(req, res, next) { + if (req.user.get('ulevel') !== 3 && req.user.get('username') !== res.comment.get('username')) { + return res.sendStatus(500) + } + next() + }, + /* MAIL */ ensureMailboxes: function (req, res, next){ diff --git a/bucky/app/router.js b/bucky/app/router.js index fc5c7ea..8eb9142 100644 --- a/bucky/app/router.js +++ b/bucky/app/router.js @@ -166,15 +166,15 @@ module.exports = function(app){ bucky.checkCommentPrivacy, bucky.destroyComment, function(req, res){ - res.send(200) + res.sendStatus(200) }) app.delete("/api/thread/:id", middleware.ensureAuthenticated, bucky.ensureThread, -// bucky.destroyThread, + bucky.checkThreadPrivacy, + bucky.destroyThread, function(req, res){ - // delete a thread - res.send(200) + res.sendStatus(200) }) app.get("/search/", |
