1 | 'use strict';
|
2 |
|
3 | const validator = require.main.require('validator');
|
4 |
|
5 | const groups = require.main.require('./src/groups');
|
6 | const user = require.main.require('./src/user');
|
7 |
|
8 | const rewards = module.exports;
|
9 |
|
10 | rewards.get = async function (rewards) {
|
11 | const groupsData = await getGroupList();
|
12 |
|
13 | rewards = rewards.concat([
|
14 | {
|
15 | rid: 'essentials/add-to-group',
|
16 | name: 'Add to Group',
|
17 | inputs: [
|
18 | {
|
19 | type: 'select',
|
20 | name: 'groupname',
|
21 | label: 'Group Name:',
|
22 | values: groupsData,
|
23 | },
|
24 | ],
|
25 | },
|
26 | {
|
27 | rid: 'essentials/remove-from-group',
|
28 | name: 'Remove from Group',
|
29 | inputs: [
|
30 | {
|
31 | type: 'select',
|
32 | name: 'groupname',
|
33 | label: 'Group Name:',
|
34 | values: groups,
|
35 | },
|
36 | ],
|
37 | },
|
38 | {
|
39 | rid: 'essentials/alert-user',
|
40 | name: 'Send alert message',
|
41 | inputs: [
|
42 | {
|
43 | type: 'text',
|
44 | name: 'title',
|
45 | label: 'Title:',
|
46 | },
|
47 | {
|
48 | type: 'text',
|
49 | name: 'message',
|
50 | label: 'Message:',
|
51 | },
|
52 | ],
|
53 | },
|
54 | {
|
55 | rid: 'essentials/award-reputation',
|
56 | name: 'Award Reputation',
|
57 | inputs: [
|
58 | {
|
59 | type: 'text',
|
60 | name: 'reputation',
|
61 | label: 'Amount of reputation:',
|
62 | },
|
63 | ],
|
64 | },
|
65 | ]);
|
66 | return rewards;
|
67 | };
|
68 |
|
69 | rewards.addToGroup = async function (data) {
|
70 | await groups.join(data.reward.groupname, data.uid);
|
71 | };
|
72 |
|
73 | rewards.removeFromGroup = async function (data) {
|
74 | await groups.leave(data.reward.groupname, data.uid);
|
75 | };
|
76 |
|
77 | rewards.alertUser = function (data) {
|
78 | const websockets = require.main.require('./src/socket.io');
|
79 | websockets.in(`uid_${data.uid}`).emit('event:alert', data.reward);
|
80 | };
|
81 |
|
82 | rewards.awardReputation = async function (data) {
|
83 | await user.incrementUserReputationBy(data.uid, data.reward.reputation);
|
84 | };
|
85 |
|
86 | async function getGroupList() {
|
87 | const groupNames = await groups.getGroups('groups:createtime', 0, -1);
|
88 |
|
89 | groupNames.sort((a, b) => {
|
90 | const isAPrivGroup = groups.isPrivilegeGroup(a);
|
91 | const isBPrivGroup = groups.isPrivilegeGroup(b);
|
92 | if (isAPrivGroup && !isBPrivGroup) {
|
93 | return 1;
|
94 | } else if (!isAPrivGroup && isBPrivGroup) {
|
95 | return -1;
|
96 | }
|
97 | if (a > b) {
|
98 | return 1;
|
99 | } else if (b > a) {
|
100 | return -1;
|
101 | }
|
102 | return 0;
|
103 | });
|
104 |
|
105 | const groupData = groupNames.map((group) => {
|
106 | let name = group;
|
107 | const matchesCatPrivsGroup = group.match(/cid:([0-9]*):privileges:groups:([\s\S]*)/);
|
108 |
|
109 | if (matchesCatPrivsGroup !== null) {
|
110 | if (matchesCatPrivsGroup[1] === '0') {
|
111 | return null;
|
112 | }
|
113 | name = `Category ${matchesCatPrivsGroup[1]} group with privilege ${matchesCatPrivsGroup[2]}`;
|
114 | } else if (groups.isPrivilegeGroup(group)) {
|
115 | return null;
|
116 | }
|
117 |
|
118 | return {
|
119 | name: validator.escape(String(name || group)),
|
120 | value: validator.escape(String(group)),
|
121 | };
|
122 | });
|
123 | return groupData.filter(Boolean);
|
124 | }
|