This repository was archived by the owner on Feb 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.js
More file actions
62 lines (55 loc) · 1.92 KB
/
library.js
File metadata and controls
62 lines (55 loc) · 1.92 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
const meta = require.main.require("./src/meta");
const async = require.main.require("async");
const db = require.main.require("./src/database");
const categories = require.main.require("./src/categories");
const winston = require.main.require('winston');
const MandatoryTag = {
config: {},
init(params, callback) {
let app = params.router;
let middleware = params.middleware;
app.get("/admin/plugins/mandatory-tag", middleware.admin.buildHeader, renderAdmin);
app.get("/api/admin/plugins/mandatory-tag", renderAdmin);
meta.settings.get("mandatory-tag", (err, options) => {
if (err) {
winston.warn(`[plugin/mandatorytag] Unable to retrieve settings, will keep defaults: ${err.message}`);
} else {
MandatoryTag.config = options;
}
});
callback();
},
addToAdminNav(header, callback) {
header.plugins.push({
route: "/plugins/mandatory-tag",
name: "Mandatory Tag"
});
callback(null, header);
},
topicPost(data, callback) {
checkTags(data, data.cid, data.tags, callback);
},
topicEdit(data, callback) {
checkTags(data, data.topic.cid, data.data.tags, callback);
}
};
function checkTags(data, cid, tags, callback)
{
if(tags && tags.length < MandatoryTag.config['cid-' + cid]) {
return callback(new Error('[[mandatorytag:no-tags,' + MandatoryTag.config['cid-' + cid] + ']]'));
}
callback(null, data);
}
function renderAdmin(req, res, next) {
async.waterfall([
async.apply(db.getSortedSetRange, "categories:cid", 0, -1),
(cids, next) => {
categories.getCategoriesFields(cids, ["cid", "name"], next);
}
], (err, data) => {
if (err)
return next(err);
res.render("admin/plugins/mandatory-tag", {categories: data});
});
}
module.exports = MandatoryTag;