-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlibrary.js
More file actions
262 lines (229 loc) · 7.25 KB
/
library.js
File metadata and controls
262 lines (229 loc) · 7.25 KB
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
/* eslint-disable no-await-in-loop */
'use strict';
const nconf = require.main.require('nconf');
const _ = require.main.require('lodash');
const validator = require.main.require('validator');
const db = require.main.require('./src/database');
const topics = require.main.require('./src/topics');
const settings = require.main.require('./src/settings');
const groups = require.main.require('./src/groups');
const user = require.main.require('./src/user');
const defaultSettings = {
enableCarousel: 1,
enableCarouselPagination: 0,
minSlides: 1,
maxSlides: 4,
};
const plugin = module.exports;
let app;
plugin.init = async function (params) {
app = params.app;
const { router } = params;
const routeHelpers = require.main.require('./src/routes/helpers');
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/recentcards', renderAdmin);
router.get('/plugins/nodebb-plugin-recent-cards/render', renderExternal);
router.get('/plugins/nodebb-plugin-recent-cards/render/style.css', renderExternalStyle);
router.get('/admin/plugins/nodebb-plugin-recent-cards/tests/external', testRenderExternal);
plugin.settings = new settings('recentcards', '1.0.0', defaultSettings);
};
plugin.addAdminNavigation = async function (header) {
header.plugins.push({
route: '/plugins/recentcards',
icon: 'fa-tint',
name: 'Recent Cards',
});
return header;
};
plugin.defineWidgets = async function (widgets) {
const groupNames = await db.getSortedSetRevRange('groups:visible:createtime', 0, -1);
let groupsData = await groups.getGroupsData(groupNames);
groupsData = groupsData.filter(Boolean);
groupsData.forEach((group) => {
group.name = validator.escape(String(group.name));
});
const html = await app.renderAsync('admin/plugins/nodebb-plugin-recent-cards/widget', {
groups: groupsData,
});
widgets.push({
widget: 'recentCards',
name: 'Recent Cards',
description: 'Recent topics carousel',
content: html,
});
return widgets;
};
plugin.getConfig = async function (config) {
config.recentCards = {
title: plugin.settings.get('title'),
opacity: plugin.settings.get('opacity'),
textShadow: plugin.settings.get('shadow'),
enableCarousel: plugin.settings.get('enableCarousel'),
enableCarouselPagination: plugin.settings.get('enableCarouselPagination'),
minSlides: plugin.settings.get('minSlides'),
maxSlides: plugin.settings.get('maxSlides'),
};
return config;
};
plugin.renderWidget = async function (widget) {
if (!isVisibleInCategory(widget)) {
return null;
}
const topics = await getTopics(widget);
widget.html = await app.renderAsync('partials/nodebb-plugin-recent-cards/header', {
topics: topics,
config: widget.templateData.config,
title: widget.data.title || '',
carouselMode: plugin.settings.get('enableCarousel'),
});
return widget;
};
function getIdsArray(data, field) {
const ids = String(data[field] || '');
return ids.split(',').map(c => c.trim()).filter(Boolean);
}
function isVisibleInCategory(widget) {
const cids = getIdsArray(widget.data, 'cid');
return !(
cids.length &&
(widget.templateData.template.category || widget.templateData.template.topic) &&
!cids.includes(String(widget.templateData.cid))
);
}
async function getTopics(widget) {
const teaserPost = widget.data.teaserPost || 'first';
const teaserParseType = widget.data.teaserParseType || 'default';
const getTopicsOptions = {
uid: widget.uid,
teaserPost,
teaserParseType,
thumbsOnly: true,
};
async function getTopicsFromSet(set) {
let start = 0;
const topicsData = [];
do {
let tids = await db.getSortedSetRevRangeByScore(set, start, 20, Date.now(), '-inf');
if (!tids.length) {
break;
}
tids = await topics.filterNotIgnoredTids(tids, widget.uid);
let nextTopics = await topics.getTopics(tids, getTopicsOptions);
nextTopics = await user.blocks.filter(widget.uid, nextTopics);
topicsData.push(...nextTopics);
start += 20;
} while (topicsData.length < 20);
return { topics: topicsData.slice(0, 20) };
}
let topicsData = {
topics: [],
};
let filterCids = getIdsArray(widget.data, 'topicsFromCid');
if (!filterCids.length && widget.templateData.cid) {
filterCids = [String(widget.templateData.cid)];
}
widget.data.sort = widget.data.sort || 'recent';
let fromGroups = widget.data.fromGroups || [];
if (fromGroups && !Array.isArray(fromGroups)) {
fromGroups = [fromGroups];
}
// hard coded to show these topic tids only
const topicsTids = getIdsArray(widget.data, 'topicsTids');
if (topicsTids.length) {
topicsData.topics = await topics.getTopics(topicsTids, getTopicsOptions);
} else if (fromGroups.length) {
const uids = _.uniq(_.flatten(await groups.getMembersOfGroups(fromGroups)));
const sets = uids.map((uid) => {
if (filterCids.length) {
return filterCids.map(cid => `cid:${cid}:uid:${uid}:tids`);
}
return `uid:${uid}:topics`;
});
topicsData = await getTopicsFromSet(sets.flat());
topicsData.topics.sort((t1, t2) => {
if (widget.data.sort === 'recent') {
return t2.lastposttime - t1.lastposttime;
} else if (widget.data.sort === 'votes') {
return t2.votes - t1.votes;
} else if (widget.data.sort === 'posts') {
return t2.postcount - t1.postcount;
}
return 0;
});
} else if (filterCids.length) {
let searchSuffix = '';
if (widget.data.sort === 'recent') {
searchSuffix += ':lastposttime';
} else if (widget.data.sort === 'votes' || widget.data.sort === 'posts' || widget.data.sort === 'create') {
searchSuffix += `:${widget.data.sort}`;
}
topicsData = await getTopicsFromSet(
filterCids.map(cid => `cid:${cid}:tids${searchSuffix}`)
);
} else {
const map = {
votes: 'topics:votes',
posts: 'topics:posts',
recent: 'topics:recent',
create: 'topics:tid',
};
topicsData = await getTopicsFromSet(map[widget.data.sort]);
}
let i = 0;
const cids = [];
let finalTopics = [];
if (!plugin.settings.get('enableCarousel')) {
while (finalTopics.length < 4 && i < topicsData.topics.length) {
const cid = String(topicsData.topics[i].cid);
if (filterCids.length || !cids.includes(cid) || topicsData.topics.length <= 4) {
cids.push(cid);
finalTopics.push(topicsData.topics[i]);
}
i += 1;
}
} else {
finalTopics = topicsData.topics;
}
const thumbStyle = widget.data.thumbnailStyle || 'background';
finalTopics.forEach((t) => {
if (t) {
const showThumbs = t.thumbs?.length && String(t?.teaser.pid) === String(t.mainPid);
t.showThumbnailInBackground = showThumbs && thumbStyle === 'background';
t.showThumbnailInline = showThumbs && thumbStyle === 'inline';
}
});
return finalTopics;
}
async function renderExternal(req, res, next) {
try {
const topics = await getTopics({
uid: req.uid,
data: {
teaserPost: 'first',
},
templateData: {},
});
res.render('partials/nodebb-plugin-recent-cards/header', {
topics: topics,
config: {
relative_path: nconf.get('url'),
},
});
} catch (err) {
next(err);
}
}
function renderExternalStyle(req, res) {
res.render('partials/nodebb-plugin-recent-cards/external/style', {
forumURL: nconf.get('url'),
});
}
function testRenderExternal(req, res) {
res.render('admin/plugins/nodebb-plugin-recent-cards/tests/external', {
forumURL: nconf.get('url'),
});
}
async function renderAdmin(req, res) {
res.render('admin/plugins/recentcards', {
title: 'Recent Cards',
});
}