Change casual submission to allow submitting multiple categories

This commit is contained in:
Ajay
2025-02-06 02:57:09 -05:00
parent 4abf57b0ce
commit ccde64e90f
3 changed files with 69 additions and 27 deletions

View File

@@ -25,13 +25,13 @@ interface ExistingVote {
} }
export async function postCasual(req: Request, res: Response) { 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); 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"); 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"); return res.status(400).send("Invalid category");
} }
@@ -54,28 +54,25 @@ export async function postCasual(req: Request, res: Response) {
const now = Date.now(); const now = Date.now();
const voteType: CasualVoteType = downvote ? CasualVoteType.Downvote : CasualVoteType.Upvote; 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; for (const category of categories) {
const UUID = existingUUID || crypto.randomUUID(); 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, voteType, now);
if (existingUUID) { if (existingUUID) {
if (!alreadyVotedTheSame) { if (!alreadyVotedTheSame) {
if (downvote) { if (downvote) {
await db.prepare("run", `UPDATE "casualVotes" SET "downvotes" = "downvotes" + 1 WHERE "UUID" = ?`, [UUID]); await db.prepare("run", `UPDATE "casualVotes" SET "downvotes" = "downvotes" + 1 WHERE "UUID" = ?`, [UUID]);
} else { } 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]);
} }
} 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 }); QueryCacher.clearBrandingCache({ videoID, hashedVideoID, service });
res.status(200).send("OK"); res.status(200).send("OK");

View File

@@ -106,7 +106,7 @@ export interface CasualVoteSubmission {
userID: UserID; userID: UserID;
service: Service; service: Service;
downvote: boolean | undefined; downvote: boolean | undefined;
category: CasualCategory; categories: CasualCategory[];
} }
export interface BrandingSegmentDBResult { export interface BrandingSegmentDBResult {

View File

@@ -22,7 +22,7 @@ describe("postCasual", () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
const res = await postCasual({ const res = await postCasual({
category: "clever", categories: ["clever"],
userID: userID1, userID: userID1,
service: Service.YouTube, service: Service.YouTube,
videoID videoID
@@ -40,7 +40,7 @@ describe("postCasual", () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
const res = await postCasual({ const res = await postCasual({
category: "clever", categories: ["clever"],
userID: userID1, userID: userID1,
service: Service.YouTube, service: Service.YouTube,
videoID videoID
@@ -58,7 +58,7 @@ describe("postCasual", () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
const res = await postCasual({ const res = await postCasual({
category: "clever", categories: ["clever"],
userID: userID2, userID: userID2,
service: Service.YouTube, service: Service.YouTube,
videoID videoID
@@ -76,7 +76,7 @@ describe("postCasual", () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
const res = await postCasual({ const res = await postCasual({
category: "clever", categories: ["clever"],
downvote: true, downvote: true,
userID: userID1, userID: userID1,
service: Service.YouTube, service: Service.YouTube,
@@ -95,7 +95,7 @@ describe("postCasual", () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
const res = await postCasual({ const res = await postCasual({
category: "clever", categories: ["clever"],
downvote: true, downvote: true,
userID: userID3, userID: userID3,
service: Service.YouTube, service: Service.YouTube,
@@ -114,7 +114,7 @@ describe("postCasual", () => {
const videoID = "postCasual1"; const videoID = "postCasual1";
const res = await postCasual({ const res = await postCasual({
category: "clever", categories: ["clever"],
downvote: false, downvote: false,
userID: userID3, userID: userID3,
service: Service.YouTube, service: Service.YouTube,
@@ -129,4 +129,49 @@ describe("postCasual", () => {
assert.strictEqual(dbVotes.downvotes, 1); 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);
});
}); });