Merge branch 'master' of https://github.com/coralproject/talk into settings-in-stream

This commit is contained in:
David Jay
2016-11-28 17:25:07 -05:00
72 changed files with 1261 additions and 708 deletions
+51 -16
View File
@@ -1,24 +1,33 @@
const express = require('express');
const _ = require('lodash');
const scraper = require('../../../services/scraper');
const Comment = require('../../../models/comment');
const User = require('../../../models/user');
const Action = require('../../../models/action');
const Asset = require('../../../models/asset');
const Setting = require('../../../models/setting');
const router = express.Router();
// Find all the comments by a specific asset_url.
// . if pre: get the comments that are accepted.
// . if post: get the comments that are new and accepted.
router.get('/', (req, res, next) => {
// Get the asset_id for this url (or create it if it doesn't exist)
Promise.all([
Asset.findOrCreateByUrl(decodeURIComponent(req.query.asset_url)),
Setting.getSettings()
// Find or create the asset by url.
Asset.findOrCreateByUrl(decodeURIComponent(req.query.asset_url))
// Add the found asset to the scraper if it's not already scraped.
.then((asset) => {
if (!asset.scraped) {
return scraper.create(asset).then(() => asset);
}
return asset;
}),
// Get the moderation setting from the settings.
Setting.getModerationSetting()
])
.then(([asset, settings]) => {
// Get the sitewide moderation setting and return the appropriate comments
@@ -31,23 +40,49 @@ router.get('/', (req, res, next) => {
return Promise.reject(new Error('Moderation setting not found.'));
}
})
// Get all the users and actions for those comments.
.then(([comments, asset, settings]) => {
// Get the user id's from the author id's as a unique array that gets
// sorted.
let userIDs = _.uniq(comments.map((comment) => comment.author_id)).sort();
// Fetch the users for which there is a comment available for them.
let users = userIDs.length > 0 ? User.findByIdArray(userIDs) : [];
// Fetch the actions for pretty much everything at this point.
let actions = Action.getActionSummaries(_.uniq([
// Actions can be on assets...
asset.id,
// Comments...
...comments.map((comment) => comment.id),
// Or Authors...
...userIDs
]), req.user ? req.user.id : false);
return Promise.all([
[asset],
// Pass back the asset that we loaded...
asset,
// It's comments...
comments,
User.findByIdArray(_.uniq(comments.map((comment) => comment.author_id))),
Action.getActionSummaries(_.uniq([
asset.id,
...comments.map((comment) => comment.id),
...comments.map((comment) => comment.author_id)
])),
// The users who wrote those comments
users,
// The actions on the above items
actions,
// And the relevant settings
settings
]);
})
.then(([assets, comments, users, actions, settings]) => {
.then(([asset, comments, users, actions, settings]) => {
res.json({
assets,
assets: [asset],
comments,
users,
actions,