diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index ff5edcc..2fa7272 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -2,6 +2,7 @@ import { Request, Response } from "express"; import { Logger } from "../utils/logger"; import { isUserVIP } from "../utils/isUserVIP"; import { getMaxResThumbnail, YouTubeAPI } from "../utils/youtubeApi"; +import { APIVideoInfo } from "../types/youtubeApi.model"; import { db, privateDB } from "../databases/databases"; import { dispatchEvent, getVoteAuthor, getVoteAuthorRaw } from "../utils/webhookUtils"; import { getFormattedTime } from "../utils/getFormattedTime"; @@ -9,7 +10,7 @@ import { getIP } from "../utils/getIP"; import { getHash } from "../utils/getHash"; import { config } from "../config"; import { UserID } from "../types/user.model"; -import { Category, CategoryActionType, HashedIP, IPAddress, SegmentUUID, Service, VideoID, VideoIDHash, Visibility } from "../types/segments.model"; +import { Category, CategoryActionType, HashedIP, IPAddress, SegmentUUID, Service, VideoID, VideoIDHash, Visibility, VideoDuration } from "../types/segments.model"; import { getCategoryActionType } from "../utils/categoryInfo"; import { QueryCacher } from "../utils/queryCacher"; import axios from "axios"; @@ -48,6 +49,30 @@ interface VoteData { finalResponse: FinalResponse; } +function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise { + if (config.newLeafURLs !== null) { + return YouTubeAPI.listVideos(videoID, ignoreCache); + } else { + return null; + } +} + +const videoDurationChanged = (segmentDuration: number, APIDuration: number) => (APIDuration > 0 && Math.abs(segmentDuration - APIDuration) > 2); + +async function checkVideoDurationChange(UUID: SegmentUUID) { + const { videoDuration, videoID, service } = await db.prepare("get", `select "videoDuration", "videoID", "service" from "sponsorTimes" where "UUID" = ?`, [UUID]); + let apiVideoInfo: APIVideoInfo = null; + if (service == Service.YouTube) { + // don't use cache since we have no information about the video length + apiVideoInfo = await getYouTubeVideoInfo(videoID); + } + const apiVideoDuration = apiVideoInfo?.data?.lengthSeconds as VideoDuration; + if (videoDurationChanged(videoDuration, apiVideoDuration)) { + Logger.info(`Video duration changed for ${videoID} from ${videoDuration} to ${apiVideoDuration}`); + await db.prepare("run", `UPDATE "sponsorTimes" SET "videoDuration" = ? WHERE "UUID" = ?`, [apiVideoDuration, UUID]); + } +} + async function sendWebhooks(voteData: VoteData) { const submissionInfoRow = await db.prepare("get", `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName", (select count(1) from "sponsorTimes" where "userID" = s."userID") count, @@ -436,6 +461,8 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise 0 && voteTypeEnum === voteTypes.normal) { + // check for video duration change + await checkVideoDurationChange(UUID); // Unhide and Lock this submission await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 1, hidden = 0, "shadowHidden" = 0 WHERE "UUID" = ?', [UUID]); diff --git a/src/types/segments.model.ts b/src/types/segments.model.ts index b863a15..6c56194 100644 --- a/src/types/segments.model.ts +++ b/src/types/segments.model.ts @@ -64,6 +64,7 @@ export interface DBSegment { hashedVideoID: VideoIDHash; timeSubmitted: number; userAgent: string; + service: Service; } export interface OverlappingSegmentGroup { diff --git a/test/cases/setUsername.ts b/test/cases/setUsername.ts index f1f3f15..89af032 100644 --- a/test/cases/setUsername.ts +++ b/test/cases/setUsername.ts @@ -92,7 +92,6 @@ describe("setUsername", () => { it("Should be able to set username that has never been set", (done) => { postSetUserName(user00PrivateUserID, username00) .then(async res => { - console.log(res.data); const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID)); assert.strictEqual(res.status, 200); assert.strictEqual(usernameInfo.userName, username00); diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 377c7d2..d8cbc81 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -56,6 +56,7 @@ describe("voteOnSponsorTime", () => { 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]); + await db.prepare("run", insertSponsorTimeQuery, ["duration-update", 1, 10, 0, 0, "duration-update-uuid-1", "testman", 0, 0, "intro", 0, 0]); const insertWarningQuery = 'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled") VALUES(?, ?, ?, ?)'; await db.prepare("run", insertWarningQuery, [warnUser01Hash, now, warnVip01Hash, 1]); @@ -299,7 +300,6 @@ describe("voteOnSponsorTime", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); - console.log(row.category); assert.strictEqual(row.category, category); done(); }) @@ -314,7 +314,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(); }) @@ -348,7 +347,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"; @@ -404,7 +403,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") @@ -564,4 +563,15 @@ describe("voteOnSponsorTime", () => { }) .catch(err => done(err)); }); + + it("Should be able to update stored videoDuration with VIP upvote", (done) => { + const UUID = "duration-update-uuid-1"; + postVote(vipUser, UUID, 1) + .then(async res => { + assert.strictEqual(res.status, 200); + const { videoDuration } = await db.prepare("get", `SELECT "videoDuration" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); + assert.strictEqual(videoDuration, 500); + done(); + }); + }); }); diff --git a/test/youtubeMock.ts b/test/youtubeMock.ts index 44d271a..bb221e0 100644 --- a/test/youtubeMock.ts +++ b/test/youtubeMock.ts @@ -30,6 +30,23 @@ export class YouTubeApiMock { ] } as APIVideoData }; + } else if (obj.id === "duration-update") { + return { + err: null, + data: { + title: "Example Title", + lengthSeconds: 500, + videoThumbnails: [ + { + quality: "maxres", + url: "https://sponsor.ajay.app/LogoSponsorBlockSimple256px.png", + second__originalUrl:"https://sponsor.ajay.app/LogoSponsorBlockSimple256px.png", + width: 1280, + height: 720 + }, + ] + } as APIVideoData + }; } else { return { err: null,