mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-19 22:18:35 +03:00
Add ability to set config
This commit is contained in:
37
src/routes/getConfig.ts
Normal file
37
src/routes/getConfig.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { getHashCache } from "../utils/getHashCache";
|
||||
import { db } from "../databases/databases";
|
||||
import { Request, Response } from "express";
|
||||
import { isUserVIP } from "../utils/isUserVIP";
|
||||
import { UserID } from "../types/user.model";
|
||||
import { Logger } from "../utils/logger";
|
||||
|
||||
export async function getConfig(req: Request, res: Response): Promise<Response> {
|
||||
const userID = req.query.userID as string;
|
||||
const key = req.query.key as string;
|
||||
|
||||
if (!userID || !key) {
|
||||
// invalid request
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
|
||||
// hash the userID
|
||||
const hashedUserID = await getHashCache(userID as UserID);
|
||||
const isVIP = (await isUserVIP(hashedUserID));
|
||||
|
||||
if (!isVIP) {
|
||||
// not authorized
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
|
||||
try {
|
||||
const row = await db.prepare("run", `SELECT "value" FROM "config" WHERE "key" = ?`, [key]);
|
||||
|
||||
return res.status(200).json({
|
||||
value: row.value
|
||||
});
|
||||
} catch (e) {
|
||||
Logger.error(e as string);
|
||||
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
}
|
||||
47
src/routes/setConfig.ts
Normal file
47
src/routes/setConfig.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { getHashCache } from "../utils/getHashCache";
|
||||
import { db } from "../databases/databases";
|
||||
import { Request, Response } from "express";
|
||||
import { isUserVIP } from "../utils/isUserVIP";
|
||||
import { UserID } from "../types/user.model";
|
||||
import { Logger } from "../utils/logger";
|
||||
|
||||
interface SetConfigRequest extends Request {
|
||||
body: {
|
||||
userID: UserID;
|
||||
key: string;
|
||||
value: string;
|
||||
}
|
||||
}
|
||||
|
||||
const allowedConfigs = [
|
||||
"old-submitter-block-date",
|
||||
"max-users-per-minute"
|
||||
];
|
||||
|
||||
export async function setConfig(req: SetConfigRequest, res: Response): Promise<Response> {
|
||||
const { body: { userID, key, value } } = req;
|
||||
|
||||
if (!userID || !allowedConfigs.includes(key)) {
|
||||
// invalid request
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
|
||||
// hash the userID
|
||||
const hashedUserID = await getHashCache(userID as UserID);
|
||||
const isVIP = (await isUserVIP(hashedUserID));
|
||||
|
||||
if (!isVIP) {
|
||||
// not authorized
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
|
||||
try {
|
||||
await db.prepare("run", `INSERT OR REPLACE INTO "config" ("key", "value") VALUES(?, ?)`, [key, value]);
|
||||
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
Logger.error(e as string);
|
||||
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user