From 6554e142cc639b2f4d69ab4aa79c07e306470d4d Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 4 Apr 2021 23:12:26 -0400 Subject: [PATCH] Add highlight category --- config.json.example | 2 +- src/config.ts | 2 +- src/routes/postSkipSegments.ts | 5 ++-- src/types/segments.model.ts | 2 +- test.json | 2 +- test/cases/postSkipSegments.ts | 45 ++++++++++++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/config.json.example b/config.json.example index f8548c1..3d9b2ef 100644 --- a/config.json.example +++ b/config.json.example @@ -22,7 +22,7 @@ "mode": "development", "readOnly": false, "webhooks": [], - "categoryList": ["sponsor", "intro", "outro", "interaction", "selfpromo", "music_offtopic"], // List of supported categories any other category will be rejected + "categoryList": ["sponsor", "intro", "outro", "interaction", "selfpromo", "preview", "music_offtopic", "highlight"], // List of supported categories any other category will be rejected "getTopUsersCacheTimeMinutes": 5, // cacheTime for getTopUsers result in minutes "maxNumberOfActiveWarnings": 3, // Users with this number of warnings will be blocked until warnings expire "hoursAfterWarningExpire": 24, diff --git a/src/config.ts b/src/config.ts index 7b15987..66960ab 100644 --- a/src/config.ts +++ b/src/config.ts @@ -16,7 +16,7 @@ addDefaults(config, { privateDBSchema: "./databases/_private.db.sql", readOnly: false, webhooks: [], - categoryList: ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic"], + categoryList: ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic", "highlight"], maxNumberOfActiveWarnings: 3, hoursAfterWarningExpires: 24, adminUserID: "", diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index 1f380da..2e0ba6c 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -4,7 +4,7 @@ import {db, privateDB} from '../databases/databases'; import {YouTubeAPI} from '../utils/youtubeApi'; import {getSubmissionUUID} from '../utils/getSubmissionUUID'; import fetch from 'node-fetch'; -import isoDurations from 'iso8601-duration'; +import isoDurations, { end } from 'iso8601-duration'; import {getHash} from '../utils/getHash'; import {getIP} from '../utils/getIP'; import {getFormattedTime} from '../utils/getFormattedTime'; @@ -425,7 +425,8 @@ export async function postSkipSegments(req: Request, res: Response) { let endTime = parseFloat(segments[i].segment[1]); if (isNaN(startTime) || isNaN(endTime) - || startTime === Infinity || endTime === Infinity || startTime < 0 || startTime >= endTime) { + || startTime === Infinity || endTime === Infinity || startTime < 0 || startTime > endTime + || (segments[i].category !== "highlight" && startTime === endTime) || (segments[i].category === "highlight" && startTime !== endTime)) { //invalid request res.status(400).send("One of your segments times are invalid (too short, startTime before endTime, etc.)"); return; diff --git a/src/types/segments.model.ts b/src/types/segments.model.ts index a95bac3..329c41a 100644 --- a/src/types/segments.model.ts +++ b/src/types/segments.model.ts @@ -4,7 +4,7 @@ import { SBRecord } from "./lib.model"; export type SegmentUUID = string & { __segmentUUIDBrand: unknown }; export type VideoID = string & { __videoIDBrand: unknown }; export type VideoDuration = number & { __videoDurationBrand: unknown }; -export type Category = string & { __categoryBrand: unknown }; +export type Category = ("sponsor" | "selfpromo" | "interaction" | "intro" | "outro" | "preview" | "music_offtopic" | "highlight") & { __categoryBrand: unknown }; export type VideoIDHash = VideoID & HashedValue; export type IPAddress = string & { __ipAddressBrand: unknown }; export type HashedIP = IPAddress & HashedValue; diff --git a/test.json b/test.json index 58eb72a..3e147fe 100644 --- a/test.json +++ b/test.json @@ -49,7 +49,7 @@ ] } ], - "categoryList": ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic"], + "categoryList": ["sponsor", "selfpromo", "interaction", "intro", "outro", "preview", "music_offtopic", "highlight"], "maxNumberOfActiveWarnings": 3, "hoursAfterWarningExpires": 24, "rateLimit": { diff --git a/test/cases/postSkipSegments.ts b/test/cases/postSkipSegments.ts index 512c2ee..e084112 100644 --- a/test/cases/postSkipSegments.ts +++ b/test/cases/postSkipSegments.ts @@ -500,6 +500,51 @@ describe('postSkipSegments', () => { .catch(err => done("Couldn't call endpoint")); }); + it('Should be rejected if segment starts and ends at the same time', (done: Done) => { + fetch(getbaseURL() + + "/api/skipSegments?videoID=qqwerty&startTime=90&endTime=90&userID=testing&category=intro", { + method: 'POST', + }) + .then(async res => { + if (res.status === 400) done(); // pass + else { + const body = await res.text(); + done("non 400 status code: " + res.status + " (" + body + ")"); + } + }) + .catch(err => done("Couldn't call endpoint")); + }); + + it('Should be accepted if highlight segment starts and ends at the same time', (done: Done) => { + fetch(getbaseURL() + + "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30&userID=testing&category=highlight", { + method: 'POST', + }) + .then(async res => { + if (res.status === 200) done(); // pass + else { + const body = await res.text(); + done("non 200 status code: " + res.status + " (" + body + ")"); + } + }) + .catch(err => done("Couldn't call endpoint")); + }); + + it('Should be rejected if highlight segment doesn\'t start and end at the same time', (done: Done) => { + fetch(getbaseURL() + + "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30.5&userID=testing&category=highlight", { + method: 'POST', + }) + .then(async res => { + if (res.status === 400) done(); // pass + else { + const body = await res.text(); + done("non 400 status code: " + res.status + " (" + body + ")"); + } + }) + .catch(err => done("Couldn't call endpoint")); + }); + it('Should be rejected if a sponsor is less than 1 second', (done: Done) => { fetch(getbaseURL() + "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30.5&userID=testing", {