-
Notifications
You must be signed in to change notification settings - Fork 9
/
UserVotingPermissions.js
156 lines (135 loc) · 5.11 KB
/
UserVotingPermissions.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
'use strict';
let UserVotingPermissions = function(Config, db, user, post) {
this.hasEnoughPostsToUpvote = async function() {
let allowed = user.postcount >= Config.minPostToUpvote();
if (!allowed) throw {
'reason': 'notEnoughPosts',
'params': [Config.minPostToUpvote()]
};
};
this.isOldEnoughToUpvote = async function() {
let now = new Date();
let xDaysAgo = now.getTime() - Config.minDaysToUpvote() * 24 * 60 * 60 * 1000;
let allowed = user.joindate < xDaysAgo;
if (!allowed) throw {
'reason': 'notOldEnough',
'params': [Config.minDaysToUpvote()]
};
};
this.hasVotedTooManyPostsInThread = async function() {
let userVotesInThread;
try {
userVotesInThread = await countVotesInThread(user.uid, post.tid);
} catch (err) {
err.reason = 'unknownError';
throw err;
}
let allowed = userVotesInThread < Config.maxVotesPerUserInThread();
if (!allowed) throw {
'reason': 'tooManyVotesInThread',
'params': [Config.maxVotesPerUserInThread()]
};
};
this.hasVotedAuthorTooManyTimesThisMonth = async function() {
let votesToAuthor;
try {
votesToAuthor = await countVotesToAuthor(user.uid, post.uid);
} catch (err) {
err.reason = 'unknownError';
throw err;
}
let allowed = votesToAuthor < Config.maxVotesToSameUserInMonth();
if (!allowed) throw {
'reason': 'tooManyVotesToSameUserThisMonth',
'params': [Config.maxVotesToSameUserInMonth()]
};
};
this.hasVotedTooManyTimesToday = async function() {
let votes;
try {
votes = await countVotesForUser(user.uid);
} catch (err) {
err.reason = 'unknownError';
throw err;
}
let allowed = votes < Config.maxVotesPerUser(user.reputation);
if (!allowed) throw {
'reason': 'tooManyVotesToday',
'params': [Config.maxVotesPerUser(user.reputation)]
};
};
this.hasEnoughPostsToDownvote = async function() {
let allowed = user.postcount >= Config.minPostToDownvote();
if (!allowed) throw {
'reason': 'notEnoughPosts',
'params': [Config.minPostToDownvote()]
};
};
this.isOldEnoughToDownvote = async function() {
let now = new Date();
let xDaysAgo = now.getTime() - Config.minDaysToDownvote() * 24 * 60 * 60 * 1000;
let allowed = user.joindate < xDaysAgo;
if (!allowed) throw {
'reason': 'notOldEnough',
'params': [Config.minDaysToDownvote()]
};
};
this.hasEnoughReputationToDownvote = async function() {
let allowed = user.reputation >= Config.minReputationToDownvote();
if (!allowed) throw {
'reason': 'notEnoughReputation',
'params': [Config.minReputationToDownvote()]
};
};
this.votingAllowedInCategory = async function() {
let categoryBlackList = Config.getDisabledCategories();
let index = categoryBlackList.indexOf(post.cid);
let allowed = index === -1;
if (!allowed) throw {'reason': 'votingDisabledInCategory'};
};
this.postIsNotTooOld = async function() {
if (Config.getMaxPostAgeDays() === 0) return;
let now = new Date();
let postAgeDays = (now - post.timestamp)/24/60/60/1000;
if (postAgeDays > Config.getMaxPostAgeDays()) throw {
'reason': 'postTooOld',
'params': [Config.getMaxPostAgeDays()]
};
};
this.hasDownvotedTooManyTimesToday = async function() {
if (Config.maxDownvotesPerDay() === 0) return;
let downvotes;
try {
downvotes = await countDownvotesForUser(user.uid);
} catch (err) {
err.reason = 'unknownError';
throw err;
}
let allowed = downvotes < Config.maxDownvotesPerDay();
if (!allowed) throw {
'reason': 'tooManyDownvotesToday',
'params': [Config.maxDownvotesPerDay()]
};
};
async function countVotesInThread(userId, threadId) {
let voteIdentifier = Config.getPerThreadLogId(userId, threadId);
let setMembers = await db.getSetMembers(voteIdentifier);
return setMembers.length;
}
async function countVotesToAuthor(userId, authorId) {
let voteIdentifier = Config.getPerAuthorLogId(userId, authorId);
let setMembers = await db.getSetMembers(voteIdentifier);
return setMembers.length;
}
async function countVotesForUser(userId) {
let voteIdentifier = Config.getPerUserLogId(userId);
let setMembers = await db.getSetMembers(voteIdentifier);
return setMembers.length;
}
async function countDownvotesForUser(userId) {
let voteIdentifier = Config.getPerUserAndTypeLogId(userId, 'downvote');
let setMembers = await db.getSetMembers(voteIdentifier);
return setMembers.length;
}
};
module.exports = UserVotingPermissions;