mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
commit
93536976d0
5 changed files with 60 additions and 6 deletions
|
@ -2,6 +2,7 @@ import { Request, Response } from "express";
|
||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
import { isUserVIP } from "../utils/isUserVIP";
|
import { isUserVIP } from "../utils/isUserVIP";
|
||||||
import { getMaxResThumbnail, YouTubeAPI } from "../utils/youtubeApi";
|
import { getMaxResThumbnail, YouTubeAPI } from "../utils/youtubeApi";
|
||||||
|
import { APIVideoInfo } from "../types/youtubeApi.model";
|
||||||
import { db, privateDB } from "../databases/databases";
|
import { db, privateDB } from "../databases/databases";
|
||||||
import { dispatchEvent, getVoteAuthor, getVoteAuthorRaw } from "../utils/webhookUtils";
|
import { dispatchEvent, getVoteAuthor, getVoteAuthorRaw } from "../utils/webhookUtils";
|
||||||
import { getFormattedTime } from "../utils/getFormattedTime";
|
import { getFormattedTime } from "../utils/getFormattedTime";
|
||||||
|
@ -9,7 +10,7 @@ import { getIP } from "../utils/getIP";
|
||||||
import { getHash } from "../utils/getHash";
|
import { getHash } from "../utils/getHash";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
import { UserID } from "../types/user.model";
|
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 { getCategoryActionType } from "../utils/categoryInfo";
|
||||||
import { QueryCacher } from "../utils/queryCacher";
|
import { QueryCacher } from "../utils/queryCacher";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
@ -48,6 +49,30 @@ interface VoteData {
|
||||||
finalResponse: FinalResponse;
|
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) {
|
async function sendWebhooks(voteData: VoteData) {
|
||||||
const submissionInfoRow = await db.prepare("get", `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName",
|
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,
|
(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
|
//oldIncrementAmount will be zero is row is null
|
||||||
await db.prepare("run", `UPDATE "sponsorTimes" SET "${columnName}" = "${columnName}" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]);
|
await db.prepare("run", `UPDATE "sponsorTimes" SET "${columnName}" = "${columnName}" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]);
|
||||||
if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) {
|
if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) {
|
||||||
|
// check for video duration change
|
||||||
|
await checkVideoDurationChange(UUID);
|
||||||
// Unhide 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]);
|
await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 1, hidden = 0, "shadowHidden" = 0 WHERE "UUID" = ?', [UUID]);
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,7 @@ export interface DBSegment {
|
||||||
hashedVideoID: VideoIDHash;
|
hashedVideoID: VideoIDHash;
|
||||||
timeSubmitted: number;
|
timeSubmitted: number;
|
||||||
userAgent: string;
|
userAgent: string;
|
||||||
|
service: Service;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OverlappingSegmentGroup {
|
export interface OverlappingSegmentGroup {
|
||||||
|
|
|
@ -92,7 +92,6 @@ describe("setUsername", () => {
|
||||||
it("Should be able to set username that has never been set", (done) => {
|
it("Should be able to set username that has never been set", (done) => {
|
||||||
postSetUserName(user00PrivateUserID, username00)
|
postSetUserName(user00PrivateUserID, username00)
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
console.log(res.data);
|
|
||||||
const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID));
|
const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID));
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
assert.strictEqual(usernameInfo.userName, username00);
|
assert.strictEqual(usernameInfo.userName, username00);
|
||||||
|
|
|
@ -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", 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", 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, ["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(?, ?, ?, ?)';
|
const insertWarningQuery = 'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled") VALUES(?, ?, ?, ?)';
|
||||||
await db.prepare("run", insertWarningQuery, [warnUser01Hash, now, warnVip01Hash, 1]);
|
await db.prepare("run", insertWarningQuery, [warnUser01Hash, now, warnVip01Hash, 1]);
|
||||||
|
@ -299,7 +300,6 @@ describe("voteOnSponsorTime", () => {
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const row = await getSegmentCategory(UUID);
|
const row = await getSegmentCategory(UUID);
|
||||||
console.log(row.category);
|
|
||||||
assert.strictEqual(row.category, category);
|
assert.strictEqual(row.category, category);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
|
@ -314,7 +314,6 @@ describe("voteOnSponsorTime", () => {
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const row = await getSegmentCategory(UUID);
|
const row = await getSegmentCategory(UUID);
|
||||||
console.log(row.category);
|
|
||||||
assert.strictEqual(row.category, "intro");
|
assert.strictEqual(row.category, "intro");
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
|
@ -348,7 +347,7 @@ describe("voteOnSponsorTime", () => {
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.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) => {
|
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 userID = vipUser;
|
||||||
const UUID = "category-change-uuid-5";
|
const UUID = "category-change-uuid-5";
|
||||||
|
@ -404,7 +403,7 @@ describe("voteOnSponsorTime", () => {
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should not be able to category-vote on an invalid UUID submission", (done) => {
|
it("Should not be able to category-vote on an invalid UUID submission", (done) => {
|
||||||
const UUID = "invalid-uuid";
|
const UUID = "invalid-uuid";
|
||||||
postVoteCategory("randomID3", UUID, "intro")
|
postVoteCategory("randomID3", UUID, "intro")
|
||||||
|
@ -564,4 +563,15 @@ describe("voteOnSponsorTime", () => {
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -30,6 +30,23 @@ export class YouTubeApiMock {
|
||||||
]
|
]
|
||||||
} as APIVideoData
|
} 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 {
|
} else {
|
||||||
return {
|
return {
|
||||||
err: null,
|
err: null,
|
||||||
|
|
Loading…
Reference in a new issue