mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-25 17:08:35 +03:00
Merge branch 'master' of https://github.com/ajayyy/SponsorBlockServer
This commit is contained in:
@@ -32,7 +32,8 @@ const getChannelInfo = async (videoID: VideoID): Promise<{id: string | null, nam
|
||||
};
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
@@ -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<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]
|
||||
))
|
||||
);
|
||||
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(`','`)}'`;
|
||||
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]);
|
||||
}
|
||||
|
||||
@@ -7,8 +7,13 @@ import { getService } from "../utils/getService";
|
||||
export async function getLockCategories(req: Request, res: Response): Promise<Response> {
|
||||
const videoID = req.query.videoID as VideoID;
|
||||
const service = getService(req.query.service as string);
|
||||
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Skip, ActionType.Mute];
|
||||
|
||||
const actionTypes: ActionType[] = req.query.actionTypes
|
||||
? JSON.parse(req.query.actionTypes as string)
|
||||
: req.query.actionType
|
||||
? Array.isArray(req.query.actionType)
|
||||
? req.query.actionType
|
||||
: [req.query.actionType]
|
||||
: [ActionType.Skip, ActionType.Mute];
|
||||
if (!videoID || !Array.isArray(actionTypes)) {
|
||||
//invalid request
|
||||
return res.sendStatus(400);
|
||||
|
||||
@@ -44,7 +44,13 @@ const mergeLocks = (source: DBLock[], actionTypes: ActionType[]): LockResultByHa
|
||||
|
||||
export async function getLockCategoriesByHash(req: Request, res: Response): Promise<Response> {
|
||||
let hashPrefix = req.params.prefix as VideoIDHash;
|
||||
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Mute, ActionType.Skip];
|
||||
const actionTypes: ActionType[] = req.query.actionTypes
|
||||
? JSON.parse(req.query.actionTypes as string)
|
||||
: req.query.actionType
|
||||
? Array.isArray(req.query.actionType)
|
||||
? req.query.actionType
|
||||
: [req.query.actionType]
|
||||
: [ActionType.Skip, ActionType.Mute];
|
||||
if (!hashPrefixTester(req.params.prefix)) {
|
||||
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
||||
}
|
||||
|
||||
@@ -27,9 +27,23 @@ const filterActionType = (actionTypes: ActionType[]) => {
|
||||
|
||||
export async function getLockReason(req: Request, res: Response): Promise<Response> {
|
||||
const videoID = req.query.videoID as VideoID;
|
||||
if (!videoID) {
|
||||
// invalid request
|
||||
return res.status(400).send("No videoID provided");
|
||||
}
|
||||
let categories: Category[] = [];
|
||||
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Skip, ActionType.Mute];
|
||||
const actionTypes: ActionType[] = req.query.actionTypes
|
||||
? JSON.parse(req.query.actionTypes as string)
|
||||
: req.query.actionType
|
||||
? Array.isArray(req.query.actionType)
|
||||
? req.query.actionType
|
||||
: [req.query.actionType]
|
||||
: [ActionType.Skip, ActionType.Mute];
|
||||
const possibleCategories = filterActionType(actionTypes);
|
||||
if (!Array.isArray(actionTypes)) {
|
||||
//invalid request
|
||||
return res.status(400).send("actionTypes parameter does not match format requirements");
|
||||
}
|
||||
try {
|
||||
categories = req.query.categories
|
||||
? JSON.parse(req.query.categories as string)
|
||||
|
||||
@@ -25,7 +25,9 @@ async function generateTopUsersStats(sortBy: string, categoryStatsEnabled = fals
|
||||
SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as "categorySumMusicOfftopic",
|
||||
SUM(CASE WHEN category = 'preview' THEN 1 ELSE 0 END) as "categorySumPreview",
|
||||
SUM(CASE WHEN category = 'poi_highlight' THEN 1 ELSE 0 END) as "categorySumHighlight",
|
||||
SUM(CASE WHEN category = 'filler' THEN 1 ELSE 0 END) as "categorySumFiller",`;
|
||||
SUM(CASE WHEN category = 'filler' THEN 1 ELSE 0 END) as "categorySumFiller",
|
||||
SUM(CASE WHEN category = 'exclusive_access' THEN 1 ELSE 0 END) as "categorySumExclusiveAccess",
|
||||
`;
|
||||
}
|
||||
|
||||
const rows = await db.prepare("all", `SELECT COUNT(*) as "totalSubmissions", SUM(views) as "viewCount",
|
||||
@@ -52,6 +54,7 @@ async function generateTopUsersStats(sortBy: string, categoryStatsEnabled = fals
|
||||
row.categorySumPreview,
|
||||
row.categorySumHighlight,
|
||||
row.categorySumFiller,
|
||||
row.categorySumExclusiveAccess
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,17 @@ async function dbGetUserSummary(userID: HashedUserID, fetchCategoryStats: boolea
|
||||
SUM(CASE WHEN "category" = 'music_offtopic' THEN 1 ELSE 0 END) as "categorySumMusicOfftopic",
|
||||
SUM(CASE WHEN "category" = 'preview' THEN 1 ELSE 0 END) as "categorySumPreview",
|
||||
SUM(CASE WHEN "category" = 'poi_highlight' THEN 1 ELSE 0 END) as "categorySumHighlight",
|
||||
SUM(CASE WHEN "category" = 'filler' THEN 1 ELSE 0 END) as "categorySumFiller",`;
|
||||
SUM(CASE WHEN "category" = 'filler' THEN 1 ELSE 0 END) as "categorySumFiller",
|
||||
SUM(CASE WHEN "category" = 'exclusive_access' THEN 1 ELSE 0 END) as "categorySumExclusiveAccess",
|
||||
`;
|
||||
}
|
||||
if (fetchActionTypeStats) {
|
||||
additionalQuery += `
|
||||
SUM(CASE WHEN "actionType" = 'skip' THEN 1 ELSE 0 END) as "typeSumSkip",
|
||||
SUM(CASE WHEN "actionType" = 'mute' THEN 1 ELSE 0 END) as "typeSumMute",`;
|
||||
SUM(CASE WHEN "actionType" = 'mute' THEN 1 ELSE 0 END) as "typeSumMute",
|
||||
SUM(CASE WHEN "actionType" = 'full' THEN 1 ELSE 0 END) as "typeSumFull",
|
||||
SUM(CASE WHEN "actionType" = 'poi' THEN 1 ELSE 0 END) as "typeSumPoi",
|
||||
`;
|
||||
}
|
||||
try {
|
||||
const row = await db.prepare("get", `
|
||||
@@ -54,12 +59,15 @@ async function dbGetUserSummary(userID: HashedUserID, fetchCategoryStats: boolea
|
||||
preview: proxy.categorySumPreview,
|
||||
poi_highlight: proxy.categorySumHighlight,
|
||||
filler: proxy.categorySumFiller,
|
||||
exclusive_access: proxy.categorySumExclusiveAccess,
|
||||
};
|
||||
}
|
||||
if (fetchActionTypeStats) {
|
||||
result.actionTypeCount = {
|
||||
skip: proxy.typeSumSkip,
|
||||
mute: proxy.typeSumMute,
|
||||
full: proxy.typeSumFull,
|
||||
poi: proxy.typeSumPoi
|
||||
};
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -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<string[]> {
|
||||
// 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
|
||||
{ category: Category, actionType: ActionType }[];
|
||||
|
||||
const filteredCategories = filterData(categories);
|
||||
const filteredActionTypes = filterData(actionTypes);
|
||||
|
||||
const locksToApply: { category: Category, actionType: ActionType }[] = [];
|
||||
const overwrittenLocks: { category: Category, actionType: ActionType }[] = [];
|
||||
for (const category of filteredCategories) {
|
||||
for (const actionType of filteredActionTypes) {
|
||||
if (!existingLocks.some((lock) => 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<s
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
submitted: reason.length === 0
|
||||
? [...filteredCategories.filter(((category) => locksToApply.some((lock) => category === lock.category)))]
|
||||
: [...filteredCategories], // Legacy
|
||||
submittedValues: [...locksToApply, ...overwrittenLocks],
|
||||
submitted: deDupArray(validLocks.map(e => e.category)),
|
||||
submittedValues: validLocks,
|
||||
});
|
||||
}
|
||||
|
||||
function filterData<T extends string>(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)];
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<APIVideoInfo> {
|
||||
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<boolean> => {
|
||||
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 || category === "poi_highlight") {
|
||||
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 || category === "poi_highlight") {
|
||||
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<Re
|
||||
}
|
||||
|
||||
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)) {
|
||||
//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,
|
||||
|
||||
Reference in New Issue
Block a user