mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: async/await routes/feeds
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var nconf = require('nconf');
|
const nconf = require('nconf');
|
||||||
var winston = require('winston');
|
const winston = require('winston');
|
||||||
var validator = require('validator');
|
const validator = require('validator');
|
||||||
|
|
||||||
var meta = require('../meta');
|
const meta = require('../meta');
|
||||||
var plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
|
|
||||||
exports.handle404 = function handle404(req, res) {
|
exports.handle404 = function handle404(req, res) {
|
||||||
var relativePath = nconf.get('relative_path');
|
const relativePath = nconf.get('relative_path');
|
||||||
var isClientScript = new RegExp('^' + relativePath + '\\/assets\\/src\\/.+\\.js(\\?v=\\w+)?$');
|
const isClientScript = new RegExp('^' + relativePath + '\\/assets\\/src\\/.+\\.js(\\?v=\\w+)?$');
|
||||||
|
|
||||||
if (plugins.hasListeners('action:meta.override404')) {
|
if (plugins.hasListeners('action:meta.override404')) {
|
||||||
return plugins.fireHook('action:meta.override404', {
|
return plugins.fireHook('action:meta.override404', {
|
||||||
@@ -38,11 +38,11 @@ exports.handle404 = function handle404(req, res) {
|
|||||||
|
|
||||||
exports.send404 = function (req, res) {
|
exports.send404 = function (req, res) {
|
||||||
res.status(404);
|
res.status(404);
|
||||||
var path = String(req.path || '');
|
const path = String(req.path || '');
|
||||||
if (res.locals.isAPI) {
|
if (res.locals.isAPI) {
|
||||||
return res.json({ path: validator.escape(path.replace(/^\/api/, '')), title: '[[global:404.title]]' });
|
return res.json({ path: validator.escape(path.replace(/^\/api/, '')), title: '[[global:404.title]]' });
|
||||||
}
|
}
|
||||||
var middleware = require('../middleware');
|
const middleware = require('../middleware');
|
||||||
middleware.buildHeader(req, res, function () {
|
middleware.buildHeader(req, res, function () {
|
||||||
res.render('404', { path: validator.escape(path), title: '[[global:404.title]]' });
|
res.render('404', { path: validator.escape(path), title: '[[global:404.title]]' });
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,22 +1,21 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const rss = require('rss');
|
||||||
var rss = require('rss');
|
const nconf = require('nconf');
|
||||||
var nconf = require('nconf');
|
const validator = require('validator');
|
||||||
var validator = require('validator');
|
|
||||||
|
|
||||||
var posts = require('../posts');
|
const posts = require('../posts');
|
||||||
var topics = require('../topics');
|
const topics = require('../topics');
|
||||||
var user = require('../user');
|
const user = require('../user');
|
||||||
var categories = require('../categories');
|
const categories = require('../categories');
|
||||||
var meta = require('../meta');
|
const meta = require('../meta');
|
||||||
var helpers = require('../controllers/helpers');
|
const helpers = require('../controllers/helpers');
|
||||||
var privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
var db = require('../database');
|
const db = require('../database');
|
||||||
var utils = require('../utils');
|
const utils = require('../utils');
|
||||||
var controllers404 = require('../controllers/404.js');
|
const controllers404 = require('../controllers/404.js');
|
||||||
|
|
||||||
var terms = {
|
const terms = {
|
||||||
daily: 'day',
|
daily: 'day',
|
||||||
weekly: 'week',
|
weekly: 'week',
|
||||||
monthly: 'month',
|
monthly: 'month',
|
||||||
@@ -38,327 +37,244 @@ module.exports = function (app, middleware) {
|
|||||||
app.get('/tags/:tag.rss', middleware.maintenanceMode, generateForTag);
|
app.get('/tags/:tag.rss', middleware.maintenanceMode, generateForTag);
|
||||||
};
|
};
|
||||||
|
|
||||||
function validateTokenIfRequiresLogin(requiresLogin, cid, req, res, callback) {
|
async function validateTokenIfRequiresLogin(requiresLogin, cid, req, res) {
|
||||||
var uid = parseInt(req.query.uid, 10) || 0;
|
const uid = parseInt(req.query.uid, 10) || 0;
|
||||||
var token = req.query.token;
|
const token = req.query.token;
|
||||||
|
|
||||||
if (!requiresLogin) {
|
if (!requiresLogin) {
|
||||||
return callback();
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uid <= 0 || !token) {
|
if (uid <= 0 || !token) {
|
||||||
return helpers.notAllowed(req, res);
|
return helpers.notAllowed(req, res);
|
||||||
}
|
}
|
||||||
|
const userToken = await db.getObjectField('user:' + uid, 'rss_token');
|
||||||
async.waterfall([
|
if (userToken !== token) {
|
||||||
function (next) {
|
await user.auth.logAttempt(uid, req.ip);
|
||||||
db.getObjectField('user:' + uid, 'rss_token', next);
|
return helpers.notAllowed(req, res);
|
||||||
},
|
}
|
||||||
function (_token, next) {
|
const userPrivileges = privileges.categories.get(cid, uid);
|
||||||
if (token === _token) {
|
if (!userPrivileges.read) {
|
||||||
async.waterfall([
|
return helpers.notAllowed(req, res);
|
||||||
function (next) {
|
}
|
||||||
privileges.categories.get(cid, uid, next);
|
return true;
|
||||||
},
|
|
||||||
function (privileges, next) {
|
|
||||||
if (!privileges.read) {
|
|
||||||
return helpers.notAllowed(req, res);
|
|
||||||
}
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
user.auth.logAttempt(uid, req.ip, next);
|
|
||||||
},
|
|
||||||
function () {
|
|
||||||
helpers.notAllowed(req, res);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForTopic(req, res, callback) {
|
async function generateForTopic(req, res) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
if (meta.config['feeds:disableRSS']) {
|
||||||
return controllers404.send404(req, res);
|
return controllers404.send404(req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
var tid = req.params.topic_id;
|
const tid = req.params.topic_id;
|
||||||
var userPrivileges;
|
|
||||||
var topic;
|
const [userPrivileges, topic] = await Promise.all([
|
||||||
async.waterfall([
|
privileges.topics.get(tid, req.uid),
|
||||||
function (next) {
|
topics.getTopicData(tid),
|
||||||
async.parallel({
|
]);
|
||||||
privileges: function (next) {
|
|
||||||
privileges.topics.get(tid, req.uid, next);
|
if (!topic || (topic.deleted && !userPrivileges.view_deleted)) {
|
||||||
},
|
return controllers404.send404(req, res);
|
||||||
topic: function (next) {
|
}
|
||||||
topics.getTopicData(tid, next);
|
|
||||||
},
|
if (await validateTokenIfRequiresLogin(!userPrivileges['topics:read'], topic.cid, req, res)) {
|
||||||
}, next);
|
const topicData = await topics.getTopicWithPosts(topic, 'tid:' + tid + ':posts', req.uid || req.query.uid || 0, 0, 25, false);
|
||||||
},
|
|
||||||
function (results, next) {
|
topics.modifyPostsByPrivilege(topicData, userPrivileges);
|
||||||
if (!results.topic || (results.topic.deleted && !results.privileges.view_deleted)) {
|
|
||||||
return controllers404.send404(req, res);
|
const feed = new rss({
|
||||||
|
title: utils.stripHTMLTags(topicData.title, utils.tags),
|
||||||
|
description: topicData.posts.length ? topicData.posts[0].content : '',
|
||||||
|
feed_url: nconf.get('url') + '/topic/' + tid + '.rss',
|
||||||
|
site_url: nconf.get('url') + '/topic/' + topicData.slug,
|
||||||
|
image_url: topicData.posts.length ? topicData.posts[0].picture : '',
|
||||||
|
author: topicData.posts.length ? topicData.posts[0].username : '',
|
||||||
|
ttl: 60,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (topicData.posts.length > 0) {
|
||||||
|
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
||||||
|
}
|
||||||
|
|
||||||
|
topicData.posts.forEach(function (postData) {
|
||||||
|
if (!postData.deleted) {
|
||||||
|
const dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString();
|
||||||
|
|
||||||
|
feed.item({
|
||||||
|
title: 'Reply to ' + utils.stripHTMLTags(topicData.title, utils.tags) + ' on ' + dateStamp,
|
||||||
|
description: postData.content,
|
||||||
|
url: nconf.get('url') + '/post/' + postData.pid,
|
||||||
|
author: postData.user ? postData.user.username : '',
|
||||||
|
date: dateStamp,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
userPrivileges = results.privileges;
|
});
|
||||||
topic = results.topic;
|
|
||||||
validateTokenIfRequiresLogin(!results.privileges['topics:read'], results.topic.cid, req, res, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
topics.getTopicWithPosts(topic, 'tid:' + tid + ':posts', req.uid || req.query.uid || 0, 0, 25, false, next);
|
|
||||||
},
|
|
||||||
function (topicData) {
|
|
||||||
topics.modifyPostsByPrivilege(topicData, userPrivileges);
|
|
||||||
|
|
||||||
var description = topicData.posts.length ? topicData.posts[0].content : '';
|
sendFeed(feed, res);
|
||||||
var image_url = topicData.posts.length ? topicData.posts[0].picture : '';
|
}
|
||||||
var author = topicData.posts.length ? topicData.posts[0].username : '';
|
|
||||||
|
|
||||||
var feed = new rss({
|
|
||||||
title: utils.stripHTMLTags(topicData.title, utils.tags),
|
|
||||||
description: description,
|
|
||||||
feed_url: nconf.get('url') + '/topic/' + tid + '.rss',
|
|
||||||
site_url: nconf.get('url') + '/topic/' + topicData.slug,
|
|
||||||
image_url: image_url,
|
|
||||||
author: author,
|
|
||||||
ttl: 60,
|
|
||||||
});
|
|
||||||
var dateStamp;
|
|
||||||
|
|
||||||
if (topicData.posts.length > 0) {
|
|
||||||
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
|
||||||
}
|
|
||||||
|
|
||||||
topicData.posts.forEach(function (postData) {
|
|
||||||
if (!postData.deleted) {
|
|
||||||
dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString();
|
|
||||||
|
|
||||||
feed.item({
|
|
||||||
title: 'Reply to ' + utils.stripHTMLTags(topicData.title, utils.tags) + ' on ' + dateStamp,
|
|
||||||
description: postData.content,
|
|
||||||
url: nconf.get('url') + '/post/' + postData.pid,
|
|
||||||
author: postData.user ? postData.user.username : '',
|
|
||||||
date: dateStamp,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
sendFeed(feed, res);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForCategory(req, res, callback) {
|
async function generateForCategory(req, res, next) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
if (meta.config['feeds:disableRSS']) {
|
||||||
return controllers404.send404(req, res);
|
return controllers404.send404(req, res);
|
||||||
}
|
}
|
||||||
var cid = req.params.category_id;
|
const cid = req.params.category_id;
|
||||||
var category;
|
|
||||||
if (!parseInt(cid, 10)) {
|
if (!parseInt(cid, 10)) {
|
||||||
return setImmediate(callback);
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
const [userPrivileges, category] = await Promise.all([
|
||||||
|
privileges.categories.get(cid, req.uid),
|
||||||
|
categories.getCategoryById({
|
||||||
|
cid: cid,
|
||||||
|
set: 'cid:' + cid + ':tids',
|
||||||
|
reverse: true,
|
||||||
|
start: 0,
|
||||||
|
stop: 25,
|
||||||
|
uid: req.uid || req.query.uid || 0,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!category) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await validateTokenIfRequiresLogin(!userPrivileges.read, cid, req, res)) {
|
||||||
|
const feed = await generateTopicsFeed({
|
||||||
|
uid: req.uid || req.query.uid || 0,
|
||||||
|
title: category.name,
|
||||||
|
description: category.description,
|
||||||
|
feed_url: '/category/' + cid + '.rss',
|
||||||
|
site_url: '/category/' + category.cid,
|
||||||
|
}, category.topics);
|
||||||
|
|
||||||
|
sendFeed(feed, res);
|
||||||
}
|
}
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
async.parallel({
|
|
||||||
privileges: function (next) {
|
|
||||||
privileges.categories.get(cid, req.uid, next);
|
|
||||||
},
|
|
||||||
category: function (next) {
|
|
||||||
categories.getCategoryById({
|
|
||||||
cid: cid,
|
|
||||||
set: 'cid:' + cid + ':tids',
|
|
||||||
reverse: true,
|
|
||||||
start: 0,
|
|
||||||
stop: 25,
|
|
||||||
uid: req.uid || req.query.uid || 0,
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
category = results.category;
|
|
||||||
if (!category) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
validateTokenIfRequiresLogin(!results.privileges.read, cid, req, res, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
generateTopicsFeed({
|
|
||||||
uid: req.uid || req.query.uid || 0,
|
|
||||||
title: category.name,
|
|
||||||
description: category.description,
|
|
||||||
feed_url: '/category/' + cid + '.rss',
|
|
||||||
site_url: '/category/' + category.cid,
|
|
||||||
}, category.topics, next);
|
|
||||||
},
|
|
||||||
function (feed) {
|
|
||||||
sendFeed(feed, res);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForTopics(req, res, next) {
|
async function generateForTopics(req, res) {
|
||||||
|
if (meta.config['feeds:disableRSS']) {
|
||||||
|
return controllers404.send404(req, res);
|
||||||
|
}
|
||||||
|
let token = null;
|
||||||
|
if (req.query.token && req.query.uid) {
|
||||||
|
token = await db.getObjectField('user:' + req.query.uid, 'rss_token');
|
||||||
|
}
|
||||||
|
|
||||||
|
await sendTopicsFeed({
|
||||||
|
uid: token && token === req.query.token ? req.query.uid : req.uid,
|
||||||
|
title: 'Most recently created topics',
|
||||||
|
description: 'A list of topics that have been created recently',
|
||||||
|
feed_url: '/topics.rss',
|
||||||
|
useMainPost: true,
|
||||||
|
}, 'topics:tid', res);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generateForRecent(req, res) {
|
||||||
|
if (meta.config['feeds:disableRSS']) {
|
||||||
|
return controllers404.send404(req, res);
|
||||||
|
}
|
||||||
|
let token = null;
|
||||||
|
if (req.query.token && req.query.uid) {
|
||||||
|
token = await db.getObjectField('user:' + req.query.uid, 'rss_token');
|
||||||
|
}
|
||||||
|
|
||||||
|
await sendTopicsFeed({
|
||||||
|
uid: token && token === req.query.token ? req.query.uid : req.uid,
|
||||||
|
title: 'Recently Active Topics',
|
||||||
|
description: 'A list of topics that have been active within the past 24 hours',
|
||||||
|
feed_url: '/recent.rss',
|
||||||
|
site_url: '/recent',
|
||||||
|
}, 'topics:recent', res);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generateForTop(req, res) {
|
||||||
|
if (meta.config['feeds:disableRSS']) {
|
||||||
|
return controllers404.send404(req, res);
|
||||||
|
}
|
||||||
|
const term = terms[req.params.term] || 'day';
|
||||||
|
|
||||||
|
let token = null;
|
||||||
|
if (req.query.token && req.query.uid) {
|
||||||
|
token = await db.getObjectField('user:' + req.query.uid, 'rss_token');
|
||||||
|
}
|
||||||
|
|
||||||
|
const uid = token && token === req.query.token ? req.query.uid : req.uid;
|
||||||
|
|
||||||
|
const result = await topics.getSortedTopics({
|
||||||
|
uid: uid,
|
||||||
|
start: 0,
|
||||||
|
stop: 19,
|
||||||
|
term: term,
|
||||||
|
sort: 'votes',
|
||||||
|
});
|
||||||
|
|
||||||
|
const feed = await generateTopicsFeed({
|
||||||
|
uid: uid,
|
||||||
|
title: 'Top Voted Topics',
|
||||||
|
description: 'A list of topics that have received the most votes',
|
||||||
|
feed_url: '/top/' + (req.params.term || 'daily') + '.rss',
|
||||||
|
site_url: '/top/' + (req.params.term || 'daily'),
|
||||||
|
}, result.topics);
|
||||||
|
|
||||||
|
sendFeed(feed, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generateForPopular(req, res) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
if (meta.config['feeds:disableRSS']) {
|
||||||
return controllers404.send404(req, res);
|
return controllers404.send404(req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
const term = terms[req.params.term] || 'day';
|
||||||
function (next) {
|
|
||||||
if (req.query.token && req.query.uid) {
|
|
||||||
db.getObjectField('user:' + req.query.uid, 'rss_token', next);
|
|
||||||
} else {
|
|
||||||
next(null, null);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
function (token, next) {
|
|
||||||
sendTopicsFeed({
|
|
||||||
uid: token && token === req.query.token ? req.query.uid : req.uid,
|
|
||||||
title: 'Most recently created topics',
|
|
||||||
description: 'A list of topics that have been created recently',
|
|
||||||
feed_url: '/topics.rss',
|
|
||||||
useMainPost: true,
|
|
||||||
}, 'topics:tid', req, res, next);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateForRecent(req, res, next) {
|
let token = null;
|
||||||
if (meta.config['feeds:disableRSS']) {
|
if (req.query.token && req.query.uid) {
|
||||||
return controllers404.send404(req, res);
|
token = await db.getObjectField('user:' + req.query.uid, 'rss_token');
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
const uid = token && token === req.query.token ? req.query.uid : req.uid;
|
||||||
function (next) {
|
|
||||||
if (req.query.token && req.query.uid) {
|
const result = await topics.getSortedTopics({
|
||||||
db.getObjectField('user:' + req.query.uid, 'rss_token', next);
|
uid: uid,
|
||||||
} else {
|
start: 0,
|
||||||
next(null, null);
|
stop: 19,
|
||||||
}
|
term: term,
|
||||||
},
|
sort: 'posts',
|
||||||
function (token, next) {
|
});
|
||||||
sendTopicsFeed({
|
|
||||||
uid: token && token === req.query.token ? req.query.uid : req.uid,
|
const feed = await generateTopicsFeed({
|
||||||
title: 'Recently Active Topics',
|
uid: uid,
|
||||||
description: 'A list of topics that have been active within the past 24 hours',
|
title: 'Popular Topics',
|
||||||
feed_url: '/recent.rss',
|
description: 'A list of topics that are sorted by post count',
|
||||||
site_url: '/recent',
|
feed_url: '/popular/' + (req.params.term || 'daily') + '.rss',
|
||||||
}, 'topics:recent', req, res, next);
|
site_url: '/popular/' + (req.params.term || 'daily'),
|
||||||
},
|
}, result.topics);
|
||||||
], next);
|
sendFeed(feed, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForTop(req, res, next) {
|
async function sendTopicsFeed(options, set, res) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
const start = options.hasOwnProperty('start') ? options.start : 0;
|
||||||
return controllers404.send404(req, res);
|
const stop = options.hasOwnProperty('stop') ? options.stop : 19;
|
||||||
}
|
const topicData = await topics.getTopicsFromSet(set, options.uid, start, stop);
|
||||||
var term = terms[req.params.term] || 'day';
|
const feed = await generateTopicsFeed(options, topicData.topics);
|
||||||
var uid;
|
sendFeed(feed, res);
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
if (req.query.token && req.query.uid) {
|
|
||||||
db.getObjectField('user:' + req.query.uid, 'rss_token', next);
|
|
||||||
} else {
|
|
||||||
next(null, null);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
function (token, next) {
|
|
||||||
uid = token && token === req.query.token ? req.query.uid : req.uid;
|
|
||||||
|
|
||||||
topics.getSortedTopics({
|
|
||||||
uid: uid,
|
|
||||||
start: 0,
|
|
||||||
stop: 19,
|
|
||||||
term: term,
|
|
||||||
sort: 'votes',
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (result, next) {
|
|
||||||
generateTopicsFeed({
|
|
||||||
uid: uid,
|
|
||||||
title: 'Top Voted Topics',
|
|
||||||
description: 'A list of topics that have received the most votes',
|
|
||||||
feed_url: '/top/' + (req.params.term || 'daily') + '.rss',
|
|
||||||
site_url: '/top/' + (req.params.term || 'daily'),
|
|
||||||
}, result.topics, next);
|
|
||||||
},
|
|
||||||
function (feed) {
|
|
||||||
sendFeed(feed, res);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForPopular(req, res, next) {
|
async function generateTopicsFeed(feedOptions, feedTopics) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
|
||||||
return controllers404.send404(req, res);
|
|
||||||
}
|
|
||||||
|
|
||||||
var term = terms[req.params.term] || 'day';
|
|
||||||
var uid;
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
if (req.query.token && req.query.uid) {
|
|
||||||
db.getObjectField('user:' + req.query.uid, 'rss_token', next);
|
|
||||||
} else {
|
|
||||||
next(null, null);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
function (token, next) {
|
|
||||||
uid = token && token === req.query.token ? req.query.uid : req.uid;
|
|
||||||
|
|
||||||
topics.getSortedTopics({
|
|
||||||
uid: uid,
|
|
||||||
start: 0,
|
|
||||||
stop: 19,
|
|
||||||
term: term,
|
|
||||||
sort: 'posts',
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (result, next) {
|
|
||||||
generateTopicsFeed({
|
|
||||||
uid: uid,
|
|
||||||
title: 'Popular Topics',
|
|
||||||
description: 'A list of topics that are sorted by post count',
|
|
||||||
feed_url: '/popular/' + (req.params.term || 'daily') + '.rss',
|
|
||||||
site_url: '/popular/' + (req.params.term || 'daily'),
|
|
||||||
}, result.topics, next);
|
|
||||||
},
|
|
||||||
function (feed) {
|
|
||||||
sendFeed(feed, res);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendTopicsFeed(options, set, req, res, next) {
|
|
||||||
var start = options.hasOwnProperty('start') ? options.start : 0;
|
|
||||||
var stop = options.hasOwnProperty('stop') ? options.stop : 19;
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
topics.getTopicsFromSet(set, options.uid, start, stop, next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
generateTopicsFeed(options, data.topics, next);
|
|
||||||
},
|
|
||||||
function (feed) {
|
|
||||||
sendFeed(feed, res);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateTopicsFeed(feedOptions, feedTopics, callback) {
|
|
||||||
feedOptions.ttl = 60;
|
feedOptions.ttl = 60;
|
||||||
feedOptions.feed_url = nconf.get('url') + feedOptions.feed_url;
|
feedOptions.feed_url = nconf.get('url') + feedOptions.feed_url;
|
||||||
feedOptions.site_url = nconf.get('url') + feedOptions.site_url;
|
feedOptions.site_url = nconf.get('url') + feedOptions.site_url;
|
||||||
|
|
||||||
feedTopics = feedTopics.filter(Boolean);
|
feedTopics = feedTopics.filter(Boolean);
|
||||||
|
|
||||||
var feed = new rss(feedOptions);
|
const feed = new rss(feedOptions);
|
||||||
|
|
||||||
if (feedTopics.length > 0) {
|
if (feedTopics.length > 0) {
|
||||||
feed.pubDate = new Date(feedTopics[0].lastposttime).toUTCString();
|
feed.pubDate = new Date(feedTopics[0].lastposttime).toUTCString();
|
||||||
}
|
}
|
||||||
|
|
||||||
async.eachSeries(feedTopics, function (topicData, next) {
|
for (const topicData of feedTopics) {
|
||||||
var feedItem = {
|
/* eslint-disable no-await-in-loop */
|
||||||
|
const feedItem = {
|
||||||
title: utils.stripHTMLTags(topicData.title, utils.tags),
|
title: utils.stripHTMLTags(topicData.title, utils.tags),
|
||||||
url: nconf.get('url') + '/topic/' + topicData.slug,
|
url: nconf.get('url') + '/topic/' + topicData.slug,
|
||||||
date: new Date(topicData.lastposttime).toUTCString(),
|
date: new Date(topicData.lastposttime).toUTCString(),
|
||||||
@@ -368,89 +284,62 @@ function generateTopicsFeed(feedOptions, feedTopics, callback) {
|
|||||||
feedItem.description = topicData.teaser.content;
|
feedItem.description = topicData.teaser.content;
|
||||||
feedItem.author = topicData.teaser.user.username;
|
feedItem.author = topicData.teaser.user.username;
|
||||||
feed.item(feedItem);
|
feed.item(feedItem);
|
||||||
return next();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
topics.getMainPost(topicData.tid, feedOptions.uid, function (err, mainPost) {
|
const mainPost = await topics.getMainPost(topicData.tid, feedOptions.uid);
|
||||||
if (err) {
|
if (!mainPost) {
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
if (!mainPost) {
|
|
||||||
feed.item(feedItem);
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
feedItem.description = mainPost.content;
|
|
||||||
feedItem.author = mainPost.user.username;
|
|
||||||
feed.item(feedItem);
|
feed.item(feedItem);
|
||||||
next();
|
return;
|
||||||
});
|
}
|
||||||
}, function (err) {
|
feedItem.description = mainPost.content;
|
||||||
callback(err, feed);
|
feedItem.author = mainPost.user && mainPost.user.username;
|
||||||
});
|
feed.item(feedItem);
|
||||||
|
}
|
||||||
|
return feed;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForRecentPosts(req, res, next) {
|
async function generateForRecentPosts(req, res) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
if (meta.config['feeds:disableRSS']) {
|
||||||
return controllers404.send404(req, res);
|
return controllers404.send404(req, res);
|
||||||
}
|
}
|
||||||
|
const postData = await posts.getRecentPosts(req.uid, 0, 19, 'month');
|
||||||
|
const feed = generateForPostsFeed({
|
||||||
|
title: 'Recent Posts',
|
||||||
|
description: 'A list of recent posts',
|
||||||
|
feed_url: '/recentposts.rss',
|
||||||
|
site_url: '/recentposts',
|
||||||
|
}, postData);
|
||||||
|
|
||||||
async.waterfall([
|
sendFeed(feed, res);
|
||||||
function (next) {
|
|
||||||
posts.getRecentPosts(req.uid, 0, 19, 'month', next);
|
|
||||||
},
|
|
||||||
function (posts) {
|
|
||||||
var feed = generateForPostsFeed({
|
|
||||||
title: 'Recent Posts',
|
|
||||||
description: 'A list of recent posts',
|
|
||||||
feed_url: '/recentposts.rss',
|
|
||||||
site_url: '/recentposts',
|
|
||||||
}, posts);
|
|
||||||
|
|
||||||
sendFeed(feed, res);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForCategoryRecentPosts(req, res, callback) {
|
async function generateForCategoryRecentPosts(req, res) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
if (meta.config['feeds:disableRSS']) {
|
||||||
return controllers404.send404(req, res);
|
return controllers404.send404(req, res);
|
||||||
}
|
}
|
||||||
var cid = req.params.category_id;
|
const cid = req.params.category_id;
|
||||||
var category;
|
|
||||||
var posts;
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
async.parallel({
|
|
||||||
privileges: function (next) {
|
|
||||||
privileges.categories.get(cid, req.uid, next);
|
|
||||||
},
|
|
||||||
category: function (next) {
|
|
||||||
categories.getCategoryData(cid, next);
|
|
||||||
},
|
|
||||||
posts: function (next) {
|
|
||||||
categories.getRecentReplies(cid, req.uid || req.query.uid || 0, 20, next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
if (!results.category) {
|
|
||||||
return controllers404.send404(req, res);
|
|
||||||
}
|
|
||||||
category = results.category;
|
|
||||||
posts = results.posts;
|
|
||||||
validateTokenIfRequiresLogin(!results.privileges.read, cid, req, res, next);
|
|
||||||
},
|
|
||||||
function () {
|
|
||||||
var feed = generateForPostsFeed({
|
|
||||||
title: category.name + ' Recent Posts',
|
|
||||||
description: 'A list of recent posts from ' + category.name,
|
|
||||||
feed_url: '/category/' + cid + '/recentposts.rss',
|
|
||||||
site_url: '/category/' + cid + '/recentposts',
|
|
||||||
}, posts);
|
|
||||||
|
|
||||||
sendFeed(feed, res);
|
const [userPrivileges, category, postData] = await Promise.all([
|
||||||
},
|
privileges.categories.get(cid, req.uid),
|
||||||
], callback);
|
categories.getCategoryData(cid),
|
||||||
|
categories.getRecentReplies(cid, req.uid || req.query.uid || 0, 20),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!category) {
|
||||||
|
return controllers404.send404(req, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await validateTokenIfRequiresLogin(!userPrivileges.read, cid, req, res)) {
|
||||||
|
const feed = generateForPostsFeed({
|
||||||
|
title: category.name + ' Recent Posts',
|
||||||
|
description: 'A list of recent posts from ' + category.name,
|
||||||
|
feed_url: '/category/' + cid + '/recentposts.rss',
|
||||||
|
site_url: '/category/' + cid + '/recentposts',
|
||||||
|
}, postData);
|
||||||
|
|
||||||
|
sendFeed(feed, res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForPostsFeed(feedOptions, posts) {
|
function generateForPostsFeed(feedOptions, posts) {
|
||||||
@@ -477,45 +366,36 @@ function generateForPostsFeed(feedOptions, posts) {
|
|||||||
return feed;
|
return feed;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForUserTopics(req, res, callback) {
|
async function generateForUserTopics(req, res, next) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
if (meta.config['feeds:disableRSS']) {
|
||||||
return controllers404.send404(req, res);
|
return controllers404.send404(req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
var userslug = req.params.userslug;
|
const userslug = req.params.userslug;
|
||||||
|
const uid = await user.getUidByUserslug(userslug);
|
||||||
async.waterfall([
|
if (!uid) {
|
||||||
function (next) {
|
return next();
|
||||||
user.getUidByUserslug(userslug, next);
|
}
|
||||||
},
|
const userData = await user.getUserFields(uid, ['uid', 'username']);
|
||||||
function (uid, next) {
|
await sendTopicsFeed({
|
||||||
if (!uid) {
|
uid: req.uid,
|
||||||
return callback();
|
title: 'Topics by ' + userData.username,
|
||||||
}
|
description: 'A list of topics that are posted by ' + userData.username,
|
||||||
user.getUserFields(uid, ['uid', 'username'], next);
|
feed_url: '/user/' + userslug + '/topics.rss',
|
||||||
},
|
site_url: '/user/' + userslug + '/topics',
|
||||||
function (userData, next) {
|
}, 'uid:' + userData.uid + ':topics', res);
|
||||||
sendTopicsFeed({
|
|
||||||
uid: req.uid,
|
|
||||||
title: 'Topics by ' + userData.username,
|
|
||||||
description: 'A list of topics that are posted by ' + userData.username,
|
|
||||||
feed_url: '/user/' + userslug + '/topics.rss',
|
|
||||||
site_url: '/user/' + userslug + '/topics',
|
|
||||||
}, 'uid:' + userData.uid + ':topics', req, res, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateForTag(req, res, next) {
|
async function generateForTag(req, res) {
|
||||||
if (meta.config['feeds:disableRSS']) {
|
if (meta.config['feeds:disableRSS']) {
|
||||||
return controllers404.send404(req, res);
|
return controllers404.send404(req, res);
|
||||||
}
|
}
|
||||||
var tag = validator.escape(String(req.params.tag));
|
const tag = validator.escape(String(req.params.tag));
|
||||||
var page = parseInt(req.query.page, 10) || 1;
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
var topicsPerPage = meta.config.topicsPerPage || 20;
|
const topicsPerPage = meta.config.topicsPerPage || 20;
|
||||||
var start = Math.max(0, (page - 1) * topicsPerPage);
|
const start = Math.max(0, (page - 1) * topicsPerPage);
|
||||||
var stop = start + topicsPerPage - 1;
|
const stop = start + topicsPerPage - 1;
|
||||||
sendTopicsFeed({
|
await sendTopicsFeed({
|
||||||
uid: req.uid,
|
uid: req.uid,
|
||||||
title: 'Topics tagged with ' + tag,
|
title: 'Topics tagged with ' + tag,
|
||||||
description: 'A list of topics that have been tagged with ' + tag,
|
description: 'A list of topics that have been tagged with ' + tag,
|
||||||
@@ -523,10 +403,10 @@ function generateForTag(req, res, next) {
|
|||||||
site_url: '/tags/' + tag,
|
site_url: '/tags/' + tag,
|
||||||
start: start,
|
start: start,
|
||||||
stop: stop,
|
stop: stop,
|
||||||
}, 'tag:' + tag + ':topics', req, res, next);
|
}, 'tag:' + tag + ':topics', res);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendFeed(feed, res) {
|
function sendFeed(feed, res) {
|
||||||
var xml = feed.xml();
|
const xml = feed.xml();
|
||||||
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user