Add ability to set config

This commit is contained in:
Ajay
2025-04-08 15:15:39 -04:00
parent 82af8f200b
commit 2aa3589312
2 changed files with 84 additions and 0 deletions

37
src/routes/getConfig.ts Normal file
View 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
View 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);
}
}