Comment stream with new and approved when post-moderated.

This commit is contained in:
gaba
2016-11-09 11:40:57 -08:00
parent a7061c29a6
commit 9ff2613ab4
4 changed files with 80 additions and 25 deletions
+13 -6
View File
@@ -61,20 +61,27 @@ CommentSchema.statics.findById = function(id) {
* Finds ALL the comments by the asset_id.
* @param {String} asset_id identifier of the asset which owns this comment (uuid)
*/
CommentSchema.statics.findAllByAssetId = function(asset_id) {
CommentSchema.statics.findByAssetId = function(asset_id) {
return Comment.find({asset_id});
};
/**
* Finds the comments by the asset_id.
* get the comments that are approved.
* if post: get the comments that new and not flagged.
* Finds the accepted comments by the asset_id.
* get the comments that are accepted.
* @param {String} asset_id identifier of the asset which owns the comments (uuid)
*/
CommentSchema.statics.findByAssetId = function(asset_id) {
CommentSchema.statics.findAcceptedByAssetId = function(asset_id) {
return Comment.find({asset_id: asset_id, status:'accepted'});
};
/**
* Finds the new and accepted comments by the asset_id.
* @param {String} asset_id identifier of the asset which owns the comments (uuid)
*/
CommentSchema.statics.findNewByAssetId = function(asset_id) {
return Comment.find({asset_id: asset_id, status: {'$in': ['accepted', '']}});
};
/**
* Find comments by an action that was performed on them.
* @param {String} action_type the type of action that was performed on the comment
@@ -126,7 +133,7 @@ CommentSchema.statics.moderationQueue = function(moderationValue) {
return comments;
});
default:
return Error('Moderation setting not found.') ;
throw new Error('Moderation setting not found.');
}
});
};
+17 -7
View File
@@ -4,16 +4,26 @@ const Comment = require('../../../models/comment');
const User = require('../../../models/user');
const Action = require('../../../models/action');
const Setting = require('../../../models/setting');
const router = express.Router();
// Find all the comments by a specific asset_id.
// . if pre: get the comments that are accepted.
// . if post: get the comments that are new and accepted.
router.get('/', (req, res, next) => {
const commentsPromise = Setting.getModerationSetting().then(({moderation}) => {
switch(moderation){
case 'pre':
return Comment.findAcceptedByAssetId(req.query.asset_id);
case 'post':
return Comment.findNewByAssetId(req.query.asset_id);
default:
throw new Error('Moderation setting not found.');
}
});
// find all the comments by a specific asset_id.
// - get the comments that are approved.
// - if post: get the comments that new and not flagged.
const commentsPromise = Comment.findByAssetId(req.query.asset_id);
// get all the users and actions for those comments.
// Get all the users and actions for those comments.
commentsPromise.then(comments => {
return Promise.all([
comments,
@@ -21,7 +31,7 @@ router.get('/', (req, res, next) => {
Action.findByItemIdArray(comments.map((comment) => comment.id))
]);
}).then(([comments, users, actions]) => {
res.json([...comments, ...users, ...actions]);
res.status(200).json([...comments, ...users, ...actions]);
}).catch(error => {
next(error);
});
+22 -4
View File
@@ -31,6 +31,13 @@ describe('Comment: models', () => {
parent_id: '',
author_id: '456',
id: '3'
}, {
body: 'comment 40',
asset_id: '123',
status: 'rejected',
parent_id: '',
author_id: '456',
id: '4'
}];
const users = [{
@@ -74,18 +81,19 @@ describe('Comment: models', () => {
describe('#findByAssetId()', () => {
it('should find an array of all comments by asset id', () => {
return Comment.findAllByAssetId('123').then((result) => {
expect(result).to.have.length(2);
return Comment.findByAssetId('123').then((result) => {
expect(result).to.have.length(3);
result.sort((a, b) => {
if (a.body < b.body) {return -1;}
else {return 1;}
});
expect(result[0]).to.have.property('body', 'comment 10');
expect(result[1]).to.have.property('body', 'comment 20');
expect(result[2]).to.have.property('body', 'comment 40');
});
});
it('should find an array of approved comments by asset id', () => {
return Comment.findByAssetId('123').then((result) => {
it('should find an array of accepted comments by asset id', () => {
return Comment.findAcceptedByAssetId('123').then((result) => {
expect(result).to.have.length(1);
result.sort((a, b) => {
if (a.body < b.body) {return -1;}
@@ -94,6 +102,16 @@ describe('Comment: models', () => {
expect(result[0]).to.have.property('body', 'comment 20');
});
});
it('should find an array of new and accepted comments by asset id', () => {
return Comment.findNewByAssetId('123').then((result) => {
expect(result).to.have.length(2);
result.sort((a, b) => {
if (a.body < b.body) {return -1;}
else {return 1;}
});
expect(result[0]).to.have.property('body', 'comment 10');
});
});
});
describe('#moderationQueue()', () => {
+28 -8
View File
@@ -12,21 +12,38 @@ const Action = require('../../../../models/action');
const User = require('../../../../models/user');
const Comment = require('../../../../models/comment');
const Setting = require('../../../../models/setting');
describe('api/stream: routes', () => {
const settings = {id: '1', moderation: 'pre'};
const comments = [{
id: 'abc',
body: 'comment 10',
asset_id: 'asset',
author_id: '123'
author_id: '123',
parent_id: '',
status: 'accepted'
}, {
id: 'def',
body: 'comment 20',
asset_id: 'asset',
author_id: '456'
author_id: '456',
parent_id: '',
status: ''
}, {
id: 'uio',
body: 'comment 30',
asset_id: 'asset',
author_id: '456',
parent_id: '',
status: ''
}, {
id: 'hij',
body: 'comment 30',
asset_id: '456'
body: 'comment 40',
asset_id: '456',
status: 'rejected'
}];
const users = [{
@@ -46,10 +63,12 @@ describe('api/stream: routes', () => {
}];
beforeEach(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
return Setting.create(settings).then(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
});
});
@@ -60,6 +79,7 @@ describe('api/stream: routes', () => {
.end(function(err, res){
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res.body.length).to.equal(3);
done();
});
});