diff --git a/models/action.js b/models/action.js index 574f3439f..2f018b3be 100644 --- a/models/action.js +++ b/models/action.js @@ -90,9 +90,7 @@ ActionSchema.statics.findCommentsIdByActionType = function(action_type, item_typ return Action.find({ 'action_type': action_type, 'item_type': item_type - }, - 'item_id' - ); + }, 'item_id'); }; const Action = mongoose.model('Action', ActionSchema); diff --git a/models/comment.js b/models/comment.js index 77e78a98d..2ac130978 100644 --- a/models/comment.js +++ b/models/comment.js @@ -30,29 +30,6 @@ const CommentSchema = new Schema({ } }); -// Comment model. -const Comment = mongoose.model('Comment', CommentSchema); - -//============================================================================== -// Service -//============================================================================== - -const CommentService = {}; - -/** - * Create a comment. - * @param {String} body content of comment - */ -CommentService.create = ({body, author_id, asset_id, parent_id, status = ''}) => { - return Comment.create({ - body, - author_id, - asset_id, - parent_id, - status, - }); -}; - //============================================================================== // Find Statics //============================================================================== @@ -62,7 +39,7 @@ CommentService.create = ({body, author_id, asset_id, parent_id, status = ''}) => * @param {String} id identifier of comment (uuid) * @return {Promise} */ -CommentService.findById = function(id) { +CommentSchema.statics.findById = function(id) { return Comment.findOne({'id': id}); }; @@ -71,7 +48,7 @@ CommentService.findById = function(id) { * @param {String} asset_id identifier of the asset which owns this comment (uuid) * @return {Promise} */ -CommentService.findByAssetId = function(asset_id) { +CommentSchema.statics.findByAssetId = function(asset_id) { return Comment.find({asset_id}); }; @@ -81,7 +58,7 @@ CommentService.findByAssetId = function(asset_id) { * @param {String} asset_id identifier of the asset which owns the comments (uuid) * @return {Promise} */ -CommentService.findAcceptedByAssetId = function(asset_id) { +CommentSchema.statics.findAcceptedByAssetId = function(asset_id) { return Comment.find({asset_id: asset_id, status:'accepted'}); }; @@ -90,7 +67,7 @@ CommentService.findAcceptedByAssetId = function(asset_id) { * @param {String} asset_id identifier of the asset which owns the comments (uuid) * @return {Promise} */ -CommentService.findAcceptedAndNewByAssetId = function(asset_id) { +CommentSchema.statics.findAcceptedAndNewByAssetId = function(asset_id) { return Comment.find({asset_id: asset_id, status: {'$in': ['accepted', '']}}); }; @@ -99,7 +76,7 @@ CommentService.findAcceptedAndNewByAssetId = function(asset_id) { * @param {String} action_type the type of action that was performed on the comment * @return {Promise} */ -CommentService.findByActionType = function(action_type) { +CommentSchema.statics.findByActionType = function(action_type) { return Action .findCommentsIdByActionType(action_type, 'comment') .then((actions) => { @@ -115,20 +92,16 @@ CommentService.findByActionType = function(action_type) { * @param {String} status the status of the comment to search for * @return {Promise} */ -CommentService.findByStatusByActionType = function(status, action_type) { +CommentSchema.statics.findByStatusByActionType = function(status, action_type) { return Action .findCommentsIdByActionType(action_type, 'comment') .then((actions) => { - return Comment.find({ - 'status': status, - 'id': { - '$in': actions.map(a => { - return a.item_id; - }) + status: status, + id: { + $in: actions.map(a => a.item_id) } }); - }); }; @@ -137,8 +110,10 @@ CommentService.findByStatusByActionType = function(status, action_type) { * @param {String} status the status of the comment to search for * @return {Promise} */ -CommentService.findByStatus = function(status) { - return Comment.find({'status': status}); +CommentSchema.statics.findByStatus = function(status) { + return Comment.find({ + status: status === 'new' ? '' : status + }); }; /** @@ -146,20 +121,23 @@ CommentService.findByStatus = function(status) { * @param {String} moderationValue pre or post moderation setting. If it is undefined then look at the settings. * @return {Promise} */ -CommentService.moderationQueue = function(moderation) { +CommentSchema.statics.moderationQueue = function(moderation) { switch(moderation){ + // Pre-moderation: New comments are shown in the moderator queues immediately. case 'pre': return Comment.findByStatus('').then((comments) => { return comments; }); + // Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users. case 'post': return Comment.findByStatusByActionType('', 'flag').then((comments) => { return comments; }); + default: - throw new Error('Moderation setting not found.'); + return Promise.reject(Error('Moderation setting not found.')); } }; @@ -173,7 +151,7 @@ CommentService.moderationQueue = function(moderation) { * @param {String} status the new status of the comment * @return {Promise} */ -CommentService.changeStatus = function(id, status) { +CommentSchema.statics.changeStatus = function(id, status) { return Comment.findOneAndUpdate({'id': id}, {$set: {'status': status}}); }; @@ -183,7 +161,7 @@ CommentService.changeStatus = function(id, status) { * @param {String} action the new action to the comment * @return {Promise} */ -CommentService.addAction = function(id, user_id, action_type) { +CommentSchema.statics.addAction = function(id, user_id, action_type) { // check that the comment exist let action = new Action({ action_type: action_type, @@ -204,7 +182,7 @@ CommentService.addAction = function(id, user_id, action_type) { * @param {String} status the new status of the comment * @return {Promise} */ -CommentService.removeById = function(id) { +CommentSchema.statics.removeById = function(id) { return Comment.remove({'id': id}); }; @@ -215,7 +193,7 @@ CommentService.removeById = function(id) { * @param {String} user_id the id of the user performing the action * @return {Promise} */ -CommentService.removeAction = function(item_id, user_id, action_type) { +CommentSchema.statics.removeAction = function(item_id, user_id, action_type) { return Action.remove({ action_type, item_type: 'comment', @@ -228,8 +206,11 @@ CommentService.removeAction = function(item_id, user_id, action_type) { * Returns all the comments in the collection. * @return {Promise} */ -CommentService.all = () => { +CommentSchema.statics.all = () => { return Comment.find(); }; -module.exports = CommentService; +// Comment model. +const Comment = mongoose.model('Comment', CommentSchema); + +module.exports = Comment; diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index cbc5ec539..acbfe3d77 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -37,7 +37,11 @@ router.get('/', (req, res, next) => { [asset], comments, User.findByIdArray(_.uniq(comments.map((comment) => comment.author_id))), - Action.getActionSummaries(_.uniq(comments.map((comment) => comment.id))) + Action.getActionSummaries(_.uniq([ + asset.id, + ...comments.map((comment) => comment.id), + ...comments.map((comment) => comment.author_id) + ])) ]); }) .then(([assets, comments, users, actions]) => { diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index cde4b9efb..b8760f3e1 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -64,13 +64,13 @@ describe('Get /comments', () => { ]); }); - it('should return all the comments', function(done){ - chai.request(app) + it('should return all the comments', () => { + return chai.request(app) .get('/api/v1/comments') - .end(function(err, res){ - expect(err).to.be.null; + .then((res) => { + expect(res).to.have.status(200); - done(); + }); }); }); @@ -122,48 +122,42 @@ describe('Get comments by status and action', () => { ]); }); - 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; + it('should return all the rejected comments', () => { + return chai.request(app) + .get('/api/v1/comments?status=rejected') + .then((res) => { expect(res).to.have.status(200); expect(res.body[0]).to.have.property('id', 'abc'); - done(); }); }); - it('should return all the approved comments', function(done){ - chai.request(app) - .get('/api/v1/comments/status/accepted') - .end(function(err, res){ - expect(err).to.be.null; + it('should return all the approved comments', () => { + return chai.request(app) + .get('/api/v1/comments?status=accepted') + .then((res) => { expect(res).to.have.status(200); expect(res.body[0]).to.have.property('id', 'hij'); - done(); }); }); - it('should return all the new comments', function(done){ - chai.request(app) - .get('/api/v1/comments/status/new') - .end(function(err, res){ - expect(err).to.be.null; + it('should return all the new comments', () => { + return chai.request(app) + .get('/api/v1/comments?status=new') + .then((res) => { expect(res).to.have.status(200); expect(res.body[0]).to.have.property('id', 'def'); - done(); }); }); - it('should return all the flagged comments', function(done){ - chai.request(app) - .get('/api/v1/comments/action/flag') - .end(function(err, res){ + it('should return all the flagged comments', () => { + return chai.request(app) + .get('/api/v1/comments?action_type=flag') + .then((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', 'abc'); - done(); + }); }); }); @@ -194,14 +188,13 @@ describe('Post /comments', () => { ]); }); - it('it should create a comment', function(done) { + it('it should create a comment', () => { chai.request(app) .post('/api/v1/comments') .send({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''}) - .end(function(err, res){ - expect(res).to.have.status(200); + .then((res) => { + expect(res).to.have.status(201); expect(res.body).to.have.property('id'); - done(); }); }); }); @@ -251,72 +244,14 @@ describe('Get /:comment_id', () => { ]); }); - it('should return the right comment for the comment_id', function(done){ - chai.request(app) + it('should return the right comment for the comment_id', () => { + return chai.request(app) .get('/api/v1/comments/abc') - .end(function(err, res){ - expect(err).to.be.null; + .then((res) => { expect(res).to.have.status(200); expect(res).to.have.property('body'); expect(res.body).to.have.property('body', 'comment 10'); - done(); - }); - }); -}); -describe('Put /:comment_id', () => { - - const comments = [{ - id: 'abc', - body: 'comment 10', - asset_id: 'asset', - author_id: '123' - }, { - id: 'def', - body: 'comment 20', - asset_id: 'asset', - author_id: '456' - }, { - id: 'hij', - body: 'comment 30', - asset_id: '456' - }]; - - const users = [{ - displayName: 'Ana', - email: 'ana@gmail.com', - password: '123' - }, { - displayName: 'Maria', - email: 'maria@gmail.com', - password: '123' - }]; - - const actions = [{ - action_type: 'flag', - item_id: 'abc' - }, { - action_type: 'like', - item_id: 'hij' - }]; - - beforeEach(() => { - return Promise.all([ - Comment.create(comments), - User.createLocalUsers(users), - Action.create(actions) - ]); - }); - - it('it should update comment', function(done) { - chai.request(app) - .post('/api/v1/comments/abc') - .send({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''}) - .end(function(err, res){ - expect(err).to.be.null; - expect(res).to.have.status(200); - expect(res.body).to.have.property('body', 'Something body.'); - done(); }); }); }); @@ -369,7 +304,7 @@ describe('Remove /:comment_id', () => { return chai.request(app) .delete('/api/v1/comments/abc') .then((res) => { - expect(res).to.have.status(201); + expect(res).to.have.status(204); return Comment.findById('abc'); }) @@ -384,7 +319,7 @@ process.on('unhandledRejection', (reason) => { console.error(reason); }); -describe('Post /:comment_id/status', () => { +describe('Put /:comment_id/status', () => { const comments = [{ id: 'abc', @@ -433,12 +368,11 @@ describe('Post /:comment_id/status', () => { it('it should update status', function() { return chai.request(app) - .post('/api/v1/comments/abc/status') + .put('/api/v1/comments/abc/status') .send({status: 'accepted'}) .then((res) => { - expect(res).to.have.status(200); - expect(res).to.have.body; - expect(res.body).to.have.property('status', 'accepted'); + expect(res).to.have.status(204); + expect(res.body).to.be.empty; }); }); }); @@ -495,7 +429,7 @@ describe('Post /:comment_id/actions', () => { .post('/api/v1/comments/abc/actions') .send({'user_id': '456', 'action_type': 'flag'}) .then((res) => { - expect(res).to.have.status(200); + expect(res).to.have.status(201); expect(res).to.have.body; expect(res.body).to.have.property('item_type', 'comment'); expect(res.body).to.have.property('action_type', 'flag'); diff --git a/tests/services/wordlist.js b/tests/services/wordlist.js index 935db4190..0ae76c176 100644 --- a/tests/services/wordlist.js +++ b/tests/services/wordlist.js @@ -2,7 +2,7 @@ const expect = require('chai').expect; const wordlist = require('../../services/wordlist'); -describe.only('wordlist: services', () => { +describe('wordlist: services', () => { before(() => wordlist.insert([ 'BAD',