mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2026-02-01 15:21:07 +03:00
add getUserID and tests
This commit is contained in:
@@ -32,6 +32,7 @@ import {endpoint as getSegmentInfo} from './routes/getSegmentInfo';
|
||||
import {postClearCache} from './routes/postClearCache';
|
||||
import { addUnlistedVideo } from './routes/addUnlistedVideo';
|
||||
import {postPurgeAllSegments} from './routes/postPurgeAllSegments';
|
||||
import {getUserID} from './routes/getUserID';
|
||||
|
||||
export function createServer(callback: () => void) {
|
||||
// Create a service (the app object is just a callback).
|
||||
@@ -146,6 +147,9 @@ function setupRoutes(app: Express) {
|
||||
app.post('/api/purgeAllSegments', postPurgeAllSegments);
|
||||
|
||||
app.post('/api/unlistedVideo', addUnlistedVideo);
|
||||
|
||||
// get userID from username
|
||||
app.get('/api/userID', getUserID);
|
||||
|
||||
if (config.postgres) {
|
||||
app.get('/database', (req, res) => dumpDatabase(req, res, true));
|
||||
|
||||
29
src/routes/getUserID.ts
Normal file
29
src/routes/getUserID.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import {db} from '../databases/databases';
|
||||
import {Logger} from '../utils/logger';
|
||||
import {Request, Response} from 'express';
|
||||
|
||||
export async function getUserID(req: Request, res: Response) {
|
||||
let username = req.query.username as string;
|
||||
|
||||
if (username == undefined) {
|
||||
//invalid request
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
// add wildcard to variable
|
||||
username = `%${username}%`
|
||||
try {
|
||||
let rows = await db.prepare('all', `SELECT "userName", "userID" FROM "userNames" WHERE "userName" LIKE ?`, [username]);
|
||||
if (rows.length === 0) {
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
} else {
|
||||
res.send(rows);
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error(err);
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user