-
Notifications
You must be signed in to change notification settings - Fork 4
/
library.js
186 lines (150 loc) · 4.9 KB
/
library.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
"use strict";
var nconf = module.parent.require('nconf');
var async = module.parent.require('async');
var validator = module.parent.require('validator');
var topics = module.parent.require('./topics');
var settings = module.parent.require('./settings');
var groups = module.parent.require('./groups');
var socketAdmin = module.parent.require('./socket.io/admin');
var defaultSettings = { title: 'Recent Topics', opacity: '1.0', textShadow: 'none', enableCarousel: 0, enableCarouselPagination: 0 };
var plugin = module.exports;
plugin.init = function(params, callback) {
var app = params.router;
var middleware = params.middleware;
app.get('/admin/plugins/recentcards', middleware.admin.buildHeader, renderAdmin);
app.get('/api/admin/plugins/recentcards', renderAdmin);
app.get('/plugins/nodebb-plugin-recent-cards-timuu/render', renderExternal);
app.get('/plugins/nodebb-plugin-recent-cards-timuu/render/style.css', renderExternalStyle);
app.get('/admin/plugins/nodebb-plugin-recent-cards-timuu/tests/external', testRenderExternal);
plugin.settings = new settings('recentcards', '1.0.0', defaultSettings);
socketAdmin.settings.syncRecentCards = function () {
plugin.settings.sync();
};
callback();
};
plugin.addAdminNavigation = function(header, callback) {
header.plugins.push({
route: '/plugins/recentcards',
icon: 'fa-tint',
name: 'Recent Cards'
});
callback(null, header);
};
function renderExternal(req, res, next) {
plugin.getCategories({
templateData: {}
}, function(err, data) {
if (err) {
return next(err);
}
data.templateData.relative_url = data.relative_url;
data.templateData.config = {
relative_path: nconf.get('url')
};
res.render('partials/nodebb-plugin-recent-cards-timuu/header', data.templateData);
});
}
function renderExternalStyle(req, res, next) {
res.render('partials/nodebb-plugin-recent-cards-timuu/external/style', {
forumURL: nconf.get('url')
});
}
function testRenderExternal(req, res, next) {
res.render('admin/plugins/nodebb-plugin-recent-cards-timuu/tests/external', {
forumURL: nconf.get('url')
});
}
plugin.getCategories = function(data, callback) {
var uid = data.req ? data.req.uid : 0;
function renderCards(err, topics) {
if (err) {
return callback(err);
}
var i = 0, cids = [], finalTopics = [];
if (!plugin.settings.get('enableCarousel')) {
while (finalTopics.length < 4 && i < topics.topics.length) {
var cid = parseInt(topics.topics[i].cid, 10);
if (cids.indexOf(cid) === -1) {
cids.push(cid);
finalTopics.push(topics.topics[i]);
}
i++;
}
} else {
finalTopics = topics.topics;
}
data.templateData.topics = finalTopics;
data.templateData.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')
};
callback(null, data);
}
if (plugin.settings.get('groupName')) {
groups.getLatestMemberPosts(plugin.settings.get('groupName'), 19, uid, function(err, posts) {
var topics = {topics: []};
for (var p = 0, pp = posts.length; p < pp; p++) {
var topic = posts[p].topic;
topic.category = posts[p].category;
topic.timestampISO = posts[p].timestampISO;
topic.user = posts[p].user;
topics.topics.push(topic);
}
renderCards(err, topics);
});
} else if (plugin.settings.get('popularTerm')) {
topics.getPopularTopics(plugin.settings.get('popularTerm'), uid, 0, 19, renderCards);
} else {
topics.getTopicsFromSet('topics:recent', uid, 0, 19, renderCards);
}
};
plugin.onNodeBBReady = function () {
if (nconf.get('isPrimary') === 'true') {
modifyCategoryTpl();
}
};
function renderAdmin(req, res, next) {
var list = [];
groups.getGroups('groups:createtime', 0, -1, function(err, groupsData) {
if (err) {
return next(err);
}
groupsData.forEach(function(group) {
if (groups.isPrivilegeGroup(group)) {
return;
}
list.push({
name: group,
value: validator.escape(String(group)),
});
});
res.render('admin/plugins/recentcards', { groups: list });
});
}
function modifyCategoryTpl(callback) {
callback = callback || function() {};
var fs = require('fs');
var path = require('path');
var tplPath = path.join(nconf.get('base_dir'), 'build/public/templates/categories.tpl');
var headerPath = path.join(nconf.get('base_dir'), 'node_modules/nodebb-plugin-recent-cards-timuu/static/templates/partials/nodebb-plugin-recent-cards/header.tpl');
async.parallel({
original: function(next) {
fs.readFile(tplPath, next);
},
header: function(next) {
fs.readFile(headerPath, next);
}
}, function(err, tpls) {
if (err) {
return callback(err);
}
var tpl = tpls.original.toString();
if (!tpl.match('<!-- Recent Cards plugin -->')) {
tpl = tpls.header.toString() + tpl;
}
fs.writeFile(tplPath, tpl, callback);
});
}