mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-07 20:17:02 +03:00
implementation
This commit is contained in:
@@ -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,31 @@ 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(APIDuration - segmentDuration) < 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 +462,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
|
||||||
|
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user