Merge pull request #451 from mchangrh/voteOnSponsorTime

rewrite voteOnSponsorTimes
This commit is contained in:
Ajay Ramachandran
2022-02-10 23:02:35 -05:00
committed by GitHub
10 changed files with 279 additions and 237 deletions

View File

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

View File

@@ -32,7 +32,8 @@ const getChannelInfo = async (videoID: VideoID): Promise<{id: string | null, nam
}; };
export async function addUserAsTempVIP(req: AddUserAsTempVIPRequest, res: Response): Promise<Response> { export async function addUserAsTempVIP(req: AddUserAsTempVIPRequest, res: Response): Promise<Response> {
const { query: { userID, adminUserID } } = req; const userID = req.query.userID;
let adminUserID = req.query.adminUserID;
const enabled = req.query?.enabled === "true"; const enabled = req.query?.enabled === "true";
const channelVideoID = req.query?.channelVideoID as VideoID; const channelVideoID = req.query?.channelVideoID as VideoID;
@@ -43,9 +44,9 @@ export async function addUserAsTempVIP(req: AddUserAsTempVIPRequest, res: Respon
} }
// hash the issuer userID // hash the issuer userID
const issuerUserID = await getHashCache(adminUserID); adminUserID = await getHashCache(adminUserID);
// check if issuer is VIP // check if issuer is VIP
const issuerIsVIP = await isUserVIP(issuerUserID as HashedUserID); const issuerIsVIP = await isUserVIP(adminUserID as HashedUserID);
if (!issuerIsVIP) { if (!issuerIsVIP) {
return res.sendStatus(403); return res.sendStatus(403);
} }

View File

@@ -2,9 +2,10 @@ import { Request, Response } from "express";
import { isUserVIP } from "../utils/isUserVIP"; import { isUserVIP } from "../utils/isUserVIP";
import { getHashCache } from "../utils/getHashCache"; import { getHashCache } from "../utils/getHashCache";
import { db } from "../databases/databases"; 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 { UserID } from "../types/user.model";
import { getService } from "../utils/getService"; import { getService } from "../utils/getService";
import { config } from "../config";
interface DeleteLockCategoriesRequest extends Request { interface DeleteLockCategoriesRequest extends Request {
body: { body: {
@@ -12,6 +13,7 @@ interface DeleteLockCategoriesRequest extends Request {
service: string; service: string;
userID: UserID; userID: UserID;
videoID: VideoID; videoID: VideoID;
actionTypes: ActionType[];
}; };
} }
@@ -22,7 +24,8 @@ export async function deleteLockCategoriesEndpoint(req: DeleteLockCategoriesRequ
videoID, videoID,
userID, userID,
categories, categories,
service service,
actionTypes
} }
} = req; } = req;
@@ -32,6 +35,7 @@ export async function deleteLockCategoriesEndpoint(req: DeleteLockCategoriesRequ
|| !categories || !categories
|| !Array.isArray(categories) || !Array.isArray(categories)
|| categories.length === 0 || categories.length === 0
|| actionTypes.length === 0
) { ) {
return res.status(400).json({ return res.status(400).json({
message: "Bad Format", 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}` }); return res.status(200).json({ message: `Removed lock categories entries for video ${videoID}` });
} }
/** export async function deleteLockCategories(videoID: VideoID, categories = config.categoryList, actionTypes = [ActionType.Skip, ActionType.Mute], service: Service): Promise<void> {
* const arrJoin = (arr: string[]): string => `'${arr.join(`','`)}'`;
* @param videoID const categoryString = arrJoin(categories);
* @param categories If null, will remove all const actionTypeString = arrJoin(actionTypes);
* @param service await db.prepare("run", `DELETE FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" IN (${categoryString}) AND "actionType" IN (${actionTypeString})`, [videoID, service]);
*/
export async function deleteLockCategories(videoID: VideoID, categories: Category[], service: Service): Promise<void> {
type DBEntry = { category: Category };
const dbEntries = await db.prepare(
"all",
'SELECT * FROM "lockCategories" WHERE "videoID" = ? AND "service" = ?',
[videoID, service]
) as Array<DBEntry>;
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]
))
);
} }

View File

@@ -5,6 +5,7 @@ import { db } from "../databases/databases";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { ActionType, Category, VideoIDHash } from "../types/segments.model"; import { ActionType, Category, VideoIDHash } from "../types/segments.model";
import { getService } from "../utils/getService"; import { getService } from "../utils/getService";
import { config } from "../config";
export async function postLockCategories(req: Request, res: Response): Promise<string[]> { export async function postLockCategories(req: Request, res: Response): Promise<string[]> {
// Collect user input data // Collect user input data
@@ -44,25 +45,18 @@ export async function postLockCategories(req: Request, res: Response): Promise<s
const existingLocks = (await db.prepare("all", 'SELECT "category", "actionType" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service])) as const existingLocks = (await db.prepare("all", 'SELECT "category", "actionType" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service])) as
{ category: Category, actionType: ActionType }[]; { category: Category, actionType: ActionType }[];
const filteredCategories = filterData(categories);
const filteredActionTypes = filterData(actionTypes);
const locksToApply: { category: Category, actionType: ActionType }[] = []; const locksToApply: { category: Category, actionType: ActionType }[] = [];
const overwrittenLocks: { category: Category, actionType: ActionType }[] = []; const overwrittenLocks: { category: Category, actionType: ActionType }[] = [];
for (const category of filteredCategories) {
for (const actionType of filteredActionTypes) { // push new/ existing locks
if (!existingLocks.some((lock) => lock.category === category && lock.actionType === actionType)) { const validLocks = createLockArray(categories, actionTypes);
locksToApply.push({ for (const { category, actionType } of validLocks) {
category, const targetArray = existingLocks.some((lock) => lock.category === category && lock.actionType === actionType)
actionType ? overwrittenLocks
: locksToApply;
targetArray.push({
category, actionType
}); });
} else {
overwrittenLocks.push({
category,
actionType
});
}
}
} }
// calculate hash of videoID // calculate hash of videoID
@@ -99,20 +93,26 @@ export async function postLockCategories(req: Request, res: Response): Promise<s
} }
res.status(200).json({ res.status(200).json({
submitted: reason.length === 0 submitted: deDupArray(validLocks.map(e => e.category)),
? [...filteredCategories.filter(((category) => locksToApply.some((lock) => category === lock.category)))] submittedValues: validLocks,
: [...filteredCategories], // Legacy
submittedValues: [...locksToApply, ...overwrittenLocks],
}); });
} }
function filterData<T extends string>(data: T[]): T[] { const isValidCategoryActionPair = (category: Category, actionType: ActionType): boolean =>
// get user categories not already submitted that match accepted format config.categorySupport?.[category]?.includes(actionType);
const filtered = data.filter((elem) => {
return !!elem.match(/^[_a-zA-Z]+$/); // filter out any invalid category/action pairs
}); type validLockArray = { category: Category, actionType: ActionType }[];
// remove any duplicates const createLockArray = (categories: Category[], actionTypes: ActionType[]): validLockArray => {
return filtered.filter((elem, index) => { const validLocks: validLockArray = [];
return filtered.indexOf(elem) === index; categories.forEach(category => {
}); actionTypes.forEach(actionType => {
if (isValidCategoryActionPair(category, actionType)) {
validLocks.push({ category, actionType });
} }
});
});
return validLocks;
};
const deDupArray = (arr: any[]): any[] => [...new Set(arr)];

View File

@@ -488,7 +488,7 @@ async function updateDataIfVideoDurationChange(videoID: VideoID, service: Servic
await db.prepare("run", `UPDATE "sponsorTimes" SET "hidden" = 1 WHERE "UUID" = ?`, [submission.UUID]); await db.prepare("run", `UPDATE "sponsorTimes" SET "hidden" = 1 WHERE "UUID" = ?`, [submission.UUID]);
} }
lockedCategoryList = []; lockedCategoryList = [];
deleteLockCategories(videoID, null, service); deleteLockCategories(videoID, null, null, service);
} }
return { return {

View File

@@ -10,7 +10,7 @@ import { getIP } from "../utils/getIP";
import { getHashCache } from "../utils/getHashCache"; import { getHashCache } from "../utils/getHashCache";
import { config } from "../config"; import { config } from "../config";
import { HashedUserID, UserID } from "../types/user.model"; 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 { QueryCacher } from "../utils/queryCacher";
import axios from "axios"; import axios from "axios";
import redis from "../utils/redis"; import redis from "../utils/redis";
@@ -52,23 +52,20 @@ interface VoteData {
} }
function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> { function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> {
if (config.newLeafURLs !== null) { return config.newLeafURLs ? YouTubeAPI.listVideos(videoID, ignoreCache) : null;
return YouTubeAPI.listVideos(videoID, ignoreCache);
} else {
return null;
}
} }
const isUserTempVIP = async (nonAnonUserID: HashedUserID, videoID: VideoID): Promise<boolean> => { const isUserTempVIP = async (nonAnonUserID: HashedUserID, videoID: VideoID): Promise<boolean> => {
const apiVideoInfo = await getYouTubeVideoInfo(videoID); const apiVideoInfo = await getYouTubeVideoInfo(videoID);
const channelID = apiVideoInfo?.data?.authorId; const channelID = apiVideoInfo?.data?.authorId;
const { err, reply } = await redis.getAsync(tempVIPKey(nonAnonUserID)); const { err, reply } = await redis.getAsync(tempVIPKey(nonAnonUserID));
return err || !reply ? false : (reply == channelID); return err || !reply ? false : (reply == channelID);
}; };
const videoDurationChanged = (segmentDuration: number, APIDuration: number) => (APIDuration > 0 && Math.abs(segmentDuration - APIDuration) > 2); 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]); const { videoDuration, videoID, service } = await db.prepare("get", `select "videoDuration", "videoID", "service" from "sponsorTimes" where "UUID" = ?`, [UUID]);
let apiVideoInfo: APIVideoInfo = null; let apiVideoInfo: APIVideoInfo = null;
if (service == Service.YouTube) { 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) { 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,
@@ -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 }> { , hashedIP: HashedIP, finalResponse: FinalResponse): Promise<{ status: number, message?: string }> {
// Check if they've already made a vote // 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]); 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 }; 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}; [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" }; 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)) { if (!config.categoryList.includes(category)) {
return { status: 400, message: "Category doesn't exist." }; 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 // 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) { if (nextCategoryLocked && !isVIP) {
return { status: 200 }; return { status: 200 };
} }
// Ignore vote if the segment is locked // Ignore vote if the segment is locked
if (!isVIP && videoInfo.locked === 1) { if (!isVIP && segmentInfo.locked === 1) {
return { status: 200 }; return { status: 200 };
} }
@@ -229,8 +251,8 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i
const timeSubmitted = Date.now(); const timeSubmitted = Date.now();
const voteAmount = isVIP ? 500 : 1; const voteAmount = (isVIP || isTempVIP) ? 500 : 1;
const ableToVote = isVIP || finalResponse.finalStatus === 200 || true; const ableToVote = isVIP || isTempVIP || finalResponse.finalStatus === 200 || true;
if (ableToVote) { if (ableToVote) {
// Add the vote // 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 // 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 submissionInfo = await db.prepare("get", `SELECT "userID", "timeSubmitted", "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
const isSubmissionVIP = submissionInfo && await isUserVIP(submissionInfo.userID); 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 // Add submission as vote
if (!currentCategoryInfo && submissionInfo) { if (!currentCategoryInfo && submissionInfo) {
await db.prepare("run", `insert into "categoryVotes" ("UUID", "category", "votes") values (?, ?, ?)`, [UUID, videoInfo.category, currentCategoryCount]); 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]);
await privateDB.prepare("run", `insert into "categoryVotes" ("UUID", "userID", "hashedIP", "category", "timeSubmitted") values (?, ?, ?, ?, ?)`, [UUID, submissionInfo.userID, "unknown", videoInfo.category, submissionInfo.timeSubmitted]);
} }
const nextCategoryCount = (nextCategoryInfo?.votes || 0) + voteAmount; const nextCategoryCount = (nextCategoryInfo?.votes || 0) + voteAmount;
//TODO: In the future, raise this number from zero to make it harder to change categories //TODO: In the future, raise this number from zero to make it harder to change categories
// VIPs change it every time // 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 // Replace the category
await db.prepare("run", `update "sponsorTimes" set "category" = ? where "UUID" = ?`, [category, UUID]); await db.prepare("run", `update "sponsorTimes" set "category" = ? where "UUID" = ?`, [category, UUID]);
} }
} }
QueryCacher.clearSegmentCache(segmentInfo);
QueryCacher.clearSegmentCache(videoInfo);
return { status: finalResponse.finalStatus }; return { status: finalResponse.finalStatus };
} }
@@ -309,12 +328,12 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise<Re
} }
export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID, type: number, category?: Category): Promise<{ status: number, message?: string, json?: unknown }> { export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID, type: number, category?: Category): Promise<{ status: number, message?: string, json?: unknown }> {
if (UUID === undefined || paramUserID === undefined || (type === undefined && category === undefined)) { // missing key parameters
//invalid request if (!UUID || !paramUserID || !(type !== undefined || category)) {
return { status: 400 }; return { status: 400 };
} }
if (paramUserID.length < 30 && config.mode !== "test") {
// Ignore this vote, invalid // Ignore this vote, invalid
if (paramUserID.length < 30 && config.mode !== "test") {
return { status: 200 }; return { status: 200 };
} }
@@ -334,45 +353,44 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
//hash the ip 5000 times so no one can get it from the database //hash the ip 5000 times so no one can get it from the database
const hashedIP: HashedIP = await getHashCache((ip + config.globalSalt) as IPAddress); const hashedIP: HashedIP = await getHashCache((ip + config.globalSalt) as IPAddress);
//check if this user is on the vip list const segmentInfo: DBSegment = await db.prepare("get", `SELECT * from "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
const videoID = await db.prepare("get", `select "videoID" from "sponsorTimes" where "UUID" = ?`, [UUID]); // segment doesnt exist
const isTempVIP = await isUserTempVIP(nonAnonUserID, videoID?.videoID || null); if (!segmentInfo) {
const isVIP = await isUserVIP(nonAnonUserID) || isTempVIP; return { status: 404 };
}
const isTempVIP = await isUserTempVIP(nonAnonUserID, segmentInfo.videoID);
const isVIP = await isUserVIP(nonAnonUserID);
//check if user voting on own submission //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 // disallow vote types 10/11
if (type === 10 || type === 11) { if (type === 10 || type === 11) {
// no longer allow type 10/11 alternative votes
return { status: 400 }; 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 not upvote
if (!isVIP && type != 1) { if (!isVIP && type != 1) {
const isSegmentLocked = async () => !!(await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]))?.locked; const isSegmentLocked = segmentInfo.locked;
const isVideoLocked = async () => !!(await db.prepare("get", `SELECT "lockCategories".category from "lockCategories" left join "sponsorTimes" const isVideoLocked = async () => !!(await db.prepare("get", `SELECT "category" FROM "lockCategories" WHERE
on ("lockCategories"."videoID" = "sponsorTimes"."videoID" and "videoID" = ? AND "service" = ? AND "category" = ? AND "actionType" = ?`,
"lockCategories"."service" = "sponsorTimes"."service" and "lockCategories".category = "sponsorTimes".category) [segmentInfo.videoID, segmentInfo.service, segmentInfo.category, segmentInfo.actionType]));
where "UUID" = ?`, [UUID])); if (isSegmentLocked || await isVideoLocked()) {
if (await isSegmentLocked() || await isVideoLocked()) {
finalResponse.blockVote = true; finalResponse.blockVote = true;
finalResponse.webhookType = VoteWebhookType.Rejected; finalResponse.webhookType = VoteWebhookType.Rejected;
finalResponse.webhookMessage = "Vote rejected: A moderator has decided that this segment is correct"; finalResponse.webhookMessage = "Vote rejected: A moderator has decided that this segment is correct";
} }
} }
if (type === undefined && category !== undefined) { // if on downvoted non-full segment and is not VIP/ tempVIP/ submitter
return categoryVote(UUID, nonAnonUserID, isVIP, isOwnSubmission, category, hashedIP, finalResponse); if (!isNaN(type) && segmentInfo.votes <= -2 && segmentInfo.actionType !== ActionType.Full &&
} !(isVIP || isTempVIP || isOwnSubmission)) {
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) { if (type == 1) {
return { status: 403, message: "Not allowed to upvote segment with too many downvotes unless you are VIP." }; return { status: 403, message: "Not allowed to upvote segment with too many downvotes unless you are VIP." };
} else if (type == 0) { } else if (type == 0) {
@@ -380,7 +398,6 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
return { status: 200 }; return { status: 200 };
} }
} }
}
const MILLISECONDS_IN_HOUR = 3600000; const MILLISECONDS_IN_HOUR = 3600000;
const now = Date.now(); const now = Date.now();
@@ -389,18 +406,26 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
)); ));
if (warnings.length >= config.maxNumberOfActiveWarnings) { 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. " + 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?" + "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; 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 { 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]); 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 incrementAmount = 0;
let oldIncrementAmount = 0; let oldIncrementAmount = 0;
@@ -417,7 +442,7 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
//unrecongnised type of vote //unrecongnised type of vote
return { status: 400 }; return { status: 400 };
} }
if (votesRow != undefined) { if (votesRow) {
if (votesRow.type === 1) { if (votesRow.type === 1) {
//upvote //upvote
oldIncrementAmount = 1; oldIncrementAmount = 1;
@@ -443,64 +468,51 @@ 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) // 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 // user is temp/ VIP/ own submission and downvoting
{videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, votes: number, views: number, userID: UserID, hidden: Visibility}; if ((isVIP || isTempVIP || isOwnSubmission) && incrementAmount < 0) {
incrementAmount = -(segmentInfo.votes + 2 - oldIncrementAmount);
if (voteTypeEnum === voteTypes.normal) {
if ((isVIP || isOwnSubmission) && incrementAmount < 0) {
//this user is a vip and a downvote
incrementAmount = -(videoInfo.votes + 2 - oldIncrementAmount);
type = incrementAmount; 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;
}
}
// Only change the database if they have made a submission before and haven't voted recently // Only change the database if they have made a submission before and haven't voted recently
const ableToVote = isVIP const userAbleToVote = (!(isOwnSubmission && incrementAmount > 0 && oldIncrementAmount >= 0)
|| (!(isOwnSubmission && incrementAmount > 0 && oldIncrementAmount >= 0)
&& (await db.prepare("get", `SELECT "userID" FROM "sponsorTimes" WHERE "userID" = ?`, [nonAnonUserID])) !== undefined && (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 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) && (await privateDB.prepare("get", `SELECT "UUID" FROM "votes" WHERE "UUID" = ? AND "hashedIP" = ? AND "userID" != ?`, [UUID, hashedIP, userID])) === undefined)
&& !finalResponse.blockVote && !finalResponse.blockVote
&& finalResponse.finalStatus === 200; && finalResponse.finalStatus === 200;
const ableToVote = isVIP || isTempVIP || userAbleToVote;
if (ableToVote) { if (ableToVote) {
//update the votes table //update the votes table
if (votesRow != undefined) { if (votesRow) {
await privateDB.prepare("run", `UPDATE "votes" SET "type" = ? WHERE "userID" = ? AND "UUID" = ?`, [type, userID, UUID]); await privateDB.prepare("run", `UPDATE "votes" SET "type" = ? WHERE "userID" = ? AND "UUID" = ?`, [type, userID, UUID]);
} else { } else {
await privateDB.prepare("run", `INSERT INTO "votes" VALUES(?, ?, ?, ?)`, [UUID, userID, hashedIP, type]); 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 // update the vote count on this sponsorTime
//oldIncrementAmount will be zero is row is null await db.prepare("run", `UPDATE "sponsorTimes" SET "votes" = "votes" + ? 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) {
// 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 // tempVIP can bring back hidden segments
if (videoInfo.hidden) await db.prepare("run", 'UPDATE "sponsorTimes" SET "videoDuration" = 0 WHERE "UUID" = ?', [UUID]); 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) {
// 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) { } 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) { if (incrementAmount - oldIncrementAmount !== 0) {
sendWebhooks({ sendWebhooks({
@@ -510,7 +522,7 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
isTempVIP, isTempVIP,
isVIP, isVIP,
isOwnSubmission, isOwnSubmission,
row: videoInfo, row: segmentInfo,
category, category,
incrementAmount, incrementAmount,
oldIncrementAmount, oldIncrementAmount,

View File

@@ -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", "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", "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", "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", "mute", "sponsor", "reason-5", "YouTube"]);
await db.prepare("run", insertLockCategoryQuery, [lockVIPUserHash, "delete-record-1", "skip", "intro", "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 = { const expected = {
submitted: [ submitted: [
"outro", "outro",
"shilling", "intro"
], ],
submittedValues: [ submittedValues: [
{ {
@@ -87,14 +88,6 @@ describe("lockCategoriesRecords", () => {
actionType: "mute", actionType: "mute",
category: "outro" category: "outro"
}, },
{
actionType: "skip",
category: "shilling"
},
{
actionType: "mute",
category: "shilling"
},
{ {
actionType: "skip", actionType: "skip",
category: "intro" category: "intro"
@@ -132,14 +125,14 @@ describe("lockCategoriesRecords", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID); const result = await checkLockCategories(videoID);
assert.strictEqual(result.length, 8); assert.strictEqual(result.length, 6);
const oldRecordNotChangeReason = result.filter(item => const oldRecordNotChangeReason = result.filter(item =>
item.reason === "reason-2" && ["sponsor", "intro"].includes(item.category) item.reason === "reason-2" && ["sponsor", "intro"].includes(item.category)
); );
const newRecordWithEmptyReason = result.filter(item => 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); assert.strictEqual(oldRecordNotChangeReason.length, 4);
done(); done();
}) })
@@ -164,7 +157,6 @@ describe("lockCategoriesRecords", () => {
const expected = { const expected = {
submitted: [ submitted: [
"outro", "outro",
"shilling",
"intro" "intro"
], ],
}; };
@@ -196,7 +188,6 @@ describe("lockCategoriesRecords", () => {
const expectedWithNewReason = [ const expectedWithNewReason = [
"outro", "outro",
"shilling",
"intro" "intro"
]; ];
@@ -204,7 +195,7 @@ describe("lockCategoriesRecords", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID); const result = await checkLockCategories(videoID);
assert.strictEqual(result.length, 8); assert.strictEqual(result.length, 6);
const newRecordWithNewReason = result.filter(item => const newRecordWithNewReason = result.filter(item =>
expectedWithNewReason.includes(item.category) && item.reason === "new reason" expectedWithNewReason.includes(item.category) && item.reason === "new reason"
); );
@@ -212,7 +203,7 @@ describe("lockCategoriesRecords", () => {
item.reason === "reason-2" item.reason === "reason-2"
); );
assert.strictEqual(newRecordWithNewReason.length, 6); assert.strictEqual(newRecordWithNewReason.length, 4);
assert.strictEqual(oldRecordNotChangeReason.length, 2); assert.strictEqual(oldRecordNotChangeReason.length, 2);
done(); done();
}) })
@@ -220,56 +211,20 @@ describe("lockCategoriesRecords", () => {
}); });
it("Should be able to submit categories with _ in the category", (done) => { it("Should be able to submit categories with _ in the category", (done) => {
const json = { const videoID = "underscore";
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 json = { const json = {
videoID, videoID,
userID: lockVIPUser, userID: lockVIPUser,
categories: [ categories: [
"word&word", "exclusive_access",
], ],
actionTypes: ["full"]
}; };
client.post(endpoint, json) client.post(endpoint, json)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID); const result = await checkLockCategories(videoID);
assert.strictEqual(result.length, 0); assert.strictEqual(result.length, 1);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@@ -418,13 +373,14 @@ describe("lockCategoriesRecords", () => {
categories: [ categories: [
"sponsor", "sponsor",
], ],
actionTypes: ["skip", "mute"]
}; };
client.delete(endpoint, { data: json }) client.delete(endpoint, { data: json })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID); const result = await checkLockCategories(videoID);
assert.strictEqual(result.length, 0); assert.strictEqual(result.length, 1);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@@ -438,6 +394,7 @@ describe("lockCategoriesRecords", () => {
categories: [ categories: [
"sponsor", "sponsor",
], ],
actionTypes: ["skip", "mute"]
}; };
client.delete(endpoint, { data: json }) client.delete(endpoint, { data: json })
@@ -531,7 +488,6 @@ describe("lockCategoriesRecords", () => {
"sponsor", "sponsor",
"intro", "intro",
"outro", "outro",
"shilling"
], ],
}; };
client.get(endpoint, { params: { videoID: "no-segments-video-id" } }) client.get(endpoint, { params: { videoID: "no-segments-video-id" } })
@@ -543,4 +499,24 @@ describe("lockCategoriesRecords", () => {
}) })
.catch(err => done(err)); .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));
});
}); });

View File

@@ -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, ["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", 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 (?)', [publicPermVIP1]);
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [publicPermVIP2]); await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [publicPermVIP2]);
// clear redis if running consecutive tests // clear redis if running consecutive tests
@@ -124,24 +123,24 @@ describe("tempVIP test", function() {
}) })
.catch(err => done(err)); .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) postVote(tempVIPOne, UUID0, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegment(UUID0); const row = await getSegment(UUID0);
assert.ok(row.votes > -2); assert.strictEqual(row.votes, 1);
assert.strictEqual(row.locked, 1); assert.strictEqual(row.locked, 0);
done(); done();
}) })
.catch(err => done(err)); .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") postVoteCategory(tempVIPOne, UUID0, "filler")
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegment(UUID0); const row = await getSegment(UUID0);
assert.strictEqual(row.category, "filler"); assert.strictEqual(row.category, "filler");
assert.strictEqual(row.locked, 1); assert.strictEqual(row.locked, 0);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View File

@@ -6,6 +6,7 @@ import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
import { YouTubeApiMock } from "../youtubeMock"; import { YouTubeApiMock } from "../youtubeMock";
import assert from "assert"; import assert from "assert";
import { client } from "../utils/httpClient"; import { client } from "../utils/httpClient";
import { arrayDeepEquals } from "../utils/partialDeepEquals";
const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI"); const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI");
const sinonStub = mockManager.mock("listVideos"); 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, ["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-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-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-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, ["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]); 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, ["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, ["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]); 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(?, ?, ?, ?)'; 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]);
@@ -73,9 +81,9 @@ describe("voteOnSponsorTime", () => {
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(vipUser)]); await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(vipUser)]);
await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]); await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]);
const insertlockCategoriesQuerry = 'INSERT INTO "lockCategories" ("videoID", "userID", "category", "reason") VALUES (?, ?, ?, ?)'; const insertlockCategoriesQuery = 'INSERT INTO "lockCategories" ("videoID", "userID", "category", "actionType") VALUES (?, ?, ?, ?)';
await db.prepare("run", insertlockCategoriesQuerry, ["no-sponsor-segments-video", "someUser", "sponsor", ""]); await db.prepare("run", insertlockCategoriesQuery, ["no-sponsor-segments-video", "someUser", "sponsor", "skip"]);
await db.prepare("run", insertlockCategoriesQuerry, ["category-change-test-1", "someUser", "preview", ""]); // sponsor should stay unlocked await db.prepare("run", insertlockCategoriesQuery, ["category-change-test-1", "someUser", "preview", "skip"]); // sponsor should stay unlocked
}); });
// constants // constants
const endpoint = "/api/voteOnSponsorTime"; const endpoint = "/api/voteOnSponsorTime";
@@ -409,15 +417,15 @@ describe("voteOnSponsorTime", () => {
const UUID = "invalid-uuid"; const UUID = "invalid-uuid";
postVoteCategory("randomID3", UUID, "intro") postVoteCategory("randomID3", UUID, "intro")
.then(res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 404);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should not be able to category-vote on an a full video segment", (done) => { it("Should not be able to category-vote on a full video segment", (done) => {
const UUID = "invalid-uuid"; const UUID = "full-video-uuid-1";
postVoteCategory("full-video-uuid-1", UUID, "selfpromo") postVoteCategory("randomID3", UUID, "selfpromo")
.then(res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
@@ -585,4 +593,33 @@ describe("voteOnSponsorTime", () => {
done(); 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();
});
});
}); });

View File

@@ -56,6 +56,13 @@ export class YouTubeApiMock {
authorId: "ChannelID" authorId: "ChannelID"
} as APIVideoData } as APIVideoData
}; };
} else if (obj.id === "duration-changed") {
return {
err: null,
data: {
lengthSeconds: 100,
} as APIVideoData
};
} else { } else {
return { return {
err: null, err: null,