/
index.js
413 lines (357 loc) · 11.9 KB
/
index.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
'use strict';
const _ = require('lodash');
const db = require('../database');
const user = require('../user');
const topics = require('../topics');
const plugins = require('../plugins');
const privileges = require('../privileges');
const cache = require('../cache');
const meta = require('../meta');
const Categories = module.exports;
require('./data')(Categories);
require('./create')(Categories);
require('./delete')(Categories);
require('./topics')(Categories);
require('./unread')(Categories);
require('./activeusers')(Categories);
require('./recentreplies')(Categories);
require('./update')(Categories);
require('./watch')(Categories);
require('./search')(Categories);
Categories.exists = async function (cids) {
return await db.exists(
Array.isArray(cids) ? cids.map(cid => `category:${cid}`) : `category:${cids}`
);
};
Categories.getCategoryById = async function (data) {
const categories = await Categories.getCategories([data.cid]);
if (!categories[0]) {
return null;
}
const category = categories[0];
data.category = category;
const promises = [
Categories.getCategoryTopics(data),
Categories.getTopicCount(data),
Categories.getWatchState([data.cid], data.uid),
getChildrenTree(category, data.uid),
];
if (category.parentCid) {
promises.push(Categories.getCategoryData(category.parentCid));
}
const [topics, topicCount, watchState, , parent] = await Promise.all(promises);
category.topics = topics.topics;
category.nextStart = topics.nextStart;
category.topic_count = topicCount;
category.isWatched = watchState[0] === Categories.watchStates.watching;
category.isTracked = watchState[0] === Categories.watchStates.tracking;
category.isNotWatched = watchState[0] === Categories.watchStates.notwatching;
category.isIgnored = watchState[0] === Categories.watchStates.ignoring;
category.parent = parent;
calculateTopicPostCount(category);
const result = await plugins.hooks.fire('filter:category.get', {
category: category,
...data,
});
return { ...result.category };
};
Categories.getAllCidsFromSet = async function (key) {
let cids = cache.get(key);
if (cids) {
return cids.slice();
}
cids = await db.getSortedSetRange(key, 0, -1);
cids = cids.map(cid => parseInt(cid, 10));
cache.set(key, cids);
return cids.slice();
};
Categories.getAllCategories = async function () {
const cids = await Categories.getAllCidsFromSet('categories:cid');
return await Categories.getCategories(cids);
};
Categories.getCidsByPrivilege = async function (set, uid, privilege) {
const cids = await Categories.getAllCidsFromSet(set);
return await privileges.categories.filterCids(privilege, cids, uid);
};
Categories.getCategoriesByPrivilege = async function (set, uid, privilege) {
const cids = await Categories.getCidsByPrivilege(set, uid, privilege);
return await Categories.getCategories(cids);
};
Categories.getModerators = async function (cid) {
const uids = await Categories.getModeratorUids([cid]);
return await user.getUsersFields(uids[0], ['uid', 'username', 'userslug', 'picture']);
};
Categories.getModeratorUids = async function (cids) {
return await privileges.categories.getUidsWithPrivilege(cids, 'moderate');
};
Categories.getCategories = async function (cids) {
if (!Array.isArray(cids)) {
throw new Error('[[error:invalid-cid]]');
}
if (!cids.length) {
return [];
}
const [categories, tagWhitelist] = await Promise.all([
Categories.getCategoriesData(cids),
Categories.getTagWhitelist(cids),
]);
categories.forEach((category, i) => {
if (category) {
category.tagWhitelist = tagWhitelist[i];
}
});
return categories;
};
Categories.setUnread = async function (tree, cids, uid) {
if (uid <= 0) {
return;
}
const { unreadCids } = await topics.getUnreadData({
uid: uid,
cid: cids,
});
if (!unreadCids.length) {
return;
}
function setCategoryUnread(category) {
if (category) {
category.unread = false;
if (unreadCids.includes(category.cid)) {
category.unread = category.topic_count > 0 && true;
} else if (category.children.length) {
category.children.forEach(setCategoryUnread);
category.unread = category.children.some(c => c && c.unread);
}
category['unread-class'] = category.unread ? 'unread' : '';
}
}
tree.forEach(setCategoryUnread);
};
Categories.getTagWhitelist = async function (cids) {
const cachedData = {};
const nonCachedCids = cids.filter((cid) => {
const data = cache.get(`cid:${cid}:tag:whitelist`);
const isInCache = data !== undefined;
if (isInCache) {
cachedData[cid] = data;
}
return !isInCache;
});
if (!nonCachedCids.length) {
return cids.map(cid => cachedData[cid]);
}
const keys = nonCachedCids.map(cid => `cid:${cid}:tag:whitelist`);
const data = await db.getSortedSetsMembers(keys);
nonCachedCids.forEach((cid, index) => {
cachedData[cid] = data[index];
cache.set(`cid:${cid}:tag:whitelist`, data[index]);
});
return cids.map(cid => cachedData[cid]);
};
// remove system tags from tag whitelist for non privileged user
Categories.filterTagWhitelist = function (tagWhitelist, isAdminOrMod) {
const systemTags = (meta.config.systemTags || '').split(',');
if (!isAdminOrMod && systemTags.length) {
return tagWhitelist.filter(tag => !systemTags.includes(tag));
}
return tagWhitelist;
};
function calculateTopicPostCount(category) {
if (!category) {
return;
}
let postCount = category.post_count;
let topicCount = category.topic_count;
if (Array.isArray(category.children)) {
category.children.forEach((child) => {
calculateTopicPostCount(child);
postCount += parseInt(child.totalPostCount, 10) || 0;
topicCount += parseInt(child.totalTopicCount, 10) || 0;
});
}
category.totalPostCount = postCount;
category.totalTopicCount = topicCount;
}
Categories.calculateTopicPostCount = calculateTopicPostCount;
Categories.getParents = async function (cids) {
const categoriesData = await Categories.getCategoriesFields(cids, ['parentCid']);
const parentCids = categoriesData.filter(c => c && c.parentCid).map(c => c.parentCid);
if (!parentCids.length) {
return cids.map(() => null);
}
const parentData = await Categories.getCategoriesData(parentCids);
const cidToParent = _.zipObject(parentCids, parentData);
return categoriesData.map(category => cidToParent[category.parentCid]);
};
Categories.getChildren = async function (cids, uid) {
const categoryData = await Categories.getCategoriesFields(cids, ['parentCid']);
const categories = categoryData.map((category, index) => ({ cid: cids[index], parentCid: category.parentCid }));
await Promise.all(categories.map(c => getChildrenTree(c, uid)));
return categories.map(c => c && c.children);
};
async function getChildrenTree(category, uid) {
let childrenCids = await Categories.getChildrenCids(category.cid);
childrenCids = await privileges.categories.filterCids('find', childrenCids, uid);
childrenCids = childrenCids.filter(cid => parseInt(category.cid, 10) !== parseInt(cid, 10));
if (!childrenCids.length) {
category.children = [];
return;
}
let childrenData = await Categories.getCategoriesData(childrenCids);
childrenData = childrenData.filter(Boolean);
childrenCids = childrenData.map(child => child.cid);
Categories.getTree([category].concat(childrenData), category.parentCid);
}
Categories.getChildrenTree = getChildrenTree;
Categories.getParentCids = async function (currentCid) {
let cid = currentCid;
const parents = [];
while (parseInt(cid, 10)) {
// eslint-disable-next-line
cid = await Categories.getCategoryField(cid, 'parentCid');
if (cid) {
parents.unshift(cid);
}
}
return parents;
};
Categories.getChildrenCids = async function (rootCid) {
let allCids = [];
async function recursive(keys) {
let childrenCids = await db.getSortedSetRange(keys, 0, -1);
childrenCids = childrenCids.filter(cid => !allCids.includes(parseInt(cid, 10)));
if (!childrenCids.length) {
return;
}
keys = childrenCids.map(cid => `cid:${cid}:children`);
childrenCids.forEach(cid => allCids.push(parseInt(cid, 10)));
await recursive(keys);
}
const key = `cid:${rootCid}:children`;
const cacheKey = `${key}:all`;
const childrenCids = cache.get(cacheKey);
if (childrenCids) {
return childrenCids.slice();
}
await recursive(key);
allCids = _.uniq(allCids);
cache.set(cacheKey, allCids);
return allCids.slice();
};
Categories.flattenCategories = function (allCategories, categoryData) {
categoryData.forEach((category) => {
if (category) {
allCategories.push(category);
if (Array.isArray(category.children) && category.children.length) {
Categories.flattenCategories(allCategories, category.children);
}
}
});
};
/**
* build tree from flat list of categories
*
* @param categories {array} flat list of categories
* @param parentCid {number} start from 0 to build full tree
*/
Categories.getTree = function (categories, parentCid) {
parentCid = parentCid || 0;
const cids = categories.map(category => category && category.cid);
const cidToCategory = {};
const parents = {};
cids.forEach((cid, index) => {
if (cid) {
categories[index].children = undefined;
cidToCategory[cid] = categories[index];
parents[cid] = { ...categories[index] };
}
});
const tree = [];
categories.forEach((category) => {
if (category) {
category.children = category.children || [];
if (!category.cid) {
return;
}
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
category.parentCid = 0;
}
if (category.parentCid === parentCid) {
tree.push(category);
category.parent = parents[parentCid];
} else {
const parent = cidToCategory[category.parentCid];
if (parent && parent.cid !== category.cid) {
category.parent = parents[category.parentCid];
parent.children = parent.children || [];
parent.children.push(category);
}
}
}
});
function sortTree(tree) {
tree.sort((a, b) => {
if (a.order !== b.order) {
return a.order - b.order;
}
return a.cid - b.cid;
});
tree.forEach((category) => {
if (category && Array.isArray(category.children)) {
sortTree(category.children);
}
});
}
sortTree(tree);
categories.forEach(c => calculateTopicPostCount(c));
return tree;
};
Categories.buildForSelect = async function (uid, privilege, fields) {
const cids = await Categories.getCidsByPrivilege('categories:cid', uid, privilege);
return await getSelectData(cids, fields);
};
Categories.buildForSelectAll = async function (fields) {
const cids = await Categories.getAllCidsFromSet('categories:cid');
return await getSelectData(cids, fields);
};
async function getSelectData(cids, fields) {
const categoryData = await Categories.getCategoriesData(cids);
const tree = Categories.getTree(categoryData);
return Categories.buildForSelectCategories(tree, fields);
}
Categories.buildForSelectCategories = function (categories, fields, parentCid) {
function recursive(category, categoriesData, level, depth) {
const bullet = level ? '• ' : '';
category.value = category.cid;
category.level = level;
category.text = level + bullet + category.name;
category.depth = depth;
categoriesData.push(category);
if (Array.isArray(category.children)) {
category.children.forEach(child => recursive(child, categoriesData, ` ${level}`, depth + 1));
}
}
parentCid = parentCid || 0;
const categoriesData = [];
const rootCategories = categories.filter(category => category && category.parentCid === parentCid);
rootCategories.sort((a, b) => {
if (a.order !== b.order) {
return a.order - b.order;
}
return a.cid - b.cid;
});
rootCategories.forEach(category => recursive(category, categoriesData, '', 0));
const pickFields = [
'cid', 'name', 'level', 'icon', 'parentCid',
'color', 'bgColor', 'backgroundImage', 'imageClass',
];
fields = fields || [];
if (fields.includes('text') && fields.includes('value')) {
return categoriesData.map(category => _.pick(category, fields));
}
if (fields.length) {
pickFields.push(...fields);
}
return categoriesData.map(category => _.pick(category, pickFields));
};
require('../promisify')(Categories);