-
Notifications
You must be signed in to change notification settings - Fork 9
/
VoteFilter.js
68 lines (57 loc) · 2 KB
/
VoteFilter.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
'use strict';
const winston = require.main.require('winston'),
reputationParams = require('./ReputationParams'),
translator = require('./translator');
function getVoteFromCommand(command) {
return {
uid: command.uid,
pid: command.data.pid,
tid: command.data.room_id.replace('topic_', '')
};
}
async function buildParams(command) {
let vote = getVoteFromCommand(command);
return await reputationParams.recoverParams(vote.uid, vote.pid);
}
let VoteFilter = function(ReputationManager, users) {
this.filterUpvote = async function (command) {
let data = await buildParams(command);
try {
await ReputationManager.userCanUpvotePost(data.user, data.post);
return command;
} catch (err) {
if (err.reason) {
throw new Error(await this.translateError(err, data));
}
winston.error('[nodebb-plugin-reputation-rules] Error on upvote filter hook');
winston.error(err);
throw err;
}
};
this.filterDownvote = async function (command) {
let data = await buildParams(command);
try {
await ReputationManager.userCanDownvotePost(data.user, data.post);
return command;
} catch (err) {
if (err.reason) {
throw new Error(await this.translateError(err, data));
}
winston.error('[nodebb-plugin-reputation-rules] Error on downvote filter hook');
winston.error(err);
throw err;
}
};
this.filterUnvote = async function (command) {
// unvote is always allowed
return command;
};
this.translateError = async function(err, data) {
if (err.reason === 'unknownError') {
winston.error(err.message);
}
let settings = await users.getSettings(data.user.uid);
return translator.translate(err.reason, err.params, settings.userLang);
}
};
module.exports = VoteFilter;