diff --git a/databases/_upgrade_sponsorTimes_31.sql b/databases/_upgrade_sponsorTimes_31.sql new file mode 100644 index 0000000..3414c46 --- /dev/null +++ b/databases/_upgrade_sponsorTimes_31.sql @@ -0,0 +1,25 @@ +BEGIN TRANSACTION; + +/* START lockCategory migrations +no sponsor migrations +no selfpromo migrations */ + +/* exclusive_access migrations */ +DELETE FROM "lockCategories" WHERE "category" = 'exclusive_access' AND "actionType" != 'full'; +/* delete all full locks on categories without full */ +DELETE FROM "lockCategories" WHERE "actionType" = 'full' AND "category" in ('interaction', 'intro', 'outro', 'preview', 'filler', 'music_offtopic', 'poi_highlight'); +/* delete all non-skip music_offtopic locks */ +DELETE FROM "lockCategories" WHERE "category" = 'music_offtopic' AND "actionType" != 'skip'; +/* convert all poi_highlight to actionType poi */ +UPDATE "lockCategories" SET "actionType" = 'poi' WHERE "category" = 'poi_highlight' AND "actionType" = 'skip'; +/* delete all non-skip poi_highlight locks */ +DELETE FROM "lockCategories" WHERE "category" = 'poi_highlight' AND "actionType" != 'poi'; + +/* END lockCategory migrations */ + +/* delete all redundant userName entries */ +DELETE FROM "userNames" WHERE "userName" = "userID" AND "locked" = 0; + +UPDATE "config" SET value = 31 WHERE key = 'version'; + +COMMIT; \ No newline at end of file diff --git a/src/routes/addUserAsTempVIP.ts b/src/routes/addUserAsTempVIP.ts index 2c81798..fb8d482 100644 --- a/src/routes/addUserAsTempVIP.ts +++ b/src/routes/addUserAsTempVIP.ts @@ -32,7 +32,8 @@ const getChannelInfo = async (videoID: VideoID): Promise<{id: string | null, nam }; export async function addUserAsTempVIP(req: AddUserAsTempVIPRequest, res: Response): Promise { - const { query: { userID, adminUserID } } = req; + const userID = req.query.userID; + let adminUserID = req.query.adminUserID; const enabled = req.query?.enabled === "true"; const channelVideoID = req.query?.channelVideoID as VideoID; @@ -43,9 +44,9 @@ export async function addUserAsTempVIP(req: AddUserAsTempVIPRequest, res: Respon } // hash the issuer userID - const issuerUserID = await getHashCache(adminUserID); + adminUserID = await getHashCache(adminUserID); // check if issuer is VIP - const issuerIsVIP = await isUserVIP(issuerUserID as HashedUserID); + const issuerIsVIP = await isUserVIP(adminUserID as HashedUserID); if (!issuerIsVIP) { return res.sendStatus(403); } diff --git a/src/routes/deleteLockCategories.ts b/src/routes/deleteLockCategories.ts index 369ea52..1e001aa 100644 --- a/src/routes/deleteLockCategories.ts +++ b/src/routes/deleteLockCategories.ts @@ -2,9 +2,10 @@ import { Request, Response } from "express"; import { isUserVIP } from "../utils/isUserVIP"; import { getHashCache } from "../utils/getHashCache"; import { db } from "../databases/databases"; -import { Category, Service, VideoID } from "../types/segments.model"; +import { ActionType, Category, Service, VideoID } from "../types/segments.model"; import { UserID } from "../types/user.model"; import { getService } from "../utils/getService"; +import { config } from "../config"; interface DeleteLockCategoriesRequest extends Request { body: { @@ -12,6 +13,7 @@ interface DeleteLockCategoriesRequest extends Request { service: string; userID: UserID; videoID: VideoID; + actionTypes: ActionType[]; }; } @@ -22,7 +24,8 @@ export async function deleteLockCategoriesEndpoint(req: DeleteLockCategoriesRequ videoID, userID, categories, - service + service, + actionTypes } } = req; @@ -32,6 +35,7 @@ export async function deleteLockCategoriesEndpoint(req: DeleteLockCategoriesRequ || !categories || !Array.isArray(categories) || categories.length === 0 + || actionTypes.length === 0 ) { return res.status(400).json({ message: "Bad Format", @@ -48,33 +52,14 @@ export async function deleteLockCategoriesEndpoint(req: DeleteLockCategoriesRequ }); } - await deleteLockCategories(videoID, categories, getService(service)); + await deleteLockCategories(videoID, categories, actionTypes, getService(service)); return res.status(200).json({ message: `Removed lock categories entries for video ${videoID}` }); } -/** - * - * @param videoID - * @param categories If null, will remove all - * @param service - */ -export async function deleteLockCategories(videoID: VideoID, categories: Category[], service: Service): Promise { - type DBEntry = { category: Category }; - const dbEntries = await db.prepare( - "all", - 'SELECT * FROM "lockCategories" WHERE "videoID" = ? AND "service" = ?', - [videoID, service] - ) as Array; - - const entries = dbEntries.filter( - ({ category }: DBEntry) => categories === null || categories.indexOf(category) !== -1); - - await Promise.all( - entries.map(({ category }: DBEntry) => db.prepare( - "run", - 'DELETE FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" = ?', - [videoID, service, category] - )) - ); +export async function deleteLockCategories(videoID: VideoID, categories = config.categoryList, actionTypes = [ActionType.Skip, ActionType.Mute], service: Service): Promise { + const arrJoin = (arr: string[]): string => `'${arr.join(`','`)}'`; + const categoryString = arrJoin(categories); + const actionTypeString = arrJoin(actionTypes); + await db.prepare("run", `DELETE FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" IN (${categoryString}) AND "actionType" IN (${actionTypeString})`, [videoID, service]); } diff --git a/src/routes/postLockCategories.ts b/src/routes/postLockCategories.ts index 400acd3..f8ccb8d 100644 --- a/src/routes/postLockCategories.ts +++ b/src/routes/postLockCategories.ts @@ -5,6 +5,7 @@ import { db } from "../databases/databases"; import { Request, Response } from "express"; import { ActionType, Category, VideoIDHash } from "../types/segments.model"; import { getService } from "../utils/getService"; +import { config } from "../config"; export async function postLockCategories(req: Request, res: Response): Promise { // Collect user input data @@ -44,25 +45,18 @@ export async function postLockCategories(req: Request, res: Response): Promise lock.category === category && lock.actionType === actionType)) { - locksToApply.push({ - category, - actionType - }); - } else { - overwrittenLocks.push({ - category, - actionType - }); - } - } + + // push new/ existing locks + const validLocks = createLockArray(categories, actionTypes); + for (const { category, actionType } of validLocks) { + const targetArray = existingLocks.some((lock) => lock.category === category && lock.actionType === actionType) + ? overwrittenLocks + : locksToApply; + targetArray.push({ + category, actionType + }); } // calculate hash of videoID @@ -99,20 +93,26 @@ export async function postLockCategories(req: Request, res: Response): Promise locksToApply.some((lock) => category === lock.category)))] - : [...filteredCategories], // Legacy - submittedValues: [...locksToApply, ...overwrittenLocks], + submitted: deDupArray(validLocks.map(e => e.category)), + submittedValues: validLocks, }); } -function filterData(data: T[]): T[] { - // get user categories not already submitted that match accepted format - const filtered = data.filter((elem) => { - return !!elem.match(/^[_a-zA-Z]+$/); +const isValidCategoryActionPair = (category: Category, actionType: ActionType): boolean => + config.categorySupport?.[category]?.includes(actionType); + +// filter out any invalid category/action pairs +type validLockArray = { category: Category, actionType: ActionType }[]; +const createLockArray = (categories: Category[], actionTypes: ActionType[]): validLockArray => { + const validLocks: validLockArray = []; + categories.forEach(category => { + actionTypes.forEach(actionType => { + if (isValidCategoryActionPair(category, actionType)) { + validLocks.push({ category, actionType }); + } + }); }); - // remove any duplicates - return filtered.filter((elem, index) => { - return filtered.indexOf(elem) === index; - }); -} + return validLocks; +}; + +const deDupArray = (arr: any[]): any[] => [...new Set(arr)]; \ No newline at end of file diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index 568199d..d704c85 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -488,7 +488,7 @@ async function updateDataIfVideoDurationChange(videoID: VideoID, service: Servic await db.prepare("run", `UPDATE "sponsorTimes" SET "hidden" = 1 WHERE "UUID" = ?`, [submission.UUID]); } lockedCategoryList = []; - deleteLockCategories(videoID, null, service); + deleteLockCategories(videoID, null, null, service); } return { diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 5a9c47b..9bce85b 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -10,7 +10,7 @@ import { getIP } from "../utils/getIP"; import { getHashCache } from "../utils/getHashCache"; import { config } from "../config"; import { HashedUserID, UserID } from "../types/user.model"; -import { Category, HashedIP, IPAddress, SegmentUUID, Service, VideoID, VideoIDHash, Visibility, VideoDuration, ActionType } from "../types/segments.model"; +import { DBSegment, Category, HashedIP, IPAddress, SegmentUUID, Service, VideoID, VideoIDHash, VideoDuration, ActionType } from "../types/segments.model"; import { QueryCacher } from "../utils/queryCacher"; import axios from "axios"; import redis from "../utils/redis"; @@ -52,23 +52,20 @@ interface VoteData { } function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise { - if (config.newLeafURLs !== null) { - return YouTubeAPI.listVideos(videoID, ignoreCache); - } else { - return null; - } + return config.newLeafURLs ? YouTubeAPI.listVideos(videoID, ignoreCache) : null; } const isUserTempVIP = async (nonAnonUserID: HashedUserID, videoID: VideoID): 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 checkVideoDurationChange(UUID: SegmentUUID) { +async function updateSegmentVideoDuration(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) { @@ -82,6 +79,36 @@ async function checkVideoDurationChange(UUID: SegmentUUID) { } } +async function checkVideoDuration(UUID: SegmentUUID) { + const { videoID, service } = await db.prepare("get", `select "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, true); + } + const apiVideoDuration = apiVideoInfo?.data?.lengthSeconds as VideoDuration; + // if no videoDuration return early + if (isNaN(apiVideoDuration)) return; + // fetch latest submission + const latestSubmission = await db.prepare("get", `SELECT "videoDuration", "UUID", "timeSubmitted" + FROM "sponsorTimes" + WHERE "videoID" = ? AND "service" = ? AND + "hidden" = 0 AND "shadowHidden" = 0 AND + "actionType" != 'full' AND + "votes" > -2 AND "videoDuration" != 0 + ORDER BY "timeSubmitted" DESC LIMIT 1`, + [videoID, service]) as {videoDuration: VideoDuration, UUID: SegmentUUID, timeSubmitted: number}; + + if (videoDurationChanged(latestSubmission.videoDuration, apiVideoDuration)) { + Logger.info(`Video duration changed for ${videoID} from ${latestSubmission.videoDuration} to ${apiVideoDuration}`); + await db.prepare("run", `UPDATE "sponsorTimes" SET "hidden" = 1 + WHERE "videoID" = ? AND "service" = ? AND "timeSubmitted" <= ? + AND "hidden" = 0 AND "shadowHidden" = 0 AND + "actionType" != 'full' AND "votes" > -2`, + [videoID, service, latestSubmission.timeSubmitted]); + } +} + 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, @@ -186,7 +213,7 @@ async function sendWebhooks(voteData: VoteData) { } } -async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, isOwnSubmission: boolean, category: Category +async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, isTempVIP: boolean, isOwnSubmission: boolean, category: Category , hashedIP: HashedIP, finalResponse: FinalResponse): Promise<{ status: number, message?: string }> { // Check if they've already made a vote const usersLastVoteInfo = await privateDB.prepare("get", `select count(*) as votes, category from "categoryVotes" where "UUID" = ? and "userID" = ? group by category`, [UUID, userID]); @@ -196,32 +223,27 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i return { status: finalResponse.finalStatus }; } - const videoInfo = (await db.prepare("get", `SELECT "category", "actionType", "videoID", "hashedVideoID", "service", "userID", "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, + const segmentInfo = (await db.prepare("get", `SELECT "category", "actionType", "videoID", "hashedVideoID", "service", "userID", "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID])) as {category: Category, actionType: ActionType, videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, userID: UserID, locked: number}; - if (!videoInfo) { - // Submission doesn't exist - return { status: 400, message: "Submission doesn't exist." }; - } - if (videoInfo.actionType === ActionType.Full) { + if (segmentInfo.actionType === ActionType.Full) { return { status: 400, message: "Not allowed to change category of a full video segment" }; } - + if (segmentInfo.actionType === ActionType.Poi) { + return { status: 400, message: "Not allowed to change category for single point segments" }; + } if (!config.categoryList.includes(category)) { return { status: 400, message: "Category doesn't exist." }; } - if (videoInfo.actionType === ActionType.Poi) { - return { status: 400, message: "Not allowed to change category for single point segments" }; - } // Ignore vote if the next category is locked - const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" = ?`, [videoInfo.videoID, videoInfo.service, category]); + const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" = ?`, [segmentInfo.videoID, segmentInfo.service, category]); if (nextCategoryLocked && !isVIP) { return { status: 200 }; } // Ignore vote if the segment is locked - if (!isVIP && videoInfo.locked === 1) { + if (!isVIP && segmentInfo.locked === 1) { return { status: 200 }; } @@ -229,8 +251,8 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i const timeSubmitted = Date.now(); - const voteAmount = isVIP ? 500 : 1; - const ableToVote = isVIP || finalResponse.finalStatus === 200 || true; + const voteAmount = (isVIP || isTempVIP) ? 500 : 1; + const ableToVote = isVIP || isTempVIP || finalResponse.finalStatus === 200 || true; if (ableToVote) { // Add the vote @@ -253,7 +275,7 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i } // See if the submissions category is ready to change - const currentCategoryInfo = await db.prepare("get", `select votes from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, videoInfo.category]); + const currentCategoryInfo = await db.prepare("get", `select votes from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, segmentInfo.category]); const submissionInfo = await db.prepare("get", `SELECT "userID", "timeSubmitted", "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); const isSubmissionVIP = submissionInfo && await isUserVIP(submissionInfo.userID); @@ -265,23 +287,20 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i // Add submission as vote if (!currentCategoryInfo && submissionInfo) { - await db.prepare("run", `insert into "categoryVotes" ("UUID", "category", "votes") values (?, ?, ?)`, [UUID, videoInfo.category, currentCategoryCount]); - - await privateDB.prepare("run", `insert into "categoryVotes" ("UUID", "userID", "hashedIP", "category", "timeSubmitted") values (?, ?, ?, ?, ?)`, [UUID, submissionInfo.userID, "unknown", videoInfo.category, submissionInfo.timeSubmitted]); + await db.prepare("run", `insert into "categoryVotes" ("UUID", "category", "votes") values (?, ?, ?)`, [UUID, segmentInfo.category, currentCategoryCount]); + await privateDB.prepare("run", `insert into "categoryVotes" ("UUID", "userID", "hashedIP", "category", "timeSubmitted") values (?, ?, ?, ?, ?)`, [UUID, submissionInfo.userID, "unknown", segmentInfo.category, submissionInfo.timeSubmitted]); } const nextCategoryCount = (nextCategoryInfo?.votes || 0) + voteAmount; //TODO: In the future, raise this number from zero to make it harder to change categories // VIPs change it every time - if (nextCategoryCount - currentCategoryCount >= Math.max(Math.ceil(submissionInfo?.votes / 2), 2) || isVIP || isOwnSubmission) { + if (nextCategoryCount - currentCategoryCount >= Math.max(Math.ceil(submissionInfo?.votes / 2), 2) || isVIP || isTempVIP || isOwnSubmission) { // Replace the category await db.prepare("run", `update "sponsorTimes" set "category" = ? where "UUID" = ?`, [category, UUID]); } } - - QueryCacher.clearSegmentCache(videoInfo); - + QueryCacher.clearSegmentCache(segmentInfo); return { status: finalResponse.finalStatus }; } @@ -309,12 +328,12 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise { - if (UUID === undefined || paramUserID === undefined || (type === undefined && category === undefined)) { - //invalid request + // missing key parameters + if (!UUID || !paramUserID || !(type !== undefined || category)) { return { status: 400 }; } + // Ignore this vote, invalid if (paramUserID.length < 30 && config.mode !== "test") { - // Ignore this vote, invalid return { status: 200 }; } @@ -334,51 +353,49 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID //hash the ip 5000 times so no one can get it from the database const hashedIP: HashedIP = await getHashCache((ip + config.globalSalt) as IPAddress); - //check if this user is on the vip list - const videoID = await db.prepare("get", `select "videoID" from "sponsorTimes" where "UUID" = ?`, [UUID]); - const isTempVIP = await isUserTempVIP(nonAnonUserID, videoID?.videoID || null); - const isVIP = await isUserVIP(nonAnonUserID) || isTempVIP; + const segmentInfo: DBSegment = await db.prepare("get", `SELECT * from "sponsorTimes" WHERE "UUID" = ?`, [UUID]); + // segment doesnt exist + if (!segmentInfo) { + return { status: 404 }; + } + + const isTempVIP = await isUserTempVIP(nonAnonUserID, segmentInfo.videoID); + const isVIP = await isUserVIP(nonAnonUserID); //check if user voting on own submission - const isOwnSubmission = (await db.prepare("get", `SELECT "UUID" as "submissionCount" FROM "sponsorTimes" where "userID" = ? AND "UUID" = ?`, [nonAnonUserID, UUID])) !== undefined; + const isOwnSubmission = nonAnonUserID === segmentInfo.userID; // disallow vote types 10/11 if (type === 10 || type === 11) { - // no longer allow type 10/11 alternative votes return { status: 400 }; } + // no type but has category, categoryVote + if (!type && category) { + return categoryVote(UUID, nonAnonUserID, isVIP, isTempVIP, isOwnSubmission, category, hashedIP, finalResponse); + } + // If not upvote if (!isVIP && type != 1) { - const isSegmentLocked = async () => !!(await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]))?.locked; - const isVideoLocked = async () => !!(await db.prepare("get", `SELECT "lockCategories".category from "lockCategories" left join "sponsorTimes" - on ("lockCategories"."videoID" = "sponsorTimes"."videoID" and - "lockCategories"."service" = "sponsorTimes"."service" and "lockCategories".category = "sponsorTimes".category) - where "UUID" = ?`, [UUID])); - - if (await isSegmentLocked() || await isVideoLocked()) { + const isSegmentLocked = segmentInfo.locked; + const isVideoLocked = async () => !!(await db.prepare("get", `SELECT "category" FROM "lockCategories" WHERE + "videoID" = ? AND "service" = ? AND "category" = ? AND "actionType" = ?`, + [segmentInfo.videoID, segmentInfo.service, segmentInfo.category, segmentInfo.actionType])); + if (isSegmentLocked || await isVideoLocked()) { finalResponse.blockVote = true; finalResponse.webhookType = VoteWebhookType.Rejected; finalResponse.webhookMessage = "Vote rejected: A moderator has decided that this segment is correct"; } } - if (type === undefined && category !== undefined) { - return categoryVote(UUID, nonAnonUserID, isVIP, isOwnSubmission, category, hashedIP, finalResponse); - } - - if (type !== undefined && !isVIP && !isOwnSubmission) { - // Check if upvoting hidden segment - const voteInfo = await db.prepare("get", `SELECT votes, "actionType" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) as - { votes: number, actionType: ActionType }; - - if (voteInfo && voteInfo.votes <= -2 && voteInfo.actionType !== ActionType.Full) { - if (type == 1) { - return { status: 403, message: "Not allowed to upvote segment with too many downvotes unless you are VIP." }; - } else if (type == 0) { - // Already downvoted enough, ignore - return { status: 200 }; - } + // if on downvoted non-full segment and is not VIP/ tempVIP/ submitter + if (!isNaN(type) && segmentInfo.votes <= -2 && segmentInfo.actionType !== ActionType.Full && + !(isVIP || isTempVIP || isOwnSubmission)) { + if (type == 1) { + return { status: 403, message: "Not allowed to upvote segment with too many downvotes unless you are VIP." }; + } else if (type == 0) { + // Already downvoted enough, ignore + return { status: 200 }; } } @@ -389,18 +406,26 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID )); if (warnings.length >= config.maxNumberOfActiveWarnings) { + const warningReason = warnings[0]?.reason; return { status: 403, message: "Vote rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes that are not malicious, and we just want to clarify the rules. " + "Could you please send a message in Discord or Matrix so we can further help you?" + - `${(warnings[0]?.reason?.length > 0 ? ` Warning reason: '${warnings[0].reason}'` : "")}` }; + `${(warningReason.length > 0 ? ` Warning reason: '${warningReason}'` : "")}` }; } const voteTypeEnum = (type == 0 || type == 1 || type == 20) ? voteTypes.normal : voteTypes.incorrect; + // no restrictions on checkDuration + // check duration of all submissions on this video + if (type <= 0) { + checkVideoDuration(UUID); + } + try { - //check if vote has already happened + // check if vote has already happened const votesRow = await privateDB.prepare("get", `SELECT "type" FROM "votes" WHERE "userID" = ? AND "UUID" = ?`, [userID, UUID]); - //-1 for downvote, 1 for upvote. Maybe more depending on reputation in the future + // -1 for downvote, 1 for upvote. Maybe more depending on reputation in the future + // oldIncrementAmount will be zero if row is null let incrementAmount = 0; let oldIncrementAmount = 0; @@ -417,7 +442,7 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID //unrecongnised type of vote return { status: 400 }; } - if (votesRow != undefined) { + if (votesRow) { if (votesRow.type === 1) { //upvote oldIncrementAmount = 1; @@ -442,65 +467,52 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID } } - //check if the increment amount should be multiplied (downvotes have more power if there have been many views) - const videoInfo = await db.prepare("get", `SELECT "videoID", "hashedVideoID", "service", "votes", "views", "userID", "hidden" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) as - {videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, votes: number, views: number, userID: UserID, hidden: Visibility}; - - if (voteTypeEnum === voteTypes.normal) { - if ((isVIP || isOwnSubmission) && incrementAmount < 0) { - //this user is a vip and a downvote - incrementAmount = -(videoInfo.votes + 2 - oldIncrementAmount); - type = incrementAmount; - } - } else if (voteTypeEnum == voteTypes.incorrect) { - if (isVIP || isOwnSubmission) { - //this user is a vip and a downvote - incrementAmount = 500 * incrementAmount; - type = incrementAmount < 0 ? 12 : 13; - } + // check if the increment amount should be multiplied (downvotes have more power if there have been many views) + // user is temp/ VIP/ own submission and downvoting + if ((isVIP || isTempVIP || isOwnSubmission) && incrementAmount < 0) { + incrementAmount = -(segmentInfo.votes + 2 - oldIncrementAmount); + type = incrementAmount; } // Only change the database if they have made a submission before and haven't voted recently - const ableToVote = isVIP - || (!(isOwnSubmission && incrementAmount > 0 && oldIncrementAmount >= 0) + const userAbleToVote = (!(isOwnSubmission && incrementAmount > 0 && oldIncrementAmount >= 0) && (await db.prepare("get", `SELECT "userID" FROM "sponsorTimes" WHERE "userID" = ?`, [nonAnonUserID])) !== undefined && (await db.prepare("get", `SELECT "userID" FROM "shadowBannedUsers" WHERE "userID" = ?`, [nonAnonUserID])) === undefined && (await privateDB.prepare("get", `SELECT "UUID" FROM "votes" WHERE "UUID" = ? AND "hashedIP" = ? AND "userID" != ?`, [UUID, hashedIP, userID])) === undefined) && !finalResponse.blockVote && finalResponse.finalStatus === 200; + + const ableToVote = isVIP || isTempVIP || userAbleToVote; + if (ableToVote) { //update the votes table - if (votesRow != undefined) { + if (votesRow) { await privateDB.prepare("run", `UPDATE "votes" SET "type" = ? WHERE "userID" = ? AND "UUID" = ?`, [type, userID, UUID]); } else { await privateDB.prepare("run", `INSERT INTO "votes" VALUES(?, ?, ?, ?)`, [UUID, userID, hashedIP, type]); } - let columnName = ""; - if (voteTypeEnum === voteTypes.normal) { - columnName = "votes"; - } else if (voteTypeEnum === voteTypes.incorrect) { - columnName = "incorrectVotes"; - } + // update the vote count on this sponsorTime + await db.prepare("run", `UPDATE "sponsorTimes" SET "votes" = "votes" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]); - //update the vote count on this sponsorTime - //oldIncrementAmount will be zero is row is null - await db.prepare("run", `UPDATE "sponsorTimes" SET "${columnName}" = "${columnName}" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]); + // tempVIP can bring back hidden segments + if (isTempVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) { + await db.prepare("run", `UPDATE "sponsorTimes" SET "hidden" = 0 WHERE "UUID" = ?`, [UUID]); + } + // additional processing for VIP + // on VIP upvote 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]); - - // Reset video duration in case that caused it to be hidden - if (videoInfo.hidden) await db.prepare("run", 'UPDATE "sponsorTimes" SET "videoDuration" = 0 WHERE "UUID" = ?', [UUID]); + // Update video duration in case that caused it to be hidden + await updateSegmentVideoDuration(UUID); + // unhide & unlock + await db.prepare("run", 'UPDATE "sponsorTimes" SET "locked" = 1, "hidden" = 0, "shadowHidden" = 0 WHERE "UUID" = ?', [UUID]); + // on VIP downvote/ undovote, also unlock submission } else if (isVIP && incrementAmount <= 0 && voteTypeEnum === voteTypes.normal) { - // Unlock if a VIP downvotes it - await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 0 WHERE "UUID" = ?', [UUID]); + await db.prepare("run", 'UPDATE "sponsorTimes" SET "locked" = 0 WHERE "UUID" = ?', [UUID]); } - QueryCacher.clearSegmentCache(videoInfo); + QueryCacher.clearSegmentCache(segmentInfo); } if (incrementAmount - oldIncrementAmount !== 0) { sendWebhooks({ @@ -510,7 +522,7 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID isTempVIP, isVIP, isOwnSubmission, - row: videoInfo, + row: segmentInfo, category, incrementAmount, oldIncrementAmount, diff --git a/test/cases/lockCategoriesRecords.ts b/test/cases/lockCategoriesRecords.ts index dbc4969..ab96c05 100644 --- a/test/cases/lockCategoriesRecords.ts +++ b/test/cases/lockCategoriesRecords.ts @@ -49,6 +49,7 @@ describe("lockCategoriesRecords", () => { await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record", "skip", "sponsor", "reason-4", "YouTube"]); await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record", "mute", "sponsor", "reason-4", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record", "full", "sponsor", "reason-4", "YouTube"]); await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record-1", "skip", "sponsor", "reason-5", "YouTube"]); await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record-1", "mute", "sponsor", "reason-5", "YouTube"]); await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record-1", "skip", "intro", "reason-5", "YouTube"]); @@ -76,7 +77,7 @@ describe("lockCategoriesRecords", () => { const expected = { submitted: [ "outro", - "shilling", + "intro" ], submittedValues: [ { @@ -87,14 +88,6 @@ describe("lockCategoriesRecords", () => { actionType: "mute", category: "outro" }, - { - actionType: "skip", - category: "shilling" - }, - { - actionType: "mute", - category: "shilling" - }, { actionType: "skip", category: "intro" @@ -132,14 +125,14 @@ describe("lockCategoriesRecords", () => { .then(async res => { assert.strictEqual(res.status, 200); const result = await checkLockCategories(videoID); - assert.strictEqual(result.length, 8); + assert.strictEqual(result.length, 6); const oldRecordNotChangeReason = result.filter(item => item.reason === "reason-2" && ["sponsor", "intro"].includes(item.category) ); const newRecordWithEmptyReason = result.filter(item => - item.reason === "" && ["outro", "shilling"].includes(item.category) + item.reason === "" && ["outro"].includes(item.category) ); - assert.strictEqual(newRecordWithEmptyReason.length, 4); + assert.strictEqual(newRecordWithEmptyReason.length, 2); assert.strictEqual(oldRecordNotChangeReason.length, 4); done(); }) @@ -164,7 +157,6 @@ describe("lockCategoriesRecords", () => { const expected = { submitted: [ "outro", - "shilling", "intro" ], }; @@ -196,7 +188,6 @@ describe("lockCategoriesRecords", () => { const expectedWithNewReason = [ "outro", - "shilling", "intro" ]; @@ -204,7 +195,7 @@ describe("lockCategoriesRecords", () => { .then(async res => { assert.strictEqual(res.status, 200); const result = await checkLockCategories(videoID); - assert.strictEqual(result.length, 8); + assert.strictEqual(result.length, 6); const newRecordWithNewReason = result.filter(item => expectedWithNewReason.includes(item.category) && item.reason === "new reason" ); @@ -212,7 +203,7 @@ describe("lockCategoriesRecords", () => { item.reason === "reason-2" ); - assert.strictEqual(newRecordWithNewReason.length, 6); + assert.strictEqual(newRecordWithNewReason.length, 4); assert.strictEqual(oldRecordNotChangeReason.length, 2); done(); }) @@ -220,56 +211,20 @@ describe("lockCategoriesRecords", () => { }); it("Should be able to submit categories with _ in the category", (done) => { - const json = { - videoID: "underscore", - userID: lockVIPUser, - categories: [ - "word_word", - ], - }; - client.post(endpoint, json) - .then(async res => { - assert.strictEqual(res.status, 200); - const result = await checkLockCategories("underscore"); - assert.strictEqual(result.length, 2); - done(); - }) - .catch(err => done(err)); - }); - - it("Should be able to submit categories with upper and lower case in the category", (done) => { - const json = { - videoID: "bothCases", - userID: lockVIPUser, - categories: [ - "wordWord", - ], - }; - client.post(endpoint, json) - .then(async res => { - assert.strictEqual(res.status, 200); - const result = await checkLockCategories("bothCases"); - assert.strictEqual(result.length, 2); - done(); - }) - .catch(err => done(err)); - }); - - it("Should not be able to submit categories with $ in the category", (done) => { - const videoID = "specialChar"; + const videoID = "underscore"; const json = { videoID, userID: lockVIPUser, categories: [ - "word&word", + "exclusive_access", ], + actionTypes: ["full"] }; - client.post(endpoint, json) .then(async res => { assert.strictEqual(res.status, 200); const result = await checkLockCategories(videoID); - assert.strictEqual(result.length, 0); + assert.strictEqual(result.length, 1); done(); }) .catch(err => done(err)); @@ -418,13 +373,14 @@ describe("lockCategoriesRecords", () => { categories: [ "sponsor", ], + actionTypes: ["skip", "mute"] }; client.delete(endpoint, { data: json }) .then(async res => { assert.strictEqual(res.status, 200); const result = await checkLockCategories(videoID); - assert.strictEqual(result.length, 0); + assert.strictEqual(result.length, 1); done(); }) .catch(err => done(err)); @@ -438,6 +394,7 @@ describe("lockCategoriesRecords", () => { categories: [ "sponsor", ], + actionTypes: ["skip", "mute"] }; client.delete(endpoint, { data: json }) @@ -531,7 +488,6 @@ describe("lockCategoriesRecords", () => { "sponsor", "intro", "outro", - "shilling" ], }; client.get(endpoint, { params: { videoID: "no-segments-video-id" } }) @@ -543,4 +499,24 @@ describe("lockCategoriesRecords", () => { }) .catch(err => done(err)); }); + + it("should be able to delete individual category lock", (done) => { + const videoID = "delete-record"; + const json = { + videoID, + userID: lockVIPUser, + categories: [ + "sponsor", + ], + actionTypes: ["full"] + }; + client.delete(endpoint, { data: json }) + .then(async res => { + assert.strictEqual(res.status, 200); + const result = await checkLockCategories(videoID); + assert.strictEqual(result.length, 0); + done(); + }) + .catch(err => done(err)); + }); }); diff --git a/test/cases/tempVip.ts b/test/cases/tempVip.ts index aadf45a..c2fbc10 100644 --- a/test/cases/tempVip.ts +++ b/test/cases/tempVip.ts @@ -64,7 +64,6 @@ describe("tempVIP test", function() { await db.prepare("run", insertSponsorTimeQuery, ["channelid-convert", 1, 9, 0, 1, "tempvip-submit", publicTempVIPOne, 0, 50, "sponsor", 0]); await db.prepare("run", insertSponsorTimeQuery, ["otherchannel", 1, 9, 0, 1, UUID1, "testman", 0, 50, "sponsor", 0]); - await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [publicPermVIP1]); await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [publicPermVIP2]); // clear redis if running consecutive tests @@ -124,24 +123,24 @@ describe("tempVIP test", function() { }) .catch(err => done(err)); }); - it("Should be able to VIP lock", (done) => { + it("Should not be able to lock segment", (done) => { postVote(tempVIPOne, UUID0, 1) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegment(UUID0); - assert.ok(row.votes > -2); - assert.strictEqual(row.locked, 1); + assert.strictEqual(row.votes, 1); + assert.strictEqual(row.locked, 0); done(); }) .catch(err => done(err)); }); - it("Should be able to VIP change category", (done) => { + it("Should be able to change category but not lock", (done) => { postVoteCategory(tempVIPOne, UUID0, "filler") .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegment(UUID0); assert.strictEqual(row.category, "filler"); - assert.strictEqual(row.locked, 1); + assert.strictEqual(row.locked, 0); done(); }) .catch(err => done(err)); diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 39898af..1b00c76 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -6,6 +6,7 @@ import * as YouTubeAPIModule from "../../src/utils/youtubeApi"; import { YouTubeApiMock } from "../youtubeMock"; import assert from "assert"; import { client } from "../utils/httpClient"; +import { arrayDeepEquals } from "../utils/partialDeepEquals"; const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI"); const sinonStub = mockManager.mock("listVideos"); @@ -46,6 +47,7 @@ describe("voteOnSponsorTime", () => { await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest", 1, 11, 2, 0, "warnvote-uuid-0", "testman", 0, 50, "sponsor", "skip", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["no-sponsor-segments-video", 1, 11, 2, 0, "no-sponsor-segments-uuid-0", "no-sponsor-segments", 0, 50, "sponsor", "skip", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["no-sponsor-segments-video", 1, 11, 2, 0, "no-sponsor-segments-uuid-1", "no-sponsor-segments", 0, 50, "intro", "skip", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["no-sponsor-segments-video", 1, 11, 2, 0, "no-sponsor-segments-uuid-2", "no-sponsor-segments", 0, 50, "sponsor", "mute", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["segment-locking-video", 1, 11, 2, 0, "segment-locking-uuid-1", "segment-locking-user", 0, 50, "intro", "skip", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["segment-hidden-video", 1, 11, 2, 0, "segment-hidden-uuid-1", "segment-hidden-user", 0, 50, "intro", "skip", 0, 1]); await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 7, 22, 0, 0, "category-change-uuid-1", categoryChangeUserHash, 0, 50, "intro", "skip", 0, 0]); @@ -58,6 +60,12 @@ describe("voteOnSponsorTime", () => { await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 7, 12, 0, 1, "category-change-uuid-8", categoryChangeUserHash, 0, 50, "intro", "skip", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["duration-update", 1, 10, 0, 0, "duration-update-uuid-1", "testman", 0, 0, "intro", "skip", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["full-video", 1, 10, 0, 0, "full-video-uuid-1", "testman", 0, 0, "sponsor", "full", 0, 0]); + // videoDuration change + await db.prepare("run", insertSponsorTimeQuery, ["duration-changed", 1, 10, 0, 0, "duration-changed-uuid-1", "testman", 1, 0, "sponsor", "skip", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["duration-changed", 1, 11, 0, 0, "duration-changed-uuid-2", "testman", 10, 0, "sponsor", "skip", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["duration-changed", 1, 12, 0, 0, "duration-changed-uuid-3", "testman", 20, 0, "sponsor", "skip", 0, 0]); + // add videoDuration to duration-changed-uuid-2 + await db.prepare("run", `UPDATE "sponsorTimes" SET "videoDuration" = 150 WHERE "UUID" = 'duration-changed-uuid-2'`); const insertWarningQuery = 'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled") VALUES(?, ?, ?, ?)'; await db.prepare("run", insertWarningQuery, [warnUser01Hash, now, warnVip01Hash, 1]); @@ -73,9 +81,9 @@ describe("voteOnSponsorTime", () => { await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(vipUser)]); await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]); - const insertlockCategoriesQuerry = 'INSERT INTO "lockCategories" ("videoID", "userID", "category", "reason") VALUES (?, ?, ?, ?)'; - await db.prepare("run", insertlockCategoriesQuerry, ["no-sponsor-segments-video", "someUser", "sponsor", ""]); - await db.prepare("run", insertlockCategoriesQuerry, ["category-change-test-1", "someUser", "preview", ""]); // sponsor should stay unlocked + const insertlockCategoriesQuery = 'INSERT INTO "lockCategories" ("videoID", "userID", "category", "actionType") VALUES (?, ?, ?, ?)'; + await db.prepare("run", insertlockCategoriesQuery, ["no-sponsor-segments-video", "someUser", "sponsor", "skip"]); + await db.prepare("run", insertlockCategoriesQuery, ["category-change-test-1", "someUser", "preview", "skip"]); // sponsor should stay unlocked }); // constants const endpoint = "/api/voteOnSponsorTime"; @@ -409,15 +417,15 @@ describe("voteOnSponsorTime", () => { const UUID = "invalid-uuid"; postVoteCategory("randomID3", UUID, "intro") .then(res => { - assert.strictEqual(res.status, 400); + assert.strictEqual(res.status, 404); done(); }) .catch(err => done(err)); }); - it("Should not be able to category-vote on an a full video segment", (done) => { - const UUID = "invalid-uuid"; - postVoteCategory("full-video-uuid-1", UUID, "selfpromo") + it("Should not be able to category-vote on a full video segment", (done) => { + const UUID = "full-video-uuid-1"; + postVoteCategory("randomID3", UUID, "selfpromo") .then(res => { assert.strictEqual(res.status, 400); done(); @@ -585,4 +593,33 @@ describe("voteOnSponsorTime", () => { done(); }); }); + + it("Should hide changed submission on any downvote", (done) => { + const UUID = "duration-changed-uuid-3"; + const videoID = "duration-changed"; + postVote(randomID2, UUID, 0) + .then(async res => { + assert.strictEqual(res.status, 200); + const hiddenSegments = await db.prepare("all", `SELECT "UUID" FROM "sponsorTimes" WHERE "videoID" = ? AND "hidden" = 1`, [videoID]); + assert.strictEqual(hiddenSegments.length, 2); + const expected = [{ + "UUID": "duration-changed-uuid-1", + }, { + "UUID": "duration-changed-uuid-2", + }]; + arrayDeepEquals(hiddenSegments, expected); + done(); + }); + }); + + it("Should be able to downvote segment with ajacent actionType lock", (done) => { + const UUID = "no-sponsor-segments-uuid-2"; + postVote(randomID2, UUID, 0) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await getSegmentVotes(UUID); + assert.strictEqual(row.votes, 1); + done(); + }); + }); }); diff --git a/test/youtubeMock.ts b/test/youtubeMock.ts index 6783120..f0b89d1 100644 --- a/test/youtubeMock.ts +++ b/test/youtubeMock.ts @@ -56,6 +56,13 @@ export class YouTubeApiMock { authorId: "ChannelID" } as APIVideoData }; + } else if (obj.id === "duration-changed") { + return { + err: null, + data: { + lengthSeconds: 100, + } as APIVideoData + }; } else { return { err: null,