refactor: sitemap to async/await

v1.18.x
Baris Usakli 6 years ago
parent e291a60964
commit 0164e51f9f

@ -1,53 +1,45 @@
'use strict';
var async = require('async');
const { Sitemap } = require('sitemap');
var nconf = require('nconf');
const nconf = require('nconf');
var db = require('./database');
var categories = require('./categories');
var topics = require('./topics');
var privileges = require('./privileges');
var meta = require('./meta');
var plugins = require('./plugins');
var utils = require('./utils');
const db = require('./database');
const categories = require('./categories');
const topics = require('./topics');
const privileges = require('./privileges');
const meta = require('./meta');
const plugins = require('./plugins');
const utils = require('./utils');
var sitemap = module.exports;
const sitemap = module.exports;
sitemap.maps = {
topics: [],
};
sitemap.render = function (callback) {
var topicsPerPage = meta.config.sitemapTopics;
var returnData = {
sitemap.render = async function () {
const topicsPerPage = meta.config.sitemapTopics;
const returnData = {
url: nconf.get('url'),
topics: [],
};
async.waterfall([
function (next) {
db.getObjectField('global', 'topicCount', next);
},
function (topicCount, next) {
var numPages = Math.ceil(Math.max(0, topicCount / topicsPerPage));
const topicCount = await db.getObjectField('global', 'topicCount');
const numPages = Math.ceil(Math.max(0, topicCount / topicsPerPage));
for (var x = 1; x <= numPages; x += 1) {
returnData.topics.push(x);
}
next(null, returnData);
},
], callback);
return returnData;
};
sitemap.getPages = function (callback) {
sitemap.getPages = async function () {
if (
sitemap.maps.pages &&
Date.now() < parseInt(sitemap.maps.pages.cacheSetTimestamp, 10) + parseInt(sitemap.maps.pages.cacheResetPeriod, 10)
) {
return callback(null, sitemap.maps.pages.toXML());
return sitemap.maps.pages.toXML();
}
var urls = [{
const urls = [{
url: '',
changefreq: 'weekly',
priority: 0.6,
@ -65,34 +57,26 @@ sitemap.getPages = function (callback) {
priority: 0.4,
}];
plugins.fireHook('filter:sitemap.getPages', { urls: urls }, function (err, data) {
if (err) {
return callback(err);
}
const data = await plugins.fireHook('filter:sitemap.getPages', { urls: urls });
sitemap.maps.pages = new Sitemap({
hostname: nconf.get('url'),
cacheTime: 1000 * 60 * 60 * 24, // Cached for 24 hours
urls: data.urls,
});
callback(null, sitemap.maps.pages.toXML());
});
return sitemap.maps.pages.toXML();
};
sitemap.getCategories = function (callback) {
sitemap.getCategories = async function () {
if (
sitemap.maps.categories &&
Date.now() < parseInt(sitemap.maps.categories.cacheSetTimestamp, 10) + parseInt(sitemap.maps.categories.cacheResetPeriod, 10)
) {
return callback(null, sitemap.maps.categories.toXML());
}
var categoryUrls = [];
categories.getCategoriesByPrivilege('categories:cid', 0, 'find', function (err, categoriesData) {
if (err) {
return callback(err);
return sitemap.maps.categories.toXML();
}
const categoryUrls = [];
const categoriesData = await categories.getCategoriesByPrivilege('categories:cid', 0, 'find');
categoriesData.forEach(function (category) {
if (category) {
categoryUrls.push({
@ -109,44 +93,31 @@ sitemap.getCategories = function (callback) {
urls: categoryUrls,
});
callback(null, sitemap.maps.categories.toXML());
});
return sitemap.maps.categories.toXML();
};
sitemap.getTopicPage = function (page, callback) {
sitemap.getTopicPage = async function (page) {
if (parseInt(page, 10) <= 0) {
return callback();
return;
}
var numTopics = meta.config.sitemapTopics;
var min = (parseInt(page, 10) - 1) * numTopics;
var max = min + numTopics;
const numTopics = meta.config.sitemapTopics;
const min = (parseInt(page, 10) - 1) * numTopics;
const max = min + numTopics;
if (
sitemap.maps.topics[page - 1] &&
Date.now() < parseInt(sitemap.maps.topics[page - 1].cacheSetTimestamp, 10) + parseInt(sitemap.maps.topics[page - 1].cacheResetPeriod, 10)
) {
return callback(null, sitemap.maps.topics[page - 1].toXML());
return sitemap.maps.topics[page - 1].toXML();
}
var topicUrls = [];
async.waterfall([
function (next) {
db.getSortedSetRevRange('topics:recent', min, max, next);
},
function (tids, next) {
privileges.topics.filterTids('topics:read', tids, 0, next);
},
function (tids, next) {
topics.getTopicsFields(tids, ['tid', 'title', 'slug', 'lastposttime'], next);
},
], function (err, topics) {
if (err) {
return callback(err);
}
const topicUrls = [];
let tids = await db.getSortedSetRevRange('topics:recent', min, max);
tids = await privileges.topics.filterTids('topics:read', tids, 0);
const topicData = await topics.getTopicsFields(tids, ['tid', 'title', 'slug', 'lastposttime']);
topics.forEach(function (topic) {
topicData.forEach(function (topic) {
if (topic) {
topicUrls.push({
url: '/topic/' + topic.slug,
@ -163,8 +134,7 @@ sitemap.getTopicPage = function (page, callback) {
urls: topicUrls,
});
callback(null, sitemap.maps.topics[page - 1].toXML());
});
return sitemap.maps.topics[page - 1].toXML();
};
sitemap.clearCache = function () {

Loading…
Cancel
Save