Make casual downvotes apply to all categories

This commit is contained in:
Ajay
2025-02-13 04:03:38 -05:00
parent d608125b41
commit 5f9b4c8acc
7 changed files with 119 additions and 68 deletions

View File

@@ -0,0 +1,7 @@
BEGIN TRANSACTION;
ALTER TABLE "casualVotes" DROP COLUMN "type";
UPDATE "config" SET value = 12 WHERE key = 'version';
COMMIT;

View File

@@ -0,0 +1,7 @@
BEGIN TRANSACTION;
ALTER TABLE "casualVotes" DROP COLUMN "downvotes";
UPDATE "config" SET value = 42 WHERE key = 'version';
COMMIT;

View File

@@ -53,7 +53,7 @@ export async function getVideoBranding(res: Response, videoID: VideoID, service:
const getCasualVotes = () => db.prepare( const getCasualVotes = () => db.prepare(
"all", "all",
`SELECT "category", "upvotes", "downvotes" FROM "casualVotes" `SELECT "category", "upvotes" FROM "casualVotes"
WHERE "videoID" = ? AND "service" = ? WHERE "videoID" = ? AND "service" = ?
ORDER BY "timeSubmitted" ASC`, ORDER BY "timeSubmitted" ASC`,
[videoID, service], [videoID, service],
@@ -131,7 +131,7 @@ export async function getVideoBrandingByHash(videoHashPrefix: VideoIDHash, servi
const getCasualVotes = () => db.prepare( const getCasualVotes = () => db.prepare(
"all", "all",
`SELECT "videoID", "category", "upvotes", "downvotes" FROM "casualVotes" `SELECT "videoID", "category", "upvotes" FROM "casualVotes"
WHERE "hashedVideoID" LIKE ? AND "service" = ? WHERE "hashedVideoID" LIKE ? AND "service" = ?
ORDER BY "timeSubmitted" ASC`, ORDER BY "timeSubmitted" ASC`,
[`${videoHashPrefix}%`, service], [`${videoHashPrefix}%`, service],
@@ -230,9 +230,10 @@ async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, fe
})) }))
.filter((a) => (fetchAll && !a.original) || a.votes >= 1 || (a.votes >= 0 && !a.original) || a.locked) as ThumbnailResult[]; .filter((a) => (fetchAll && !a.original) || a.votes >= 1 || (a.votes >= 0 && !a.original) || a.locked) as ThumbnailResult[];
const casualVotes = dbCasualVotes.map((r) => ({ const casualDownvotes = dbCasualVotes.filter((r) => r.category === "downvote")[0];
const casualVotes = dbCasualVotes.filter((r) => r.category !== "downvote").map((r) => ({
id: r.category, id: r.category,
count: r.upvotes - r.downvotes count: r.upvotes - (casualDownvotes?.upvotes ?? 0)
})).filter((a) => a.count > 0); })).filter((a) => a.count > 0);
const videoDuration = dbSegments.filter(s => s.videoDuration !== 0)[0]?.videoDuration ?? null; const videoDuration = dbSegments.filter(s => s.videoDuration !== 0)[0]?.videoDuration ?? null;

View File

@@ -14,26 +14,25 @@ import { QueryCacher } from "../utils/queryCacher";
import { acquireLock } from "../utils/redisLock"; import { acquireLock } from "../utils/redisLock";
import { checkBanStatus } from "../utils/checkBan"; import { checkBanStatus } from "../utils/checkBan";
enum CasualVoteType {
Upvote = 1,
Downvote = 2
}
interface ExistingVote { interface ExistingVote {
UUID: BrandingUUID; UUID: BrandingUUID;
type: number; type: number;
} }
export async function postCasual(req: Request, res: Response) { export async function postCasual(req: Request, res: Response) {
const { videoID, userID, downvote, categories } = req.body as CasualVoteSubmission; const { videoID, userID, downvote } = req.body as CasualVoteSubmission;
let categories = req.body.categories as CasualCategory[];
const service = getService(req.body.service); const service = getService(req.body.service);
if (downvote) {
categories = ["downvote" as CasualCategory];
} else if (!categories.every((c) => config.casualCategoryList.includes(c))) {
return res.status(400).send("Invalid category");
}
if (!videoID || !userID || userID.length < 30 || !service || !categories || !Array.isArray(categories)) { if (!videoID || !userID || userID.length < 30 || !service || !categories || !Array.isArray(categories)) {
return res.status(400).send("Bad Request"); return res.status(400).send("Bad Request");
} }
if (!categories.every((c) => config.casualCategoryList.includes(c))) {
return res.status(400).send("Invalid category");
}
try { try {
const hashedUserID = await getHashCache(userID); const hashedUserID = await getHashCache(userID);
@@ -52,24 +51,18 @@ export async function postCasual(req: Request, res: Response) {
} }
const now = Date.now(); const now = Date.now();
const voteType: CasualVoteType = downvote ? CasualVoteType.Downvote : CasualVoteType.Upvote;
for (const category of categories) { for (const category of categories) {
const existingUUID = (await db.prepare("get", `SELECT "UUID" from "casualVotes" where "videoID" = ? AND "category" = ?`, [videoID, category]))?.UUID; const existingUUID = (await db.prepare("get", `SELECT "UUID" from "casualVotes" where "videoID" = ? AND "category" = ?`, [videoID, category]))?.UUID;
const UUID = existingUUID || crypto.randomUUID(); const UUID = existingUUID || crypto.randomUUID();
const alreadyVotedTheSame = await handleExistingVotes(videoID, service, UUID, hashedUserID, hashedIP, category, voteType, now); const alreadyVotedTheSame = await handleExistingVotes(videoID, service, UUID, hashedUserID, hashedIP, category, downvote, now);
if (existingUUID) { if (existingUUID) {
if (!alreadyVotedTheSame) { if (!alreadyVotedTheSame) {
if (downvote) {
await db.prepare("run", `UPDATE "casualVotes" SET "downvotes" = "downvotes" + 1 WHERE "UUID" = ?`, [UUID]);
} else {
await db.prepare("run", `UPDATE "casualVotes" SET "upvotes" = "upvotes" + 1 WHERE "UUID" = ?`, [UUID]); await db.prepare("run", `UPDATE "casualVotes" SET "upvotes" = "upvotes" + 1 WHERE "UUID" = ?`, [UUID]);
} }
}
} else { } else {
await db.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "hashedVideoID", "timeSubmitted", "UUID", "category", "upvotes", "downvotes") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, await db.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "hashedVideoID", "timeSubmitted", "UUID", "category", "upvotes") VALUES (?, ?, ?, ?, ?, ?, ?)`,
[videoID, service, hashedVideoID, now, UUID, category, downvote ? 0 : 1, downvote ? 1 : 0]); [videoID, service, hashedVideoID, now, UUID, category, 1]);
} }
} }
@@ -85,24 +78,30 @@ export async function postCasual(req: Request, res: Response) {
} }
async function handleExistingVotes(videoID: VideoID, service: Service, UUID: string, async function handleExistingVotes(videoID: VideoID, service: Service, UUID: string,
hashedUserID: HashedUserID, hashedIP: HashedIP, category: CasualCategory, voteType: CasualVoteType, now: number): Promise<boolean> { hashedUserID: HashedUserID, hashedIP: HashedIP, category: CasualCategory, downvote: boolean, now: number): Promise<boolean> {
const existingVote = await privateDB.prepare("get", `SELECT "UUID", "type" from "casualVotes" WHERE "videoID" = ? AND "service" = ? AND "userID" = ? AND category = ?`, [videoID, service, hashedUserID, category]) as ExistingVote; const existingVote = await privateDB.prepare("get", `SELECT "UUID" from "casualVotes" WHERE "videoID" = ? AND "service" = ? AND "userID" = ? AND "category" = ?`, [videoID, service, hashedUserID, category]) as ExistingVote;
if (existingVote) { if (existingVote) {
if (existingVote.type === voteType) {
return true; return true;
}
if (existingVote.type === CasualVoteType.Upvote) {
await db.prepare("run", `UPDATE "casualVotes" SET "upvotes" = "upvotes" - 1 WHERE "UUID" = ?`, [UUID]);
} else { } else {
await db.prepare("run", `UPDATE "casualVotes" SET "downvotes" = "downvotes" - 1 WHERE "UUID" = ?`, [UUID]); if (downvote) {
// Remove upvotes for all categories on this video
const existingUpvotes = await privateDB.prepare("all", `SELECT "category" from "casualVotes" WHERE "category" != 'downvote' AND "videoID" = ? AND "service" = ? AND "userID" = ?`, [videoID, service, hashedUserID]);
for (const existingUpvote of existingUpvotes) {
await db.prepare("run", `UPDATE "casualVotes" SET "upvotes" = "upvotes" - 1 WHERE "videoID" = ? AND "category" = ?`, [videoID, existingUpvote.category]);
await privateDB.prepare("run", `DELETE FROM "casualVotes" WHERE "videoID" = ? AND "userID" = ? AND "category" = ?`, [videoID, hashedUserID, existingUpvote.category]);
}
} else {
// Undo a downvote if it exists
const existingDownvote = await privateDB.prepare("get", `SELECT "UUID" from "casualVotes" WHERE "category" = 'downvote' AND "videoID" = ? AND "service" = ? AND "userID" = ?`, [videoID, service, hashedUserID]) as ExistingVote;
if (existingDownvote) {
await db.prepare("run", `UPDATE "casualVotes" SET "upvotes" = "upvotes" - 1 WHERE "category" = 'downvote' AND "videoID" = ?`, [videoID]);
await privateDB.prepare("run", `DELETE FROM "casualVotes" WHERE "category" = 'downvote' AND "videoID" = ? AND "userID" = ?`, [videoID, hashedUserID]);
}
}
} }
await privateDB.prepare("run", `DELETE FROM "casualVotes" WHERE "UUID" = ?`, [existingVote.UUID]); await privateDB.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "userID", "hashedIP", "category", "timeSubmitted") VALUES (?, ?, ?, ?, ?, ?)`,
} [videoID, service, hashedUserID, hashedIP, category, now]);
await privateDB.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "userID", "hashedIP", "category", "type", "timeSubmitted") VALUES (?, ?, ?, ?, ?, ?, ?)`,
[videoID, service, hashedUserID, hashedIP, category, voteType, now]);
return false; return false;
} }

View File

@@ -3,7 +3,7 @@ import { UserID } from "./user.model";
export type BrandingUUID = string & { readonly __brandingUUID: unique symbol }; export type BrandingUUID = string & { readonly __brandingUUID: unique symbol };
export type CasualCategory = ("funny" | "creative" | "clever" | "descriptive" | "other") & { __casualCategoryBrand: unknown }; export type CasualCategory = ("funny" | "creative" | "clever" | "descriptive" | "other" | "downvote") & { __casualCategoryBrand: unknown };
export interface BrandingDBSubmissionData { export interface BrandingDBSubmissionData {
videoID: VideoID, videoID: VideoID,

View File

@@ -47,7 +47,7 @@ describe("getBranding", () => {
const thumbnailTimestampsQuery = `INSERT INTO "thumbnailTimestamps" ("UUID", "timestamp") VALUES (?, ?)`; const thumbnailTimestampsQuery = `INSERT INTO "thumbnailTimestamps" ("UUID", "timestamp") VALUES (?, ?)`;
const thumbnailVotesQuery = `INSERT INTO "thumbnailVotes" ("UUID", "votes", "locked", "shadowHidden", "downvotes", "removed") VALUES (?, ?, ?, ?, ?, ?)`; const thumbnailVotesQuery = `INSERT INTO "thumbnailVotes" ("UUID", "votes", "locked", "shadowHidden", "downvotes", "removed") VALUES (?, ?, ?, ?, ?, ?)`;
const segmentQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "description", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; const segmentQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "description", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
const insertCasualVotesQuery = `INSERT INTO "casualVotes" ("UUID", "videoID", "service", "hashedVideoID", "category", "upvotes", "downvotes", "timeSubmitted") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`; const insertCasualVotesQuery = `INSERT INTO "casualVotes" ("UUID", "videoID", "service", "hashedVideoID", "category", "upvotes", "timeSubmitted") VALUES (?, ?, ?, ?, ?, ?, ?)`;
await Promise.all([ await Promise.all([
db.prepare("run", titleQuery, [videoID1, "title1", 0, "userID1", Service.YouTube, videoID1Hash, 1, "UUID1"]), db.prepare("run", titleQuery, [videoID1, "title1", 0, "userID1", Service.YouTube, videoID1Hash, 1, "UUID1"]),
@@ -150,9 +150,10 @@ describe("getBranding", () => {
]); ]);
await Promise.all([ await Promise.all([
db.prepare("run", insertCasualVotesQuery, ["postBrandCasual1", videoIDCasual, Service.YouTube, videoIDCasualHash, "clever", 1, 0, Date.now()]), db.prepare("run", insertCasualVotesQuery, ["postBrandCasual1", videoIDCasual, Service.YouTube, videoIDCasualHash, "clever", 1, Date.now()]),
db.prepare("run", insertCasualVotesQuery, ["postBrandCasual2", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "clever", 1, 1, Date.now()]), db.prepare("run", insertCasualVotesQuery, ["postBrandCasual2", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "clever", 1, Date.now()]),
db.prepare("run", insertCasualVotesQuery, ["postBrandCasual3", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "other", 4, 1, Date.now()]) db.prepare("run", insertCasualVotesQuery, ["postBrandCasual2d", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "downvote", 1, Date.now()]),
db.prepare("run", insertCasualVotesQuery, ["postBrandCasual3", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "other", 4, Date.now()]),
]); ]);
}); });

View File

@@ -16,7 +16,7 @@ describe("postCasual", () => {
data data
}); });
const queryCasualVotesByVideo = (videoID: string, all = false) => db.prepare(all ? "all" : "get", `SELECT * FROM "casualVotes" WHERE "videoID" = ? ORDER BY "timeSubmitted" DESC`, [videoID]); const queryCasualVotesByVideo = (videoID: string, all = false) => db.prepare(all ? "all" : "get", `SELECT * FROM "casualVotes" WHERE "videoID" = ? ORDER BY "timeSubmitted" ASC`, [videoID]);
it("submit casual vote", async () => { it("submit casual vote", async () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
@@ -33,7 +33,6 @@ describe("postCasual", () => {
assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 1); assert.strictEqual(dbVotes.upvotes, 1);
assert.strictEqual(dbVotes.downvotes, 0);
}); });
it("submit same casual vote again", async () => { it("submit same casual vote again", async () => {
@@ -51,7 +50,6 @@ describe("postCasual", () => {
assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 1); assert.strictEqual(dbVotes.upvotes, 1);
assert.strictEqual(dbVotes.downvotes, 0);
}); });
it("submit casual upvote", async () => { it("submit casual upvote", async () => {
@@ -69,14 +67,12 @@ describe("postCasual", () => {
assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes.category, "clever");
assert.strictEqual(dbVotes.upvotes, 2); assert.strictEqual(dbVotes.upvotes, 2);
assert.strictEqual(dbVotes.downvotes, 0);
}); });
it("submit casual downvote from same user", async () => { it("submit casual downvote from same user", async () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
const res = await postCasual({ const res = await postCasual({
categories: ["clever"],
downvote: true, downvote: true,
userID: userID1, userID: userID1,
service: Service.YouTube, service: Service.YouTube,
@@ -84,18 +80,19 @@ describe("postCasual", () => {
}); });
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID); const dbVotes = await queryCasualVotesByVideo(videoID, true);
assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes[0].category, "clever");
assert.strictEqual(dbVotes.upvotes, 1); assert.strictEqual(dbVotes[0].upvotes, 1);
assert.strictEqual(dbVotes.downvotes, 1);
assert.strictEqual(dbVotes[1].category, "downvote");
assert.strictEqual(dbVotes[1].upvotes, 1);
}); });
it("submit casual downvote from different user", async () => { it("submit casual downvote from different user", async () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
const res = await postCasual({ const res = await postCasual({
categories: ["clever"],
downvote: true, downvote: true,
userID: userID3, userID: userID3,
service: Service.YouTube, service: Service.YouTube,
@@ -103,11 +100,13 @@ describe("postCasual", () => {
}); });
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID); const dbVotes = await queryCasualVotesByVideo(videoID, true);
assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes[0].category, "clever");
assert.strictEqual(dbVotes.upvotes, 1); assert.strictEqual(dbVotes[0].upvotes, 1);
assert.strictEqual(dbVotes.downvotes, 2);
assert.strictEqual(dbVotes[1].category, "downvote");
assert.strictEqual(dbVotes[1].upvotes, 2);
}); });
it("submit casual upvote from same user", async () => { it("submit casual upvote from same user", async () => {
@@ -122,11 +121,13 @@ describe("postCasual", () => {
}); });
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID); const dbVotes = await queryCasualVotesByVideo(videoID, true);
assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes[0].category, "clever");
assert.strictEqual(dbVotes.upvotes, 2); assert.strictEqual(dbVotes[0].upvotes, 2);
assert.strictEqual(dbVotes.downvotes, 1);
assert.strictEqual(dbVotes[1].category, "downvote");
assert.strictEqual(dbVotes[1].upvotes, 1);
}); });
it("submit multiple casual votes", async () => { it("submit multiple casual votes", async () => {
@@ -144,22 +145,19 @@ describe("postCasual", () => {
assert.strictEqual(dbVotes[0].category, "clever"); assert.strictEqual(dbVotes[0].category, "clever");
assert.strictEqual(dbVotes[0].upvotes, 1); assert.strictEqual(dbVotes[0].upvotes, 1);
assert.strictEqual(dbVotes[0].downvotes, 0);
assert.strictEqual(dbVotes[1].category, "other"); assert.strictEqual(dbVotes[1].category, "other");
assert.strictEqual(dbVotes[1].upvotes, 1); assert.strictEqual(dbVotes[1].upvotes, 1);
assert.strictEqual(dbVotes[1].downvotes, 0);
}); });
it("submit multiple casual downvotes", async () => { it("downvote on video with previous votes with multiple categories", async () => {
const videoID = "postCasual3"; const videoID = "postCasual2";
const res = await postCasual({ const res = await postCasual({
categories: ["clever", "other"], downvote: true,
userID: userID1, userID: userID1,
service: Service.YouTube, service: Service.YouTube,
videoID, videoID
downvote: true
}); });
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -167,11 +165,49 @@ describe("postCasual", () => {
assert.strictEqual(dbVotes[0].category, "clever"); assert.strictEqual(dbVotes[0].category, "clever");
assert.strictEqual(dbVotes[0].upvotes, 0); assert.strictEqual(dbVotes[0].upvotes, 0);
assert.strictEqual(dbVotes[0].downvotes, 1);
assert.strictEqual(dbVotes[1].category, "other"); assert.strictEqual(dbVotes[1].category, "other");
assert.strictEqual(dbVotes[1].upvotes, 0); assert.strictEqual(dbVotes[1].upvotes, 0);
assert.strictEqual(dbVotes[1].downvotes, 1);
assert.strictEqual(dbVotes[2].category, "downvote");
assert.strictEqual(dbVotes[2].upvotes, 1);
});
it("upvote on video with previous downvotes with multiple categories", async () => {
const videoID = "postCasual2";
const res = await postCasual({
categories: ["clever", "other"],
userID: userID1,
service: Service.YouTube,
videoID
});
assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID, true);
assert.strictEqual(dbVotes[0].category, "clever");
assert.strictEqual(dbVotes[0].upvotes, 1);
assert.strictEqual(dbVotes[1].category, "other");
assert.strictEqual(dbVotes[1].upvotes, 1);
});
it("downvote on video with no existing votes", async () => {
const videoID = "postCasual3";
const res = await postCasual({
userID: userID1,
service: Service.YouTube,
videoID,
downvote: true
});
assert.strictEqual(res.status, 200);
const dbVotes = await queryCasualVotesByVideo(videoID);
assert.strictEqual(dbVotes.category, "downvote");
assert.strictEqual(dbVotes.upvotes, 1);
}); });
}); });