-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
42 lines (33 loc) · 1.26 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
'use strict';
const meta = require.main.require('./src/meta');
const controllerHelpers = require.main.require('./src/controllers/helpers');
const controllers = require('./lib/controllers');
const plugin = module.exports;
plugin.init = async (params) => {
const { router, middleware/* , controllers */ } = params;
const routeHelpers = require.main.require('./src/routes/helpers');
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/topic-redirect', middleware, [], controllers.renderAdminPage);
};
plugin.addAdminNavigation = (header) => {
header.plugins.push({
route: '/plugins/topic-redirect',
icon: 'fa-tint',
name: 'Topic Redirect',
});
return header;
};
plugin.handleTopicRedirect = async ({ req, res }) => {
if (['/api/topic', '/topic'].some(prefix => req.url.startsWith(prefix))) {
let { redirects } = await meta.settings.get('topic-redirect');
if (redirects && redirects.length) {
redirects = redirects.reduce((memo, cur) => {
memo.set(parseInt(cur.tid, 10), cur);
return memo;
}, new Map());
if (redirects.has(parseInt(req.params.topic_id, 10))) {
const redirect = redirects.get(parseInt(req.params.topic_id, 10));
controllerHelpers.redirect(res, redirect.url, redirect.type === 308); // 307 for everything else
}
}
}
};