Merge pull request #258 from coralproject/streams-by-asset-id

Streams by asset
This commit is contained in:
David Erwin
2017-01-25 17:48:41 -05:00
committed by GitHub
6 changed files with 112 additions and 31 deletions
@@ -1,9 +1,17 @@
import {graphql} from 'react-apollo';
import STREAM_QUERY from './streamQuery.graphql';
import pym from 'coral-framework/PymConnection';
let url = pym.parentUrl.split('#')[0] || 'http://localhost:3000/';
function getQueryVariable(variable) {
let query = window.location.search.substring(1);
let vars = query.split('&');
for (let i = 0; i < vars.length; i++) {
let pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) === variable) {
return decodeURIComponent(pair[1]);
}
}
}
export const queryStream = graphql(STREAM_QUERY, {
options: {variables: {asset_url: url}}
options: {variables: {asset_url: getQueryVariable('asset_url')}}
});
+47
View File
@@ -0,0 +1,47 @@
const express = require('express');
const router = express.Router();
const Assets = require('../../services/assets');
const body = 'Lorem ipsum dolor sponge amet, consectetur adipiscing clam. Ut lobortis sollicitudin pillar a ornare. Curabitur dignissim vestibulum cay non rhoncus. Cras laoreet ante vel nunc hendrerit, shelf imperdiet neque egestas. Suspendisse aliquet iaculis fermentum. Talk volutpat, tellus posuere laoreet consequat, mi lacus laoreet massa, sed vehicula mauris velit non lectus. Integer non trust nec neque congue faucibus porttitor sit amet elkhorn.';
router.get('/id/:asset_id', (req, res, next) => {
return Assets.findById(req.params.asset_id)
.then(asset => {
if (asset === null) {
return res.json({'message': 'Asset not found'});
}
res.render('article', {
title: asset.title,
asset_url: asset.url,
body: '',
basePath: '/client/embed/stream'
});
})
.catch((err) => next(err));
});
router.get('/title/:asset_title', (req, res) => {
return res.render('article', {
title: req.params.asset_title.split('-').join(' '),
asset_url: '',
body: body,
basePath: '/client/embed/stream'
});
});
router.get('/', (req, res, next) => {
let skip = req.query.skip ? parseInt(req.query.skip) : 0;
let limit = req.query.limit ? parseInt(req.query.limit) : 25;
return Assets.all(skip, limit)
.then(assets => {
res.render('articles', {
assets: assets
});
})
.catch(err => next(err));
});
module.exports = router;
+3 -7
View File
@@ -4,17 +4,13 @@ const router = express.Router();
router.use('/api/v1', require('./api'));
router.use('/admin', require('./admin'));
router.use('/embed', require('./embed'));
router.use('/assets', require('./assets'));
router.get('/', (req, res) => {
return res.render('article', {
title: 'Coral Talk',
basePath: '/client/embed/stream'
});
});
router.get('/assets/:asset_title', (req, res) => {
return res.render('article', {
title: req.params.asset_title.split('-').join(' '),
asset_url: '',
body: '',
basePath: '/client/embed/stream'
});
});
+15 -9
View File
@@ -88,15 +88,18 @@ module.exports = class AssetsService {
* @param {String} value string to search by.
* @return {Promise}
*/
static search(value) {
static search(value = '', skip = null, limit = null) {
if (value.length === 0) {
return AssetsService.all();
return AssetsService.all(skip, limit);
} else {
return AssetModel.find({
$text: {
$search: value
}
});
return AssetModel
.find({
$text: {
$search: value
}
})
.skip(skip)
.limit(limit);
}
}
@@ -110,7 +113,10 @@ module.exports = class AssetsService {
return AssetModel.find(query);
}
static all() {
return AssetModel.find({});
static all(skip = null, limit = null) {
return AssetModel
.find({})
.skip(skip)
.limit(limit);
}
};
+22 -12
View File
@@ -20,29 +20,39 @@
<body>
<main>
<h1><%= title %></h1>
<p>
Lorem ipsum dolor sponge amet, consectetur adipiscing clam.
Ut lobortis sollicitudin pillar a ornare. Curabitur dignissim
vestibulum cay non rhoncus. Cras laoreet ante vel nunc hendrerit,
shelf imperdiet neque egestas. Suspendisse aliquet iaculis fermentum.
Talk volutpat, tellus posuere laoreet consequat, mi lacus laoreet massa,
sed vehicula mauris velit non lectus. Integer non trust nec neque congue
faucibus porttitor sit amet elkhorn.
</p>
<p><a href="/admin">Visit the moderation console</a></p>
<p><%= body %></p>
<p><a href="/admin">Admin</a> - <a href="/assets">All Assets</a></p>
<div id='coralStreamEmbed'></div>
</main>
<script type='text/javascript' src='<%= basePath %>/pym.v1.min.js'></script>
<script>
var ready = false;
var pymParent = new pym.Parent('coralStreamEmbed', '/embed/stream', {title: 'Talk Comments', id:'coralStreamIframe', name: 'coralStreamIframe'});
// default to using the window.location
var url = window.location.protocol + '//' + window.location.host + window.location.pathname;
// if a url is passed into the template prefer it to the current url
<%if (asset_url.length > 0) { %>
url = '<%= asset_url %>';
<%}%>
var pymParent = new pym.Parent('coralStreamEmbed', '/embed/stream?asset_url=' + encodeURIComponent(url), {title: 'Talk Comments', id:'coralStreamIframe', name: 'coralStreamIframe', asset_url: url});
pymParent.onMessage('height', function(height) {document.querySelector('#coralStreamEmbed iframe').height = height + 'px'})
pymParent.onMessage('childReady', function () {
var interval = setInterval(function () {
if (ready) {
window.clearInterval(interval);
pymParent.sendMessage('DOMContentLoaded', window.location.hash);
// default to using the window.location
var url = window.location.hash;
// if a url is passed into the template prefer it to the current url
<%if (asset_url.length > 0) { %>
url = '<%= asset_url %>';
<%}%>
pymParent.sendMessage('DOMContentLoaded', url);
}
}, 100);
});
+14
View File
@@ -0,0 +1,14 @@
<html>
<body>
<h1>
Asset list
</h1>
<% assets.forEach(function (asset) { %>
<a href="/assets/id/<%= asset.id %>"><%= asset.url %></a><br />
<% }) %>
<p>
(For dev use only. FYI, you can: ?skip=100&limit=25)
</p>
</body>
</html>