one final push, cleanup + organize + lint; made feeds/meta/plugins routes follow same pattern as other route files
parent
1dbc47b890
commit
c0cd6148f4
@ -1,201 +1,202 @@
|
|||||||
(function (Feeds) {
|
"use strict";
|
||||||
var posts = require('../posts'),
|
|
||||||
topics = require('../topics'),
|
|
||||||
categories = require('../categories'),
|
|
||||||
|
|
||||||
rss = require('rss'),
|
var posts = require('../posts'),
|
||||||
nconf = require('nconf'),
|
topics = require('../topics'),
|
||||||
|
categories = require('../categories'),
|
||||||
|
|
||||||
ThreadTools = require('../threadTools'),
|
rss = require('rss'),
|
||||||
CategoryTools = require('../categoryTools');
|
nconf = require('nconf'),
|
||||||
|
|
||||||
Feeds.createRoutes = function(app){
|
ThreadTools = require('../threadTools'),
|
||||||
app.get('/topic/:topic_id.rss', hasTopicPrivileges, generateForTopic);
|
CategoryTools = require('../categoryTools');
|
||||||
app.get('/category/:category_id.rss', hasCategoryPrivileges, generateForCategory);
|
|
||||||
app.get('/recent.rss', generateForRecent);
|
|
||||||
app.get('/popular.rss', generateForPopular);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
function hasTopicPrivileges(req, res, next) {
|
||||||
|
var tid = req.params.topic_id;
|
||||||
|
var uid = req.user ? req.user.uid || 0 : 0;
|
||||||
|
|
||||||
function hasTopicPrivileges(req, res, next) {
|
ThreadTools.privileges(tid, uid, function(err, privileges) {
|
||||||
var tid = req.params.topic_id;
|
if(err) {
|
||||||
var uid = req.user ? req.user.uid || 0 : 0;
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
ThreadTools.privileges(tid, uid, function(err, privileges) {
|
if(!privileges.read) {
|
||||||
if(err) {
|
return res.redirect('403');
|
||||||
return next(err);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(!privileges.read) {
|
return next();
|
||||||
return res.redirect('403');
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return next();
|
function hasCategoryPrivileges(req, res, next) {
|
||||||
});
|
var cid = req.params.category_id;
|
||||||
}
|
var uid = req.user ? req.user.uid || 0 : 0;
|
||||||
|
|
||||||
function hasCategoryPrivileges(req, res, next) {
|
CategoryTools.privileges(cid, uid, function(err, privileges) {
|
||||||
var cid = req.params.category_id;
|
if(err) {
|
||||||
var uid = req.user ? req.user.uid || 0 : 0;
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
CategoryTools.privileges(cid, uid, function(err, privileges) {
|
if(!privileges.read) {
|
||||||
if(err) {
|
return res.redirect('403');
|
||||||
return next(err);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(!privileges.read) {
|
return next();
|
||||||
return res.redirect('403');
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return next();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
function generateForTopic(req, res, next) {
|
||||||
|
var tid = req.params.topic_id;
|
||||||
|
|
||||||
function generateForTopic(req, res, next) {
|
topics.getTopicWithPosts(tid, 0, 0, 25, function (err, topicData) {
|
||||||
var tid = req.params.topic_id;
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
topics.getTopicWithPosts(tid, 0, 0, 25, function (err, topicData) {
|
var description = topicData.posts.length ? topicData.posts[0].content : '';
|
||||||
if (err) {
|
var image_url = topicData.posts.length ? topicData.posts[0].picture : '';
|
||||||
return next(err);
|
var author = topicData.posts.length ? topicData.posts[0].username : '';
|
||||||
}
|
|
||||||
|
|
||||||
var description = topicData.posts.length ? topicData.posts[0].content : '';
|
var feed = new rss({
|
||||||
var image_url = topicData.posts.length ? topicData.posts[0].picture : '';
|
title: topicData.title,
|
||||||
var author = topicData.posts.length ? topicData.posts[0].username : '';
|
description: description,
|
||||||
|
feed_url: nconf.get('url') + '/topic/' + tid + '.rss',
|
||||||
var feed = new rss({
|
site_url: nconf.get('url') + '/topic/' + topicData.slug,
|
||||||
title: topicData.title,
|
image_url: image_url,
|
||||||
description: description,
|
author: author,
|
||||||
feed_url: nconf.get('url') + '/topic/' + tid + '.rss',
|
ttl: 60
|
||||||
site_url: nconf.get('url') + '/topic/' + topicData.slug,
|
}),
|
||||||
image_url: image_url,
|
dateStamp;
|
||||||
author: author,
|
|
||||||
ttl: 60
|
|
||||||
}),
|
|
||||||
dateStamp;
|
|
||||||
|
|
||||||
// Add pubDate if topic contains posts
|
|
||||||
if (topicData.posts.length > 0) {
|
|
||||||
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
|
||||||
}
|
|
||||||
|
|
||||||
topicData.posts.forEach(function(postData) {
|
// Add pubDate if topic contains posts
|
||||||
if (parseInt(postData.deleted, 10) === 0) {
|
if (topicData.posts.length > 0) {
|
||||||
dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString();
|
feed.pubDate = new Date(parseInt(topicData.posts[0].timestamp, 10)).toUTCString();
|
||||||
|
}
|
||||||
feed.item({
|
|
||||||
title: 'Reply to ' + topicData.title + ' on ' + dateStamp,
|
|
||||||
description: postData.content,
|
|
||||||
url: nconf.get('url') + '/topic/' + topicData.slug + '#' + postData.pid,
|
|
||||||
author: postData.username,
|
|
||||||
date: dateStamp
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var xml = feed.xml();
|
topicData.posts.forEach(function(postData) {
|
||||||
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
if (parseInt(postData.deleted, 10) === 0) {
|
||||||
});
|
dateStamp = new Date(parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)).toUTCString();
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
function generateForCategory(req, res, next) {
|
|
||||||
var cid = req.params.category_id;
|
|
||||||
|
|
||||||
categories.getCategoryById(cid, 0, 25, 0, function (err, categoryData) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var feed = new rss({
|
|
||||||
title: categoryData.name,
|
|
||||||
description: categoryData.description,
|
|
||||||
feed_url: nconf.get('url') + '/category/' + cid + '.rss',
|
|
||||||
site_url: nconf.get('url') + '/category/' + categoryData.cid,
|
|
||||||
ttl: 60
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add pubDate if category has topics
|
|
||||||
if (categoryData.topics.length > 0) feed.pubDate = new Date(parseInt(categoryData.topics[0].lastposttime, 10)).toUTCString();
|
|
||||||
|
|
||||||
categoryData.topics.forEach(function(topicData) {
|
|
||||||
feed.item({
|
feed.item({
|
||||||
title: topicData.title,
|
title: 'Reply to ' + topicData.title + ' on ' + dateStamp,
|
||||||
url: nconf.get('url') + '/topic/' + topicData.slug,
|
description: postData.content,
|
||||||
author: topicData.username,
|
url: nconf.get('url') + '/topic/' + topicData.slug + '#' + postData.pid,
|
||||||
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
|
author: postData.username,
|
||||||
|
date: dateStamp
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
var xml = feed.xml();
|
|
||||||
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
function generateForRecent(req, res, next) {
|
var xml = feed.xml();
|
||||||
topics.getLatestTopics(0, 0, 19, 'month', function (err, recentData) {
|
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
||||||
if(err){
|
});
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var feed = new rss({
|
}
|
||||||
title: 'Recently Active Topics',
|
|
||||||
description: 'A list of topics that have been active within the past 24 hours',
|
|
||||||
feed_url: nconf.get('url') + '/recent.rss',
|
|
||||||
site_url: nconf.get('url') + '/recent',
|
|
||||||
ttl: 60
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add pubDate if recent topics list contains topics
|
function generateForCategory(req, res, next) {
|
||||||
if (recentData.topics.length > 0) {
|
var cid = req.params.category_id;
|
||||||
feed.pubDate = new Date(parseInt(recentData.topics[0].lastposttime, 10)).toUTCString();
|
|
||||||
}
|
|
||||||
|
|
||||||
recentData.topics.forEach(function(topicData) {
|
categories.getCategoryById(cid, 0, 25, 0, function (err, categoryData) {
|
||||||
feed.item({
|
if (err) {
|
||||||
title: topicData.title,
|
return next(err);
|
||||||
url: nconf.get('url') + '/topic/' + topicData.slug,
|
}
|
||||||
author: topicData.username,
|
|
||||||
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
|
var feed = new rss({
|
||||||
});
|
title: categoryData.name,
|
||||||
|
description: categoryData.description,
|
||||||
|
feed_url: nconf.get('url') + '/category/' + cid + '.rss',
|
||||||
|
site_url: nconf.get('url') + '/category/' + categoryData.cid,
|
||||||
|
ttl: 60
|
||||||
});
|
});
|
||||||
|
|
||||||
var xml = feed.xml();
|
// Add pubDate if category has topics
|
||||||
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
if (categoryData.topics.length > 0) {
|
||||||
|
feed.pubDate = new Date(parseInt(categoryData.topics[0].lastposttime, 10)).toUTCString();
|
||||||
|
}
|
||||||
|
|
||||||
|
categoryData.topics.forEach(function(topicData) {
|
||||||
|
feed.item({
|
||||||
|
title: topicData.title,
|
||||||
|
url: nconf.get('url') + '/topic/' + topicData.slug,
|
||||||
|
author: topicData.username,
|
||||||
|
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
function generateForPopular(req, res, next) {
|
|
||||||
topics.getTopicsFromSet(0, 'topics:posts', 0, 19, function (err, popularData) {
|
|
||||||
if(err){
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var feed = new rss({
|
var xml = feed.xml();
|
||||||
title: 'Popular Topics',
|
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
||||||
description: 'A list of topics that are sorted by post count',
|
});
|
||||||
feed_url: nconf.get('url') + '/popular.rss',
|
}
|
||||||
site_url: nconf.get('url') + '/popular',
|
|
||||||
ttl: 60
|
function generateForRecent(req, res, next) {
|
||||||
});
|
topics.getLatestTopics(0, 0, 19, 'month', function (err, recentData) {
|
||||||
|
if(err){
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
var feed = new rss({
|
||||||
|
title: 'Recently Active Topics',
|
||||||
|
description: 'A list of topics that have been active within the past 24 hours',
|
||||||
|
feed_url: nconf.get('url') + '/recent.rss',
|
||||||
|
site_url: nconf.get('url') + '/recent',
|
||||||
|
ttl: 60
|
||||||
|
});
|
||||||
|
|
||||||
// Add pubDate if recent topics list contains topics
|
// Add pubDate if recent topics list contains topics
|
||||||
if (popularData.topics.length > 0) {
|
if (recentData.topics.length > 0) {
|
||||||
feed.pubDate = new Date(parseInt(popularData.topics[0].lastposttime, 10)).toUTCString();
|
feed.pubDate = new Date(parseInt(recentData.topics[0].lastposttime, 10)).toUTCString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recentData.topics.forEach(function(topicData) {
|
||||||
|
feed.item({
|
||||||
|
title: topicData.title,
|
||||||
|
url: nconf.get('url') + '/topic/' + topicData.slug,
|
||||||
|
author: topicData.username,
|
||||||
|
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
popularData.topics.forEach(function(topicData) {
|
var xml = feed.xml();
|
||||||
feed.item({
|
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
||||||
title: topicData.title,
|
});
|
||||||
url: nconf.get('url') + '/topic/' + topicData.slug,
|
}
|
||||||
author: topicData.username,
|
|
||||||
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
|
function generateForPopular(req, res, next) {
|
||||||
});
|
topics.getTopicsFromSet(0, 'topics:posts', 0, 19, function (err, popularData) {
|
||||||
|
if(err){
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
var feed = new rss({
|
||||||
|
title: 'Popular Topics',
|
||||||
|
description: 'A list of topics that are sorted by post count',
|
||||||
|
feed_url: nconf.get('url') + '/popular.rss',
|
||||||
|
site_url: nconf.get('url') + '/popular',
|
||||||
|
ttl: 60
|
||||||
});
|
});
|
||||||
|
|
||||||
var xml = feed.xml();
|
// Add pubDate if recent topics list contains topics
|
||||||
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
if (popularData.topics.length > 0) {
|
||||||
|
feed.pubDate = new Date(parseInt(popularData.topics[0].lastposttime, 10)).toUTCString();
|
||||||
|
}
|
||||||
|
|
||||||
|
popularData.topics.forEach(function(topicData) {
|
||||||
|
feed.item({
|
||||||
|
title: topicData.title,
|
||||||
|
url: nconf.get('url') + '/topic/' + topicData.slug,
|
||||||
|
author: topicData.username,
|
||||||
|
date: new Date(parseInt(topicData.lastposttime, 10)).toUTCString()
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
|
||||||
}(exports));
|
var xml = feed.xml();
|
||||||
|
res.type('xml').set('Content-Length', Buffer.byteLength(xml)).send(xml);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = function(app, middleware, controllers){
|
||||||
|
app.get('/topic/:topic_id.rss', hasTopicPrivileges, generateForTopic);
|
||||||
|
app.get('/category/:category_id.rss', hasCategoryPrivileges, generateForCategory);
|
||||||
|
app.get('/recent.rss', generateForRecent);
|
||||||
|
app.get('/popular.rss', generateForPopular);
|
||||||
|
};
|
@ -1,67 +1,78 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
var path = require('path'),
|
var path = require('path'),
|
||||||
nconf = require('nconf'),
|
nconf = require('nconf'),
|
||||||
less = require('less'),
|
less = require('less'),
|
||||||
|
|
||||||
meta = require('../meta'),
|
meta = require('../meta'),
|
||||||
db = require('../database'),
|
db = require('../database'),
|
||||||
plugins = require('../plugins');
|
plugins = require('../plugins'),
|
||||||
|
|
||||||
(function (Meta) {
|
minificationEnabled = false;
|
||||||
Meta.createRoutes = function(app) {
|
|
||||||
app.get('/stylesheet.css', function(req, res) {
|
|
||||||
if (meta.css.cache) {
|
function sendMinifiedJS(req, res, next) {
|
||||||
res.type('text/css').send(200, meta.css.cache);
|
function sendCached() {
|
||||||
return;
|
return res.type('text/javascript').send(meta.js.cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) {
|
if (meta.js.cache) {
|
||||||
var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'),
|
sendCached();
|
||||||
baseThemePath = path.join(nconf.get('themes_path'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla')),
|
} else {
|
||||||
paths = [baseThemePath, path.join(__dirname, '../../node_modules')],
|
if (minificationEnabled) {
|
||||||
source = '@import "./theme";',
|
meta.js.minify(function() {
|
||||||
x, numLESS;
|
sendCached();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Compress only
|
||||||
|
meta.js.concatenate(function() {
|
||||||
|
sendCached();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add the imports for each LESS file
|
function sendStylesheet(req, res, next) {
|
||||||
for(x=0,numLESS=plugins.lessFiles.length;x<numLESS;x++) {
|
if (meta.css.cache) {
|
||||||
source += '\n@import "./' + plugins.lessFiles[x] + '";';
|
res.type('text/css').send(200, meta.css.cache);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var parser = new (less.Parser)({
|
db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) {
|
||||||
paths: paths
|
var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'),
|
||||||
});
|
baseThemePath = path.join(nconf.get('themes_path'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla')),
|
||||||
|
paths = [baseThemePath, path.join(__dirname, '../../node_modules')],
|
||||||
|
source = '@import "./theme";',
|
||||||
|
x, numLESS;
|
||||||
|
|
||||||
parser.parse(source, function(err, tree) {
|
// Add the imports for each LESS file
|
||||||
if (err) {
|
for(x=0,numLESS=plugins.lessFiles.length;x<numLESS;x++) {
|
||||||
res.send(500, err.message);
|
source += '\n@import "./' + plugins.lessFiles[x] + '";';
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
meta.css.cache = tree.toCSS({
|
var parser = new (less.Parser)({
|
||||||
compress: true
|
paths: paths
|
||||||
});
|
|
||||||
res.type('text/css').send(200, meta.css.cache);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/nodebb.min.js', function(req, res) {
|
parser.parse(source, function(err, tree) {
|
||||||
var sendCached = function() {
|
if (err) {
|
||||||
return res.type('text/javascript').send(meta.js.cache);
|
res.send(500, err.message);
|
||||||
}
|
return;
|
||||||
if (meta.js.cache) {
|
|
||||||
sendCached();
|
|
||||||
} else {
|
|
||||||
if (app.enabled('minification')) {
|
|
||||||
meta.js.minify(function() {
|
|
||||||
sendCached();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Compress only
|
|
||||||
meta.js.concatenate(function() {
|
|
||||||
sendCached();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
meta.css.cache = tree.toCSS({
|
||||||
|
compress: true
|
||||||
|
});
|
||||||
|
res.type('text/css').send(200, meta.css.cache);
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
})(exports);
|
}
|
||||||
|
|
||||||
|
module.exports = function(app, middleware, controllers) {
|
||||||
|
minificationEnabled = app.enabled('minification');
|
||||||
|
|
||||||
|
app.get('/stylesheet.css', sendStylesheet);
|
||||||
|
app.get('/nodebb.min.js', sendMinifiedJS);
|
||||||
|
app.get('/sitemap.xml', controllers.sitemap);
|
||||||
|
app.get('/robots.txt', controllers.robots);
|
||||||
|
};
|
Loading…
Reference in New Issue