From 1823a91d54707a2b3eb5e9cbcbaf7a3c76154247 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 4 Sep 2021 00:33:37 -0400 Subject: [PATCH] Limit mute action type to specific categories --- src/config.ts | 10 ++++++++++ src/routes/postSkipSegments.ts | 8 ++++++-- test/cases/postSkipSegments.ts | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index 2aaaf12..7c3ef4d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -20,6 +20,16 @@ addDefaults(config, { readOnly: false, webhooks: [], categoryList: ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic", "poi_highlight"], + categorySupport: { + sponsor: ["skip", "mute"], + selfpromo: ["skip", "mute"], + interaction: ["skip", "mute"], + intro: ["skip"], + outro: ["skip"], + preview: ["skip"], + music_offtopic: ["skip"], + poi_highlight: ["skip"], + }, maxNumberOfActiveWarnings: 1, hoursAfterWarningExpires: 24, adminUserID: "", diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index 72f23a1..054fe6b 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -348,8 +348,8 @@ function checkInvalidFields(videoID: any, userID: any, segments: Array): Ch return CHECK_PASS; } -async function checkEachSegmentValid(userID: string, videoID: VideoID - , segments: Array, service: string, isVIP: boolean, lockedCategoryList: Array): Promise { +async function checkEachSegmentValid(userID: string, videoID: VideoID, + segments: Array, service: string, isVIP: boolean, lockedCategoryList: Array): Promise { for (let i = 0; i < segments.length; i++) { if (segments[i] === undefined || segments[i].segment === undefined || segments[i].category === undefined) { @@ -379,6 +379,10 @@ async function checkEachSegmentValid(userID: string, videoID: VideoID }; } + if (!config.categorySupport[segments[i].category]?.includes(segments[i].actionType)) { + return { pass: false, errorMessage: "ActionType is not supported with this category.", errorCode: 400 }; + } + const startTime = parseFloat(segments[i].segment[0]); const endTime = parseFloat(segments[i].segment[1]); diff --git a/test/cases/postSkipSegments.ts b/test/cases/postSkipSegments.ts index b1f9929..0fef115 100644 --- a/test/cases/postSkipSegments.ts +++ b/test/cases/postSkipSegments.ts @@ -137,6 +137,31 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); + it("Should not be able to submit an intro with mute action type (JSON method)", (done: Done) => { + fetch(`${getbaseURL()}/api/postVideoSponsorTimes`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + userID: "testtesttesttesttesttesttesttesttest", + videoID: "dQw4w9WgXpQ", + segments: [{ + segment: [0, 10], + category: "intro", + actionType: "mute" + }], + }), + }) + .then(async res => { + assert.strictEqual(res.status, 400); + const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "actionType" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXpQ"]); + assert.strictEqual(row, undefined); + done(); + }) + .catch(err => done(err)); + }); + it("Should be able to submit a single time with a duration from the YouTube API (JSON method)", (done: Done) => { fetch(`${getbaseURL()}/api/postVideoSponsorTimes`, { method: "POST",