mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-24 16:38:41 +03:00
Merge branch 'master' of https://github.com/ajayyy/SponsorBlockServer into mute-skip
This commit is contained in:
27
src/routes/getLockCategories.ts
Normal file
27
src/routes/getLockCategories.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import {db} from '../databases/databases';
|
||||
import {Logger} from '../utils/logger';
|
||||
import {Request, Response} from 'express';
|
||||
import { Category, VideoID } from "../types/segments.model";
|
||||
|
||||
export async function getLockCategories(req: Request, res: Response): Promise<Response> {
|
||||
const videoID = req.query.videoID as VideoID;
|
||||
|
||||
if (videoID == undefined) {
|
||||
//invalid request
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
|
||||
try {
|
||||
// Get existing lock categories markers
|
||||
const lockedCategories = await db.prepare('all', 'SELECT "category" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category}[];
|
||||
if (lockedCategories.length === 0 || !lockedCategories[0]) return res.sendStatus(404);
|
||||
// map to array in JS becaues of SQL incompatibilities
|
||||
const categories = Object.values(lockedCategories).map((entry) => entry.category);
|
||||
return res.send({
|
||||
categories
|
||||
});
|
||||
} catch (err) {
|
||||
Logger.error(err);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
}
|
||||
56
src/routes/getLockCategoriesByHash.ts
Normal file
56
src/routes/getLockCategoriesByHash.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import {db} from '../databases/databases';
|
||||
import {Logger} from '../utils/logger';
|
||||
import {Request, Response} from 'express';
|
||||
import {hashPrefixTester} from '../utils/hashPrefixTester';
|
||||
import { Category, VideoID, VideoIDHash } from "../types/segments.model";
|
||||
|
||||
interface LockResultByHash {
|
||||
videoID: VideoID,
|
||||
hash: VideoIDHash,
|
||||
categories: Category[]
|
||||
}
|
||||
|
||||
interface DBLock {
|
||||
videoID: VideoID,
|
||||
hash: VideoIDHash,
|
||||
category: Category
|
||||
}
|
||||
|
||||
const mergeLocks = (source: DBLock[]) => {
|
||||
const dest: LockResultByHash[] = [];
|
||||
for (const obj of source) {
|
||||
// videoID already exists
|
||||
const destMatch = dest.find(s => s.videoID === obj.videoID);
|
||||
if (destMatch) {
|
||||
// push to categories
|
||||
destMatch.categories.push(obj.category);
|
||||
} else {
|
||||
dest.push({
|
||||
videoID: obj.videoID,
|
||||
hash: obj.hash,
|
||||
categories: [obj.category]
|
||||
});
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
};
|
||||
|
||||
|
||||
export async function getLockCategoriesByHash(req: Request, res: Response): Promise<Response> {
|
||||
let hashPrefix = req.params.prefix as VideoIDHash;
|
||||
if (!hashPrefixTester(req.params.prefix)) {
|
||||
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
||||
}
|
||||
hashPrefix = hashPrefix.toLowerCase() as VideoIDHash;
|
||||
|
||||
try {
|
||||
// Get existing lock categories markers
|
||||
const lockedRows = await db.prepare('all', 'SELECT "videoID", "hashedVideoID" as "hash", "category" from "lockCategories" where "hashedVideoID" LIKE ?', [hashPrefix + '%']) as DBLock[];
|
||||
if (lockedRows.length === 0 || !lockedRows[0]) return res.sendStatus(404);
|
||||
// merge all locks
|
||||
return res.send(mergeLocks(lockedRows));
|
||||
} catch (err) {
|
||||
Logger.error(err);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user