You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/sitemap.js

114 lines
2.7 KiB
JavaScript

'use strict';
var path = require('path'),
async = require('async'),
sm = require('sitemap'),
url = require('url'),
nconf = require('nconf'),
db = require('./database'),
categories = require('./categories'),
topics = require('./topics'),
privileges = require('./privileges'),
11 years ago
utils = require('../public/src/utils'),
sitemap = {
11 years ago
obj: undefined,
getStaticUrls: function(callback) {
callback(null, [{
url: '',
changefreq: 'weekly',
priority: '0.6'
}, {
11 years ago
url: '/recent',
changefreq: 'daily',
priority: '0.4'
}, {
11 years ago
url: '/users',
changefreq: 'daily',
priority: '0.4'
}]);
},
getDynamicUrls: function(callback) {
var returnUrls = [];
10 years ago
async.parallel({
categoryUrls: function(next) {
var categoryUrls = [];
11 years ago
categories.getCategoriesByPrivilege(0, 'find', function(err, categoriesData) {
if (err) {
return next(err);
}
11 years ago
categoriesData.forEach(function(category) {
10 years ago
if (category) {
categoryUrls.push({
url: '/category/' + category.cid + '/' + encodeURIComponent(utils.slugify(category.name)),
changefreq: 'weekly',
priority: '0.4'
});
}
});
next(null, categoryUrls);
});
},
10 years ago
topicUrls: function(next) {
var topicUrls = [];
db.getSortedSetRevRange('topics:recent', 0, 49, function(err, tids) {
if (err) {
return next(err);
}
privileges.topics.filter('read', tids, 0, function(err, tids) {
if (err) {
return next(err);
}
10 years ago
topics.getTopicsFields(tids, ['tid', 'title', 'lastposttime'], function(err, topics) {
if (err) {
return next(err);
}
11 years ago
topics.forEach(function(topic) {
10 years ago
if (topic) {
topicUrls.push({
url: '/topic/' + topic.tid + '/' + encodeURIComponent(utils.slugify(topic.title)),
lastmodISO: utils.toISOString(topic.lastposttime),
changefreq: 'daily',
priority: '0.6'
});
}
});
next(null, topicUrls);
});
});
});
}
10 years ago
}, function(err, data) {
if (!err) {
10 years ago
returnUrls = data.categoryUrls.concat(data.topicUrls);
}
callback(err, returnUrls);
});
},
render: function(callback) {
11 years ago
if (sitemap.obj !== undefined && sitemap.obj.cache.length) {
10 years ago
return sitemap.obj.toXML(callback);
}
11 years ago
10 years ago
async.parallel([sitemap.getStaticUrls, sitemap.getDynamicUrls], function(err, urls) {
urls = urls[0].concat(urls[1]);
sitemap.obj = sm.createSitemap({
hostname: nconf.get('url'),
cacheTime: 1000 * 60 * 60, // Cached for 1 hour
urls: urls
11 years ago
});
10 years ago
sitemap.obj.toXML(callback);
});
}
};
module.exports = sitemap;