-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
index.js
312 lines (278 loc) · 9.4 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
'use strict';
const _ = require('lodash');
const validator = require('validator');
const db = require('../database');
const posts = require('../posts');
const utils = require('../utils');
const plugins = require('../plugins');
const meta = require('../meta');
const user = require('../user');
const categories = require('../categories');
const privileges = require('../privileges');
const social = require('../social');
const Topics = module.exports;
require('./data')(Topics);
require('./create')(Topics);
require('./delete')(Topics);
require('./sorted')(Topics);
require('./unread')(Topics);
require('./recent')(Topics);
require('./user')(Topics);
require('./fork')(Topics);
require('./posts')(Topics);
require('./follow')(Topics);
require('./tags')(Topics);
require('./teaser')(Topics);
Topics.scheduled = require('./scheduled');
require('./suggested')(Topics);
require('./tools')(Topics);
Topics.thumbs = require('./thumbs');
require('./bookmarks')(Topics);
require('./merge')(Topics);
Topics.events = require('./events');
Topics.exists = async function (tids) {
return await db.exists(
Array.isArray(tids) ? tids.map(tid => `topic:${tid}`) : `topic:${tids}`
);
};
Topics.getTopicsFromSet = async function (set, uid, start, stop) {
const tids = await db.getSortedSetRevRange(set, start, stop);
const topics = await Topics.getTopics(tids, uid);
Topics.calculateTopicIndices(topics, start);
return { topics: topics, nextStart: stop + 1 };
};
Topics.getTopics = async function (tids, options) {
let uid = options;
if (typeof options === 'object') {
uid = options.uid;
}
tids = await privileges.topics.filterTids('topics:read', tids, uid);
return await Topics.getTopicsByTids(tids, options);
};
Topics.getTopicsByTids = async function (tids, options) {
if (!Array.isArray(tids) || !tids.length) {
return [];
}
let uid = options;
if (typeof options === 'object') {
uid = options.uid;
}
async function loadTopics() {
const topics = await Topics.getTopicsData(tids);
const uids = _.uniq(topics.map(t => t && t.uid && t.uid.toString()).filter(v => utils.isNumber(v)));
const cids = _.uniq(topics.map(t => t && t.cid && t.cid.toString()).filter(v => utils.isNumber(v)));
const guestTopics = topics.filter(t => t && t.uid === 0);
async function loadGuestHandles() {
const mainPids = guestTopics.map(t => t.mainPid);
const postData = await posts.getPostsFields(mainPids, ['handle']);
return postData.map(p => p.handle);
}
async function loadShowfullnameSettings() {
if (meta.config.hideFullname) {
return uids.map(() => ({ showfullname: false }));
}
const data = await db.getObjectsFields(uids.map(uid => `user:${uid}:settings`), ['showfullname']);
data.forEach((settings) => {
settings.showfullname = parseInt(settings.showfullname, 10) === 1;
});
return data;
}
const [teasers, users, userSettings, categoriesData, guestHandles, thumbs] = await Promise.all([
Topics.getTeasers(topics, options),
user.getUsersFields(uids, ['uid', 'username', 'fullname', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status']),
loadShowfullnameSettings(),
categories.getCategoriesFields(cids, ['cid', 'name', 'slug', 'icon', 'backgroundImage', 'imageClass', 'bgColor', 'color', 'disabled']),
loadGuestHandles(),
Topics.thumbs.load(topics),
]);
users.forEach((userObj, idx) => {
// Hide fullname if needed
if (!userSettings[idx].showfullname) {
userObj.fullname = undefined;
}
});
return {
topics,
teasers,
usersMap: _.zipObject(uids, users),
categoriesMap: _.zipObject(cids, categoriesData),
tidToGuestHandle: _.zipObject(guestTopics.map(t => t.tid), guestHandles),
thumbs,
};
}
const [result, hasRead, followData, bookmarks, callerSettings] = await Promise.all([
loadTopics(),
Topics.hasReadTopics(tids, uid),
Topics.getFollowData(tids, uid),
Topics.getUserBookmarks(tids, uid),
user.getSettings(uid),
]);
const sortNewToOld = callerSettings.topicPostSort === 'newest_to_oldest';
result.topics.forEach((topic, i) => {
if (topic) {
topic.thumbs = result.thumbs[i];
topic.category = result.categoriesMap[topic.cid];
topic.user = topic.uid ? result.usersMap[topic.uid] : { ...result.usersMap[topic.uid] };
if (result.tidToGuestHandle[topic.tid]) {
topic.user.username = validator.escape(result.tidToGuestHandle[topic.tid]);
topic.user.displayname = topic.user.username;
}
topic.teaser = result.teasers[i] || null;
topic.isOwner = topic.uid === parseInt(uid, 10);
topic.ignored = followData[i].ignoring;
topic.followed = followData[i].following;
topic.unread = parseInt(uid, 10) <= 0 || (!hasRead[i] && !topic.ignored);
topic.bookmark = bookmarks[i] && (sortNewToOld ?
Math.max(1, topic.postcount + 2 - bookmarks[i]) :
Math.min(topic.postcount, bookmarks[i] + 1));
topic.unreplied = !topic.teaser;
topic.icons = [];
}
});
const filteredTopics = result.topics.filter(topic => topic && topic.category && !topic.category.disabled);
const hookResult = await plugins.hooks.fire('filter:topics.get', { topics: filteredTopics, uid: uid });
return hookResult.topics;
};
Topics.getTopicWithPosts = async function (topicData, set, uid, start, stop, reverse) {
const [
posts,
category,
tagWhitelist,
threadTools,
followData,
bookmark,
postSharing,
deleter,
merger,
forker,
related,
thumbs,
events,
] = await Promise.all([
Topics.getTopicPosts(topicData, set, start, stop, uid, reverse),
categories.getCategoryData(topicData.cid),
categories.getTagWhitelist([topicData.cid]),
plugins.hooks.fire('filter:topic.thread_tools', { topic: topicData, uid: uid, tools: [] }),
Topics.getFollowData([topicData.tid], uid),
Topics.getUserBookmark(topicData.tid, uid),
social.getActivePostSharing(),
getDeleter(topicData),
getMerger(topicData),
getForker(topicData),
Topics.getRelatedTopics(topicData, uid),
Topics.thumbs.load([topicData]),
Topics.events.get(topicData.tid, uid, reverse),
]);
topicData.thumbs = thumbs[0];
topicData.posts = posts;
topicData.events = events;
topicData.posts.forEach((p) => {
p.events = events.filter(
event => event.timestamp >= p.eventStart && event.timestamp < p.eventEnd
);
p.eventStart = undefined;
p.eventEnd = undefined;
});
topicData.category = category;
topicData.tagWhitelist = tagWhitelist[0];
topicData.minTags = category.minTags;
topicData.maxTags = category.maxTags;
topicData.thread_tools = threadTools.tools;
topicData.isFollowing = followData[0].following;
topicData.isNotFollowing = !followData[0].following && !followData[0].ignoring;
topicData.isIgnoring = followData[0].ignoring;
topicData.bookmark = bookmark;
topicData.postSharing = postSharing;
topicData.deleter = deleter;
if (deleter) {
topicData.deletedTimestampISO = utils.toISOString(topicData.deletedTimestamp);
}
topicData.merger = merger;
if (merger) {
topicData.mergedTimestampISO = utils.toISOString(topicData.mergedTimestamp);
}
topicData.forker = forker;
if (forker) {
topicData.forkTimestampISO = utils.toISOString(topicData.forkTimestamp);
}
topicData.related = related || [];
topicData.unreplied = topicData.postcount === 1;
topicData.icons = [];
const result = await plugins.hooks.fire('filter:topic.get', { topic: topicData, uid: uid });
return result.topic;
};
async function getDeleter(topicData) {
if (!parseInt(topicData.deleterUid, 10)) {
return null;
}
return await user.getUserFields(topicData.deleterUid, ['username', 'userslug', 'picture']);
}
async function getMerger(topicData) {
if (!parseInt(topicData.mergerUid, 10)) {
return null;
}
const [
merger,
mergedIntoTitle,
] = await Promise.all([
user.getUserFields(topicData.mergerUid, ['username', 'userslug', 'picture']),
Topics.getTopicField(topicData.mergeIntoTid, 'title'),
]);
merger.mergedIntoTitle = mergedIntoTitle;
return merger;
}
async function getForker(topicData) {
if (!parseInt(topicData.forkerUid, 10)) {
return null;
}
const [
forker,
forkedFromTitle,
] = await Promise.all([
user.getUserFields(topicData.forkerUid, ['username', 'userslug', 'picture']),
Topics.getTopicField(topicData.forkedFromTid, 'title'),
]);
forker.forkedFromTitle = forkedFromTitle;
return forker;
}
Topics.getMainPost = async function (tid, uid) {
const mainPosts = await Topics.getMainPosts([tid], uid);
return Array.isArray(mainPosts) && mainPosts.length ? mainPosts[0] : null;
};
Topics.getMainPids = async function (tids) {
if (!Array.isArray(tids) || !tids.length) {
return [];
}
const topicData = await Topics.getTopicsFields(tids, ['mainPid']);
return topicData.map(topic => topic && topic.mainPid);
};
Topics.getMainPosts = async function (tids, uid) {
const mainPids = await Topics.getMainPids(tids);
return await getMainPosts(mainPids, uid);
};
async function getMainPosts(mainPids, uid) {
let postData = await posts.getPostsByPids(mainPids, uid);
postData = await user.blocks.filter(uid, postData);
postData.forEach((post) => {
if (post) {
post.index = 0;
}
});
return await Topics.addPostData(postData, uid);
}
Topics.isLocked = async function (tid) {
const locked = await Topics.getTopicField(tid, 'locked');
return locked === 1;
};
Topics.search = async function (tid, term) {
if (!tid || !term) {
throw new Error('[[error:invalid-data]]');
}
const result = await plugins.hooks.fire('filter:topic.search', {
tid: tid,
term: term,
ids: [],
});
return Array.isArray(result) ? result : result.ids;
};
require('../promisify')(Topics);