Fix rejecting downvotes

This commit is contained in:
Ajay Ramachandran
2021-03-19 18:19:45 -04:00
parent a27adffec0
commit 8f2ea30da0

View File

@@ -20,6 +20,11 @@ const voteTypes = {
incorrect: 1, incorrect: 1,
}; };
interface FinalResponse {
finalStatus: number
finalMessage: string
}
interface VoteData { interface VoteData {
UUID: string; UUID: string;
nonAnonUserID: string; nonAnonUserID: string;
@@ -33,7 +38,7 @@ interface VoteData {
category: string; category: string;
incrementAmount: number; incrementAmount: number;
oldIncrementAmount: number; oldIncrementAmount: number;
finalMessage: string; finalResponse: FinalResponse;
} }
async function sendWebhooks(voteData: VoteData) { async function sendWebhooks(voteData: VoteData) {
@@ -112,7 +117,7 @@ async function sendWebhooks(voteData: VoteData) {
getFormattedTime(submissionInfoRow.startTime) + " to " + getFormattedTime(submissionInfoRow.endTime), getFormattedTime(submissionInfoRow.startTime) + " to " + getFormattedTime(submissionInfoRow.endTime),
"color": 10813440, "color": 10813440,
"author": { "author": {
"name": voteData.finalMessage ?? getVoteAuthor(userSubmissionCountRow.submissionCount, voteData.isVIP, voteData.isOwnSubmission), "name": voteData.finalResponse?.finalMessage ?? getVoteAuthor(userSubmissionCountRow.submissionCount, voteData.isVIP, voteData.isOwnSubmission),
}, },
"thumbnail": { "thumbnail": {
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "", "url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
@@ -142,13 +147,14 @@ async function sendWebhooks(voteData: VoteData) {
} }
} }
async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string, hashedIP: string, res: Response) { async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string
, hashedIP: string, finalResponse: FinalResponse, res: Response) {
// Check if they've already made a vote // Check if they've already made a vote
const usersLastVoteInfo = await privateDB.prepare('get', `select count(*) as votes, category from "categoryVotes" where "UUID" = ? and "userID" = ? group by category`, [UUID, userID]); const usersLastVoteInfo = await privateDB.prepare('get', `select count(*) as votes, category from "categoryVotes" where "UUID" = ? and "userID" = ? group by category`, [UUID, userID]);
if (usersLastVoteInfo?.category === category) { if (usersLastVoteInfo?.category === category) {
// Double vote, ignore // Double vote, ignore
res.sendStatus(200); res.sendStatus(finalResponse.finalStatus);
return; return;
} }
@@ -216,7 +222,7 @@ async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnS
await db.prepare('run', `update "sponsorTimes" set "category" = ? where "UUID" = ?`, [category, UUID]); await db.prepare('run', `update "sponsorTimes" set "category" = ? where "UUID" = ?`, [category, UUID]);
} }
res.sendStatus(200); res.sendStatus(finalResponse.finalStatus);
} }
export function getUserID(req: Request): UserID { export function getUserID(req: Request): UserID {
@@ -240,8 +246,10 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
const userID = getHash(paramUserID + UUID); const userID = getHash(paramUserID + UUID);
// To force a non 200, change this early // To force a non 200, change this early
let finalStatus = 200; let finalResponse: FinalResponse = {
let finalMessage = null; finalStatus: 200,
finalMessage: null
}
//x-forwarded-for if this server is behind a proxy //x-forwarded-for if this server is behind a proxy
const ip = getIP(req); const ip = getIP(req);
@@ -263,14 +271,13 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
' where "UUID" = ?', [UUID])); ' where "UUID" = ?', [UUID]));
if (await isSegmentLocked() || await isVideoLocked()) { if (await isSegmentLocked() || await isVideoLocked()) {
finalStatus = 403; finalResponse.finalStatus = 403;
finalMessage = "Vote rejected: A moderator has decided that this segment is correct" finalResponse.finalMessage = "Vote rejected: A moderator has decided that this segment is correct"
return;
} }
} }
if (type === undefined && category !== undefined) { if (type === undefined && category !== undefined) {
return categoryVote(UUID, nonAnonUserID, isVIP, isOwnSubmission, category, hashedIP, res); return categoryVote(UUID, nonAnonUserID, isVIP, isOwnSubmission, category, hashedIP, finalResponse, res);
} }
if (type == 1 && !isVIP && !isOwnSubmission) { if (type == 1 && !isVIP && !isOwnSubmission) {
@@ -421,7 +428,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
} }
} }
res.status(finalStatus).send(finalMessage ?? undefined); res.status(finalResponse.finalStatus).send(finalResponse.finalMessage ?? undefined);
if (incrementAmount - oldIncrementAmount !== 0) { if (incrementAmount - oldIncrementAmount !== 0) {
sendWebhooks({ sendWebhooks({
@@ -434,7 +441,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
category, category,
incrementAmount, incrementAmount,
oldIncrementAmount, oldIncrementAmount,
finalMessage finalResponse
}); });
} }
} catch (err) { } catch (err) {