mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-09 13:07:02 +03:00
update getLockReason
This commit is contained in:
@@ -13,7 +13,6 @@ export async function getLockCategories(req: Request, res: Response): Promise<Re
|
|||||||
//invalid request
|
//invalid request
|
||||||
return res.sendStatus(400);
|
return res.sendStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get existing lock categories markers
|
// Get existing lock categories markers
|
||||||
const row = await db.prepare("all", 'SELECT "category", "reason", "actionType" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service]) as {category: Category, reason: string, actionType: ActionType}[];
|
const row = await db.prepare("all", 'SELECT "category", "reason", "actionType" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service]) as {category: Category, reason: string, actionType: ActionType}[];
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { db } from "../databases/databases";
|
import { db } from "../databases/databases";
|
||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { Category, VideoID } from "../types/segments.model";
|
import { Category, VideoID, ActionType } from "../types/segments.model";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
|
|
||||||
const possibleCategoryList = config.categoryList;
|
const categorySupportList = config.categorySupport;
|
||||||
interface lockArray {
|
interface lockArray {
|
||||||
category: Category;
|
category: Category;
|
||||||
locked: number,
|
locked: number,
|
||||||
@@ -13,9 +13,23 @@ interface lockArray {
|
|||||||
userName: string,
|
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> {
|
export async function getLockReason(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[] = [];
|
||||||
|
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Skip, ActionType.Mute];
|
||||||
|
const possibleCategories = filterActionType(actionTypes);
|
||||||
try {
|
try {
|
||||||
categories = req.query.categories
|
categories = req.query.categories
|
||||||
? JSON.parse(req.query.categories as string)
|
? 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)");
|
return res.status(400).send("Bad parameter: categories (invalid JSON)");
|
||||||
}
|
}
|
||||||
// only take valid categories
|
// 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
|
//invalid request
|
||||||
return res.sendStatus(400);
|
return res.sendStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get existing lock categories markers
|
// 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
|
// map to object array
|
||||||
const locks = [];
|
const locks = [];
|
||||||
const userIDs = new Set();
|
const userIDs = new Set();
|
||||||
// get all locks for video, check if requested later
|
// get all locks for video, check if requested later
|
||||||
for (const lock of row) {
|
for (const lock of row) {
|
||||||
locks.push({
|
if (actionTypes.includes(lock.actionType))
|
||||||
category: lock.category,
|
locks.push({
|
||||||
locked: 1,
|
category: lock.category,
|
||||||
reason: lock.reason,
|
locked: 1,
|
||||||
userID: lock?.userID || "",
|
reason: lock.reason,
|
||||||
userName: "",
|
userID: lock?.userID || "",
|
||||||
} as lockArray);
|
userName: "",
|
||||||
|
} as lockArray);
|
||||||
userIDs.add(lock.userID);
|
userIDs.add(lock.userID);
|
||||||
}
|
}
|
||||||
// all userName from userIDs
|
// all userName from userIDs
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export interface SBSConfig {
|
|||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
webhooks: WebhookConfig[];
|
webhooks: WebhookConfig[];
|
||||||
categoryList: string[];
|
categoryList: string[];
|
||||||
|
categorySupport: Record<string, string[]>;
|
||||||
getTopUsersCacheTimeMinutes: number;
|
getTopUsersCacheTimeMinutes: number;
|
||||||
maxNumberOfActiveWarnings: number;
|
maxNumberOfActiveWarnings: number;
|
||||||
hoursAfterWarningExpires: number;
|
hoursAfterWarningExpires: number;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { HashedValue } from "./hash.model";
|
import { HashedValue } from "./hash.model";
|
||||||
import { SBRecord } from "./lib.model";
|
import { SBRecord } from "./lib.model";
|
||||||
import { UserID } from "./user.model";
|
import { HashedUserID, UserID } from "./user.model";
|
||||||
|
|
||||||
export type SegmentUUID = string & { __segmentUUIDBrand: unknown };
|
export type SegmentUUID = string & { __segmentUUIDBrand: unknown };
|
||||||
export type VideoID = string & { __videoIDBrand: unknown };
|
export type VideoID = string & { __videoIDBrand: unknown };
|
||||||
@@ -107,11 +107,14 @@ export enum CategoryActionType {
|
|||||||
POI
|
POI
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LockCategory {
|
export interface DBLock {
|
||||||
category: Category,
|
|
||||||
reason: string,
|
|
||||||
videoID: VideoID,
|
videoID: VideoID,
|
||||||
userID: UserID
|
userID: HashedUserID,
|
||||||
|
actionType: ActionType,
|
||||||
|
category: Category,
|
||||||
|
hashedVideoID: VideoIDHash,
|
||||||
|
reason: string,
|
||||||
|
service: Service,
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum SortableFields {
|
export enum SortableFields {
|
||||||
|
|||||||
@@ -171,7 +171,7 @@ describe("getLockCategories", () => {
|
|||||||
reason: "1-longer-reason",
|
reason: "1-longer-reason",
|
||||||
actionTypes: ["mute"]
|
actionTypes: ["mute"]
|
||||||
};
|
};
|
||||||
assert.deepStrictEqual(res.data, expected);
|
mixedDeepEquals(res.data, expected);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
@@ -190,7 +190,7 @@ describe("getLockCategories", () => {
|
|||||||
reason: "3-longer-reason",
|
reason: "3-longer-reason",
|
||||||
actionTypes
|
actionTypes
|
||||||
};
|
};
|
||||||
assert.deepStrictEqual(res.data, expected);
|
mixedDeepEquals(res.data, expected);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { getHash } from "../../src/utils/getHash";
|
|||||||
import { db } from "../../src/databases/databases";
|
import { db } from "../../src/databases/databases";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
|
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||||
|
|
||||||
const endpoint = "/api/lockReason";
|
const endpoint = "/api/lockReason";
|
||||||
|
|
||||||
@@ -20,12 +21,13 @@ describe("getLockReason", () => {
|
|||||||
await db.prepare("run", insertVipUserNameQuery, [vipUserID1, vipUserName1]);
|
await db.prepare("run", insertVipUserNameQuery, [vipUserID1, vipUserName1]);
|
||||||
await db.prepare("run", insertVipUserNameQuery, [vipUserID2, vipUserName2]);
|
await db.prepare("run", insertVipUserNameQuery, [vipUserID2, vipUserName2]);
|
||||||
|
|
||||||
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") VALUES (?, ?, ?, ?)';
|
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType", "category", "reason") VALUES (?, ?, ?, ?, ?)';
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "sponsor", "sponsor-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "sponsor", "sponsor-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "interaction", "interaction-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "interaction", "interaction-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "preview", "preview-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "preview", "preview-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "music_offtopic", "nonmusic-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "mute", "music_offtopic", "nonmusic-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "outro", "outro-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "mute", "outro", "outro-reason"]);
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "full", "selfpromo", "selfpromo-reason"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
after(async () => {
|
after(async () => {
|
||||||
@@ -96,7 +98,7 @@ describe("getLockReason", () => {
|
|||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return all categories if none specified", (done) => {
|
it("should return all skip + mute categories if none specified", (done) => {
|
||||||
client.get(endpoint, { params: { videoID: "getLockReason" } })
|
client.get(endpoint, { params: { videoID: "getLockReason" } })
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
@@ -109,10 +111,8 @@ describe("getLockReason", () => {
|
|||||||
{ category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 },
|
{ category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
|
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "filler", locked: 0, reason: "", userID: "", userName: "" },
|
{ category: "filler", locked: 0, reason: "", userID: "", userName: "" },
|
||||||
{ category: "poi_highlight", locked: 0, reason: "", userID: "", userName: "" },
|
|
||||||
{ category: "chapter", locked: 0, reason: "", userID: "", userName: "" }
|
|
||||||
];
|
];
|
||||||
assert.deepStrictEqual(res.data, expected);
|
partialDeepEquals(res.data, expected, false);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
@@ -126,4 +126,18 @@ describe("getLockReason", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should be able to get by actionType", (done) => {
|
||||||
|
client.get(endpoint, { params: { videoID: "getLockReason", actionTypes: ["full"] } })
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const expected = [
|
||||||
|
{ category: "selfpromo", locked: 1, reason: "sponsor-reason", userID: vipUserID2, userName: vipUserName2 },
|
||||||
|
{ category: "sponsor", locked: 0, reason: "", userID: "", userName: "" }
|
||||||
|
];
|
||||||
|
partialDeepEquals(res.data, expected);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { getHash } from "../../src/utils/getHash";
|
import { getHash } from "../../src/utils/getHash";
|
||||||
import { db } from "../../src/databases/databases";
|
import { db } from "../../src/databases/databases";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { LockCategory } from "../../src/types/segments.model";
|
import { UserID } from "../../src/types/user.model";
|
||||||
|
import { Category, VideoID } from "../../src/types/segments.model";
|
||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||||
|
|
||||||
@@ -13,6 +14,13 @@ const stringDeepEquals = (a: string[], b: string[]): boolean => {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface LockCategory {
|
||||||
|
category: Category,
|
||||||
|
reason: string,
|
||||||
|
videoID: VideoID,
|
||||||
|
userID: UserID
|
||||||
|
}
|
||||||
|
|
||||||
const endpoint = "/api/lockCategories";
|
const endpoint = "/api/lockCategories";
|
||||||
const submitEndpoint = "/api/skipSegments";
|
const submitEndpoint = "/api/skipSegments";
|
||||||
const checkLockCategories = (videoID: string): Promise<LockCategory[]> => db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID]);
|
const checkLockCategories = (videoID: string): Promise<LockCategory[]> => db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID]);
|
||||||
|
|||||||
Reference in New Issue
Block a user