forked from dealbee/nodebb-plugin-user-level
-
Notifications
You must be signed in to change notification settings - Fork 4
/
library.js
67 lines (53 loc) · 1.78 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
'use strict';
const controllers = require('./lib/controllers');
const meta = require.main.require('./src/meta');
const plugin = module.exports;
plugin.init = function (params, callback) {
const { router } = params;
const helpers = require.main.require('./src/routes/helpers');
helpers.setupPageRoute(router, '/user-levels', controllers.renderUserLevelsPage);
helpers.setupAdminPageRoute(router, '/admin/plugins/user-level', controllers.renderAdminPage);
callback();
};
async function getLevelList() {
const settings = await meta.settings.get('user-level');
let levelList = settings['level-list'] || [];
levelList = levelList.filter(Boolean);
levelList.forEach((level) => {
level['min-reputation'] = parseInt(level['min-reputation'], 10);
});
levelList.sort((a, b) => b['min-reputation'] - a['min-reputation']);
levelList.forEach((level, i) => {
level['level-index'] = levelList.length - i;
level['next-level'] = levelList[i - 1];
});
return levelList;
}
plugin.filterPostsGetUserInfoForPosts = async (hookData) => {
const levelList = await getLevelList();
hookData.users.forEach((user) => {
if (user) {
user.level = {
...levelList.find(l => l['min-reputation'] <= user.reputation),
reputation: user.reputation,
};
}
});
return hookData;
};
plugin.filterAccountProfileBuild = async (hookData) => {
const levelList = await getLevelList();
hookData.templateData.level = levelList.find(l => l['min-reputation'] <= hookData.templateData.reputation);
if (hookData.templateData.level) {
hookData.templateData.level.reputation = hookData.templateData.reputation;
}
return hookData;
};
plugin.addAdminNavigation = function (header, callback) {
header.plugins.push({
route: '/plugins/user-level',
icon: 'fa-tint',
name: 'User level',
});
callback(null, header);
};