Merge pull request #334 from HaiDang666/update-query

Add Limit 1 when check user as vip
This commit is contained in:
Ajay Ramachandran 2021-08-03 21:06:51 -04:00 committed by GitHub
commit 56a36f34a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 9 deletions

View file

@ -2,9 +2,12 @@ import {getHash} from "../utils/getHash";
import {db} from "../databases/databases";
import {config} from "../config";
import {Request, Response} from "express";
import { isUserVIP } from "../utils/isUserVIP";
import { HashedUserID } from "../types/user.model";
export async function addUserAsVIP(req: Request, res: Response): Promise<Response> {
const userID = req.query.userID as string;
const userID = req.query.userID as HashedUserID;
let adminUserIDInput = req.query.adminUserID as string;
const enabled = req.query.enabled === undefined
@ -25,12 +28,12 @@ export async function addUserAsVIP(req: Request, res: Response): Promise<Respons
}
//check to see if this user is already a vip
const row = await db.prepare("get", 'SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?', [userID]);
const userIsVIP = await isUserVIP(userID);
if (enabled && row.userCount == 0) {
if (enabled && !userIsVIP) {
//add them to the vip list
await db.prepare("run", 'INSERT INTO "vipUsers" VALUES(?)', [userID]);
} else if (!enabled && row.userCount > 0) {
} else if (!enabled && userIsVIP) {
//remove them from the shadow ban list
await db.prepare("run", 'DELETE FROM "vipUsers" WHERE "userID" = ?', [userID]);
}

View file

@ -5,11 +5,12 @@ import { config } from "../config";
import { Category, Service, VideoID, VideoIDHash } from "../types/segments.model";
import { UserID } from "../types/user.model";
import { QueryCacher } from "../utils/queryCacher";
import { isUserVIP } from "../utils/isUserVIP";
export async function shadowBanUser(req: Request, res: Response): Promise<Response> {
const userID = req.query.userID as string;
const userID = req.query.userID as UserID;
const hashedIP = req.query.hashedIP as string;
let adminUserIDInput = req.query.adminUserID as string;
const adminUserIDInput = req.query.adminUserID as UserID;
const enabled = req.query.enabled === undefined
? true
@ -27,9 +28,9 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
}
//hash the userID
adminUserIDInput = getHash(adminUserIDInput);
const adminUserID = getHash(adminUserIDInput);
const isVIP = (await db.prepare("get", `SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?`, [adminUserIDInput])).userCount > 0;
const isVIP = await isUserVIP(adminUserID);
if (!isVIP) {
//not authorized
return res.sendStatus(403);

View file

@ -281,7 +281,7 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise<Re
const hashedIP: HashedIP = getHash((ip + config.globalSalt) as IPAddress);
//check if this user is on the vip list
const isVIP = (await db.prepare("get", `SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?`, [nonAnonUserID])).userCount > 0;
const isVIP = await isUserVIP(nonAnonUserID);
//check if user voting on own submission
const isOwnSubmission = (await db.prepare("get", `SELECT "UUID" as "submissionCount" FROM "sponsorTimes" where "userID" = ? AND "UUID" = ?`, [nonAnonUserID, UUID])) !== undefined;