From 626687f05b92115d44e470e6c25650f874fbc13e Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 14:18:43 -0800 Subject: [PATCH 1/6] Adds moderations queues. --- routes/api/comments/index.js | 57 +++++++++++++++++++++---- tests/routes/api/comments/index.js | 68 ++++++++++++++++++++++++++++++ tests/routes/api/stream/index.js | 1 - 3 files changed, 116 insertions(+), 10 deletions(-) diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 3acca5b3e..4753b0355 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -1,10 +1,11 @@ const express = require('express'); const Comment = require('../../../models/comment'); +const Action = require('../../../models/action'); const router = express.Router(); //============================================================================== -// Routes +// Get Routes //============================================================================== router.get('/', (req, res, next) => { @@ -23,6 +24,40 @@ router.get('/:comment_id', (req, res, next) => { }); }); +//============================================================================== +// Moderation Queues Routes +//============================================================================== + +router.get('/action/:action_type', (req, res, next) => { + Action.find({'action_type': req.params.action_type, 'item_type': 'comment'}).then((actions) => { + // search all the comments that are in actions by id: item_id + // populate user by user_id + res.status(200).json(actions); + }).catch(error => { + next(error); + }); +}); + +router.get('/status/rejected', (req, res, next) => { + Comment.find({'status': 'rejected'}).then((comments) => { + res.status(200).json(comments); + }).catch(error => { + next(error); + }); +}); + +router.get('/status/pending', (req, res, next) => { + Comment.find({'status': ''}).then((comments) => { + res.status(200).json(comments); + }).catch(error => { + next(error); + }); +}); + +//============================================================================== +// Post Routes +//============================================================================== + router.post('/', (req, res, next) => { const {body, author_id, asset_id, parent_id, status} = req.body; let comment = new Comment({body, author_id, asset_id, parent_id, status}); @@ -48,14 +83,6 @@ router.post('/:comment_id', (req, res, next) => { }); }); -router.delete('/:comment_id', (req, res, next) => { - Comment.remove(req.params.comment_id).then(() => { - res.status(201).send('OK. Deleted'); - }).catch(error => { - next(error); - }); -}); - router.post('/:comment_id/status', (req, res, next) => { Comment.changeStatus(req.params.comment_id, req.body.status).then((comment) => { res.status(200).send(comment); @@ -72,4 +99,16 @@ router.post('/:comment_id/actions', (req, res, next) => { }); }); +//============================================================================== +// Delete Routes +//============================================================================== + +router.delete('/:comment_id', (req, res, next) => { + Comment.remove(req.params.comment_id).then(() => { + res.status(201).send('OK. Deleted'); + }).catch(error => { + next(error); + }); +}); + module.exports = router; diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 4fbe52e94..6416d1412 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -66,6 +66,74 @@ describe('Get /comments', () => { }); }); +describe('Get moderation queues rejected, pending', () => { + const comments = [{ + id: 'abc', + body: 'comment 10', + asset_id: 'asset', + author_id: '123', + status: 'rejected' + }, { + id: 'def', + body: 'comment 20', + asset_id: 'asset', + author_id: '456' + }, { + id: 'hij', + body: 'comment 30', + asset_id: '456', + status: 'accepted' + }]; + + const users = [{ + id: '123', + display_name: 'Ana', + }, { + id: '456', + display_name: 'Maria', + }]; + + const actions = [{ + action_type: 'flag', + item_id: 'abc' + }, { + action_type: 'like', + item_id: 'hij' + }]; + + beforeEach(() => { + return Comment.create(comments).then(() => { + return User.create(users); + }).then(() => { + return Action.create(actions); + }); + }); + + it('should return all the rejected comments', function(done){ + chai.request(app) + .get('/api/v1/comments/status/rejected') + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + expect(res.body[0]).to.have.property('id'); + expect(res.body[0].id).to.equal('abc'); + done(); + }); + }); + + it('should return all the pending comments', function(done){ + chai.request(app) + .get('/api/v1/comments/status/pending') + .end(function(err, res){ + expect(err).to.be.null; + expect(res).to.have.status(200); + expect(res.body[0]).to.have.property('id'); + expect(res.body[0].id).to.equal('def'); + done(); + }); + }); +}); + describe('Post /comments', () => { const users = [{ id: '123', diff --git a/tests/routes/api/stream/index.js b/tests/routes/api/stream/index.js index 9744abb0c..b1348e88a 100644 --- a/tests/routes/api/stream/index.js +++ b/tests/routes/api/stream/index.js @@ -60,7 +60,6 @@ describe('api/stream: routes', () => { .end(function(err, res){ expect(err).to.be.null; expect(res).to.have.status(200); - if (err) {return done(err);} done(); }); }); From 0c04e765d683e97991845ff8fdce5e39f4887645 Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 15:10:56 -0800 Subject: [PATCH 2/6] Moderation queue on flagged comments. --- routes/api/comments/index.js | 6 +++--- tests/routes/api/comments/index.js | 27 ++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index f2166d34e..e1f3ab9c9 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -30,9 +30,9 @@ router.get('/:comment_id', (req, res, next) => { router.get('/action/:action_type', (req, res, next) => { Action.find({'action_type': req.params.action_type, 'item_type': 'comment'}).then((actions) => { - // search all the comments that are in actions by id: item_id - // populate user by user_id - res.status(200).json(actions); + Comment.find({'id': {'$in': actions.map(function(a){return a.item_id;})}}).exec(function(err, comments){ + res.status(200).json(comments); + }); }).catch(error => { next(error); }); diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 6416d1412..9b43a97c4 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -66,7 +66,7 @@ describe('Get /comments', () => { }); }); -describe('Get moderation queues rejected, pending', () => { +describe('Get moderation queues rejected, pending, flags', () => { const comments = [{ id: 'abc', body: 'comment 10', @@ -95,10 +95,12 @@ describe('Get moderation queues rejected, pending', () => { const actions = [{ action_type: 'flag', - item_id: 'abc' + item_id: 'abc', + item_type: 'comment' }, { action_type: 'like', - item_id: 'hij' + item_id: 'hij', + item_type: 'comment' }]; beforeEach(() => { @@ -132,6 +134,19 @@ describe('Get moderation queues rejected, pending', () => { done(); }); }); + + it('should return all the flagged comments', function(done){ + chai.request(app) + .get('/api/v1/comments/action/flag') + .end(function(err, res){ + expect(res).to.have.status(200); + expect(err).to.be.null; + expect(res.body.length).to.equal(1); + expect(res.body[0]).to.have.property('id'); + expect(res.body[0].id).to.equal('abc'); + done(); + }); + }); }); describe('Post /comments', () => { @@ -196,10 +211,12 @@ describe('Get /:comment_id', () => { const actions = [{ action_type: 'flag', - item_id: 'abc' + item_id: 'abc', + item_type: 'comment' }, { action_type: 'like', - item_id: 'hij' + item_id: 'hij', + item_type: 'comment' }]; beforeEach(() => { From c8c82fd03b21bbcd4f44b5d331eea2bef850d3c8 Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 18:10:17 -0800 Subject: [PATCH 3/6] Move all DB functions into models. Clean up tests. --- models/action.js | 28 +++++++++++++++- models/comment.js | 52 ++++++++++++++++++++++++++++++ routes/api/comments/index.js | 27 +++++++++------- tests/routes/api/comments/index.js | 22 ++++++------- 4 files changed, 103 insertions(+), 26 deletions(-) diff --git a/models/action.js b/models/action.js index 27c63747a..1d75c483c 100644 --- a/models/action.js +++ b/models/action.js @@ -28,7 +28,7 @@ ActionSchema.statics.findById = function(id) { }; /** - * Finds users in an array of ids. + * Finds actions in an array of ids. * @param {String} ids array of user identifiers (uuid) */ ActionSchema.statics.findByItemIdArray = function(item_ids) { @@ -37,6 +37,32 @@ ActionSchema.statics.findByItemIdArray = function(item_ids) { }); }; +/** + * Finds all comments for a specific action. + * @param {String} action_type type of action + * @param {String} item_type type of item the action is on +*/ +ActionSchema.statics.findByType = function(action_type, item_type) { + return Action.find({ + 'action_type': action_type, + 'item_type': item_type + }); +}; + +/** + * Finds all comments ids for a specific action. + * @param {String} action_type type of action + * @param {String} item_type type of item the action is on +*/ +ActionSchema.statics.findCommentsIdByActionType = function(action_type, item_type) { + return Action.find({ + 'action_type': action_type, + 'item_type': item_type + }, + 'item_id' + ); +}; + const Action = mongoose.model('Action', ActionSchema); module.exports = Action; diff --git a/models/comment.js b/models/comment.js index 6ddc3b0ee..3aad5d978 100644 --- a/models/comment.js +++ b/models/comment.js @@ -31,6 +31,23 @@ const CommentSchema = new Schema({ } }); +//============================================================================== +// New Statics +//============================================================================== + +/** + * Create a comment. + * @param {String} body content of comment +*/ +CommentSchema.statics.new = function(body, author_id, asset_id, parent_id, status, username) { + let comment = new Comment({body, author_id, asset_id, parent_id, status, username}); + return comment.save(); +}; + +//============================================================================== +// Find Statics +//============================================================================== + /** * Finds a comment by the id. * @param {String} id identifier of comment (uuid) @@ -47,6 +64,28 @@ CommentSchema.statics.findByAssetId = function(asset_id) { return Comment.find({asset_id}); }; +/** + * Find comments by an action that was performed on them. + * @param {String} action_type the type of action that was performed on the comment +*/ +CommentSchema.statics.findByActionType = function(action_type) { + return Action.findCommentsIdByActionType(action_type, 'comment').then((actions) => { + return Comment.find({'id': {'$in': actions.map(function(a){return a.item_id;})}}); + }); +}; + +/** + * Find comments by their status. + * @param {String} status the status to search for +*/ +CommentSchema.statics.findByStatus = function(status) { + return Comment.find({'status': status}); +}; + +//============================================================================== +// Update Statics +//============================================================================== + /** * Change the status of a comment. * @param {String} id identifier of the comment (uuid) @@ -72,6 +111,19 @@ CommentSchema.statics.addAction = function(id, user_id, action_type) { return action.save(); }; +//============================================================================== +// Remove Statics +//============================================================================== + +/** + * Change the status of a comment. + * @param {String} id identifier of the comment (uuid) + * @param {String} status the new status of the comment +*/ +CommentSchema.statics.removeById = function(id) { + return Comment.remove({'id': 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 e1f3ab9c9..b3e2bdee7 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -1,6 +1,5 @@ const express = require('express'); const Comment = require('../../../models/comment'); -const Action = require('../../../models/action'); const router = express.Router(); @@ -29,17 +28,15 @@ router.get('/:comment_id', (req, res, next) => { //============================================================================== router.get('/action/:action_type', (req, res, next) => { - Action.find({'action_type': req.params.action_type, 'item_type': 'comment'}).then((actions) => { - Comment.find({'id': {'$in': actions.map(function(a){return a.item_id;})}}).exec(function(err, comments){ - res.status(200).json(comments); - }); + Comment.findByActionType(req.params.action_type).then((comments) => { + res.status(200).json(comments); }).catch(error => { next(error); }); }); router.get('/status/rejected', (req, res, next) => { - Comment.find({'status': 'rejected'}).then((comments) => { + Comment.findByStatus('rejected').then((comments) => { res.status(200).json(comments); }).catch(error => { next(error); @@ -47,7 +44,7 @@ router.get('/status/rejected', (req, res, next) => { }); router.get('/status/pending', (req, res, next) => { - Comment.find({'status': ''}).then((comments) => { + Comment.findByStatus('').then((comments) => { res.status(200).json(comments); }).catch(error => { next(error); @@ -60,12 +57,18 @@ router.get('/status/pending', (req, res, next) => { router.post('/', (req, res, next) => { const {body, author_id, asset_id, parent_id, status, username} = req.body; - let comment = new Comment({body, author_id, asset_id, parent_id, status, username}); - comment.save().then(({id}) => { - res.status(200).send({'id': id}); + Comment.new(body, author_id, asset_id, parent_id, status, username).then((comment) => { + res.status(200).send({'id': comment.id}); }).catch(error => { next(error); }); + + // let comment = new Comment({body, author_id, asset_id, parent_id, status, username}); + // comment.save().then(({id}) => { + // res.status(200).send({'id': id}); + // }).catch(error => { + // next(error); + // }); }); router.post('/:comment_id', (req, res, next) => { @@ -104,8 +107,8 @@ router.post('/:comment_id/actions', (req, res, next) => { //============================================================================== router.delete('/:comment_id', (req, res, next) => { - Comment.remove(req.params.comment_id).then(() => { - res.status(201).send('OK. Deleted'); + Comment.removeById(req.params.comment_id).then(() => { + res.status(201).send('OK. Removed'); }).catch(error => { next(error); }); diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 9b43a97c4..eaf32adee 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -297,7 +297,7 @@ describe('Put /:comment_id', () => { }); }); -describe('Delete /:comment_id', () => { +describe('Remove /:comment_id', () => { const comments = [{ id: 'abc', @@ -343,9 +343,10 @@ describe('Delete /:comment_id', () => { chai.request(app) .delete('/api/v1/comments/abc') .end(function(err, res){ + expect(err).to.be.null; expect(res).to.have.status(201); - Comment.findById({'id': 'abc'}).then((comment) => { - expect(comment).to.be.null; + Comment.findById('abc').then((comment) => { + expect(comment).to.be.empty; }); done(); }); @@ -405,8 +406,7 @@ describe('Post /:comment_id/status', () => { expect(err).to.be.null; expect(res).to.have.status(200); expect(res).to.have.body; - expect(res.body).to.have.property('status'); - expect(res.body.status).to.equal('accepted'); + expect(res.body).to.have.property('status', 'accepted'); done(); }); }); @@ -465,14 +465,10 @@ describe('Post /:comment_id/actions', () => { expect(err).to.be.null; expect(res).to.have.status(200); expect(res).to.have.body; - expect(res.body).to.have.property('item_type'); - expect(res.body.item_type).to.equal('comment'); - expect(res.body).to.have.property('action_type'); - expect(res.body.action_type).to.equal('flag'); - expect(res.body).to.have.property('item_id'); - expect(res.body.item_id).to.equal('abc'); - expect(res.body).to.have.property('user_id'); - expect(res.body.user_id).to.equal('456'); + expect(res.body).to.have.property('item_type', 'comment'); + expect(res.body).to.have.property('action_type', 'flag'); + expect(res.body).to.have.property('item_id', 'abc'); + expect(res.body).to.have.property('user_id', '456'); done(); }); }); From 588a8de489869b08ad3c198539363a444db2d231 Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 7 Nov 2016 18:16:04 -0800 Subject: [PATCH 4/6] MOve two lines into one. --- tests/routes/api/settings/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/routes/api/settings/index.js b/tests/routes/api/settings/index.js index 8c2cd221e..985e0fe1c 100644 --- a/tests/routes/api/settings/index.js +++ b/tests/routes/api/settings/index.js @@ -24,8 +24,7 @@ describe('GET /settings', () => { expect(err).to.be.null; expect(res).to.have.status(200); expect(res).to.be.json; - expect(res.body).to.have.property('moderation'); - expect(res.body.moderation).to.equal('pre'); + expect(res.body).to.have.property('moderation', 'pre'); done(err); }); }); @@ -55,8 +54,7 @@ describe('update settings', () => { expect(err).to.be.null; expect(res).to.have.status(200); expect(res).to.be.json; - expect(res.body).to.have.property('moderation'); - expect(res.body.moderation).to.equal('post'); + expect(res.body).to.have.property('moderation', 'post'); }); }); }); From 242e64198ab4e705314a4dd770f2a7b3590a737a Mon Sep 17 00:00:00 2001 From: David Erwin Date: Tue, 8 Nov 2016 13:09:09 -0500 Subject: [PATCH 5/6] Add autoprefixer to dependencies --- package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0c7aab592..45470f3c8 100644 --- a/package.json +++ b/package.json @@ -42,12 +42,13 @@ }, "homepage": "https://github.com/coralproject/talk#readme", "dependencies": { + "autoprefixer": "^6.5.2", "body-parser": "^1.15.2", "debug": "^2.2.0", "express": "^4.14.0", "mongoose": "^4.6.5", - "uuid": "^2.0.3", - "morgan": "^1.7.0" + "morgan": "^1.7.0", + "uuid": "^2.0.3" }, "devDependencies": { "babel-core": "6.14.0", From ed01d5441813160104789bd280863cc2c700826c Mon Sep 17 00:00:00 2001 From: gaba Date: Tue, 8 Nov 2016 10:23:59 -0800 Subject: [PATCH 6/6] It fixes random test on get comment by comment_id. --- tests/routes/api/comments/index.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index eaf32adee..378860bcb 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -117,8 +117,7 @@ describe('Get moderation queues rejected, pending, flags', () => { .end(function(err, res){ expect(err).to.be.null; expect(res).to.have.status(200); - expect(res.body[0]).to.have.property('id'); - expect(res.body[0].id).to.equal('abc'); + expect(res.body[0]).to.have.property('id', 'abc'); done(); }); }); @@ -129,8 +128,7 @@ describe('Get moderation queues rejected, pending, flags', () => { .end(function(err, res){ expect(err).to.be.null; expect(res).to.have.status(200); - expect(res.body[0]).to.have.property('id'); - expect(res.body[0].id).to.equal('def'); + expect(res.body[0]).to.have.property('id', 'def'); done(); }); }); @@ -142,8 +140,7 @@ describe('Get moderation queues rejected, pending, flags', () => { expect(res).to.have.status(200); expect(err).to.be.null; expect(res.body.length).to.equal(1); - expect(res.body[0]).to.have.property('id'); - expect(res.body[0].id).to.equal('abc'); + expect(res.body[0]).to.have.property('id', 'abc'); done(); }); }); @@ -229,13 +226,12 @@ describe('Get /:comment_id', () => { it('should return the right comment for the comment_id', function(done){ chai.request(app) - .get('/api/v1/comments') - .query({'comment_id': 'abc'}) + .get('/api/v1/comments/abc') .end(function(err, res){ expect(err).to.be.null; expect(res).to.have.status(200); - expect(res.body[0]).to.have.property('body'); - expect(res.body[0].body).to.equal('comment 10'); + expect(res).to.have.property('body'); + expect(res.body).to.have.property('body', 'comment 10'); done(); }); }); @@ -290,8 +286,7 @@ describe('Put /:comment_id', () => { .end(function(err, res){ expect(err).to.be.null; expect(res).to.have.status(200); - expect(res.body).to.have.property('body'); - expect(res.body.body).to.equal('Something body.'); + expect(res.body).to.have.property('body', 'Something body.'); done(); }); });