Merge pull request #385 from mchangrh/384-fix

Implement #384
This commit is contained in:
Ajay Ramachandran 2021-10-19 23:30:09 -04:00 committed by GitHub
commit 93536976d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 6 deletions

View file

@ -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<APIVideoInfo> {
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<Re
//oldIncrementAmount will be zero is row is null
await db.prepare("run", `UPDATE "sponsorTimes" SET "${columnName}" = "${columnName}" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]);
if (isVIP && incrementAmount > 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]);

View file

@ -64,6 +64,7 @@ export interface DBSegment {
hashedVideoID: VideoIDHash;
timeSubmitted: number;
userAgent: string;
service: Service;
}
export interface OverlappingSegmentGroup {

View file

@ -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);

View file

@ -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();
});
});
});

View file

@ -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,