mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 19:47:00 +03:00
fix logic in getLockReason
This commit is contained in:
@@ -4,6 +4,13 @@ import {Request, Response} from "express";
|
|||||||
import { Category, VideoID } from "../types/segments.model";
|
import { Category, VideoID } from "../types/segments.model";
|
||||||
import {config} from "../config";
|
import {config} from "../config";
|
||||||
|
|
||||||
|
const possibleCategoryList = config.categoryList;
|
||||||
|
interface lockArray {
|
||||||
|
category: Category;
|
||||||
|
locked: number,
|
||||||
|
reason: string
|
||||||
|
}
|
||||||
|
|
||||||
export async function getLockCategories(req: Request, res: Response): Promise<Response> {
|
export async function getLockCategories(req: Request, res: Response): Promise<Response> {
|
||||||
const videoID = req.query.videoID as VideoID;
|
const videoID = req.query.videoID as VideoID;
|
||||||
let categories: Category[] = [];
|
let categories: Category[] = [];
|
||||||
@@ -14,13 +21,15 @@ export async function getLockCategories(req: Request, res: Response): Promise<Re
|
|||||||
? Array.isArray(req.query.category)
|
? Array.isArray(req.query.category)
|
||||||
? req.query.category
|
? req.query.category
|
||||||
: [req.query.category]
|
: [req.query.category]
|
||||||
: ["sponsor"];
|
: []; // default to empty, will be set to all
|
||||||
if (!Array.isArray(categories)) {
|
if (!Array.isArray(categories)) {
|
||||||
return res.status(400).send("Categories parameter does not match format requirements.");
|
return res.status(400).send("Categories parameter does not match format requirements.");
|
||||||
}
|
}
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
return res.status(400).send("Bad parameter: categories (invalid JSON)");
|
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));
|
||||||
|
|
||||||
if (videoID == undefined) {
|
if (videoID == undefined) {
|
||||||
//invalid request
|
//invalid request
|
||||||
@@ -32,23 +41,28 @@ export async function getLockCategories(req: Request, res: Response): Promise<Re
|
|||||||
const row = await db.prepare("all", 'SELECT "category", "reason" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string}[];
|
const row = await db.prepare("all", 'SELECT "category", "reason" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string}[];
|
||||||
// map to object array
|
// map to object array
|
||||||
const locks = [];
|
const locks = [];
|
||||||
|
const lockedCategories = [] as string[];
|
||||||
|
// get all locks for video, check if requested later
|
||||||
for (const lock of row) {
|
for (const lock of row) {
|
||||||
locks.push({
|
locks.push({
|
||||||
category: lock.category,
|
category: lock.category,
|
||||||
locked: 1,
|
locked: 1,
|
||||||
reason: lock.reason
|
reason: lock.reason
|
||||||
});
|
} as lockArray);
|
||||||
|
lockedCategories.push(lock.category);
|
||||||
}
|
}
|
||||||
const notLocked = categories.filter(reqCategory => config.categoryList.includes(reqCategory));
|
// add empty locks for categories requested but not locked
|
||||||
for (const noLock of notLocked) {
|
const noLockCategories = searchCategories.filter(x => !lockedCategories.includes(x));
|
||||||
|
for (const noLock of noLockCategories) {
|
||||||
locks.push({
|
locks.push({
|
||||||
category: noLock,
|
category: noLock,
|
||||||
locked: 0,
|
locked: 0,
|
||||||
reason: ""
|
reason: ""
|
||||||
});
|
} as lockArray);
|
||||||
}
|
}
|
||||||
const filtered = locks.filter(lock => categories.includes(lock.category));
|
// return real and fake locks that were requested
|
||||||
return res.send(...filtered, ...notLocked );
|
const filtered = locks.filter(lock => searchCategories.includes(lock.category));
|
||||||
|
return res.send(filtered);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(err as string);
|
Logger.error(err as string);
|
||||||
return res.sendStatus(500);
|
return res.sendStatus(500);
|
||||||
|
|||||||
Reference in New Issue
Block a user