diff --git a/src/routes/postCasual.ts b/src/routes/postCasual.ts index 1feb494..88c2943 100644 --- a/src/routes/postCasual.ts +++ b/src/routes/postCasual.ts @@ -25,13 +25,13 @@ interface ExistingVote { } export async function postCasual(req: Request, res: Response) { - const { videoID, userID, downvote, category } = req.body as CasualVoteSubmission; + const { videoID, userID, downvote, categories } = req.body as CasualVoteSubmission; const service = getService(req.body.service); - if (!videoID || !userID || userID.length < 30 || !service || !category) { + if (!videoID || !userID || userID.length < 30 || !service || !categories || !Array.isArray(categories)) { return res.status(400).send("Bad Request"); } - if (!config.casualCategoryList.includes(category)) { + if (!categories.every((c) => config.casualCategoryList.includes(c))) { return res.status(400).send("Invalid category"); } @@ -54,28 +54,25 @@ export async function postCasual(req: Request, res: Response) { const now = Date.now(); const voteType: CasualVoteType = downvote ? CasualVoteType.Downvote : CasualVoteType.Upvote; - const existingUUID = (await db.prepare("get", `SELECT "UUID" from "casualVotes" where "videoID" = ? AND "category" = ?`, [videoID, category]))?.UUID; - const UUID = existingUUID || crypto.randomUUID(); + 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); - 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]); + const alreadyVotedTheSame = await handleExistingVotes(videoID, service, UUID, hashedUserID, hashedIP, category, voteType, 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]); + } } + } 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]); } - } else { - if (downvote) { - throw new Error("Title submission doesn't exist"); - } - - 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]); } - //todo: cache clearing QueryCacher.clearBrandingCache({ videoID, hashedVideoID, service }); res.status(200).send("OK"); diff --git a/src/types/branding.model.ts b/src/types/branding.model.ts index 2f3df4b..bc1c28c 100644 --- a/src/types/branding.model.ts +++ b/src/types/branding.model.ts @@ -106,7 +106,7 @@ export interface CasualVoteSubmission { userID: UserID; service: Service; downvote: boolean | undefined; - category: CasualCategory; + categories: CasualCategory[]; } export interface BrandingSegmentDBResult { diff --git a/test/cases/postCasual.ts b/test/cases/postCasual.ts index b2f999a..0cd4804 100644 --- a/test/cases/postCasual.ts +++ b/test/cases/postCasual.ts @@ -22,7 +22,7 @@ describe("postCasual", () => { const videoID = "postCasual1"; const res = await postCasual({ - category: "clever", + categories: ["clever"], userID: userID1, service: Service.YouTube, videoID @@ -40,7 +40,7 @@ describe("postCasual", () => { const videoID = "postCasual1"; const res = await postCasual({ - category: "clever", + categories: ["clever"], userID: userID1, service: Service.YouTube, videoID @@ -58,7 +58,7 @@ describe("postCasual", () => { const videoID = "postCasual1"; const res = await postCasual({ - category: "clever", + categories: ["clever"], userID: userID2, service: Service.YouTube, videoID @@ -76,7 +76,7 @@ describe("postCasual", () => { const videoID = "postCasual1"; const res = await postCasual({ - category: "clever", + categories: ["clever"], downvote: true, userID: userID1, service: Service.YouTube, @@ -95,7 +95,7 @@ describe("postCasual", () => { const videoID = "postCasual1"; const res = await postCasual({ - category: "clever", + categories: ["clever"], downvote: true, userID: userID3, service: Service.YouTube, @@ -114,7 +114,7 @@ describe("postCasual", () => { const videoID = "postCasual1"; const res = await postCasual({ - category: "clever", + categories: ["clever"], downvote: false, userID: userID3, service: Service.YouTube, @@ -129,4 +129,49 @@ describe("postCasual", () => { assert.strictEqual(dbVotes.downvotes, 1); }); + it("submit multiple casual votes", 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[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"; + + const res = await postCasual({ + categories: ["clever", "other"], + userID: userID1, + service: Service.YouTube, + videoID, + downvote: true + }); + + assert.strictEqual(res.status, 200); + const dbVotes = await queryCasualVotesByVideo(videoID, true); + + 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); + }); + });