traintimesPWA/server/lib/twitter.js
Martin Donnelly 1e03d1697a wip
2020-08-16 20:05:28 +01:00

68 lines
1.4 KiB
JavaScript

/**
* Created by WebStorm.
* User: martin
* Date: 30/04/2020
* Time: 16:19
*/
const logger = require('log4js').getLogger('Twitter');
const dbmanager = require('../db/dbmanager');
let intervalID = 0;
let eventEmitter;
logger.level = 'debug';
function doTweetUpdates() {
logger.info('doTweetUpdates');
dbmanager.getLatestTweets(10000)
.then((data) => {
// console.log(data);
data.forEach((item) => {
logger.debug(`t-${item.userIDStr}`);
eventEmitter.emit('sendTweet', `t-${item.userIDStr}`, item);
});
})
.catch((err) => {
console.error(err.message);
});
}
function startTweetUpdates() {
intervalID = setInterval(() => {
doTweetUpdates();
}, 10000);
}
exports.top = (req, res) => {
dbmanager.getTopOne()
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
'message': err.message || 'Some error occurred while querying the database.'
});
});
};
exports.all = (req, res) => {
const _list = req.body.list || [];
dbmanager.getAll(_list)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
'message': err.message || 'Some error occurred while querying the database.'
});
});
};
exports.updates = (newEmitter) => {
logger.info('Setting events');
eventEmitter = newEmitter;
startTweetUpdates();
};