-
Notifications
You must be signed in to change notification settings - Fork 9
/
library.js
198 lines (159 loc) · 7.39 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
'use strict';
let plugin = {'settingsVersion': '1.1.3'},
db = require.main.require('./src/database'),
users = require.main.require('./src/user'),
Settings = require.main.require('./src/settings'),
SocketAdmin = require.main.require('./src/socket.io/admin'),
winston = require.main.require('winston'),
reputationParams = require('./ReputationParams'),
VoteLog = require('./VoteLog'),
Config = require('./Config.js'),
ReputationManager = null,
VoteFilter = null,
pluginSettings = null;
plugin.onLoad = function (params, callback) {
ReputationManager = new (require('./ReputationManager'))(Config);
VoteFilter = new (require('./VoteFilter'))(ReputationManager, users);
let { router } = params;
const routeHelpers = require.main.require('./src/routes/helpers');
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/reputation-rules', function renderAdmin(req, res) {
res.render('admin/plugins/reputation-rules', {
title: 'Reputation Rules',
});
});
SocketAdmin.settings.syncReputationRules = function () {
pluginSettings.sync(function () {
winston.info("[reputation-rules] settings updated");
Config.setSettings(pluginSettings.get());
});
};
let defaultSettings = Config.getSettings();
pluginSettings = new Settings('reputation-rules', plugin.settingsVersion, defaultSettings, function () {
winston.info("[reputation-rules] settings loaded");
Config.setSettings(pluginSettings.get());
callback();
});
};
plugin.filterUpvote = async function (command) {
winston.verbose('[plugin-reputation-rules][hook:filterUpvote] user id: ' + command.uid + ', post id: ' + command.data.pid);
return await VoteFilter.filterUpvote(command);
};
plugin.filterDownvote = async function (command) {
winston.verbose('[plugin-reputation-rules][hook:filterDownvote] user id: ' + command.uid + ', post id: ' + command.data.pid);
return await VoteFilter.filterDownvote(command);
};
plugin.filterUnvote = async function (command) {
winston.verbose('[plugin-reputation-rules][hook:filterUnvote] user id: ' + command.uid + ', post id: ' + command.data.pid);
return await VoteFilter.filterUnvote(command);
};
plugin.upvote = async function (vote) {
winston.verbose('[plugin-reputation-rules][hook:upvote] user id: ' + vote.uid + ', post id: ' + vote.pid + ', current: ' + vote.current);
try {
let data = await reputationParams.recoverParams(vote.uid, vote.pid);
// undo downvote (if needed)
if (vote.current === 'downvote') {
await undoDownvote(data.user, data.author, data.post);
}
// calculate extra reputation points
// and give the extra points to the author
let extraPoints = ReputationManager.calculateUpvoteWeight(data.user);
await increaseUserReputation(data.author.uid, extraPoints);
// log this operation so we can undo it in the future
let voteLog = new VoteLog(data, extraPoints, 'upvote');
await ReputationManager.logVote(voteLog);
} catch (err) {
winston.error('[plugin-reputation-rules] Error on upvote hook', err);
throw err;
}
};
plugin.downvote = async function (vote) {
winston.verbose('[plugin-reputation-rules][hook:downvote] user id: ' + vote.uid + ', post id: ' + vote.pid + ', current: ' + vote.current);
try {
let data = await reputationParams.recoverParams(vote.uid, vote.pid);
// undo upvote (if needed)
if (vote.current === 'upvote') {
await undoUpvote(data.user, data.author, data.post);
}
// calculate weight and reduce author's reputation
// reduce voter's reputation (cost of downvoting)
let extraPoints = ReputationManager.calculateDownvoteWeight(data.user);
await decreaseUserReputation(data.author.uid, extraPoints);
await decreaseUserReputation(data.user.uid, Config.downvotePenalization());
//log this operation so we can undo it in the future
let voteLog = new VoteLog(data, extraPoints, 'downvote');
await ReputationManager.logVote(voteLog);
} catch(err) {
winston.error('[plugin-reputation-rules] Error on downvote hook', err);
throw err;
}
};
plugin.unvote = async function (vote) {
winston.verbose('[plugin-reputation-rules][hook:unvote] user id: ' + vote.uid + ', post id: ' + vote.pid + ', current: ' + vote.current);
try {
let data = await reputationParams.recoverParams(vote.uid, vote.pid);
if (vote.current === 'downvote') {
await undoDownvote(data.user, data.author, data.post);
} else if (vote.current === 'upvote') {
await undoUpvote(data.user, data.author, data.post);
}
let voteLogIdentifier = {
'voterId': data.user.uid,
'authorId': data.author.uid,
'topicId': parseInt(data.post.tid),
'postId': data.post.pid
};
await ReputationManager.logVoteUndone(voteLogIdentifier);
} catch(err) {
winston.error('[plugin-reputation-rules] Error on unvote hook', err);
throw err;
}
};
plugin.adminHeader = function (custom_header, callback) {
custom_header.plugins.push({
"route": '/plugins/reputation-rules',
"icon": 'fa-ban',
"name": 'Reputation Rules'
});
callback(null, custom_header);
};
/* ----------------------------------------------------------------------------------- */
async function undoUpvote(user, author, post) {
winston.verbose('[plugin-reputation-rules][undoUpvote] user id: ' + user.uid);
let voteLog = await ReputationManager.findVoteLog(user, author, post);
if (!voteLog) {
winston.verbose('[plugin-reputation-rules] error trying to undo upvote: vote log not found, maybe this vote was casted before the plugin was installed');
return;
}
let amount = voteLog.amount;
await decreaseUserReputation(author.uid, amount);
}
async function undoDownvote(user, author, post) {
winston.verbose('[plugin-reputation-rules][undoDownvote] user id: ' + user.uid);
let voteLog = await ReputationManager.findVoteLog(user, author, post);
if (!voteLog) {
winston.verbose('[plugin-reputation-rules] error trying to undo downvote: vote log not found, maybe this vote was casted before the plugin was installed');
return;
}
let amount = voteLog.amount;
await increaseUserReputation(author.uid, amount);
//author reputation restored, now remove voter's penalization
let penalization = Config.downvotePenalization();
await increaseUserReputation(user.uid, penalization);
}
async function decreaseUserReputation(uid, amount) {
if (amount <= 0) {
return;
}
winston.verbose("[plugin-reputation-rules][decreaseUserReputation] decrease user's reputation (" + uid + ") by " + amount);
let newReputation = await users.decrementUserFieldBy(uid, 'reputation', amount);
await db.sortedSetAdd('users:reputation', newReputation, uid);
}
async function increaseUserReputation(uid, amount) {
if (amount <= 0) {
return;
}
winston.verbose("[plugin-reputation-rules][increaseUserReputation] increase user's reputation (" + uid + ") by " + amount);
let newReputation = await users.incrementUserFieldBy(uid, 'reputation', amount);
await db.sortedSetAdd('users:reputation', newReputation, uid);
}
module.exports = plugin;