mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-10 13:37:01 +03:00
Change casual submission to allow submitting multiple categories
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -106,7 +106,7 @@ export interface CasualVoteSubmission {
|
||||
userID: UserID;
|
||||
service: Service;
|
||||
downvote: boolean | undefined;
|
||||
category: CasualCategory;
|
||||
categories: CasualCategory[];
|
||||
}
|
||||
|
||||
export interface BrandingSegmentDBResult {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user