-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
198 lines (175 loc) · 5.93 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
'use strict';
const _ = require.main.require('lodash');
const db = require.main.require('./src/database');
const routeHelpers = require.main.require('./src/routes/helpers');
const controllerHelpers = require.main.require('./src/controllers/helpers');
const posts = require.main.require('./src/posts');
const topics = require.main.require('./src/topics');
const categories = require.main.require('./src/categories');
const user = require.main.require('./src/user');
const meta = require.main.require('./src/meta');
const privileges = require.main.require('./src/privileges');
const translator = require.main.require('./src/translator');
const feed = module.exports;
feed.init = async function (params) {
routeHelpers.setupPageRoute(params.router, '/feed', renderFeed);
};
async function renderFeed(req, res) {
let cids = getCidsArray(req.query.cid);
const showFollowed = req.loggedIn && req.query.users === 'followed';
const showAllPosts = req.query.posts === 'all';
const page = Math.max(1, parseInt(req.query.page, 10) || 1);
const [followedUids, categoryData, userCids, canPost] = await Promise.all([
showFollowed ? db.getSortedSetRevRange(`following:${req.uid}`, 0, -1) : [],
controllerHelpers.getSelectedCategory(cids),
user.getCategoriesByStates(req.uid, [
categories.watchStates.watching,
categories.watchStates.tracking,
categories.watchStates.notwatching,
]),
privileges.categories.canPostTopic(req.uid),
]);
const readableCids = await privileges.categories.filterCids('topics:read', userCids, req.uid);
if (Array.isArray(cids)) {
cids = cids.filter(cid => readableCids.includes(cid));
}
let selectedCids = cids || readableCids || [];
if (!cids) {
selectedCids = readableCids.filter(cid => cid !== -1);
}
const start = Math.max(0, (page - 1) * meta.config.postsPerPage);
const stop = start + meta.config.postsPerPage - 1;
let sets = [];
let pagePids = [];
if (showAllPosts) {
if (showFollowed) {
sets = _.flatten(
followedUids.map(uid => selectedCids.map(cid => `cid:${cid}:uid:${uid}:pids`))
);
} else {
sets = selectedCids.map(cid => `cid:${cid}:pids`);
}
pagePids = await db.getSortedSetRevRange(sets, start, stop);
} else if (showFollowed) {
sets = _.flatten(
followedUids.map(uid => selectedCids.map(cid => `cid:${cid}:uid:${uid}:tids`))
);
const pageTids = await db.getSortedSetRevRange(sets, start, stop);
pagePids = (await topics.getTopicsFields(pageTids, ['mainPid'])).map(t => t && t.mainPid);
} else {
sets = selectedCids.map(cid => `cid:${cid}:tids:create`);
const pinnedSets = selectedCids.map(cid => `cid:${cid}:tids:pinned`);
const [pageTids, pinnedTids] = await Promise.all([
db.getSortedSetRevRange(sets, start, stop),
db.getSortedSetRevRange(pinnedSets, 0, -1),
]);
const [pageTopics, pinnedTopics] = await Promise.all([
topics.getTopicsFields(pageTids, ['mainPid', 'timestamp']),
topics.getTopicsFields(pinnedTids, ['mainPid', 'timestamp']),
]);
const lastTid = pageTopics[pageTids.length - 1];
if (lastTid) {
const firstTs = start === 0 ? Date.now() : await getTopicTs(sets, start - 1);
const lastTs = lastTid.timestamp;
const pinnedTopicsOnThisPage = pinnedTopics.filter(
t => t && t.timestamp >= lastTs && t.timestamp <= firstTs
);
pagePids = pageTopics.concat(pinnedTopicsOnThisPage).sort(
(t1, t2) => t2.timestamp - t1.timestamp
).map(t => t.mainPid);
}
}
const postData = await posts.getPostSummaryByPids(pagePids, req.uid, {
stripTags: false,
extraFields: ['bookmarks'],
});
delete req.query._;
const uniqTids = _.uniq(postData.map(p => p.tid));
const [topicData, { upvotes }, bookmarkStatus] = await Promise.all([
topics.getTopicsFields(uniqTids, ['tid', 'numThumbs', 'mainPid']),
posts.getVoteStatusByPostIDs(pagePids, req.uid),
posts.hasBookmarked(pagePids, req.uid),
]);
const thumbs = await topics.thumbs.load(topicData);
const tidToThumbs = _.zipObject(uniqTids, thumbs);
postData.forEach((p, index) => {
p.pid = encodeURIComponent(p.pid);
if (p.topic) {
p.topic = { ...p.topic };
if (String(p.pid) === String(p.topic.mainPid)) {
p.topic.thumbs = tidToThumbs[p.tid];
}
p.topic.postcount = Math.max(0, p.topic.postcount - 1);
p.topic.teaserPid = p.topic.teaserPid ? encodeURIComponent(p.topic.teaserPid) : p.topic.teaserPid;
}
p.upvoted = upvotes[index];
p.bookmarked = bookmarkStatus[index];
if (!p.isMainpost) {
p.repliedString = translator.compile('feed:replied-in-ago', p.topic.title, p.timestampISO);
}
});
let title = '[[feed:feed]]';
if (meta.config.homePageRoute && meta.config.homePageRoute !== 'feed') {
title = meta.config.homePageTitle || '[[global:home]]';
}
res.render('feed', {
title,
posts: postData,
allCategoriesUrl: 'feed' + controllerHelpers.buildQueryString(req.query, 'cid', ''),
currentPage: page,
showFollowed,
showAllPosts,
selectedCategory: categoryData.selectedCategory,
selectedCids: categoryData.selectedCids,
showThumbs: req.loggedIn || meta.config.privateUploads !== 1,
canPost,
});
}
async function getTopicTs(sets, index) {
const topic = await db.getSortedSetRangeWithScores(sets, index, index);
return topic ? topic.score : Date.now();
}
function getCidsArray(cid) {
if (cid && !Array.isArray(cid)) {
cid = [cid];
}
return cid && cid.map(cid => parseInt(cid, 10));
}
feed.defineWidgetAreas = async function (areas) {
areas = areas.concat([
{
name: 'Feed Page (Header)',
template: 'feed.tpl',
location: 'header',
},
{
name: 'Feed Page (Left)',
template: 'feed.tpl',
location: 'left',
},
{
name: 'Feed Page (Right)',
template: 'feed.tpl',
location: 'right',
},
{
name: 'Feed Page (Footer)',
template: 'feed.tpl',
location: 'footer',
},
]);
return areas;
};
feed.addNavItem = async function (items) {
items.push({
id: 'feed',
route: '/feed',
title: '[[feed:feed]]',
enabled: true,
iconClass: 'fa-list',
textClass: 'd-lg-none',
text: '[[feed:feed]]',
groups: [],
});
return items;
};