allow removing own segments with a downvote

This commit is contained in:
Joe Dowd 2020-08-21 22:32:34 +01:00
parent c1d4ba3c80
commit 16c68dd51d
4 changed files with 46 additions and 4 deletions

View file

@ -1,5 +1,6 @@
var config = require('./src/config.js'); var config = require('./src/config.js');
var createServer = require('./src/app.js'); var createServer = require('./src/app.js');
const logger = require('./src/utils/logger.js');
var server = createServer(() => { var server = createServer(() => {
console.log("Server started on port " + config.port + "."); logger.info("Server started on port " + config.port + ".");
}); });

View file

@ -1,6 +1,6 @@
var MysqlInterface = require('sync-mysql'); var MysqlInterface = require('sync-mysql');
var config = require('../config.js'); var config = require('../config.js');
var logger = require('../utils/logger.js'); const logger = require('../utils/logger.js');
class Mysql { class Mysql {
constructor(msConfig) { constructor(msConfig) {

View file

@ -97,6 +97,9 @@ async function voteOnSponsorTime(req, res) {
//check if this user is on the vip list //check if this user is on the vip list
let isVIP = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [nonAnonUserID]).userCount > 0; let isVIP = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [nonAnonUserID]).userCount > 0;
//check if user voting on own submission
let isOwnSubmission = !!db.prepare('all', 'SELECT UUID as submissionCount FROM sponsorTimes where userID = ? AND UUID = ?', [nonAnonUserID, UUID]).length;
if (type === undefined && category !== undefined) { if (type === undefined && category !== undefined) {
return categoryVote(UUID, userID, isVIP, category, hashedIP, res); return categoryVote(UUID, userID, isVIP, category, hashedIP, res);
} }
@ -166,13 +169,13 @@ async function voteOnSponsorTime(req, res) {
let row = db.prepare('get', "SELECT votes, views FROM sponsorTimes WHERE UUID = ?", [UUID]); let row = db.prepare('get', "SELECT votes, views FROM sponsorTimes WHERE UUID = ?", [UUID]);
if (voteTypeEnum === voteTypes.normal) { if (voteTypeEnum === voteTypes.normal) {
if (isVIP && incrementAmount < 0) { if ((isVIP || isOwnSubmission) && incrementAmount < 0) {
//this user is a vip and a downvote //this user is a vip and a downvote
incrementAmount = - (row.votes + 2 - oldIncrementAmount); incrementAmount = - (row.votes + 2 - oldIncrementAmount);
type = incrementAmount; type = incrementAmount;
} }
} else if (voteTypeEnum == voteTypes.incorrect) { } else if (voteTypeEnum == voteTypes.incorrect) {
if (isVIP) { if (isVIP || isOwnSubmission) {
//this user is a vip and a downvote //this user is a vip and a downvote
incrementAmount = 500 * incrementAmount; incrementAmount = 500 * incrementAmount;
type = incrementAmount < 0 ? 12 : 13; type = incrementAmount < 0 ? 12 : 13;

View file

@ -20,6 +20,8 @@ describe('voteOnSponsorTime', () => {
db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-9', '" + getHash("randomID2") + "', 0, 50, 'sponsor', 0)"); db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-9', '" + getHash("randomID2") + "', 0, 50, 'sponsor', 0)");
db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-10', '" + getHash("randomID3") + "', 0, 50, 'sponsor', 0)"); db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-10', '" + getHash("randomID3") + "', 0, 50, 'sponsor', 0)");
db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-11', '" + getHash("randomID4") + "', 0, 50, 'sponsor', 0)"); db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-11', '" + getHash("randomID4") + "', 0, 50, 'sponsor', 0)");
db.exec(startOfQuery + "('own-submission-video', 1, 11, 500, 'own-submission-uuid', '"+ getHash('own-submission-id') +"', 0, 50, 'sponsor', 0)");
db.exec(startOfQuery + "('not-own-submission-video', 1, 11, 500, 'not-own-submission-uuid', '"+ getHash('somebody-else-id') +"', 0, 50, 'sponsor', 0)");
db.exec("INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser") + "')"); db.exec("INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser") + "')");
privateDB.exec("INSERT INTO shadowBannedUsers (userID) VALUES ('" + getHash("randomID4") + "')"); privateDB.exec("INSERT INTO shadowBannedUsers (userID) VALUES ('" + getHash("randomID4") + "')");
@ -151,6 +153,42 @@ describe('voteOnSponsorTime', () => {
}); });
}); });
it('should be able to completely downvote your own segment', (done) => {
request.get(utils.getbaseURL()
+ "/api/voteOnSponsorTime?userID=own-submission-id&UUID=own-submission-uuid&type=0", null,
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["own-submission-uuid"]);
if (row.votes <= -2) {
done()
} else {
done("Vote did not succeed. Submission went from 500 votes to " + row.votes);
}
} else {
done("Status code was " + res.statusCode);
}
});
});
it('should not be able to completely downvote somebody elses segment', (done) => {
request.get(utils.getbaseURL()
+ "/api/voteOnSponsorTime?userID=randomID2&UUID=not-own-submission-uuid&type=0", null,
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["not-own-submission-uuid"]);
if (row.votes === 499) {
done()
} else {
done("Vote did not succeed. Submission went from 500 votes to " + row.votes);
}
} else {
done("Status code was " + res.statusCode);
}
});
});
it('Should be able to vote for a category and it should immediately change (for now)', (done) => { it('Should be able to vote for a category and it should immediately change (for now)', (done) => {
request.get(utils.getbaseURL() request.get(utils.getbaseURL()
+ "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=intro", null, + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=intro", null,