-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
category.js
179 lines (155 loc) · 5.67 KB
/
category.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
'use strict';
const nconf = require('nconf');
const db = require('../database');
const privileges = require('../privileges');
const user = require('../user');
const categories = require('../categories');
const meta = require('../meta');
const pagination = require('../pagination');
const helpers = require('./helpers');
const utils = require('../utils');
const translator = require('../translator');
const analytics = require('../analytics');
const categoryController = module.exports;
categoryController.get = async function (req, res, next) {
const cid = req.params.category_id;
if ((req.params.topic_index && !utils.isNumber(req.params.topic_index)) || !utils.isNumber(cid)) {
return next();
}
const [categoryFields, userPrivileges, userSettings, rssToken] = await Promise.all([
categories.getCategoryFields(cid, ['slug', 'disabled', 'topic_count']),
privileges.categories.get(cid, req.uid),
user.getSettings(req.uid),
user.auth.getFeedToken(req.uid),
]);
if (!categoryFields.slug || (categoryFields && categoryFields.disabled)) {
return next();
}
if (!userPrivileges.read) {
return helpers.notAllowed(req, res);
}
if (!res.locals.isAPI && (!req.params.slug || categoryFields.slug !== cid + '/' + req.params.slug) && (categoryFields.slug && categoryFields.slug !== cid + '/')) {
return helpers.redirect(res, '/category/' + categoryFields.slug);
}
const topicCount = categoryFields.topic_count;
let topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) - 1 : 0;
if (topicIndex < 0 || topicIndex > Math.max(topicCount - 1, 0)) {
return helpers.redirect(res, '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : ''));
}
let pageCount = Math.max(1, Math.ceil(topicCount / userSettings.topicsPerPage));
let currentPage = parseInt(req.query.page, 10) || 1;
if (userSettings.usePagination && (currentPage < 1 || currentPage > pageCount)) {
return next();
}
if (!userSettings.usePagination) {
topicIndex = Math.max(0, topicIndex - (Math.ceil(userSettings.topicsPerPage / 2) - 1));
} else if (!req.query.page) {
const index = Math.max(parseInt((topicIndex || 0), 10), 0);
currentPage = Math.ceil((index + 1) / userSettings.topicsPerPage);
topicIndex = 0;
}
const targetUid = await user.getUidByUserslug(req.query.author);
const start = ((currentPage - 1) * userSettings.topicsPerPage) + topicIndex;
const stop = start + userSettings.topicsPerPage - 1;
const categoryData = await categories.getCategoryById({
uid: req.uid,
cid: cid,
start: start,
stop: stop,
sort: req.query.sort || userSettings.categoryTopicSort,
settings: userSettings,
query: req.query,
tag: req.query.tag,
targetUid: targetUid,
});
if (!categoryData) {
return next();
}
categories.modifyTopicsByPrivilege(categoryData.topics, userPrivileges);
if (categoryData.link) {
await db.incrObjectField('category:' + categoryData.cid, 'timesClicked');
return helpers.redirect(res, categoryData.link);
}
await buildBreadcrumbs(req, categoryData);
if (categoryData.children.length) {
const allCategories = [];
categories.flattenCategories(allCategories, categoryData.children);
await categories.getRecentTopicReplies(allCategories, req.uid, req.query);
}
categoryData.title = translator.escape(categoryData.name);
categoryData.description = translator.escape(categoryData.description);
categoryData.privileges = userPrivileges;
categoryData.showSelect = userPrivileges.editable;
categoryData.showTopicTools = userPrivileges.editable;
categoryData.rssFeedUrl = nconf.get('url') + '/category/' + categoryData.cid + '.rss';
if (parseInt(req.uid, 10)) {
categories.markAsRead([cid], req.uid);
categoryData.rssFeedUrl += '?uid=' + req.uid + '&token=' + rssToken;
}
addTags(categoryData, res);
categoryData['feeds:disableRSS'] = meta.config['feeds:disableRSS'] || 0;
categoryData['reputation:disabled'] = meta.config['reputation:disabled'];
pageCount = Math.max(1, Math.ceil(categoryData.topic_count / userSettings.topicsPerPage));
categoryData.pagination = pagination.create(currentPage, pageCount, req.query);
categoryData.pagination.rel.forEach(function (rel) {
rel.href = nconf.get('url') + '/category/' + categoryData.slug + rel.href;
res.locals.linkTags.push(rel);
});
analytics.increment(['pageviews:byCid:' + categoryData.cid]);
res.render('category', categoryData);
};
async function buildBreadcrumbs(req, categoryData) {
const breadcrumbs = [
{
text: categoryData.name,
url: nconf.get('relative_path') + '/category/' + categoryData.slug,
cid: categoryData.cid,
},
];
const crumbs = await helpers.buildCategoryBreadcrumbs(categoryData.parentCid);
if (req.originalUrl.startsWith(nconf.get('relative_path') + '/api/category') || req.originalUrl.startsWith(nconf.get('relative_path') + '/category')) {
categoryData.breadcrumbs = crumbs.concat(breadcrumbs);
}
}
function addTags(categoryData, res) {
res.locals.metaTags = [
{
name: 'title',
content: categoryData.name,
},
{
property: 'og:title',
content: categoryData.name,
},
{
name: 'description',
content: categoryData.description,
},
{
property: 'og:type',
content: 'website',
},
];
if (categoryData.backgroundImage) {
if (!categoryData.backgroundImage.startsWith('http')) {
categoryData.backgroundImage = nconf.get('url') + categoryData.backgroundImage;
}
res.locals.metaTags.push({
property: 'og:image',
content: categoryData.backgroundImage,
});
}
res.locals.linkTags = [
{
rel: 'up',
href: nconf.get('url'),
},
];
if (!categoryData['feeds:disableRSS']) {
res.locals.linkTags.push({
rel: 'alternate',
type: 'application/rss+xml',
href: categoryData.rssFeedUrl,
});
}
}