-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.js
More file actions
144 lines (126 loc) · 4.45 KB
/
library.js
File metadata and controls
144 lines (126 loc) · 4.45 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
const async = require.main.require("async");
const meta = require.main.require('./src/meta');
const request = require.main.require('request');
const nconf = require.main.require("nconf");
const search = require.main.require("./src/search");
const topics = require.main.require('./src/topics');
const categoryIdRegex = /c-(\d)+/g;
const Publisher = {
publishCategoryId: 0,
discordWebHook: "change-me",
init(params, callback) {
let app = params.router;
let middleware = params.middleware;
app.get("/admin/plugins/mff-publisher", middleware.admin.buildHeader, renderAdmin);
app.get("/api/admin/plugins/mff-publisher", renderAdmin);
app.get("/publisherapi/topics", getPreviousTopics);
meta.settings.get("mff-publisher", (error, options) => {
if (error) {
console.log(`[plugin/mff-publisher] Unable to retrieve settings, will keep defaults: ${error.message}`);
} else {
if (options.hasOwnProperty("publishcategoryid")) {
Publisher.publishCategoryId = options["publishcategoryid"];
}
if (options.hasOwnProperty("discordwebhook")) {
Publisher.discordWebHook = options["discordwebhook"];
}
}
});
callback();
},
addAdminToNav(header, callback) {
header.plugins.push({
route: "/plugins/mff-publisher",
name: 'MFF Publisher'
});
callback(null, header);
},
getWidget(widgets, callback) {
widgets.push({
name: "MFF Publisher",
widget: "",
description: "",
content: ""
});
callback(null, widgets);
},
renderWidget(widget, callback) {
async.parallel({
previous: async.apply(getPreviousTopics)
}, (error, data) => {
if (error) {
callback(error);
}
//data.relative_path = nconf.get("relative_path");
widget.req.app.render("widgets/publisher", data, (error, html) => {
widget.html = html;
callback(error, html);
});
});
}
};
function renderAdmin(req, res) {
res.render("admin/plugins/mff-publisher");
}
// TODO Change name
function getPreviousTopics(req, res) {
searchInPost(req, res, [Publisher.publishCategoryId])
}
function searchInPost(req, res, categories) {
const data = {
query: '',
searchIn: 'titles',
matchWords: 'all',
categories: categories,
searchChildren: false,
hasTags: req.query.hasTags,
sortBy: '',
qs: req.query
};
search.search(data, (error, results) => {
if (error) {
console.log(error);
return res.status(500).json({error: "Error while performing the search"});
}
let tids = results.posts.map(post => post && post.tid);
topics.getTopicsTags(tids, (error2, postTags) => {
if (error2) {
console.log(error2);
return res.status(500).json({error: "Error while getting topic tags"})
}
if (results.posts.length === 0) {
return res.status(200).json({message: "No result"});
}
let response = {};
for (let tags of postTags) {
for (let tag of tags) {
if (tag.match(categoryIdRegex)) {
if (isTagInFilter(tag, req.query.hasTags)) {
response[tag] = [];
}
}
}
}
for (let i in results.posts) {
let post = {
title: results.posts[i].topic.title,
url: nconf.get('url') + '/topic/' + results.posts[i].topic.slug
};
if (postTags[i].length !== 0) {
for (let tag of postTags[i]) {
if (tag.match(categoryIdRegex)) {
if (isTagInFilter(tag, req.query.hasTags)) {
response[tag].push(post);
}
}
}
}
}
return res.status(200).json(response);
});
});
}
function isTagInFilter(tag, tagsFilter) {
return !tagsFilter || (tagsFilter && tagsFilter.indexOf(tag) >= 0);
}
module.exports = Publisher;