mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-07 03:57:06 +03:00
getLockCategories + lint
This commit is contained in:
@@ -45,7 +45,7 @@ import { youtubeApiProxy } from "./routes/youtubeApiProxy";
|
|||||||
import { getChapterNames } from "./routes/getChapterNames";
|
import { getChapterNames } from "./routes/getChapterNames";
|
||||||
import { postRating } from "./routes/ratings/postRating";
|
import { postRating } from "./routes/ratings/postRating";
|
||||||
import { getRating } from "./routes/ratings/getRating";
|
import { getRating } from "./routes/ratings/getRating";
|
||||||
import { postClearCache as ratingPostClearCache } from "./routes/ratings/postClearCache"
|
import { postClearCache as ratingPostClearCache } from "./routes/ratings/postClearCache";
|
||||||
import { getTopCategoryUsers } from "./routes/getTopCategoryUsers";
|
import { getTopCategoryUsers } from "./routes/getTopCategoryUsers";
|
||||||
import { addUserAsTempVIP } from "./routes/addUserAsTempVIP";
|
import { addUserAsTempVIP } from "./routes/addUserAsTempVIP";
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ 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 { hashPrefixTester } from "../utils/hashPrefixTester";
|
import { hashPrefixTester } from "../utils/hashPrefixTester";
|
||||||
import { Category, VideoID, VideoIDHash } from "../types/segments.model";
|
import { ActionType, Category, VideoID, VideoIDHash } from "../types/segments.model";
|
||||||
|
|
||||||
interface LockResultByHash {
|
interface LockResultByHash {
|
||||||
videoID: VideoID,
|
videoID: VideoID,
|
||||||
@@ -16,11 +16,13 @@ interface DBLock {
|
|||||||
hash: VideoIDHash,
|
hash: VideoIDHash,
|
||||||
category: Category,
|
category: Category,
|
||||||
reason: string,
|
reason: string,
|
||||||
|
actionType: ActionType,
|
||||||
}
|
}
|
||||||
|
|
||||||
const mergeLocks = (source: DBLock[]) => {
|
const mergeLocks = (source: DBLock[], actionTypes: ActionType[]) => {
|
||||||
const dest: LockResultByHash[] = [];
|
const dest: LockResultByHash[] = [];
|
||||||
for (const obj of source) {
|
for (const obj of source) {
|
||||||
|
if (!actionTypes.includes(obj.actionType)) continue;
|
||||||
// videoID already exists
|
// videoID already exists
|
||||||
const destMatch = dest.find(s => s.videoID === obj.videoID);
|
const destMatch = dest.find(s => s.videoID === obj.videoID);
|
||||||
if (destMatch) {
|
if (destMatch) {
|
||||||
@@ -42,6 +44,7 @@ const mergeLocks = (source: DBLock[]) => {
|
|||||||
|
|
||||||
export async function getLockCategoriesByHash(req: Request, res: Response): Promise<Response> {
|
export async function getLockCategoriesByHash(req: Request, res: Response): Promise<Response> {
|
||||||
let hashPrefix = req.params.prefix as VideoIDHash;
|
let hashPrefix = req.params.prefix as VideoIDHash;
|
||||||
|
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Mute, ActionType.Skip];
|
||||||
if (!hashPrefixTester(req.params.prefix)) {
|
if (!hashPrefixTester(req.params.prefix)) {
|
||||||
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
||||||
}
|
}
|
||||||
@@ -49,10 +52,10 @@ export async function getLockCategoriesByHash(req: Request, res: Response): Prom
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Get existing lock categories markers
|
// Get existing lock categories markers
|
||||||
const lockedRows = await db.prepare("all", 'SELECT "videoID", "hashedVideoID" as "hash", "category", "reason" from "lockCategories" where "hashedVideoID" LIKE ?', [`${hashPrefix}%`]) as DBLock[];
|
const lockedRows = await db.prepare("all", 'SELECT "videoID", "hashedVideoID" as "hash", "category", "reason", "actionType" from "lockCategories" where "hashedVideoID" LIKE ?', [`${hashPrefix}%`]) as DBLock[];
|
||||||
if (lockedRows.length === 0 || !lockedRows[0]) return res.sendStatus(404);
|
if (lockedRows.length === 0 || !lockedRows[0]) return res.sendStatus(404);
|
||||||
// merge all locks
|
// merge all locks
|
||||||
return res.send(mergeLocks(lockedRows));
|
return res.send(mergeLocks(lockedRows, actionTypes));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(err as string);
|
Logger.error(err as string);
|
||||||
return res.sendStatus(500);
|
return res.sendStatus(500);
|
||||||
|
|||||||
@@ -2,34 +2,36 @@ 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 { ActionType } from "../../src/types/segments.model";
|
||||||
|
|
||||||
const fakeHash = "b05a20424f24a53dac1b059fb78d861ba9723645026be2174c93a94f9106bb35";
|
const fakeHash = "b05a20424f24a53dac1b059fb78d861ba9723645026be2174c93a94f9106bb35";
|
||||||
const endpoint = "/api/lockCategories";
|
const endpoint = "/api/lockCategories";
|
||||||
const getLockCategories = (hash: string) => client.get(`${endpoint}/${hash}`);
|
const getLockCategories = (hash: string, actionTypes = [ActionType.Mute, ActionType.Skip]) => client.get(`${endpoint}/${hash}`, { params: { actionTypes } });
|
||||||
|
|
||||||
describe("getLockCategoriesByHash", () => {
|
describe("getLockCategoriesByHash", () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
||||||
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesHashVIP")]);
|
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesHashVIP")]);
|
||||||
|
|
||||||
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason", "hashedVideoID") VALUES (?, ?, ?, ?, ?)';
|
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType", "category", "reason", "hashedVideoID") VALUES (?, ?, ?, ?, ?, ?)';
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "sponsor", "1-reason-short", getHash("getLockHash1", 1)]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "skip", "sponsor", "1-reason-short", getHash("getLockHash1", 1)]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "interaction", "1-reason-longer", getHash("getLockHash1", 1)]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "skip", "interaction", "1-reason-longer", getHash("getLockHash1", 1)]);
|
||||||
|
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash2", "preview", "2-reason", getHash("getLockHash2", 1)]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash2", "skip", "preview", "2-reason", getHash("getLockHash2", 1)]);
|
||||||
|
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash3", "nonmusic", "3-reason", getHash("getLockHash3", 1)]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash3", "skip", "nonmusic", "3-reason", getHash("getLockHash3", 1)]);
|
||||||
|
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-1", "outro", "fake1-reason", fakeHash]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-1", "mute", "outro", "fake1-reason", fakeHash]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "intro", "fake2-longer-reason", fakeHash]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "mute", "intro", "fake2-longer-reason", fakeHash]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "preview", "fake2-short", fakeHash]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "mute", "preview", "fake2-short", fakeHash]);
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "full", "sponsor", "fake2-notshown", fakeHash]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Database should be greater or equal to version 20", async () => {
|
it("Database should be greater or equal to version 29", async () => {
|
||||||
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
||||||
assert(
|
assert(
|
||||||
version >= 20,
|
version >= 29,
|
||||||
`Version isn't greater than 20. Version is ${version}`);
|
`Version isn't greater than 29. Version is ${version}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get multiple locks in one object", (done) => {
|
it("Should be able to get multiple locks in one object", (done) => {
|
||||||
@@ -163,4 +165,22 @@ describe("getLockCategoriesByHash", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should be able to get by actionType", (done) => {
|
||||||
|
getLockCategories(fakeHash.substring(0,5), [ActionType.Full])
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const expected = [{
|
||||||
|
videoID: "fakehash-2",
|
||||||
|
hash: fakeHash,
|
||||||
|
categories: [
|
||||||
|
"sponsor"
|
||||||
|
],
|
||||||
|
reason: "fake2-notshown"
|
||||||
|
}];
|
||||||
|
assert.deepStrictEqual(res.data, expected);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user