diff --git a/databases/_upgrade_private_12.sql b/databases/_upgrade_private_12.sql new file mode 100644 index 0000000..bf222fb --- /dev/null +++ b/databases/_upgrade_private_12.sql @@ -0,0 +1,7 @@ +BEGIN TRANSACTION; + +ALTER TABLE "casualVotes" DROP COLUMN "type"; + +UPDATE "config" SET value = 12 WHERE key = 'version'; + +COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_42.sql b/databases/_upgrade_sponsorTimes_42.sql new file mode 100644 index 0000000..e2fcf3d --- /dev/null +++ b/databases/_upgrade_sponsorTimes_42.sql @@ -0,0 +1,7 @@ +BEGIN TRANSACTION; + +ALTER TABLE "casualVotes" DROP COLUMN "downvotes"; + +UPDATE "config" SET value = 42 WHERE key = 'version'; + +COMMIT; diff --git a/src/routes/getBranding.ts b/src/routes/getBranding.ts index 06053e2..345d2cc 100644 --- a/src/routes/getBranding.ts +++ b/src/routes/getBranding.ts @@ -53,7 +53,7 @@ export async function getVideoBranding(res: Response, videoID: VideoID, service: const getCasualVotes = () => db.prepare( "all", - `SELECT "category", "upvotes", "downvotes" FROM "casualVotes" + `SELECT "category", "upvotes" FROM "casualVotes" WHERE "videoID" = ? AND "service" = ? ORDER BY "timeSubmitted" ASC`, [videoID, service], @@ -131,7 +131,7 @@ export async function getVideoBrandingByHash(videoHashPrefix: VideoIDHash, servi const getCasualVotes = () => db.prepare( "all", - `SELECT "videoID", "category", "upvotes", "downvotes" FROM "casualVotes" + `SELECT "videoID", "category", "upvotes" FROM "casualVotes" WHERE "hashedVideoID" LIKE ? AND "service" = ? ORDER BY "timeSubmitted" ASC`, [`${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[]; - 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, - count: r.upvotes - r.downvotes + count: r.upvotes - (casualDownvotes?.upvotes ?? 0) })).filter((a) => a.count > 0); const videoDuration = dbSegments.filter(s => s.videoDuration !== 0)[0]?.videoDuration ?? null; diff --git a/src/routes/postCasual.ts b/src/routes/postCasual.ts index 88c2943..8a026d3 100644 --- a/src/routes/postCasual.ts +++ b/src/routes/postCasual.ts @@ -14,26 +14,25 @@ import { QueryCacher } from "../utils/queryCacher"; import { acquireLock } from "../utils/redisLock"; import { checkBanStatus } from "../utils/checkBan"; -enum CasualVoteType { - Upvote = 1, - Downvote = 2 -} - interface ExistingVote { UUID: BrandingUUID; type: number; } 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); + 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)) { return res.status(400).send("Bad Request"); } - if (!categories.every((c) => config.casualCategoryList.includes(c))) { - return res.status(400).send("Invalid category"); - } try { const hashedUserID = await getHashCache(userID); @@ -52,24 +51,18 @@ export async function postCasual(req: Request, res: Response) { } const now = Date.now(); - const voteType: CasualVoteType = downvote ? CasualVoteType.Downvote : CasualVoteType.Upvote; - for (const category of categories) { const existingUUID = (await db.prepare("get", `SELECT "UUID" from "casualVotes" where "videoID" = ? AND "category" = ?`, [videoID, category]))?.UUID; 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 (!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 { - await db.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "hashedVideoID", "timeSubmitted", "UUID", "category", "upvotes", "downvotes") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, - [videoID, service, hashedVideoID, now, UUID, category, downvote ? 0 : 1, downvote ? 1 : 0]); + await db.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "hashedVideoID", "timeSubmitted", "UUID", "category", "upvotes") VALUES (?, ?, ?, ?, ?, ?, ?)`, + [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, - hashedUserID: HashedUserID, hashedIP: HashedIP, category: CasualCategory, voteType: CasualVoteType, now: number): Promise { - 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; + hashedUserID: HashedUserID, hashedIP: HashedIP, category: CasualCategory, downvote: boolean, now: number): Promise { + 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.type === voteType) { - return true; - } - - if (existingVote.type === CasualVoteType.Upvote) { - await db.prepare("run", `UPDATE "casualVotes" SET "upvotes" = "upvotes" - 1 WHERE "UUID" = ?`, [UUID]); + return true; + } else { + 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 { - await db.prepare("run", `UPDATE "casualVotes" SET "downvotes" = "downvotes" - 1 WHERE "UUID" = ?`, [UUID]); + // 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", "type", "timeSubmitted") VALUES (?, ?, ?, ?, ?, ?, ?)`, - [videoID, service, hashedUserID, hashedIP, category, voteType, now]); + await privateDB.prepare("run", `INSERT INTO "casualVotes" ("videoID", "service", "userID", "hashedIP", "category", "timeSubmitted") VALUES (?, ?, ?, ?, ?, ?)`, + [videoID, service, hashedUserID, hashedIP, category, now]); return false; } \ No newline at end of file diff --git a/src/types/branding.model.ts b/src/types/branding.model.ts index bc1c28c..f4eae0e 100644 --- a/src/types/branding.model.ts +++ b/src/types/branding.model.ts @@ -3,7 +3,7 @@ import { UserID } from "./user.model"; 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 { videoID: VideoID, diff --git a/test/cases/getBranding.ts b/test/cases/getBranding.ts index 31b693b..bfd47d0 100644 --- a/test/cases/getBranding.ts +++ b/test/cases/getBranding.ts @@ -47,7 +47,7 @@ describe("getBranding", () => { const thumbnailTimestampsQuery = `INSERT INTO "thumbnailTimestamps" ("UUID", "timestamp") 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 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([ db.prepare("run", titleQuery, [videoID1, "title1", 0, "userID1", Service.YouTube, videoID1Hash, 1, "UUID1"]), @@ -150,9 +150,10 @@ describe("getBranding", () => { ]); await Promise.all([ - db.prepare("run", insertCasualVotesQuery, ["postBrandCasual1", videoIDCasual, Service.YouTube, videoIDCasualHash, "clever", 1, 0, Date.now()]), - db.prepare("run", insertCasualVotesQuery, ["postBrandCasual2", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "clever", 1, 1, Date.now()]), - db.prepare("run", insertCasualVotesQuery, ["postBrandCasual3", videoIDCasualDownvoted, Service.YouTube, videoIDCasualDownvotedHash, "other", 4, 1, 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, 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()]), ]); }); diff --git a/test/cases/postCasual.ts b/test/cases/postCasual.ts index 0cd4804..588e136 100644 --- a/test/cases/postCasual.ts +++ b/test/cases/postCasual.ts @@ -16,7 +16,7 @@ describe("postCasual", () => { 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 () => { const videoID = "postCasual1"; @@ -33,7 +33,6 @@ describe("postCasual", () => { assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes.upvotes, 1); - assert.strictEqual(dbVotes.downvotes, 0); }); it("submit same casual vote again", async () => { @@ -51,7 +50,6 @@ describe("postCasual", () => { assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes.upvotes, 1); - assert.strictEqual(dbVotes.downvotes, 0); }); it("submit casual upvote", async () => { @@ -69,14 +67,12 @@ describe("postCasual", () => { assert.strictEqual(dbVotes.category, "clever"); assert.strictEqual(dbVotes.upvotes, 2); - assert.strictEqual(dbVotes.downvotes, 0); }); it("submit casual downvote from same user", async () => { const videoID = "postCasual1"; const res = await postCasual({ - categories: ["clever"], downvote: true, userID: userID1, service: Service.YouTube, @@ -84,18 +80,19 @@ describe("postCasual", () => { }); assert.strictEqual(res.status, 200); - const dbVotes = await queryCasualVotesByVideo(videoID); + const dbVotes = await queryCasualVotesByVideo(videoID, true); - assert.strictEqual(dbVotes.category, "clever"); - assert.strictEqual(dbVotes.upvotes, 1); - assert.strictEqual(dbVotes.downvotes, 1); + assert.strictEqual(dbVotes[0].category, "clever"); + assert.strictEqual(dbVotes[0].upvotes, 1); + + assert.strictEqual(dbVotes[1].category, "downvote"); + assert.strictEqual(dbVotes[1].upvotes, 1); }); it("submit casual downvote from different user", async () => { const videoID = "postCasual1"; const res = await postCasual({ - categories: ["clever"], downvote: true, userID: userID3, service: Service.YouTube, @@ -103,11 +100,13 @@ describe("postCasual", () => { }); assert.strictEqual(res.status, 200); - const dbVotes = await queryCasualVotesByVideo(videoID); + const dbVotes = await queryCasualVotesByVideo(videoID, true); - assert.strictEqual(dbVotes.category, "clever"); - assert.strictEqual(dbVotes.upvotes, 1); - assert.strictEqual(dbVotes.downvotes, 2); + assert.strictEqual(dbVotes[0].category, "clever"); + assert.strictEqual(dbVotes[0].upvotes, 1); + + assert.strictEqual(dbVotes[1].category, "downvote"); + assert.strictEqual(dbVotes[1].upvotes, 2); }); it("submit casual upvote from same user", async () => { @@ -122,11 +121,13 @@ describe("postCasual", () => { }); assert.strictEqual(res.status, 200); - const dbVotes = await queryCasualVotesByVideo(videoID); + const dbVotes = await queryCasualVotesByVideo(videoID, true); - assert.strictEqual(dbVotes.category, "clever"); - assert.strictEqual(dbVotes.upvotes, 2); - assert.strictEqual(dbVotes.downvotes, 1); + assert.strictEqual(dbVotes[0].category, "clever"); + assert.strictEqual(dbVotes[0].upvotes, 2); + + assert.strictEqual(dbVotes[1].category, "downvote"); + assert.strictEqual(dbVotes[1].upvotes, 1); }); it("submit multiple casual votes", async () => { @@ -144,22 +145,19 @@ describe("postCasual", () => { assert.strictEqual(dbVotes[0].category, "clever"); assert.strictEqual(dbVotes[0].upvotes, 1); - assert.strictEqual(dbVotes[0].downvotes, 0); assert.strictEqual(dbVotes[1].category, "other"); assert.strictEqual(dbVotes[1].upvotes, 1); - assert.strictEqual(dbVotes[1].downvotes, 0); }); - it("submit multiple casual downvotes", async () => { - const videoID = "postCasual3"; + it("downvote on video with previous votes with multiple categories", async () => { + const videoID = "postCasual2"; const res = await postCasual({ - categories: ["clever", "other"], + downvote: true, userID: userID1, service: Service.YouTube, - videoID, - downvote: true + videoID }); assert.strictEqual(res.status, 200); @@ -167,11 +165,49 @@ describe("postCasual", () => { assert.strictEqual(dbVotes[0].category, "clever"); assert.strictEqual(dbVotes[0].upvotes, 0); - assert.strictEqual(dbVotes[0].downvotes, 1); assert.strictEqual(dbVotes[1].category, "other"); 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); }); });