forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.js
189 lines (162 loc) · 5 KB
/
sitemap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
const { SitemapStream, streamToPromise } = require('sitemap');
const nconf = require('nconf');
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');
const sitemap = module.exports;
sitemap.maps = {
topics: [],
};
sitemap.render = async function () {
const topicsPerPage = meta.config.sitemapTopics;
const returnData = {
url: nconf.get('url'),
topics: [],
};
const [topicCount, categories, pages] = await Promise.all([
db.getObjectField('global', 'topicCount'),
getSitemapCategories(),
getSitemapPages(),
]);
returnData.categories = categories.length > 0;
returnData.pages = pages.length > 0;
const numPages = Math.ceil(Math.max(0, topicCount / topicsPerPage));
for (let x = 1; x <= numPages; x += 1) {
returnData.topics.push(x);
}
return returnData;
};
async function getSitemapPages() {
const urls = [{
url: '',
changefreq: 'weekly',
priority: 0.6,
}, {
url: `${nconf.get('relative_path')}/recent`,
changefreq: 'daily',
priority: 0.4,
}, {
url: `${nconf.get('relative_path')}/users`,
changefreq: 'daily',
priority: 0.4,
}, {
url: `${nconf.get('relative_path')}/groups`,
changefreq: 'daily',
priority: 0.4,
}];
const data = await plugins.hooks.fire('filter:sitemap.getPages', { urls: urls });
return data.urls;
}
sitemap.getPages = async function () {
if (sitemap.maps.pages && Date.now() < sitemap.maps.pagesCacheExpireTimestamp) {
return sitemap.maps.pages;
}
const urls = await getSitemapPages();
if (!urls.length) {
sitemap.maps.pages = '';
sitemap.maps.pagesCacheExpireTimestamp = Date.now() + (1000 * 60 * 60 * 24);
return sitemap.maps.pages;
}
sitemap.maps.pages = await urlsToSitemap(urls);
sitemap.maps.pagesCacheExpireTimestamp = Date.now() + (1000 * 60 * 60 * 24);
return sitemap.maps.pages;
};
async function getSitemapCategories() {
const cids = await categories.getCidsByPrivilege('categories:cid', 0, 'find');
const categoryData = await categories.getCategoriesFields(cids, ['slug']);
const data = await plugins.hooks.fire('filter:sitemap.getCategories', {
categories: categoryData,
});
return data.categories;
}
sitemap.getCategories = async function () {
if (sitemap.maps.categories && Date.now() < sitemap.maps.categoriesCacheExpireTimestamp) {
return sitemap.maps.categories;
}
const categoryUrls = [];
const categoriesData = await getSitemapCategories();
categoriesData.forEach((category) => {
if (category) {
categoryUrls.push({
url: `${nconf.get('relative_path')}/category/${category.slug}`,
changefreq: 'weekly',
priority: 0.4,
});
}
});
if (!categoryUrls.length) {
sitemap.maps.categories = '';
sitemap.maps.categoriesCacheExpireTimestamp = Date.now() + (1000 * 60 * 60 * 24);
return sitemap.maps.categories;
}
sitemap.maps.categories = await urlsToSitemap(categoryUrls);
sitemap.maps.categoriesCacheExpireTimestamp = Date.now() + (1000 * 60 * 60 * 24);
return sitemap.maps.categories;
};
sitemap.getTopicPage = async function (page) {
if (parseInt(page, 10) <= 0) {
return;
}
const numTopics = meta.config.sitemapTopics;
const start = (parseInt(page, 10) - 1) * numTopics;
const stop = start + numTopics - 1;
if (sitemap.maps.topics[page - 1] && Date.now() < sitemap.maps.topics[page - 1].cacheExpireTimestamp) {
return sitemap.maps.topics[page - 1].sm;
}
const topicUrls = [];
let tids = await db.getSortedSetRange('topics:tid', start, stop);
tids = await privileges.topics.filterTids('topics:read', tids, 0);
const topicData = await topics.getTopicsFields(tids, ['tid', 'title', 'slug', 'lastposttime']);
const data = await plugins.hooks.fire('filter:sitemap.getCategories', {
page: page,
topics: topicData,
});
if (!data.topics.length) {
sitemap.maps.topics[page - 1] = {
sm: '',
cacheExpireTimestamp: Date.now() + (1000 * 60 * 60 * 24),
};
return sitemap.maps.topics[page - 1].sm;
}
data.topics.forEach((topic) => {
if (topic) {
topicUrls.push({
url: `${nconf.get('relative_path')}/topic/${topic.slug}`,
lastmodISO: utils.toISOString(topic.lastposttime),
changefreq: 'daily',
priority: 0.6,
});
}
});
sitemap.maps.topics[page - 1] = {
sm: await urlsToSitemap(topicUrls),
cacheExpireTimestamp: Date.now() + (1000 * 60 * 60 * 24),
};
return sitemap.maps.topics[page - 1].sm;
};
async function urlsToSitemap(urls) {
if (!urls.length) {
return '';
}
const smStream = new SitemapStream({ hostname: nconf.get('url') });
urls.forEach(url => smStream.write(url));
smStream.end();
return (await streamToPromise(smStream)).toString();
}
sitemap.clearCache = function () {
if (sitemap.maps.pages) {
sitemap.maps.pagesCacheExpireTimestamp = 0;
}
if (sitemap.maps.categories) {
sitemap.maps.categoriesCacheExpireTimestamp = 0;
}
sitemap.maps.topics.forEach((topicMap) => {
topicMap.cacheExpireTimestamp = 0;
});
};
require('./promisify')(sitemap);