Silently reject votes

This commit is contained in:
Ajay Ramachandran
2021-06-08 20:20:05 -04:00
parent 1c8c76831e
commit 4225d9b3b3
3 changed files with 34 additions and 10 deletions

View File

@@ -19,9 +19,17 @@ const voteTypes = {
incorrect: 1, incorrect: 1,
}; };
enum VoteWebhookType {
Normal,
Rejected
}
interface FinalResponse { interface FinalResponse {
blockVote: boolean,
finalStatus: number finalStatus: number
finalMessage: string finalMessage: string,
webhookType: VoteWebhookType,
webhookMessage: string
} }
interface VoteData { interface VoteData {
@@ -52,7 +60,15 @@ async function sendWebhooks(voteData: VoteData) {
if (submissionInfoRow !== undefined && userSubmissionCountRow != undefined) { if (submissionInfoRow !== undefined && userSubmissionCountRow != undefined) {
let webhookURL: string = null; let webhookURL: string = null;
if (voteData.voteTypeEnum === voteTypes.normal) { if (voteData.voteTypeEnum === voteTypes.normal) {
webhookURL = config.discordReportChannelWebhookURL; switch (voteData.finalResponse.webhookType) {
case VoteWebhookType.Normal:
webhookURL = config.discordReportChannelWebhookURL;
break;
case VoteWebhookType.Rejected:
webhookURL = config.discordFailedReportChannelWebhookURL;
break;
}
} else if (voteData.voteTypeEnum === voteTypes.incorrect) { } else if (voteData.voteTypeEnum === voteTypes.incorrect) {
webhookURL = config.discordCompletelyIncorrectReportWebhookURL; webhookURL = config.discordCompletelyIncorrectReportWebhookURL;
} }
@@ -114,7 +130,9 @@ 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.finalResponse?.finalMessage ?? getVoteAuthor(userSubmissionCountRow.submissionCount, voteData.isVIP, voteData.isOwnSubmission), "name": voteData.finalResponse?.webhookMessage ??
voteData.finalResponse?.finalMessage ??
getVoteAuthor(userSubmissionCountRow.submissionCount, voteData.isVIP, voteData.isOwnSubmission),
}, },
"thumbnail": { "thumbnail": {
"url": getMaxResThumbnail(data) || "", "url": getMaxResThumbnail(data) || "",
@@ -252,8 +270,11 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
// To force a non 200, change this early // To force a non 200, change this early
let finalResponse: FinalResponse = { let finalResponse: FinalResponse = {
blockVote: false,
finalStatus: 200, finalStatus: 200,
finalMessage: null finalMessage: null,
webhookType: VoteWebhookType.Normal,
webhookMessage: null
} }
//x-forwarded-for if this server is behind a proxy //x-forwarded-for if this server is behind a proxy
@@ -276,8 +297,9 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
' where "UUID" = ?', [UUID])); ' where "UUID" = ?', [UUID]));
if (await isSegmentLocked() || await isVideoLocked()) { if (await isSegmentLocked() || await isVideoLocked()) {
finalResponse.finalStatus = 403; finalResponse.blockVote = true;
finalResponse.finalMessage = "Vote rejected: A moderator has decided that this segment is correct" finalResponse.webhookType = VoteWebhookType.Normal
finalResponse.webhookMessage = "Vote rejected: A moderator has decided that this segment is correct"
} }
} }
@@ -384,6 +406,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
&& (await db.prepare("get", `SELECT "userID" FROM "sponsorTimes" WHERE "userID" = ?`, [nonAnonUserID])) !== undefined && (await db.prepare("get", `SELECT "userID" FROM "sponsorTimes" WHERE "userID" = ?`, [nonAnonUserID])) !== undefined
&& (await privateDB.prepare("get", `SELECT "userID" FROM "shadowBannedUsers" WHERE "userID" = ?`, [nonAnonUserID])) === undefined && (await privateDB.prepare("get", `SELECT "userID" FROM "shadowBannedUsers" WHERE "userID" = ?`, [nonAnonUserID])) === undefined
&& (await privateDB.prepare("get", `SELECT "UUID" FROM "votes" WHERE "UUID" = ? AND "hashedIP" = ? AND "userID" != ?`, [UUID, hashedIP, userID])) === undefined) && (await privateDB.prepare("get", `SELECT "UUID" FROM "votes" WHERE "UUID" = ? AND "hashedIP" = ? AND "userID" != ?`, [UUID, hashedIP, userID])) === undefined)
&& !finalResponse.blockVote
&& finalResponse.finalStatus === 200; && finalResponse.finalStatus === 200;
if (ableToVote) { if (ableToVote) {

View File

@@ -8,6 +8,7 @@ export interface SBSConfig {
adminUserID: string; adminUserID: string;
newLeafURLs?: string[]; newLeafURLs?: string[];
discordReportChannelWebhookURL?: string; discordReportChannelWebhookURL?: string;
discordFailedReportChannelWebhookURL?: string;
discordFirstTimeSubmissionsWebhookURL?: string; discordFirstTimeSubmissionsWebhookURL?: string;
discordCompletelyIncorrectReportWebhookURL?: string; discordCompletelyIncorrectReportWebhookURL?: string;
neuralBlockURL?: string; neuralBlockURL?: string;

View File

@@ -446,10 +446,10 @@ describe('voteOnSponsorTime', () => {
+ "/api/voteOnSponsorTime?userID=randomID&UUID=no-sponsor-segments-uuid-0&type=0") + "/api/voteOnSponsorTime?userID=randomID&UUID=no-sponsor-segments-uuid-0&type=0")
.then(async res => { .then(async res => {
let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["no-sponsor-segments-uuid-0"]); let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["no-sponsor-segments-uuid-0"]);
if (res.status === 403 && row.votes === 2) { if (res.status === 200 && row.votes === 2) {
done(); done();
} else { } else {
done("Status code was " + res.status + " instead of 403, row was " + JSON.stringify(row)); done("Status code was " + res.status + " instead of 200, row was " + JSON.stringify(row));
} }
}) })
.catch(err => done(err)); .catch(err => done(err));
@@ -474,10 +474,10 @@ describe('voteOnSponsorTime', () => {
+ "/api/voteOnSponsorTime?userID=randomID&UUID=no-sponsor-segments-uuid-0&category=outro") + "/api/voteOnSponsorTime?userID=randomID&UUID=no-sponsor-segments-uuid-0&category=outro")
.then(async res => { .then(async res => {
let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["no-sponsor-segments-uuid-0"]); let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["no-sponsor-segments-uuid-0"]);
if (res.status === 403 && row.category === "sponsor") { if (res.status === 200 && row.category === "sponsor") {
done(); done();
} else { } else {
done("Status code was " + res.status + " instead of 403, row was " + JSON.stringify(row)); done("Status code was " + res.status + " instead of 200, row was " + JSON.stringify(row));
} }
}) })
.catch(err => done(err)); .catch(err => done(err));