mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-10 13:37:01 +03:00
add getLockCategories
This commit is contained in:
@@ -33,6 +33,7 @@ import {postClearCache} from './routes/postClearCache';
|
|||||||
import { addUnlistedVideo } from './routes/addUnlistedVideo';
|
import { addUnlistedVideo } from './routes/addUnlistedVideo';
|
||||||
import {postPurgeAllSegments} from './routes/postPurgeAllSegments';
|
import {postPurgeAllSegments} from './routes/postPurgeAllSegments';
|
||||||
import {getUserID} from './routes/getUserID';
|
import {getUserID} from './routes/getUserID';
|
||||||
|
import {getLockCategories} from './routes/getLockCategories';
|
||||||
import ExpressPromiseRouter from 'express-promise-router';
|
import ExpressPromiseRouter from 'express-promise-router';
|
||||||
|
|
||||||
export function createServer(callback: () => void) {
|
export function createServer(callback: () => void) {
|
||||||
@@ -155,6 +156,9 @@ function setupRoutes(router: Router) {
|
|||||||
// get userID from username
|
// get userID from username
|
||||||
router.get('/api/userID', getUserID);
|
router.get('/api/userID', getUserID);
|
||||||
|
|
||||||
|
// get lock categores from userID
|
||||||
|
router.get('/api/lockCategories', getLockCategories);
|
||||||
|
|
||||||
if (config.postgres) {
|
if (config.postgres) {
|
||||||
router.get('/database', (req, res) => dumpDatabase(req, res, true));
|
router.get('/database', (req, res) => dumpDatabase(req, res, true));
|
||||||
router.get('/database.json', (req, res) => dumpDatabase(req, res, false));
|
router.get('/database.json', (req, res) => dumpDatabase(req, res, false));
|
||||||
|
|||||||
28
src/routes/getLockCategories.ts
Normal file
28
src/routes/getLockCategories.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import {db} from '../databases/databases';
|
||||||
|
import {getHash} from '../utils/getHash';
|
||||||
|
import {Logger} from '../utils/logger';
|
||||||
|
import {Request, Response} from 'express';
|
||||||
|
import { Category, CategoryActionType, DBSegment, HashedIP, IPAddress, OverlappingSegmentGroup, Segment, SegmentCache, SegmentUUID, Service, VideoData, VideoID, VideoIDHash, Visibility, VotableObject } from "../types/segments.model";
|
||||||
|
import { UserID } from '../types/user.model';
|
||||||
|
|
||||||
|
export async function getLockCategories(req: Request, res: Response) {
|
||||||
|
const videoID = req.query.videoID as VideoID;
|
||||||
|
|
||||||
|
if (videoID == undefined) {
|
||||||
|
//invalid request
|
||||||
|
return res.sendStatus(400);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get existing lock categories markers
|
||||||
|
let lockCategoryList = await db.prepare('all', 'SELECT "category", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, userID: UserID}[]
|
||||||
|
if (lockCategoryList.length === 0 || !lockCategoryList[0]) {
|
||||||
|
return res.sendStatus(404);
|
||||||
|
} else {
|
||||||
|
return res.send(lockCategoryList)
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
Logger.error(err);
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
84
test/cases/getLockCategories.ts
Normal file
84
test/cases/getLockCategories.ts
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import fetch from 'node-fetch';
|
||||||
|
import {Done, getbaseURL} from '../utils';
|
||||||
|
import {getHash} from '../../src/utils/getHash';
|
||||||
|
import {db} from '../../src/databases/databases';
|
||||||
|
|
||||||
|
|
||||||
|
describe('lockCategoriesRecords', () => {
|
||||||
|
before(async () => {
|
||||||
|
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
||||||
|
await db.prepare("run", insertVipUserQuery, [getHash("VIPUser-getLockCategories")]);
|
||||||
|
|
||||||
|
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category") VALUES (?, ?, ?)';
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [getHash("VIPUser-getLockCategories"), 'getLock-1', 'sponsor']);
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [getHash("VIPUser-getLockCategories"), 'getLock-1', 'intro']);
|
||||||
|
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [getHash("VIPUser-getLockCategories"), 'getLock-2', 'preview']);
|
||||||
|
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [getHash("VIPUser-getLockCategories"), 'getLock-3', 'nonmusic']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should update the database version when starting the application', async () => {
|
||||||
|
let version = (await db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version'])).value;
|
||||||
|
if (version > 1) return;
|
||||||
|
else return 'Version isn\'t greater than 1. Version is ' + version;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should be able to get multiple locks', (done: Done) => {
|
||||||
|
fetch(getbaseURL() + '/api/lockCategories?videoID=getLock-1')
|
||||||
|
.then(async res => {
|
||||||
|
if (res.status !== 200) {
|
||||||
|
done("non 200");
|
||||||
|
} else {
|
||||||
|
const data = await res.json();
|
||||||
|
if (data.length !== 2) {
|
||||||
|
done(`Returned incorrect number of locks "${data.length}"`);
|
||||||
|
} else if (data[0].category !== "sponsor") {
|
||||||
|
done(`Returned incorrect category "${data[0].category}"`);
|
||||||
|
} else if (data[1].category !== "intro") {
|
||||||
|
done(`Returned incorrect category "${data[1].category}"`);
|
||||||
|
} else {
|
||||||
|
done(); // pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => ("couldn't call endpoint"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should be able to get single locks', (done: Done) => {
|
||||||
|
fetch(getbaseURL() + '/api/lockCategories?videoID=getLock-2')
|
||||||
|
.then(async res => {
|
||||||
|
if (res.status !== 200) {
|
||||||
|
done("non 200");
|
||||||
|
} else {
|
||||||
|
const data = await res.json();
|
||||||
|
if (data.length !== 1) {
|
||||||
|
done('Returned incorrect number of locks "' + data.length + '"');
|
||||||
|
} else if (data[0].category !== "preview") {
|
||||||
|
done(`Returned incorrect category "${data[0].category}"`);
|
||||||
|
} else {
|
||||||
|
done(); // pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => ("couldn't call endpoint"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 404 if no lock exists', (done: Done) => {
|
||||||
|
fetch(getbaseURL() + '/api/lockCategories?videoID=getLock-0')
|
||||||
|
.then(res => {
|
||||||
|
if (res.status !== 404) done('non 404 (' + res.status + ')');
|
||||||
|
else done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => ("couldn't call endpoint"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 400 if no videoID specified', (done: Done) => {
|
||||||
|
fetch(getbaseURL() + '/api/lockCategories')
|
||||||
|
.then(res => {
|
||||||
|
if (res.status !== 400) done('non 400 (' + res.status + ')');
|
||||||
|
else done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => ("couldn't call endpoint"));
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user