From 48d88614fbeaa9447f762f50a90d1f0408b0c033 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 20 Jun 2021 13:41:35 -0400 Subject: [PATCH] Move shadow ban list to public db Warning: Migration is not automatic --- databases/_private.db.sql | 3 --- databases/_sponsorTimes_indexes.sql | 7 +++++++ databases/_upgrade_sponsorTimes_14.sql | 9 +++++++++ src/routes/getTopUsers.ts | 8 ++------ src/routes/postSkipSegments.ts | 2 +- src/routes/shadowBanUser.ts | 10 +++++----- src/routes/voteOnSponsorTime.ts | 2 +- test/cases/shadowBanUser.ts | 12 ++++++------ test/cases/unBan.ts | 6 +++--- test/cases/voteOnSponsorTime.ts | 2 +- 10 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 databases/_upgrade_sponsorTimes_14.sql diff --git a/databases/_private.db.sql b/databases/_private.db.sql index a66ed9b..0da7016 100644 --- a/databases/_private.db.sql +++ b/databases/_private.db.sql @@ -1,8 +1,5 @@ BEGIN TRANSACTION; -CREATE TABLE IF NOT EXISTS "shadowBannedUsers" ( - "userID" TEXT NOT NULL -); CREATE TABLE IF NOT EXISTS "votes" ( "UUID" TEXT NOT NULL, "userID" TEXT NOT NULL, diff --git a/databases/_sponsorTimes_indexes.sql b/databases/_sponsorTimes_indexes.sql index 2ad3f75..40fe33e 100644 --- a/databases/_sponsorTimes_indexes.sql +++ b/databases/_sponsorTimes_indexes.sql @@ -63,4 +63,11 @@ CREATE INDEX IF NOT EXISTS "noSegments_videoID" CREATE INDEX IF NOT EXISTS "categoryVotes_UUID_public" ON public."categoryVotes" USING btree ("UUID" COLLATE pg_catalog."default" ASC NULLS LAST, category COLLATE pg_catalog."default" ASC NULLS LAST) + TABLESPACE pg_default; + +-- shadowBannedUsers + +CREATE INDEX IF NOT EXISTS "shadowBannedUsers_index" + ON public."shadowBannedUsers" USING btree + ("userID" COLLATE pg_catalog."default" ASC NULLS LAST) TABLESPACE pg_default; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_14.sql b/databases/_upgrade_sponsorTimes_14.sql new file mode 100644 index 0000000..c21b83c --- /dev/null +++ b/databases/_upgrade_sponsorTimes_14.sql @@ -0,0 +1,9 @@ +BEGIN TRANSACTION; + +CREATE TABLE IF NOT EXISTS "shadowBannedUsers" ( + "userID" TEXT NOT NULL +); + +UPDATE "config" SET value = 14 WHERE key = 'version'; + +COMMIT; diff --git a/src/routes/getTopUsers.ts b/src/routes/getTopUsers.ts index 347791b..2826e25 100644 --- a/src/routes/getTopUsers.ts +++ b/src/routes/getTopUsers.ts @@ -29,8 +29,8 @@ async function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boole SUM("votes") as "userVotes", ` + additionalFields + `IFNULL("userNames"."userName", "sponsorTimes"."userID") as "userName" FROM "sponsorTimes" LEFT JOIN "userNames" ON "sponsorTimes"."userID"="userNames"."userID" - LEFT JOIN "privateDB"."shadowBannedUsers" ON "sponsorTimes"."userID"="privateDB"."shadowBannedUsers"."userID" - WHERE "sponsorTimes"."votes" > -1 AND "sponsorTimes"."shadowHidden" != 1 AND "privateDB"."shadowBannedUsers"."userID" IS NULL + LEFT JOIN "shadowBannedUsers" ON "sponsorTimes"."userID"="shadowBannedUsers"."userID" + WHERE "sponsorTimes"."votes" > -1 AND "sponsorTimes"."shadowHidden" != 1 AND "shadowBannedUsers"."userID" IS NULL GROUP BY IFNULL("userName", "sponsorTimes"."userID") HAVING "userVotes" > 20 ORDER BY "${sortBy}" DESC LIMIT 100`, []); @@ -71,10 +71,6 @@ export async function getTopUsers(req: Request, res: Response) { return; } - //TODO: remove. This is broken for now - res.status(200).send(); - return; - //setup which sort type to use let sortBy = ''; if (sortType == 0) { diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index b79b855..22515e1 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -485,7 +485,7 @@ export async function postSkipSegments(req: Request, res: Response) { } //check to see if this user is shadowbanned - const shadowBanRow = await privateDB.prepare('get', `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); + const shadowBanRow = await db.prepare('get', `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); let shadowBanned = shadowBanRow.userCount; diff --git a/src/routes/shadowBanUser.ts b/src/routes/shadowBanUser.ts index 948367f..f02c9a0 100644 --- a/src/routes/shadowBanUser.ts +++ b/src/routes/shadowBanUser.ts @@ -1,4 +1,4 @@ -import {db, privateDB} from '../databases/databases'; +import {db} from '../databases/databases'; import {getHash} from '../utils/getHash'; import {Request, Response} from 'express'; import { config } from '../config'; @@ -39,13 +39,13 @@ export async function shadowBanUser(req: Request, res: Response) { if (userID) { //check to see if this user is already shadowbanned - const row = await privateDB.prepare('get', `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); + const row = await db.prepare('get', `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); if (enabled && row.userCount == 0) { //add them to the shadow ban list //add it to the table - await privateDB.prepare('run', `INSERT INTO "shadowBannedUsers" VALUES(?)`, [userID]); + await db.prepare('run', `INSERT INTO "shadowBannedUsers" VALUES(?)`, [userID]); //find all previous submissions and hide them if (unHideOldSubmissions) { @@ -62,7 +62,7 @@ export async function shadowBanUser(req: Request, res: Response) { } } else if (!enabled && row.userCount > 0) { //remove them from the shadow ban list - await privateDB.prepare('run', `DELETE FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); + await db.prepare('run', `DELETE FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); //find all previous submissions and unhide them if (unHideOldSubmissions) { @@ -106,7 +106,7 @@ export async function shadowBanUser(req: Request, res: Response) { } } /*else if (!enabled && row.userCount > 0) { // //remove them from the shadow ban list - // await privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]); + // await db.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]); // //find all previous submissions and unhide them // if (unHideOldSubmissions) { diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 147139e..c985694 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -404,7 +404,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { const ableToVote = isVIP || (!(isOwnSubmission && incrementAmount > 0) && (await db.prepare("get", `SELECT "userID" FROM "sponsorTimes" WHERE "userID" = ?`, [nonAnonUserID])) !== undefined - && (await privateDB.prepare("get", `SELECT "userID" FROM "shadowBannedUsers" WHERE "userID" = ?`, [nonAnonUserID])) === undefined + && (await db.prepare("get", `SELECT "userID" FROM "shadowBannedUsers" WHERE "userID" = ?`, [nonAnonUserID])) === undefined && (await privateDB.prepare("get", `SELECT "UUID" FROM "votes" WHERE "UUID" = ? AND "hashedIP" = ? AND "userID" != ?`, [UUID, hashedIP, userID])) === undefined) && !finalResponse.blockVote && finalResponse.finalStatus === 200; diff --git a/test/cases/shadowBanUser.ts b/test/cases/shadowBanUser.ts index 5d6af7e..29dee8e 100644 --- a/test/cases/shadowBanUser.ts +++ b/test/cases/shadowBanUser.ts @@ -17,7 +17,7 @@ describe('shadowBanUser', () => { db.prepare("run", startOfQuery + "('testtesttest', 1, 11, 2, 0, 'shadow-3-uuid-0', 'shadowBanned3', 0, 50, 'sponsor', 'YouTube', 100, 0, 1, '" + getHash('testtesttest', 1) + "')"); db.prepare("run", startOfQuery + "('testtesttest2', 1, 11, 2, 0, 'shadow-3-uuid-0-1', 'shadowBanned3', 0, 50, 'sponsor', 'PeerTube', 120, 0, 1, '" + getHash('testtesttest2', 1) + "')"); db.prepare("run", startOfQuery + "('testtesttest', 20, 33, 2, 0, 'shadow-3-uuid-2', 'shadowBanned3', 0, 50, 'intro', 'YouTube', 101, 0, 1, '" + getHash('testtesttest', 1) + "')"); - privateDB.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('shadowBanned3')`); + db.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('shadowBanned3')`); db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("shadow-ban-vip") + "')"); }); @@ -31,7 +31,7 @@ describe('shadowBanUser', () => { if (res.status !== 200) done("Status code was: " + res.status); else { const videoRow = await db.prepare('all', `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned", 1]); - const shadowRow = await privateDB.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned"]); + const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned"]); if (shadowRow && videoRow?.length === 3) { done(); @@ -51,7 +51,7 @@ describe('shadowBanUser', () => { if (res.status !== 200) done("Status code was: " + res.status); else { const videoRow = await db.prepare('all', `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned", 1]); - const shadowRow = await privateDB.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned"]); + const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned"]); if (!shadowRow && videoRow?.length === 3) { done(); @@ -71,7 +71,7 @@ describe('shadowBanUser', () => { if (res.status !== 200) done("Status code was: " + res.status); else { const videoRow: {category: string, shadowHidden: number}[] = (await db.prepare('all', `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned2", 1])); - const shadowRow = await privateDB.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned2"]); + const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned2"]); if (shadowRow && 2 == videoRow?.length && 2 === videoRow?.filter((elem) => elem?.category === "sponsor")?.length) { done(); @@ -91,7 +91,7 @@ describe('shadowBanUser', () => { if (res.status !== 200) done("Status code was: " + res.status); else { const videoRow = await db.prepare('all', `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned2", 1]); - const shadowRow = await privateDB.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned2"]); + const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned2"]); if (!shadowRow && videoRow?.length === 0) { done(); @@ -111,7 +111,7 @@ describe('shadowBanUser', () => { if (res.status !== 200) done("Status code was: " + res.status); else { const videoRow = await db.prepare('all', `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned3", 1]); - const shadowRow = await privateDB.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned3"]); + const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned3"]); if (!shadowRow && videoRow?.length === 1 && videoRow[0]?.category === "intro") { done(); diff --git a/test/cases/unBan.ts b/test/cases/unBan.ts index 379fcfc..0f54c8d 100644 --- a/test/cases/unBan.ts +++ b/test/cases/unBan.ts @@ -8,9 +8,9 @@ import { Logger } from '../../src/utils/logger.js'; describe('unBan', () => { before(async () => { - await privateDB.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('testMan-unBan')`); - await privateDB.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('testWoman-unBan')`); - await privateDB.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('testEntity-unBan')`); + await db.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('testMan-unBan')`); + await db.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('testWoman-unBan')`); + await db.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('testEntity-unBan')`); await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("VIPUser-unBan") + "')"); await db.prepare("run", `INSERT INTO "lockCategories" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-unBan") + "', 'unBan-videoID-1', 'sponsor')"); diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index b2cbc27..9c9587e 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -57,7 +57,7 @@ describe('voteOnSponsorTime', () => { await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("VIPUser") + "')"); - await privateDB.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES ('` + getHash("randomID4") + "')"); + await db.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES ('` + getHash("randomID4") + "')"); await db.prepare("run", `INSERT INTO "lockCategories" ("videoID", "userID", "category") VALUES ('no-sponsor-segments-video', 'someUser', 'sponsor')`);