Skip to content

Instantly share code, notes, and snippets.

@godfreyd godfreyd/index.js
Last active Jun 18, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.