Merge branch 'master' into admin-routes

This commit is contained in:
Riley Davis
2016-11-08 12:04:20 -07:00
committed by GitHub
7 changed files with 234 additions and 40 deletions
+27 -1
View File
@@ -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;
+52
View File
@@ -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;
+1
View File
@@ -42,6 +42,7 @@
},
"homepage": "https://github.com/coralproject/talk#readme",
"dependencies": {
"autoprefixer": "^6.5.2",
"body-parser": "^1.15.2",
"debug": "^2.2.0",
"ejs": "^2.5.2",
+56 -14
View File
@@ -4,7 +4,7 @@ const Comment = require('../../../models/comment');
const router = express.Router();
//==============================================================================
// Routes
// Get Routes
//==============================================================================
router.get('/', (req, res, next) => {
@@ -23,16 +23,54 @@ router.get('/:comment_id', (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});
//==============================================================================
// Moderation Queues Routes
//==============================================================================
router.get('/action/:action_type', (req, res, next) => {
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.findByStatus('rejected').then((comments) => {
res.status(200).json(comments);
}).catch(error => {
next(error);
});
});
router.get('/status/pending', (req, res, next) => {
Comment.findByStatus('').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, username} = req.body;
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) => {
Comment.findById(req.params.comment_id).then((comment) => {
comment.body = req.body.body;
@@ -48,14 +86,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 +102,16 @@ router.post('/:comment_id/actions', (req, res, next) => {
});
});
//==============================================================================
// Delete Routes
//==============================================================================
router.delete('/:comment_id', (req, res, next) => {
Comment.removeById(req.params.comment_id).then(() => {
res.status(201).send('OK. Removed');
}).catch(error => {
next(error);
});
});
module.exports = router;
+97 -21
View File
@@ -66,6 +66,86 @@ describe('Get /comments', () => {
});
});
describe('Get moderation queues rejected, pending, flags', () => {
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',
item_type: 'comment'
}, {
action_type: 'like',
item_id: 'hij',
item_type: 'comment'
}];
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', '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', 'def');
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', 'abc');
done();
});
});
});
describe('Post /comments', () => {
const users = [{
id: '123',
@@ -128,10 +208,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(() => {
@@ -144,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();
});
});
@@ -205,14 +286,13 @@ 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();
});
});
});
describe('Delete /:comment_id', () => {
describe('Remove /:comment_id', () => {
const comments = [{
id: 'abc',
@@ -258,9 +338,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();
});
@@ -320,8 +401,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();
});
});
@@ -380,14 +460,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();
});
});
+1 -3
View File
@@ -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);
});
});
@@ -33,7 +32,6 @@ describe('GET /settings', () => {
// update the settings.
describe('update settings', () => {
it('should respond ok to a PUT', () => {
return Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true})
.then(() => {
-1
View File
@@ -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();
});
});