diff --git a/models/comment.js b/models/comment.js index 8eab6aff4..492c06e9c 100644 --- a/models/comment.js +++ b/models/comment.js @@ -104,14 +104,14 @@ CommentSchema.statics.findByStatusByActionType = function(status, action_type) { return Action .findCommentsIdByActionType(action_type, 'comment') .then((actions) => { - + return Comment.find({ - 'status': status, + 'status': status, 'id': { '$in': actions.map(a => { return a.item_id; }) - } + } }); }); @@ -188,6 +188,21 @@ CommentSchema.statics.removeById = function(id) { return Comment.remove({'id': id}); }; +/** + * Remove an action from the comment. + * @param {String} id identifier of the comment (uuid) + * @param {String} action_type the type of the action to be removed + * @param {String} user_id the id of the user performing the action +*/ +CommentSchema.statics.removeAction = function(id, user_id, action_type) { + return Action.remove({ + action_type: action_type, + item_type: 'comment', + item_id: id, + user_id: user_id + }); +}; + const Comment = mongoose.model('Comment', CommentSchema); module.exports = Comment; diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 5126bf93c..525dc87e6 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -54,7 +54,7 @@ router.get('/status/rejected', (req, res, next) => { // Pre-moderation: New comments are shown in the moderator queues immediately. // Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users. router.get('/status/pending', (req, res, next) => { - + Setting .getModerationSetting() .then(({moderation}) => { @@ -76,9 +76,9 @@ router.get('/status/pending', (req, res, next) => { //============================================================================== router.post('/', (req, res, next) => { - + const {body, author_id, asset_id, parent_id, status, username} = req.body; - + Comment .new(body, author_id, asset_id, parent_id, status, username) .then((comment) => { @@ -109,7 +109,7 @@ router.post('/:comment_id', (req, res, next) => { }); router.post('/:comment_id/status', (req, res, next) => { - + Comment .changeStatus(req.params.comment_id, req.body.status) .then(comment => res.status(200).send(comment)) @@ -143,4 +143,15 @@ router.delete('/:comment_id', (req, res, next) => { }); }); +router.delete('/:comment_id/actions', (req, res, next) => { + Comment + .removeAction(req.params.comment_id, req.body.user_id, req.body.action_type) + .then(() => { + res.status(201).send('OK. Removed'); + }) + .catch(error => { + next(error); + }); +}); + module.exports = router; diff --git a/tests/models/comment.js b/tests/models/comment.js index 94e45625a..f8dd7f9f5 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -134,4 +134,15 @@ describe('Comment: models', () => { // }); // }); }); + + describe('#removeAction', () => { + it('should remove an action', () => { + return Comment.removeAction('3', '123', 'flag').then(() => { + return Action.findByItemIdArray(['123']); + }) + .then((actions) => { + expect(actions.length).to.equal(0); + }); + }); + }); });