/
library.js
87 lines (66 loc) · 2.03 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
"use strict";
var plugin = {},
async = module.parent.require('async'),
topics = module.parent.require('./topics'),
emitter = module.parent.require('./emitter');
emitter.on('nodebb:ready', modifyCategoryTpl);
plugin.init = function(params, callback) {
var app = params.router,
middleware = params.middleware,
controllers = params.controllers;
app.get('/admin/plugins/quickstart', middleware.admin.buildHeader, renderAdmin);
app.get('/api/admin/plugins/quickstart', renderAdmin);
callback();
};
plugin.addAdminNavigation = function(header, callback) {
header.plugins.push({
route: '/plugins/quickstart',
icon: 'fa-tint',
name: 'Quickstart'
});
callback(null, header);
};
plugin.getCategories = function(data, callback) {
topics.getTopicsFromSet('topics:recent', data.req.uid, 0, 19, function(err, topics) {
if (err) {
return next(err);
}
var i = 0, cids = [], finalTopics = [];
while (finalTopics.length < 8 && i < topics.topics.length) {
if (parseInt(topics.topics[i].cid, 10) !== 21)
finalTopics.push(topics.topics[i]);
i++;
}
data.templateData.topics = finalTopics;
callback(null, data);
});
};
function renderAdmin(req, res, next) {
res.render('admin/plugins/quickstart', {});
}
function modifyCategoryTpl(callback) {
callback = callback || function() {};
var fs = require('fs'),
path = require('path'),
nconf = module.parent.require('nconf'),
tplPath = path.join(nconf.get('base_dir'), 'public/templates/categories.tpl'),
headerPath = path.join(nconf.get('base_dir'), 'node_modules/nodebb-plugin-recent-cards/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);
});
}
module.exports = plugin;