From 356974b47859f041e26d500b7fdaa87e6b0a3b2d Mon Sep 17 00:00:00 2001 From: Haidang666 Date: Thu, 30 Sep 2021 13:56:55 +0700 Subject: [PATCH 1/9] add service to table only have videoID --- DatabaseSchema.md | 11 ++-- databases/_private_indexes.sql | 4 +- databases/_sponsorTimes_indexes.sql | 4 +- databases/_upgrade_private_3.sql | 10 ++++ databases/_upgrade_sponsorTimes_24.sql | 21 +++++++ src/routes/addUnlistedVideo.ts | 4 +- src/routes/deleteLockCategories.ts | 23 +++++--- src/routes/getLockCategories.ts | 6 +- src/routes/getSkipSegments.ts | 5 +- src/routes/postLockCategories.ts | 14 +++-- src/routes/postSkipSegments.ts | 23 ++++---- src/routes/shadowBanUser.ts | 4 +- src/routes/voteOnSponsorTime.ts | 3 +- src/utils/getSubmissionUUID.ts | 13 ++++- src/utils/reputation.ts | 2 +- test/cases/getLockCategories.ts | 77 ++++++++++++++++++++++++-- test/cases/getSubmissionUUID.ts | 6 +- test/cases/lockCategoriesRecords.ts | 18 +++--- 18 files changed, 188 insertions(+), 60 deletions(-) create mode 100644 databases/_upgrade_private_3.sql create mode 100644 databases/_upgrade_sponsorTimes_24.sql diff --git a/DatabaseSchema.md b/DatabaseSchema.md index 935f978..5f41446 100644 --- a/DatabaseSchema.md +++ b/DatabaseSchema.md @@ -37,7 +37,7 @@ | views | INTEGER | not null | | category | TEXT | not null, default 'sponsor' | | actionType | TEXT | not null, default 'skip' | -| service | TEXT | not null, default 'Youtube' | +| service | TEXT | not null, default 'YouTube' | | videoDuration | INTEGER | not null, default '0' | | hidden | INTEGER | not null, default '0' | | reputation | REAL | not null, default '0' | @@ -96,10 +96,11 @@ | category | TEXT | not null | | hashedVideoID | TEXT | not null, default '' | | reason | TEXT | not null, default '' | +| service | TEXT | not null, default 'YouTube' | | index | field | | -- | :--: | -| noSegments_videoID | videoID | +| lockCategories_videoID | videoID, service, category | ### warnings @@ -135,6 +136,7 @@ | views | TEXT | not null | | channelID | TEXT | not null | | timeSubmitted | INTEGER | not null | +| service | TEXT | not null, default 'YouTube' | ### config @@ -159,7 +161,7 @@ | views | INTEGER | not null | | category | TEXT | not null, default 'sponsor' | | actionType | TEXT | not null, default 'skip' | -| service | TEXT | not null, default 'Youtube' | +| service | TEXT | not null, default 'YouTube' | | videoDuration | INTEGER | not null, default '0' | | hidden | INTEGER | not null, default '0' | | reputation | REAL | not null, default '0' | @@ -208,11 +210,12 @@ | videoID | TEXT | not null | | hashedIP | TEXT | not null | | timeSubmitted | INTEGER | not null | +| service | TEXT | not null, default 'YouTube' | | index | field | | -- | :--: | | sponsorTimes_hashedIP | hashedIP | -| privateDB_sponsorTimes_videoID | videoID | +| privateDB_sponsorTimes_videoID_v2 | videoID, service | ### config diff --git a/databases/_private_indexes.sql b/databases/_private_indexes.sql index d930c85..48ed490 100644 --- a/databases/_private_indexes.sql +++ b/databases/_private_indexes.sql @@ -5,9 +5,9 @@ CREATE INDEX IF NOT EXISTS "sponsorTimes_hashedIP" ("hashedIP" COLLATE pg_catalog."default" ASC NULLS LAST) TABLESPACE pg_default; -CREATE INDEX IF NOT EXISTS "privateDB_sponsorTimes_videoID" +CREATE INDEX IF NOT EXISTS "privateDB_sponsorTimes_videoID_v2" ON public."sponsorTimes" USING btree - ("videoID" ASC NULLS LAST) + ("videoID" ASC NULLS LAST, service COLLATE pg_catalog."default" ASC NULLS LAST) ; -- votes diff --git a/databases/_sponsorTimes_indexes.sql b/databases/_sponsorTimes_indexes.sql index 40fe33e..cfcab8f 100644 --- a/databases/_sponsorTimes_indexes.sql +++ b/databases/_sponsorTimes_indexes.sql @@ -53,9 +53,9 @@ CREATE INDEX IF NOT EXISTS "warnings_issueTime" -- lockCategories -CREATE INDEX IF NOT EXISTS "noSegments_videoID" +CREATE INDEX IF NOT EXISTS "lockCategories_videoID" ON public."lockCategories" USING btree - ("videoID" COLLATE pg_catalog."default" ASC NULLS LAST, category COLLATE pg_catalog."default" ASC NULLS LAST) + ("videoID" COLLATE pg_catalog."default" ASC NULLS LAST, service COLLATE pg_catalog."default" ASC NULLS LAST, category COLLATE pg_catalog."default" ASC NULLS LAST) TABLESPACE pg_default; -- categoryVotes diff --git a/databases/_upgrade_private_3.sql b/databases/_upgrade_private_3.sql new file mode 100644 index 0000000..ecc5ecd --- /dev/null +++ b/databases/_upgrade_private_3.sql @@ -0,0 +1,10 @@ +BEGIN TRANSACTION; + +ALTER TABLE "sponsorTimes" ADD "service" TEXT NOT NULL default 'YouTube'; +-- UPDATE "sponsorTimes" SET "service" = "YouTube"; + +DROP INDEX IF EXISTS "privateDB_sponsorTimes_videoID"; + +UPDATE "config" SET value = 3 WHERE key = 'version'; + +COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_24.sql b/databases/_upgrade_sponsorTimes_24.sql new file mode 100644 index 0000000..fc35878 --- /dev/null +++ b/databases/_upgrade_sponsorTimes_24.sql @@ -0,0 +1,21 @@ +BEGIN TRANSACTION; + +ALTER TABLE "lockCategories" ADD "service" TEXT NOT NULL default 'YouTube'; + +UPDATE "lockCategories" +SET "service" = "sponsorTimes"."service" +FROM "sponsorTimes" +WHERE "lockCategories"."videoID" = "sponsorTimes"."videoID"; + +ALTER TABLE "unlistedVideos" ADD "service" TEXT NOT NULL default 'YouTube'; + +UPDATE "unlistedVideos" +SET "service" = "sponsorTimes"."service" +FROM "sponsorTimes" +WHERE "unlistedVideos"."videoID" = "sponsorTimes"."videoID"; + +DROP INDEX IF EXISTS "noSegments_videoID"; + +UPDATE "config" SET value = 24 WHERE key = 'version'; + +COMMIT; diff --git a/src/routes/addUnlistedVideo.ts b/src/routes/addUnlistedVideo.ts index db5fef4..4c6cdba 100644 --- a/src/routes/addUnlistedVideo.ts +++ b/src/routes/addUnlistedVideo.ts @@ -1,5 +1,6 @@ import { Request, Response } from "express"; import { db } from "../databases/databases"; +import { getService } from "../utils/getService"; import { Logger } from "../utils/logger"; /** @@ -14,6 +15,7 @@ export function addUnlistedVideo(req: Request, res: Response): Response { const year = req.body.year || 0; const views = req.body.views || 0; const channelID = req.body.channelID || "Unknown"; + const service = getService(req.body.service); if (videoID === undefined || typeof(videoID) !== "string" || videoID.length !== 11) { return res.status(400).send("Invalid parameters"); @@ -21,7 +23,7 @@ export function addUnlistedVideo(req: Request, res: Response): Response { try { const timeSubmitted = Date.now(); - db.prepare("run", `INSERT INTO "unlistedVideos" ("videoID", "year", "views", "channelID", "timeSubmitted") values (?, ?, ?, ?, ?)`, [videoID, year, views, channelID, timeSubmitted]); + db.prepare("run", `INSERT INTO "unlistedVideos" ("videoID", "year", "views", "channelID", "timeSubmitted", "service") values (?, ?, ?, ?, ?, ?)`, [videoID, year, views, channelID, timeSubmitted, service]); } catch (err) { Logger.error(err as string); return res.sendStatus(500); diff --git a/src/routes/deleteLockCategories.ts b/src/routes/deleteLockCategories.ts index f50df50..ec1b665 100644 --- a/src/routes/deleteLockCategories.ts +++ b/src/routes/deleteLockCategories.ts @@ -2,14 +2,16 @@ import { Request, Response } from "express"; import { isUserVIP } from "../utils/isUserVIP"; import { getHash } from "../utils/getHash"; import { db } from "../databases/databases"; -import { Category, VideoID } from "../types/segments.model"; +import { Category, Service, VideoID } from "../types/segments.model"; import { UserID } from "../types/user.model"; +import { getService } from "../utils/getService"; export async function deleteLockCategoriesEndpoint(req: Request, res: Response): Promise { // Collect user input data const videoID = req.body.videoID as VideoID; const userID = req.body.userID as UserID; const categories = req.body.categories as Category[]; + const service = getService(req.body.service); // Check input data is valid if (!videoID @@ -33,7 +35,7 @@ export async function deleteLockCategoriesEndpoint(req: Request, res: Response): }); } - await deleteLockCategories(videoID, categories); + await deleteLockCategories(videoID, categories, service); return res.status(200).json({ message: `Removed lock categories entrys for video ${videoID}` }); } @@ -42,13 +44,20 @@ export async function deleteLockCategoriesEndpoint(req: Request, res: Response): * * @param videoID * @param categories If null, will remove all + * @param service */ -export async function deleteLockCategories(videoID: VideoID, categories: Category[]): Promise { - const entries = (await db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID])).filter((entry: any) => { - return categories === null || categories.indexOf(entry.category) !== -1; - }); +export async function deleteLockCategories(videoID: VideoID, categories: Category[], service: Service): Promise { + const entries = ( + await db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ? AND "service" = ?', [videoID, service])) + .filter((entry: any) => { + return categories === null || categories.indexOf(entry.category) !== -1; + }); for (const entry of entries) { - await db.prepare("run", 'DELETE FROM "lockCategories" WHERE "videoID" = ? AND "category" = ?', [videoID, entry.category]); + await db.prepare( + "run", + 'DELETE FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" = ?', + [videoID, service, entry.category] + ); } } diff --git a/src/routes/getLockCategories.ts b/src/routes/getLockCategories.ts index 735df8e..46e2f41 100644 --- a/src/routes/getLockCategories.ts +++ b/src/routes/getLockCategories.ts @@ -2,18 +2,20 @@ import { db } from "../databases/databases"; import { Logger } from "../utils/logger"; import { Request, Response } from "express"; import { Category, VideoID } from "../types/segments.model"; +import { getService } from "../utils/getService"; export async function getLockCategories(req: Request, res: Response): Promise { const videoID = req.query.videoID as VideoID; + const service = getService(req.query.service as string); if (videoID == undefined) { //invalid request return res.sendStatus(400); } - + console.log(service); try { // Get existing lock categories markers - const row = await db.prepare("all", 'SELECT "category", "reason" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string}[]; + const row = await db.prepare("all", 'SELECT "category", "reason" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service]) as {category: Category, reason: string}[]; // map categories to array in JS becaues of SQL incompatibilities const categories = row.map(item => item.category); if (categories.length === 0 || !categories[0]) return res.sendStatus(404); diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index 16c87c6..7782fed 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -27,8 +27,9 @@ async function prepareCategorySegments(req: Request, videoID: VideoID, category: if (cache.shadowHiddenSegmentIPs[videoID] === undefined) cache.shadowHiddenSegmentIPs[videoID] = {}; if (cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] === undefined) { - cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] = await privateDB.prepare("all", 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ? AND "timeSubmitted" = ?', - [videoID, segment.timeSubmitted]) as { hashedIP: HashedIP }[]; + const service = getService(req?.query?.service as string); + cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] = await privateDB.prepare("all", 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ? AND "timeSubmitted" = ? AND "service" = ?', + [videoID, segment.timeSubmitted, service]) as { hashedIP: HashedIP }[]; } //if this isn't their ip, don't send it to them diff --git a/src/routes/postLockCategories.ts b/src/routes/postLockCategories.ts index ef541af..3d0de9d 100644 --- a/src/routes/postLockCategories.ts +++ b/src/routes/postLockCategories.ts @@ -4,6 +4,7 @@ import { isUserVIP } from "../utils/isUserVIP"; import { db } from "../databases/databases"; import { Request, Response } from "express"; import { VideoIDHash } from "../types/segments.model"; +import { getService } from "../utils/getService"; export async function postLockCategories(req: Request, res: Response): Promise { // Collect user input data @@ -11,6 +12,7 @@ export async function postLockCategories(req: Request, res: Response): Promise, isVIP: boolean, service:string, apiVideoInfo: APIVideoInfo, decreaseVotes: number): Promise { // Auto moderator check if (!isVIP && service == Service.YouTube) { - const autoModerateResult = await autoModerateSubmission(apiVideoInfo, { userID, videoID, segments });//startTime, endTime, category: segments[i].category}); + const autoModerateResult = await autoModerateSubmission(apiVideoInfo, { userID, videoID, segments, service });//startTime, endTime, category: segments[i].category}); + if (autoModerateResult == "Rejected based on NeuralBlock predictions.") { // If NB automod rejects, the submission will start with -2 votes. // Note, if one submission is bad all submissions will be affected. @@ -431,8 +432,8 @@ async function checkByAutoModerator(videoID: any, userID: any, segments: Array ?`, [hashedIP, videoID, yesterday]); + const rateLimitCheckRow = await privateDB.prepare("get", `SELECT COUNT(*) as count FROM "sponsorTimes" WHERE "hashedIP" = ? AND "videoID" = ? AND "timeSubmitted" > ? AND "service" = ?`, [hashedIP, videoID, yesterday, service]); if (rateLimitCheckRow.count >= 10) { //too many sponsors for the same video from the same ip address @@ -613,7 +614,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise item.UUID); const allSegments = (await db.prepare("all", `SELECT "UUID" FROM "sponsorTimes" st WHERE "st"."userID" = ?`, [userID])) .map((item: {UUID: string}) => item.UUID); @@ -120,7 +120,7 @@ export async function shadowBanUser(req: Request, res: Response): Promise `'${c}'`).join(",")}) AND NOT EXISTS ( SELECT "videoID", "category" FROM "lockCategories" WHERE - "sponsorTimes"."videoID" = "lockCategories"."videoID" AND "sponsorTimes"."category" = "lockCategories"."category")`, [userID]); + "sponsorTimes"."videoID" = "lockCategories"."videoID" AND "sponsorTimes"."service" = "lockCategories"."service" AND "sponsorTimes"."category" = "lockCategories"."category")`, [userID]); // clear cache for all old videos (await db.prepare("all", `SELECT "videoID", "hashedVideoID", "service", "votes", "views" FROM "sponsorTimes" WHERE "userID" = ?`, [userID])) diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index c3783b5..3bfb8f8 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -290,7 +290,8 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise !!(await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]))?.locked; const isVideoLocked = async () => !!(await db.prepare("get", `SELECT "lockCategories".category from "lockCategories" left join "sponsorTimes" - on ("lockCategories"."videoID" = "sponsorTimes"."videoID" and "lockCategories".category = "sponsorTimes".category) + on ("lockCategories"."videoID" = "sponsorTimes"."videoID" and + "lockCategories"."service" = "sponsorTimes"."service" and "lockCategories".category = "sponsorTimes".category) where "UUID" = ?`, [UUID])); if (await isSegmentLocked() || await isVideoLocked()) { diff --git a/src/utils/getSubmissionUUID.ts b/src/utils/getSubmissionUUID.ts index 27abd99..4d90c58 100644 --- a/src/utils/getSubmissionUUID.ts +++ b/src/utils/getSubmissionUUID.ts @@ -1,8 +1,15 @@ import { getHash } from "./getHash"; import { HashedValue } from "../types/hash.model"; -import { ActionType, VideoID } from "../types/segments.model"; +import { ActionType, VideoID, Service } from "../types/segments.model"; import { UserID } from "../types/user.model"; -export function getSubmissionUUID(videoID: VideoID, actionType: ActionType, userID: UserID, startTime: number, endTime: number): HashedValue{ - return `4${getHash(`${videoID}${startTime}${endTime}${userID}${actionType}`, 1)}` as HashedValue; +export function getSubmissionUUID( + videoID: VideoID, + actionType: ActionType, + userID: UserID, + startTime: number, + endTime: number, + service: Service +) : HashedValue { + return `5${getHash(`${videoID}${startTime}${endTime}${userID}${actionType}${service}`, 1)}` as HashedValue; } diff --git a/src/utils/reputation.ts b/src/utils/reputation.ts index e1d5e40..7f44384 100644 --- a/src/utils/reputation.ts +++ b/src/utils/reputation.ts @@ -38,7 +38,7 @@ export async function getReputation(userID: UserID): Promise { c."category" = "a"."category" LIMIT 1) AND EXISTS ( SELECT * FROM "lockCategories" as l - WHERE l."videoID" = "a"."videoID" AND l."category" = "a"."category" LIMIT 1) + WHERE l."videoID" = "a"."videoID" AND l."service" = "a"."service" AND l."category" = "a"."category" LIMIT 1) THEN 1 ELSE 0 END) AS "mostUpvotedInLockedVideoSum" FROM "sponsorTimes" as "a" WHERE "userID" = ?`, [userID, weekAgo, pastDate, userID]) as Promise; diff --git a/test/cases/getLockCategories.ts b/test/cases/getLockCategories.ts index 2b2ff9c..b8285bf 100644 --- a/test/cases/getLockCategories.ts +++ b/test/cases/getLockCategories.ts @@ -4,19 +4,21 @@ import assert from "assert"; import { client } from "../utils/httpClient"; const endpoint = "/api/lockCategories"; const getLockCategories = (videoID: string) => client.get(endpoint, { params: { videoID } }); +const getLockCategoriesWithService = (videoID: string, service: string) => client.get(endpoint, { params: { videoID, service } }); describe("getLockCategories", () => { before(async () => { const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)'; await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesVIP")]); - const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") VALUES (?, ?, ?, ?)'; - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock1", "sponsor", "1-short"]); - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock1", "interaction", "1-longer-reason"]); + const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason", "service") VALUES (?, ?, ?, ?, ?)'; + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock1", "sponsor", "1-short", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock1", "interaction", "1-longer-reason", "YouTube"]); - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock2", "preview", "2-reason"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock2", "preview", "2-reason", "YouTube"]); - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock3", "nonmusic", "3-reason"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock3", "nonmusic", "3-reason", "PeerTube"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock3", "sponsor", "3-reason", "YouTube"]); }); it("Should update the database version when starting the application", async () => { @@ -74,4 +76,69 @@ describe("getLockCategories", () => { }) .catch(err => done(err)); }); + + it("Should be able to get multiple locks with service", (done) => { + getLockCategoriesWithService("getLock1", "YouTube") + .then(res => { + assert.strictEqual(res.status, 200); + const expected = { + categories: [ + "sponsor", + "interaction" + ], + reason: "1-longer-reason" + }; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to get single locks with service", (done) => { + getLockCategoriesWithService("getLock3", "PeerTube") + .then(res => { + assert.strictEqual(res.status, 200); + const expected = { + categories: [ + "nonmusic" + ], + reason: "3-reason" + }; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to get single locks with service", (done) => { + getLockCategoriesWithService("getLock3", "Youtube") + .then(res => { + assert.strictEqual(res.status, 200); + const expected = { + categories: [ + "sponsor" + ], + reason: "3-reason" + }; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("should return result from Youtube service if service not match", (done) => { + getLockCategoriesWithService("getLock3", "Dailymotion") + .then(res => { + assert.strictEqual(res.status, 200); + const expected = { + categories: [ + "sponsor" + ], + reason: "3-reason" + }; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); }); diff --git a/test/cases/getSubmissionUUID.ts b/test/cases/getSubmissionUUID.ts index ad6ae63..61ec053 100644 --- a/test/cases/getSubmissionUUID.ts +++ b/test/cases/getSubmissionUUID.ts @@ -1,10 +1,12 @@ import { getSubmissionUUID } from "../../src/utils/getSubmissionUUID"; import assert from "assert"; -import { ActionType, VideoID } from "../../src/types/segments.model"; +import { ActionType, VideoID, Service } from "../../src/types/segments.model"; import { UserID } from "../../src/types/user.model"; describe("getSubmissionUUID", () => { it("Should return the hashed value", () => { - assert.strictEqual(getSubmissionUUID("video001" as VideoID, "skip" as ActionType, "testuser001" as UserID, 13.33337, 42.000001), "48ad47e445e67a7b963d9200037b36ec706eddcb752fdadc7bb2f061b56be6a23"); + assert.strictEqual( + getSubmissionUUID("video001" as VideoID, "skip" as ActionType, "testuser001" as UserID, 13.33337, 42.000001, Service.YouTube), + "529611b4cdd7319e705a32ae9557a02e59c8dbc1306097b2d2d5807c6405e9b1a"); }); }); diff --git a/test/cases/lockCategoriesRecords.ts b/test/cases/lockCategoriesRecords.ts index 15a23f8..ef1f28a 100644 --- a/test/cases/lockCategoriesRecords.ts +++ b/test/cases/lockCategoriesRecords.ts @@ -23,18 +23,18 @@ describe("lockCategoriesRecords", () => { const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)'; await db.prepare("run", insertVipUserQuery, [lockVIPUserHash]); - const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") VALUES (?, ?, ?, ?)'; - await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "no-segments-video-id", "sponsor", "reason-1"]); - await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "no-segments-video-id", "intro", "reason-1"]); + const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason", "service") VALUES (?, ?, ?, ?, ?)'; + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "no-segments-video-id", "sponsor", "reason-1", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "no-segments-video-id", "intro", "reason-1", "YouTube"]); - await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "no-segments-video-id-1", "sponsor", "reason-2"]); - await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "no-segments-video-id-1", "intro", "reason-2"]); - await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "lockCategoryVideo", "sponsor", "reason-3"]); + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "no-segments-video-id-1", "sponsor", "reason-2", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "no-segments-video-id-1", "intro", "reason-2", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "lockCategoryVideo", "sponsor", "reason-3", "YouTube"]); - await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record", "sponsor", "reason-4"]); + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record", "sponsor", "reason-4", "YouTube"]); - await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record-1", "sponsor", "reason-5"]); - await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record-1", "intro", "reason-5"]); + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record-1", "sponsor", "reason-5", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record-1", "intro", "reason-5", "YouTube"]); }); it("Should update the database version when starting the application", async () => { From bb2a007ed160de96fa8719be3e77bf19baaedb15 Mon Sep 17 00:00:00 2001 From: FlorianZahn Date: Sat, 2 Oct 2021 04:39:54 +0200 Subject: [PATCH 2/9] first changes and timeout on 2nd test --- src/routes/voteOnSponsorTime.ts | 20 +++- test/cases/voteOnSponsorTime.ts | 203 +++++++++++++++++++++++++++----- 2 files changed, 190 insertions(+), 33 deletions(-) diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index c3783b5..2c1f1f2 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -162,8 +162,8 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i return res.sendStatus(finalResponse.finalStatus); } - const videoInfo = (await db.prepare("get", `SELECT "category", "videoID", "hashedVideoID", "service", "userID" FROM "sponsorTimes" WHERE "UUID" = ?`, - [UUID])) as {category: Category, videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, userID: UserID}; + const videoInfo = (await db.prepare("get", `SELECT "category", "videoID", "hashedVideoID", "service", "userID", "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, + [UUID])) as {category: Category, videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, userID: UserID, locked: number}; if (!videoInfo) { // Submission doesn't exist return res.status(400).send("Submission doesn't exist."); @@ -176,6 +176,20 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i return res.status(400).send("Cannot vote for this category"); } + // Ignore vote if the next category is locked + const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "category" = ?`, [videoInfo.videoID, category]); + if (nextCategoryLocked) { + if (!isVIP) { + return res.status(200); + } // In an else statement, add a warning in the future for VIPs, that the next category is locked + } + + // Ignore vote if the segment is locked + if (!isVIP && videoInfo.locked === 1) { + console.log("dalfäkjsdöfbsdfoöjasdökjb"); + return res.status(200); + } + const nextCategoryInfo = await db.prepare("get", `select votes from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, category]); const timeSubmitted = Date.now(); @@ -424,7 +438,7 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise 0 && voteTypeEnum === voteTypes.normal) { - // Unide and Lock this submission + // Unhide and Lock this submission await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 1, hidden = 0, "shadowHidden" = 0 WHERE "UUID" = ?', [UUID]); // Reset video duration in case that caused it to be hidden diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 32f937b..d6a3a77 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -12,6 +12,7 @@ const sinonStub = mockManager.mock("listVideos"); sinonStub.callsFake(YouTubeApiMock.listVideos); const vipUser = "VIPUser"; const randomID2 = "randomID2"; +const categoryChangeUser = "category-change-user"; describe("voteOnSponsorTime", () => { before(async () => { @@ -19,34 +20,43 @@ describe("voteOnSponsorTime", () => { const warnVip01Hash = getHash("warn-vip01"); const warnUser01Hash = getHash("warn-voteuser01"); const warnUser02Hash = getHash("warn-voteuser02"); + const categoryChangeUserHash = getHash(categoryChangeUser); const MILLISECONDS_IN_HOUR = 3600000; const warningExpireTime = MILLISECONDS_IN_HOUR * config.hoursAfterWarningExpires; - const insertSponsorTimeQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", "views", "category", "shadowHidden", "hidden") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; - await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest", 1, 11, 2, "vote-uuid-0", "testman", 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 2, "vote-uuid-1", "testman", 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 10, "vote-uuid-1.5", "testman", 0, 50, "outro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 10, "vote-uuid-1.6", "testman", 0, 50, "interaction", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest3", 20, 33, 10, "vote-uuid-2", "testman", 0, 50, "intro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest,test", 1, 11, 100, "vote-uuid-3", "testman", 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 1, 11, 2, "vote-uuid-4", "testman", 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, "vote-uuid-5", "testman", 0, 50, "intro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, "vote-uuid-5_1", "testman", 0, 50, "intro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 1, 11, 2, "vote-uuid-6", "testman", 0, 50, "intro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 20, 33, 2, "vote-uuid-7", "testman", 0, 50, "intro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, "vote-uuid-8", getHash("randomID"), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-9", getHash(randomID2), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-10", getHash("randomID3"), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-11", getHash("randomID4"), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["own-submission-video", 1, 11, 500, "own-submission-uuid", getHash("own-submission-id"), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["not-own-submission-video", 1, 11, 500, "not-own-submission-uuid", getHash("somebody-else-id"), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["incorrect-category", 1, 11, 500, "incorrect-category", getHash("somebody-else-id"), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["incorrect-category-change", 1, 11, 500, "incorrect-category-change", getHash("somebody-else-id"), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest", 1, 11, 2, "warnvote-uuid-0", "testman", 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["no-sponsor-segments-video", 1, 11, 2, "no-sponsor-segments-uuid-0", "no-sponsor-segments", 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["no-sponsor-segments-video", 1, 11, 2, "no-sponsor-segments-uuid-1", "no-sponsor-segments", 0, 50, "intro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["segment-locking-video", 1, 11, 2, "segment-locking-uuid-1", "segment-locking-user", 0, 50, "intro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["segment-hidden-video", 1, 11, 2, "segment-hidden-uuid-1", "segment-hidden-user", 0, 50, "intro", 0, 1]); + const insertSponsorTimeQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "shadowHidden", "hidden") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; + await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest", 1, 11, 2, 0, "vote-uuid-0", "testman", 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 2, 0, "vote-uuid-1", "testman", 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 10, 0, "vote-uuid-1.5", "testman", 0, 50, "outro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 10, 0, "vote-uuid-1.6", "testman", 0, 50, "interaction", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest3", 20, 33, 10, 0, "vote-uuid-2", "testman", 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest,test", 1, 11, 100, 0, "vote-uuid-3", "testman", 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 1, 11, 2, 0, "vote-uuid-4", "testman", 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, 0, "vote-uuid-5", "testman", 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, 0, "vote-uuid-5_1", "testman", 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 1, 11, 2, 0, "vote-uuid-6", "testman", 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 20, 33, 2, 0, "vote-uuid-7", "testman", 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, 0, "vote-uuid-8", getHash("randomID"), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-9", getHash(randomID2), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-10", getHash("randomID3"), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-11", getHash("randomID4"), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["own-submission-video", 1, 11, 500, 0, "own-submission-uuid", getHash("own-submission-id"), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["not-own-submission-video", 1, 11, 500, 0, "not-own-submission-uuid", getHash("somebody-else-id"), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["incorrect-category", 1, 11, 500, 0, "incorrect-category", getHash("somebody-else-id"), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["incorrect-category-change", 1, 11, 500, 0, "incorrect-category-change", getHash("somebody-else-id"), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest", 1, 11, 2, 0, "warnvote-uuid-0", "testman", 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["no-sponsor-segments-video", 1, 11, 2, 0, "no-sponsor-segments-uuid-0", "no-sponsor-segments", 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["no-sponsor-segments-video", 1, 11, 2, 0, "no-sponsor-segments-uuid-1", "no-sponsor-segments", 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["segment-locking-video", 1, 11, 2, 0, "segment-locking-uuid-1", "segment-locking-user", 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["segment-hidden-video", 1, 11, 2, 0, "segment-hidden-uuid-1", "segment-hidden-user", 0, 50, "intro", 0, 1]); + await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 7, 22, 0, 0, "category-change-uuid-1", categoryChangeUserHash, 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 8, 22, 0, 1, "category-change-uuid-2", categoryChangeUserHash, 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 9, 22, 0, 0, "category-change-uuid-3", categoryChangeUserHash, 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 7, 12, 0, 1, "category-change-uuid-4", categoryChangeUserHash, 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 7, 13, 0, 0, "category-change-uuid-5", categoryChangeUserHash, 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 8, 12, 0, 1, "category-change-uuid-6", categoryChangeUserHash, 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 9, 14, 0, 0, "category-change-uuid-7", categoryChangeUserHash, 0, 50, "intro", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 7, 12, 0, 1, "category-change-uuid-8", categoryChangeUserHash, 0, 50, "intro", 0, 0]); const insertWarningQuery = 'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled") VALUES(?, ?, ?, ?)'; await db.prepare("run", insertWarningQuery, [warnUser01Hash, now, warnVip01Hash, 1]); @@ -62,7 +72,9 @@ describe("voteOnSponsorTime", () => { await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(vipUser)]); await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]); - await db.prepare("run", 'INSERT INTO "lockCategories" ("videoID", "userID", "category") VALUES (?, ?, ?)', ["no-sponsor-segments-video", "someUser", "sponsor"]); + const insertlockCategoriesQuerry = 'INSERT INTO "lockCategories" ("videoID", "userID", "category", "reason") VALUES (?, ?, ?, ?)'; + await db.prepare("run", insertlockCategoriesQuerry, ["no-sponsor-segments-video", "someUser", "sponsor", ""]); + await db.prepare("run", insertlockCategoriesQuerry, ["category-change-test-1", "someUser", "preview", ""]); // sponsor should stay unlocked }); // constants const endpoint = "/api/voteOnSponsorTime"; @@ -172,7 +184,7 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("should be able to completely downvote your own segment", (done) => { + it("should be able to completely downvote your own segment (segment unlocked)", (done) => { const UUID = "own-submission-uuid"; postVote("own-submission-id", UUID, 0) .then(async res => { @@ -238,7 +250,7 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should be able to change your vote for a category and it should add your vote to the database", (done) => { + it("Should be able to change your vote for a category and it should add your vote to the database(segment unlocked, nextCatgeory unlocked)", (done) => { const UUID = "vote-uuid-4"; postVoteCategory(randomID2, UUID, "outro") .then(async res => { @@ -280,7 +292,7 @@ describe("voteOnSponsorTime", () => { }); }); - + /* it("VIP should be able to vote for a category and it should immediately change", (done) => { const UUID = "vote-uuid-5"; postVoteCategory(vipUser, UUID, "outro") @@ -294,7 +306,7 @@ describe("voteOnSponsorTime", () => { }) .catch(err => done(err)); }); - +// old test it("Submitter should be able to vote for a category and it should immediately change", (done) => { const UUID = "vote-uuid-5_1"; postVoteCategory("testman", UUID, "outro") @@ -305,8 +317,139 @@ describe("voteOnSponsorTime", () => { done(); }) .catch(err => done(err)); + });*/ + + it("Submitter should be able to vote for a category and it should immediately change (segment unlocked, nextCatgeory unlocked, notVip)", (done) => { + const userID = categoryChangeUser; + const UUID = "category-change-uuid-1"; + const category = "sponsor"; + postVoteCategory(userID, UUID, category) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentCategory(UUID); + console.log(row.category) + assert.strictEqual(row.category, category); + done(); + }) + .catch(err => done(err)); }); + it("Submitter's vote on the category should not work (segment locked, nextCatgeory unlocked, notVip)", (done) => { + const userID = categoryChangeUser; + const UUID = "category-change-uuid-2"; + const category = "sponsor"; + postVoteCategory(userID, UUID, category) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentCategory(UUID); + console.log(row.category) + assert.strictEqual(row.category, "intro"); + done(); + }) + .catch(err => done(err)); + }); + + it("Submitter's vote on the category should not work (segment unlocked, nextCatgeory locked, notVip)", (done) => { + const userID = categoryChangeUser; + const UUID = "category-change-uuid-3"; + const category = "preview"; + postVoteCategory(userID, UUID, category) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentCategory(UUID); + console.log(row.category) + assert.strictEqual(row.category, "intro"); + done(); + }) + .catch(err => done(err)); + }); + + it("Submitter's vote on the category should not work (segment locked, nextCatgeory locked, notVip)", (done) => { + const userID = categoryChangeUser; + const UUID = "category-change-uuid-4"; + const category = "preview"; + postVoteCategory(userID, UUID, category) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentCategory(UUID); + console.log(row.category) + assert.strictEqual(row.category, "intro"); + done(); + }) + .catch(err => done(err)); + }); +//adjbaelrfkbfjgöäkldfgölkndfgölfdngöfkdln + it("Vip should be able to vote for a category and it should immediately change (segment unlocked, nextCatgeory unlocked, Vip)", (done) => { + const userID = vipUser; + const UUID = "category-change-uuid-5"; + const category = "sponsor"; + postVoteCategory(userID, UUID, category) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentCategory(UUID); + assert.strictEqual(row.category, category); + done(); + }) + .catch(err => done(err)); + }); + + it("Vip should be able to vote for a category and it should immediately change (segment locked, nextCatgeory unlocked, Vip)", (done) => { + const userID = vipUser; + const UUID = "category-change-uuid-6"; + const category = "sponsor"; + postVoteCategory(userID, UUID, category) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentCategory(UUID); + assert.strictEqual(row.category, category); + done(); + }) + .catch(err => done(err)); + }); + + it("Vip should be able to vote for a category and it should immediately change (segment unlocked, nextCatgeory locked, Vip)", (done) => { + const userID = vipUser; + const UUID = "category-change-uuid-7"; + const category = "preview"; + postVoteCategory(userID, UUID, category) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentCategory(UUID); + assert.strictEqual(row.category, category); + done(); + }) + .catch(err => done(err)); + }); + + it("Vip should be able to vote for a category and it should immediately change (segment locked, nextCatgeory locked, Vip)", (done) => { + const userID = vipUser; + const UUID = "category-change-uuid-8"; + const category = "preview"; + postVoteCategory(userID, UUID, category) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentCategory(UUID); + assert.strictEqual(row.category, category); + done(); + }) + .catch(err => done(err)); + }); + + + + + + + + + + + + + + + + it("Should not be able to category-vote on an invalid UUID submission", (done) => { const UUID = "invalid-uuid"; postVoteCategory("randomID3", UUID, "intro") From aacd297b3b27621032e811a3448e138ccd58eb36 Mon Sep 17 00:00:00 2001 From: FlorianZahn Date: Sat, 2 Oct 2021 05:04:18 +0200 Subject: [PATCH 3/9] Fixed bugs where normal users could cirumvent vip locks by changing categories --- src/routes/voteOnSponsorTime.ts | 4 ++-- test/cases/voteOnSponsorTime.ts | 35 +++------------------------------ 2 files changed, 5 insertions(+), 34 deletions(-) diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 2c1f1f2..10c4221 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -180,14 +180,14 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "category" = ?`, [videoInfo.videoID, category]); if (nextCategoryLocked) { if (!isVIP) { - return res.status(200); + return res.sendStatus(200); } // In an else statement, add a warning in the future for VIPs, that the next category is locked } // Ignore vote if the segment is locked if (!isVIP && videoInfo.locked === 1) { console.log("dalfäkjsdöfbsdfoöjasdökjb"); - return res.status(200); + return res.sendStatus(200); } const nextCategoryInfo = await db.prepare("get", `select votes from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, category]); diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index d6a3a77..14751db 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -33,7 +33,7 @@ describe("voteOnSponsorTime", () => { await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest,test", 1, 11, 100, 0, "vote-uuid-3", "testman", 0, 50, "sponsor", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 1, 11, 2, 0, "vote-uuid-4", "testman", 0, 50, "sponsor", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, 0, "vote-uuid-5", "testman", 0, 50, "intro", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, 0, "vote-uuid-5_1", "testman", 0, 50, "intro", 0, 0]); + //await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, 0, "vote-uuid-5_1", getHash("testman"), 0, 50, "intro", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 1, 11, 2, 0, "vote-uuid-6", "testman", 0, 50, "intro", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 20, 33, 2, 0, "vote-uuid-7", "testman", 0, 50, "intro", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, 0, "vote-uuid-8", getHash("randomID"), 0, 50, "sponsor", 0, 0]); @@ -292,33 +292,6 @@ describe("voteOnSponsorTime", () => { }); }); - /* - it("VIP should be able to vote for a category and it should immediately change", (done) => { - const UUID = "vote-uuid-5"; - postVoteCategory(vipUser, UUID, "outro") - .then(async res => { - assert.strictEqual(res.status, 200); - const row = await getSegmentCategory(UUID); - const row2 = await db.prepare("get", `SELECT votes FROM "categoryVotes" WHERE "UUID" = ? and category = ?`, [UUID, "outro"]); - assert.strictEqual(row.category, "outro"); - assert.strictEqual(row2.votes, 500); - done(); - }) - .catch(err => done(err)); - }); -// old test - it("Submitter should be able to vote for a category and it should immediately change", (done) => { - const UUID = "vote-uuid-5_1"; - postVoteCategory("testman", UUID, "outro") - .then(async res => { - assert.strictEqual(res.status, 200); - const row = await getSegmentCategory("vote-uuid-5"); - assert.strictEqual(row.category, "outro"); - done(); - }) - .catch(err => done(err)); - });*/ - it("Submitter should be able to vote for a category and it should immediately change (segment unlocked, nextCatgeory unlocked, notVip)", (done) => { const userID = categoryChangeUser; const UUID = "category-change-uuid-1"; @@ -327,7 +300,7 @@ describe("voteOnSponsorTime", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); - console.log(row.category) + console.log(row.category); assert.strictEqual(row.category, category); done(); }) @@ -342,7 +315,7 @@ describe("voteOnSponsorTime", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); - console.log(row.category) + console.log(row.category); assert.strictEqual(row.category, "intro"); done(); }) @@ -357,7 +330,6 @@ describe("voteOnSponsorTime", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); - console.log(row.category) assert.strictEqual(row.category, "intro"); done(); }) @@ -372,7 +344,6 @@ describe("voteOnSponsorTime", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); - console.log(row.category) assert.strictEqual(row.category, "intro"); done(); }) From 7135aa33694f437a3fcf5822cf677101eb5b2aa5 Mon Sep 17 00:00:00 2001 From: FlorianZahn Date: Sat, 2 Oct 2021 05:11:26 +0200 Subject: [PATCH 4/9] removing console.log() commands --- src/routes/voteOnSponsorTime.ts | 1 - test/cases/voteOnSponsorTime.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 10c4221..95e6003 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -186,7 +186,6 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i // Ignore vote if the segment is locked if (!isVIP && videoInfo.locked === 1) { - console.log("dalfäkjsdöfbsdfoöjasdökjb"); return res.sendStatus(200); } diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 14751db..82f8375 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -33,7 +33,6 @@ describe("voteOnSponsorTime", () => { await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest,test", 1, 11, 100, 0, "vote-uuid-3", "testman", 0, 50, "sponsor", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 1, 11, 2, 0, "vote-uuid-4", "testman", 0, 50, "sponsor", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, 0, "vote-uuid-5", "testman", 0, 50, "intro", 0, 0]); - //await db.prepare("run", insertSponsorTimeQuery, ["vote-test3", 7, 22, -3, 0, "vote-uuid-5_1", getHash("testman"), 0, 50, "intro", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 1, 11, 2, 0, "vote-uuid-6", "testman", 0, 50, "intro", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 20, 33, 2, 0, "vote-uuid-7", "testman", 0, 50, "intro", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, 0, "vote-uuid-8", getHash("randomID"), 0, 50, "sponsor", 0, 0]); From 0aa286442f2ec6af95a7c7c340d148ac3d577f81 Mon Sep 17 00:00:00 2001 From: FlorianZahn Date: Sun, 3 Oct 2021 18:53:06 +0200 Subject: [PATCH 5/9] Vip locked segment voting, handle client side Co-authored-by: Ajay Ramachandran --- src/routes/voteOnSponsorTime.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 95e6003..e0f31d0 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -178,10 +178,8 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i // Ignore vote if the next category is locked const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "category" = ?`, [videoInfo.videoID, category]); - if (nextCategoryLocked) { - if (!isVIP) { - return res.sendStatus(200); - } // In an else statement, add a warning in the future for VIPs, that the next category is locked + if (nextCategoryLocked && !isVIP) { + return res.sendStatus(200); } // Ignore vote if the segment is locked From 178c4d97920431ce1499fb4ef10071e2b04f09b3 Mon Sep 17 00:00:00 2001 From: FlorianZahn Date: Sun, 3 Oct 2021 18:53:34 +0200 Subject: [PATCH 6/9] Remove comment Co-authored-by: Ajay Ramachandran --- test/cases/voteOnSponsorTime.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 82f8375..640e43f 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -348,7 +348,6 @@ describe("voteOnSponsorTime", () => { }) .catch(err => done(err)); }); -//adjbaelrfkbfjgöäkldfgölkndfgölfdngöfkdln it("Vip should be able to vote for a category and it should immediately change (segment unlocked, nextCatgeory unlocked, Vip)", (done) => { const userID = vipUser; const UUID = "category-change-uuid-5"; From 2388dea8592825e9022641266dd6a2b7e23b8d32 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 3 Oct 2021 12:55:49 -0400 Subject: [PATCH 7/9] Remove extra log --- src/routes/getLockCategories.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/getLockCategories.ts b/src/routes/getLockCategories.ts index 46e2f41..693b7de 100644 --- a/src/routes/getLockCategories.ts +++ b/src/routes/getLockCategories.ts @@ -12,7 +12,7 @@ export async function getLockCategories(req: Request, res: Response): Promise Date: Sun, 3 Oct 2021 19:03:49 +0200 Subject: [PATCH 8/9] Improved formatting --- test/cases/voteOnSponsorTime.ts | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 640e43f..377c7d2 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -300,7 +300,7 @@ describe("voteOnSponsorTime", () => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); console.log(row.category); - assert.strictEqual(row.category, category); + assert.strictEqual(row.category, category); done(); }) .catch(err => done(err)); @@ -348,6 +348,7 @@ describe("voteOnSponsorTime", () => { }) .catch(err => done(err)); }); + it("Vip should be able to vote for a category and it should immediately change (segment unlocked, nextCatgeory unlocked, Vip)", (done) => { const userID = vipUser; const UUID = "category-change-uuid-5"; @@ -403,22 +404,7 @@ describe("voteOnSponsorTime", () => { }) .catch(err => done(err)); }); - - - - - - - - - - - - - - - - + it("Should not be able to category-vote on an invalid UUID submission", (done) => { const UUID = "invalid-uuid"; postVoteCategory("randomID3", UUID, "intro") From 7e23bc9eeb5c0f048a7af409d12fc68774e5d319 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 3 Oct 2021 13:12:14 -0400 Subject: [PATCH 9/9] Check for service in category vote --- src/routes/voteOnSponsorTime.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index eb04ac1..2d12115 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -177,7 +177,7 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i } // Ignore vote if the next category is locked - const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "category" = ?`, [videoInfo.videoID, category]); + const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" = ?`, [videoInfo.videoID, videoInfo.service, category]); if (nextCategoryLocked && !isVIP) { return res.sendStatus(200); }