From d02d78f325f2ea5f7056c4cadf681302f9be3a3f Mon Sep 17 00:00:00 2001 From: Michael C Date: Thu, 31 Mar 2022 16:43:10 -0400 Subject: [PATCH] add 80% tempVIP - move isUserTempVIP to own file - reduce allSegmentDuration instead of forEach - don't return decreaseVotes from autoModerator - completely skip autoModCheck if VIP --- src/routes/postSkipSegments.ts | 48 +++++++++++++++------------------ src/routes/voteOnSponsorTime.ts | 13 ++------- src/utils/isUserTempVIP.ts | 19 +++++++++++++ 3 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 src/utils/isUserTempVIP.ts diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index 5ed0efc..2d89516 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -16,6 +16,7 @@ import { getReputation } from "../utils/reputation"; import { APIVideoData, APIVideoInfo } from "../types/youtubeApi.model"; import { HashedUserID, UserID } from "../types/user.model"; import { isUserVIP } from "../utils/isUserVIP"; +import { isUserTempVIP } from "../utils/isUserTempVIP"; import { parseUserAgent } from "../utils/userAgent"; import { getService } from "../utils/getService"; import axios from "axios"; @@ -81,19 +82,19 @@ async function sendWebhooks(apiVideoInfo: APIVideoInfo, userID: string, videoID: if (config.discordFirstTimeSubmissionsWebhookURL === null || userSubmissionCountRow.submissionCount > 1) return; axios.post(config.discordFirstTimeSubmissionsWebhookURL, { - "embeds": [{ - "title": data?.title, - "url": `https://www.youtube.com/watch?v=${videoID}&t=${(parseInt(startTime.toFixed(0)) - 2)}s#requiredSegment=${UUID}`, - "description": `Submission ID: ${UUID}\ + embeds: [{ + title: data?.title, + url: `https://www.youtube.com/watch?v=${videoID}&t=${(parseInt(startTime.toFixed(0)) - 2)}s#requiredSegment=${UUID}`, + description: `Submission ID: ${UUID}\ \n\nTimestamp: \ ${getFormattedTime(startTime)} to ${getFormattedTime(endTime)}\ \n\nCategory: ${segmentInfo.category}`, - "color": 10813440, - "author": { - "name": userID, + color: 10813440, + author: { + name: userID, }, - "thumbnail": { - "url": getMaxResThumbnail(data) || "", + thumbnail: { + url: getMaxResThumbnail(data) || "", }, }], }) @@ -151,9 +152,8 @@ async function autoModerateSubmission(apiVideoInfo: APIVideoInfo, //merge all the times into non-overlapping arrays const allSegmentsSorted = mergeTimeSegments(allSegmentTimes.sort((a, b) => a[0] - b[0] || a[1] - b[1])); - let allSegmentDuration = 0; //sum all segment times together - allSegmentsSorted.forEach(segmentInfo => allSegmentDuration += segmentInfo[1] - segmentInfo[0]); + const allSegmentDuration = allSegmentsSorted.reduce((acc, curr) => acc + (curr[1] - curr[0]), 0); if (allSegmentDuration > (duration / 100) * 80) { // Reject submission if all segments combine are over 80% of the video @@ -325,24 +325,20 @@ async function checkEachSegmentValid(rawIP: IPAddress, paramUserID: UserID, user return CHECK_PASS; } -async function checkByAutoModerator(videoID: any, userID: any, segments: Array, isVIP: boolean, service:string, apiVideoInfo: APIVideoInfo, decreaseVotes: number, videoDuration: number): Promise { +async function checkByAutoModerator(videoID: any, userID: any, segments: Array, service:string, apiVideoInfo: APIVideoInfo, videoDuration: number): Promise { // Auto moderator check - if (!isVIP && service == Service.YouTube) { + if (service == Service.YouTube) { const autoModerateResult = await autoModerateSubmission(apiVideoInfo, { userID, videoID, segments, service, videoDuration });//startTime, endTime, category: segments[i].category}); if (autoModerateResult) { return { pass: false, errorCode: 403, - errorMessage: `Request rejected by auto moderator: ${autoModerateResult} If this is an issue, send a message on Discord.`, - decreaseVotes + errorMessage: `Request rejected by auto moderator: ${autoModerateResult} If this is an issue, send a message on Discord.` }; } } - return { - ...CHECK_PASS, - decreaseVotes - }; + return CHECK_PASS; } async function updateDataIfVideoDurationChange(videoID: VideoID, service: Service, videoDuration: VideoDuration, videoDurationParam: VideoDuration) { @@ -498,6 +494,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise => { - const apiVideoInfo = await getYouTubeVideoInfo(videoID); - const channelID = apiVideoInfo?.data?.authorId; - const { err, reply } = await redis.getAsync(tempVIPKey(nonAnonUserID)); - - return err || !reply ? false : (reply == channelID); -}; - const videoDurationChanged = (segmentDuration: number, APIDuration: number) => (APIDuration > 0 && Math.abs(segmentDuration - APIDuration) > 2); async function updateSegmentVideoDuration(UUID: SegmentUUID) { diff --git a/src/utils/isUserTempVIP.ts b/src/utils/isUserTempVIP.ts new file mode 100644 index 0000000..dc098e0 --- /dev/null +++ b/src/utils/isUserTempVIP.ts @@ -0,0 +1,19 @@ +import redis from "../utils/redis"; +import { tempVIPKey } from "../utils/redisKeys"; +import { HashedUserID } from "../types/user.model"; +import { YouTubeAPI } from "../utils/youtubeApi"; +import { APIVideoInfo } from "../types/youtubeApi.model"; +import { VideoID } from "../types/segments.model"; +import { config } from "../config"; + +function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise { + return config.newLeafURLs ? YouTubeAPI.listVideos(videoID, ignoreCache) : null; +} + +export const isUserTempVIP = async (hashedUserID: HashedUserID, videoID: VideoID): Promise => { + const apiVideoInfo = await getYouTubeVideoInfo(videoID); + const channelID = apiVideoInfo?.data?.authorId; + const { err, reply } = await redis.getAsync(tempVIPKey(hashedUserID)); + + return err || !reply ? false : (reply == channelID); +}; \ No newline at end of file