mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-25 08:58:23 +03:00
update lockCategories
- migration to remove invalid locks - lockCategories poi_highlight is now actionType poi - deleteLockCategories now takes actionType - update postLockCategories response, serverside filtering for accepted categories - fix tests accordingly
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -106,8 +106,11 @@ async function checkVideoDuration(UUID: SegmentUUID) {
|
||||
|
||||
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 submissionTime < ?`,
|
||||
[videoID, service, latestSubmission.submissionTime]);
|
||||
await db.prepare("run", `UPDATE "sponsorTimes" SET "hidden" = 1
|
||||
WHERE videoID = ? AND service = ? AND submissionTime < ?
|
||||
hidden" = 0 AND "shadowHidden" = 0 AND
|
||||
"actionType" != 'full' AND "votes" > -2`,
|
||||
[videoID, service, latestSubmission.submissionTime]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,13 +234,12 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i
|
||||
if (segmentInfo.actionType === ActionType.Full) {
|
||||
return { status: 400, message: "Not allowed to change category of a full video segment" };
|
||||
}
|
||||
|
||||
if (!config.categoryList.includes(category)) {
|
||||
return { status: 400, message: "Category doesn't exist." };
|
||||
}
|
||||
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." };
|
||||
}
|
||||
|
||||
// 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" = ?`, [segmentInfo.videoID, segmentInfo.service, category]);
|
||||
@@ -417,6 +419,12 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
|
||||
|
||||
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
|
||||
const votesRow = await privateDB.prepare("get", `SELECT "type" FROM "votes" WHERE "userID" = ? AND "UUID" = ?`, [userID, UUID]);
|
||||
@@ -496,11 +504,6 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
|
||||
// update the vote count on this sponsorTime
|
||||
await db.prepare("run", `UPDATE "sponsorTimes" SET "votes" = "votes" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]);
|
||||
|
||||
// check duration of all submissions on this video
|
||||
if (type < 0) {
|
||||
checkVideoDuration(UUID);
|
||||
}
|
||||
|
||||
// additional procesing for VIP
|
||||
// on VIP upvote
|
||||
if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) {
|
||||
|
||||
Reference in New Issue
Block a user