Skip to content

Instantly share code, notes, and snippets.

@godfreyd
Last active June 18, 2017 08:26
Show Gist options
  • Save godfreyd/3420597de46509b02c69707d596c8dc4 to your computer and use it in GitHub Desktop.
Save godfreyd/3420597de46509b02c69707d596c8dc4 to your computer and use it in GitHub Desktop.
var Render = require('../render'),
render = Render.render,
config = require('../config'),
moment = require('moment'),
helpers = require('../helpers'),
env = process.env;
/**
* Функция-обертка, контроллер.
* @function
* @param {object} req - Запрос.
* @param {object} res - Ответ.
*/
function getContent(req, res) {
var passport = req.session.passport || {},
servicesTwitter = config.services.twitter;
var query = req.query || {},
q = query.q ? '#' + query.q : '#bem',
lang = query.lang || 'en',
count = query.count || 12,
twitter = typeof twitter === 'undefined' && !req.xhr ? true : query.twitter;
var twitterParams = {
max_id: query.max_id,
count: count,
lang: lang,
result_type: 'recent',
q: q
};
var twitterRequest = twitter ? helpers.twitter(servicesTwitter, twitterParams) : [];
Promise.all([twitterRequest]).then(function(results) {
var twitterResults = results[0],
results = ([]).concat(twitterResults.tweets || [])
.sort(function(a, b) {
return +new Date(b.time) - +new Date(a.time);
})
.map(function(item) {
item.time = moment(item.time).fromNow();
return item;
});
render(req, res, {
view: 'index',
title: 'Social Services Search Robot',
meta: {
description: 'With the Social Services Search Robot you can find the latest tweets and videos',
og: {
url: 'http://localhost:3000',
siteName: 'BEM'
}
},
q: q,
user: passport.user,
maxid: twitterResults.nextPageId,
result: results
}, req.xhr && { block: 'result' });
}).catch(function(err) {
console.log(err);
res.status(500);
render(req, res, {
view: '500'
});
});
}
module.exports = {
getContent
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment