Files
talk/routes/api/stream/index.js
T
2016-11-10 12:16:58 -05:00

45 lines
1.3 KiB
JavaScript

const express = require('express');
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.findAcceptedAndNewByAssetId(req.query.asset_id);
default:
throw new Error('Moderation setting not found.');
}
});
// Get all the users and actions for those comments.
commentsPromise.then(comments => {
return Promise.all([
comments,
User.findByIdArray(comments.map((comment) => comment.author_id)),
Action.getActionSummaries(comments.map((comment) => comment.id))
]);
}).then(([comments, users, actions]) => {
res.json({
comments,
users,
actions
});
}).catch(error => {
next(error);
});
});
module.exports = router;