mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-13 23:17:02 +03:00
Add remove feature to warnUser
This commit is contained in:
@@ -2,9 +2,10 @@ import {Logger} from '../utils/logger';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import {isUserVIP} from '../utils/isUserVIP';
|
||||
import {Request, Response} from 'express';
|
||||
import { HashedUserID, UserID } from '../types/user.model';
|
||||
|
||||
export function getIsUserVIP(req: Request, res: Response): void {
|
||||
let userID = req.query.userID as string;
|
||||
const userID = req.query.userID as UserID;
|
||||
|
||||
if (userID == undefined) {
|
||||
//invalid request
|
||||
@@ -13,12 +14,12 @@ export function getIsUserVIP(req: Request, res: Response): void {
|
||||
}
|
||||
|
||||
//hash the userID
|
||||
userID = getHash(userID);
|
||||
const hashedUserID: HashedUserID = getHash(userID);
|
||||
|
||||
try {
|
||||
let vipState = isUserVIP(userID);
|
||||
let vipState = isUserVIP(hashedUserID);
|
||||
res.status(200).json({
|
||||
hashedUserID: userID,
|
||||
hashedUserID: hashedUserID,
|
||||
vip: vipState,
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
@@ -3,23 +3,33 @@ import {Logger} from '../utils/logger';
|
||||
import {db} from '../databases/databases';
|
||||
import {isUserVIP} from '../utils/isUserVIP';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import { HashedUserID, UserID } from '../types/user.model';
|
||||
|
||||
export function postWarning(req: Request, res: Response) {
|
||||
// Collect user input data
|
||||
let issuerUserID = getHash(req.body.issuerUserID);
|
||||
let userID = req.body.userID;
|
||||
let issuerUserID: HashedUserID = getHash(<UserID> req.body.issuerUserID);
|
||||
let userID: UserID = req.body.userID;
|
||||
let issueTime = new Date().getTime();
|
||||
let enabled: boolean = req.body.enabled ?? true;
|
||||
|
||||
// Ensure user is a VIP
|
||||
if (!isUserVIP(issuerUserID)) {
|
||||
Logger.debug("Permission violation: User " + issuerUserID + " attempted to warn user " + userID + "."); // maybe warn?
|
||||
Logger.warn("Permission violation: User " + issuerUserID + " attempted to warn user " + userID + ".");
|
||||
res.status(403).json({"message": "Not a VIP"});
|
||||
return;
|
||||
}
|
||||
|
||||
db.prepare('run', 'INSERT INTO warnings (userID, issueTime, issuerUserID) VALUES (?, ?, ?)', [userID, issueTime, issuerUserID]);
|
||||
res.status(200).json({
|
||||
message: "Warning issued to user '" + userID + "'.",
|
||||
});
|
||||
let resultStatus = "";
|
||||
|
||||
if (enabled) {
|
||||
db.prepare('run', 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES (?, ?, ?, 1)', [userID, issueTime, issuerUserID]);
|
||||
resultStatus = "issued to";
|
||||
} else {
|
||||
db.prepare('run', 'UPDATE warnings SET enabled = 0', []);
|
||||
resultStatus = "removed from";
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
message: "Warning " + resultStatus + " user '" + userID + "'.",
|
||||
});
|
||||
}
|
||||
|
||||
1
src/types/hash.model.ts
Normal file
1
src/types/hash.model.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type HashedValue = string & { __hashBrand: unknown };
|
||||
4
src/types/user.model.ts
Normal file
4
src/types/user.model.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { HashedValue } from "./hash.model";
|
||||
|
||||
export type UserID = string & { __userIDBrand: unknown };
|
||||
export type HashedUserID = UserID & HashedValue;
|
||||
@@ -1,12 +1,13 @@
|
||||
import crypto from 'crypto';
|
||||
import { HashedValue } from '../types/hash.model';
|
||||
|
||||
export function getHash(value: string, times = 5000) {
|
||||
if (times <= 0) return "";
|
||||
export function getHash<T extends string>(value: T, times = 5000): T & HashedValue {
|
||||
if (times <= 0) return "" as T & HashedValue;
|
||||
|
||||
for (let i = 0; i < times; i++) {
|
||||
let hashCreator = crypto.createHash('sha256');
|
||||
value = hashCreator.update(value).digest('hex');
|
||||
value = hashCreator.update(value).digest('hex') as T;
|
||||
}
|
||||
|
||||
return value;
|
||||
return value as T & HashedValue;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {db} from '../databases/databases';
|
||||
import { HashedUserID } from '../types/user.model';
|
||||
|
||||
export function isUserVIP(userID: string): boolean {
|
||||
export function isUserVIP(userID: HashedUserID): boolean {
|
||||
return db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]).userCount > 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user