-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlibrary.js
219 lines (184 loc) · 5.79 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
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
'use strict';
const plugin = module.exports;
const mkdirp = require('mkdirp');
const async = require('async');
const nconf = require.main.require('nconf');
const winston = require.main.require('winston');
const db = require.main.require('./src/database');
const user = require.main.require('./src/user');
const widgets = require.main.require('./src/widgets');
const groups = require.main.require('./src/groups');
const controllerHelpers = require.main.require('./src/controllers/helpers');
const pubsub = require.main.require('./src/pubsub');
const fs = require('fs');
const path = require('path');
plugin.init = async function (params) {
var app = params.router;
var middleware = params.middleware;
var helpers = require.main.require('./src/routes/helpers');
helpers.setupAdminPageRoute(app, '/admin/plugins/custom-pages', renderAdmin);
app.get('*', function routeToCustomPage(req, res, next) {
if (!plugin.pagesHash || !plugin.pagesHash[cleanPath(req.path)]) {
return setImmediate(next);
}
res.locals.isAPI = req.path.startsWith('/api');
let middlewares = [middleware.maintenanceMode, middleware.registrationComplete, middleware.pageView, middleware.pluginHooks];
if (!res.locals.isAPI) {
middlewares = [middleware.busyCheck, middleware.applyCSRF, middleware.buildHeader].concat(middlewares);
}
async.eachSeries(middlewares, function (middleware, next) {
middleware(req, res, next);
}, async function (err) {
if (err) {
return next(err);
}
await renderCustomPage(req, res, next);
});
});
var SocketAdmin = require.main.require('./src/socket.io/admin');
SocketAdmin.settings.saveCustomPages = async function (socket, data) {
await resetWidgets(data);
pubsub.publish('custom-pages:save', data);
await db.set('plugins:custom-pages', JSON.stringify(data));
};
const pages = await getCustomPages();
await plugin.saveTemplates(pages);
};
pubsub.on('custom-pages:save', async function (pages) {
storeData(pages);
await plugin.saveTemplates(pages);
});
function cleanPath(path) {
return path.replace(/\/(api\/)?/, '').replace(/\/$/, '');
}
async function renderCustomPage(req, res) {
var path = cleanPath(req.path);
var groupList = plugin.pagesHash[path].groups ? plugin.pagesHash[path].groups.split(',') : [];
const isAdmin = await user.isAdministrator(req.uid);
if (!isAdmin && groupList.length) {
const groupMembership = await groups.isMemberOfGroups(req.uid, groupList);
if (!groupMembership.some(a => !!a)) {
return controllerHelpers.notAllowed(req, res);
}
}
const userData = await user.getUsers([req.uid], req.uid);
res.render(path, {
title: plugin.pagesHash[path].name,
user: userData[0],
});
}
async function renderAdmin(req, res) {
const [pages, groups] = await Promise.all([
getCustomPages(),
getGroupList(),
]);
res.render('admin/plugins/custom-pages', {
title: 'Custom Pages',
pages: pages,
groups: groups,
});
}
async function getCustomPages() {
if (plugin.pagesCache) {
return plugin.pagesCache;
}
const data = await db.get('plugins:custom-pages');
storeData(JSON.parse(data));
return plugin.pagesCache;
}
function storeData(pages) {
if (pages == null) {
pages = [];
}
// Eliminate errors in route definition
plugin.pagesCache = pages.map(function (pageObj) {
pageObj.route = pageObj.route.replace(/^\/*/g, ''); // trim leading slashes from route
return pageObj;
});
plugin.pagesHash = plugin.pagesCache.reduce(function (memo, cur) {
memo[cur.route] = cur;
return memo;
}, {});
}
async function getGroupList() {
const groupNames = await groups.getGroups('groups:createtime', 0, -1);
return groupNames.filter(groupName => !groups.isPrivilegeGroup(groupName));
}
plugin.setWidgetAreas = async function (areas) {
const data = await getCustomPages();
for (var d in data) {
if (data.hasOwnProperty(d)) {
areas = areas.concat([
{
name: data[d].name + ' Header',
template: data[d].route + '.tpl',
location: 'header',
},
{
name: data[d].name + ' Footer',
template: data[d].route + '.tpl',
location: 'footer',
},
{
name: data[d].name + ' Sidebar',
template: data[d].route + '.tpl',
location: 'sidebar',
},
{
name: data[d].name + ' Content',
template: data[d].route + '.tpl',
location: 'content',
},
]);
}
}
return areas;
};
plugin.addAdminNavigation = async function (header) {
header.plugins.push({
route: '/plugins/custom-pages',
icon: 'fa-mobile',
name: 'Custom Pages',
});
return header;
};
plugin.saveTemplates = async function (pages) {
if (!nconf.get('isPrimary')) {
return;
}
const bjs = require.main.require('benchpressjs');
const customTPL = await fs.promises.readFile(path.join(__dirname, 'templates/custom-page.tpl'), 'utf-8');
try {
await async.each(pages, async function (pageObj) {
const route = pageObj.route;
const jsPath = path.join(nconf.get('views_dir'), route + '.js');
const tplPath = path.join(nconf.get('views_dir'), route + '.tpl');
const compiled = await bjs.precompile(customTPL, {});
if (path.dirname(route) !== '.') {
// Subdirectories specified
await mkdirp(path.join(nconf.get('views_dir'), path.dirname(route)));
}
await saveFiles(jsPath, tplPath, compiled, customTPL);
});
} catch (err) {
winston.error('[plugin/custom-pages] Could not save templates!');
winston.error(' ' + err.message);
throw err;
}
};
async function saveFiles(jsPath, tplPath, compiled, customTPL) {
await fs.promises.writeFile(jsPath, compiled);
await fs.promises.writeFile(tplPath, customTPL);
}
async function resetWidgets(data) {
var removedRoutes = [];
if (plugin.pagesHash) {
Object.keys(plugin.pagesHash).forEach(function (route) {
var match = data.find(page => page.route === route);
if (!match) {
removedRoutes.push(route);
}
});
}
await widgets.resetTemplates(removedRoutes);
}