mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-17 13:08:49 +03:00
update getLockReason
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { db } from "../databases/databases";
|
||||
import { Logger } from "../utils/logger";
|
||||
import { Request, Response } from "express";
|
||||
import { Category, VideoID } from "../types/segments.model";
|
||||
import { Category, VideoID, ActionType } from "../types/segments.model";
|
||||
import { config } from "../config";
|
||||
|
||||
const possibleCategoryList = config.categoryList;
|
||||
const categorySupportList = config.categorySupport;
|
||||
interface lockArray {
|
||||
category: Category;
|
||||
locked: number,
|
||||
@@ -13,9 +13,23 @@ interface lockArray {
|
||||
userName: string,
|
||||
}
|
||||
|
||||
const filterActionType = (actionTypes: ActionType[]) => {
|
||||
const filterCategories = new Set();
|
||||
for (const [key, value] of Object.entries(categorySupportList)) {
|
||||
for (const type of actionTypes) {
|
||||
if (value.includes(type)) {
|
||||
filterCategories.add(key as Category);
|
||||
}
|
||||
}
|
||||
}
|
||||
return [...filterCategories];
|
||||
};
|
||||
|
||||
export async function getLockReason(req: Request, res: Response): Promise<Response> {
|
||||
const videoID = req.query.videoID as VideoID;
|
||||
let categories: Category[] = [];
|
||||
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Skip, ActionType.Mute];
|
||||
const possibleCategories = filterActionType(actionTypes);
|
||||
try {
|
||||
categories = req.query.categories
|
||||
? JSON.parse(req.query.categories as string)
|
||||
@@ -31,28 +45,32 @@ export async function getLockReason(req: Request, res: Response): Promise<Respon
|
||||
return res.status(400).send("Bad parameter: categories (invalid JSON)");
|
||||
}
|
||||
// only take valid categories
|
||||
const searchCategories = (categories.length === 0 ) ? possibleCategoryList : categories.filter(x => possibleCategoryList.includes(x));
|
||||
const searchCategories = (categories.length === 0 )
|
||||
? possibleCategories
|
||||
: categories.filter(x =>
|
||||
possibleCategories.includes(x));
|
||||
|
||||
if (videoID == undefined) {
|
||||
if (!videoID || !Array.isArray(actionTypes)) {
|
||||
//invalid request
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
|
||||
try {
|
||||
// Get existing lock categories markers
|
||||
const row = await db.prepare("all", 'SELECT "category", "reason", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string, userID: string }[];
|
||||
const row = await db.prepare("all", 'SELECT "category", "reason", "actionType", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string, actionType: ActionType, userID: string }[];
|
||||
// map to object array
|
||||
const locks = [];
|
||||
const userIDs = new Set();
|
||||
// get all locks for video, check if requested later
|
||||
for (const lock of row) {
|
||||
locks.push({
|
||||
category: lock.category,
|
||||
locked: 1,
|
||||
reason: lock.reason,
|
||||
userID: lock?.userID || "",
|
||||
userName: "",
|
||||
} as lockArray);
|
||||
if (actionTypes.includes(lock.actionType))
|
||||
locks.push({
|
||||
category: lock.category,
|
||||
locked: 1,
|
||||
reason: lock.reason,
|
||||
userID: lock?.userID || "",
|
||||
userName: "",
|
||||
} as lockArray);
|
||||
userIDs.add(lock.userID);
|
||||
}
|
||||
// all userName from userIDs
|
||||
|
||||
Reference in New Issue
Block a user