From 1a7b716820b8c103e2366f69611b9a901a67123c Mon Sep 17 00:00:00 2001 From: David Jay Date: Mon, 14 Nov 2016 15:51:17 -0500 Subject: [PATCH] Updating stream endpoint to expect asset_url and to return asset. --- routes/api/stream/index.js | 26 ++++++++++++++++-------- tests/routes/api/stream/index.js | 35 +++++++++++++++++--------------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/routes/api/stream/index.js b/routes/api/stream/index.js index 3f49d48fc..db1d2bc36 100644 --- a/routes/api/stream/index.js +++ b/routes/api/stream/index.js @@ -3,35 +3,45 @@ const express = require('express'); 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_id. +// 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) => { - const commentsPromise = Setting.getModerationSetting().then(({moderation}) => { + + // Get the asset_id for this url (or create it if it doesn't exist) + Promise.all([ + Asset.findOrCreateByUrl(req.query.asset_url), + Setting.getModerationSetting() + ]) + .then(([asset, {moderation}]) => { + // Get the sitewide moderation setting and return the appropriate comments switch(moderation){ case 'pre': - return Comment.findAcceptedByAssetId(req.query.asset_id); + return Promise.all([Comment.findAcceptedByAssetId(asset.id), asset]); case 'post': - return Comment.findAcceptedAndNewByAssetId(req.query.asset_id); + return Promise.all([Comment.findAcceptedAndNewByAssetId(asset.id), asset]); default: throw new Error('Moderation setting not found.'); } - }); - + }) // Get all the users and actions for those comments. - commentsPromise.then(comments => { + .then(([comments, asset]) => { return Promise.all([ + [asset], comments, User.findByIdArray(comments.map((comment) => comment.author_id)), Action.getActionSummaries(comments.map((comment) => comment.id)) ]); - }).then(([comments, users, actions]) => { + }) + .then(([assets, comments, users, actions]) => { res.json({ + assets, comments, users, actions diff --git a/tests/routes/api/stream/index.js b/tests/routes/api/stream/index.js index c967cb932..009184b6b 100644 --- a/tests/routes/api/stream/index.js +++ b/tests/routes/api/stream/index.js @@ -11,6 +11,7 @@ chai.use(require('chai-http')); const Action = require('../../../../models/action'); const User = require('../../../../models/user'); const Comment = require('../../../../models/comment'); +const Asset = require('../../../../models/asset'); const Setting = require('../../../../models/setting'); @@ -21,14 +22,12 @@ describe('api/stream: routes', () => { const comments = [{ id: 'abc', body: 'comment 10', - asset_id: 'asset', author_id: '', parent_id: '', status: 'accepted' }, { id: 'def', body: 'comment 20', - asset_id: 'asset', author_id: '', parent_id: '', status: '' @@ -66,29 +65,33 @@ describe('api/stream: routes', () => { beforeEach(() => { - return User - .createLocalUsers(users) - .then(users => { + return Promise.all([ + User.createLocalUsers(users), + Asset.findOrCreateByUrl('http://test.com') + ]) + .then(([users, asset]) => { - comments[0].author_id = users[0].id; - comments[1].author_id = users[1].id; - - return Promise.all([ - Comment.create(comments), - Action.create(actions), - Setting.create(settings) - ]); + comments[0].author_id = users[0].id; + comments[1].author_id = users[1].id; - }); + comments[0].asset_id = asset.id; + comments[1].asset_id = asset.id; + return Promise.all([ + Comment.create(comments), + Action.create(actions), + Setting.create(settings) + ]); + }); }); - it('should return a stream with comments, users and actions', () => { + it('should return a stream with comments, users and actions for an existing asset', () => { return chai.request(app) .get('/api/v1/stream') - .query({'asset_id': 'asset'}) + .query({'asset_url': 'http://test.com'}) .then(res => { expect(res).to.have.status(200); + expect(res.body.assets.length).to.equal(1); expect(res.body.comments.length).to.equal(1); expect(res.body.users.length).to.equal(1); expect(res.body.actions.length).to.equal(1);