From fa759ae7b2840ec489ebb72011eccddb9dbc713e Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 1 Mar 2021 19:21:27 -0500 Subject: [PATCH 01/16] Add docker compose for deps --- .gitignore | 1 + docker/database.env.example | 3 +++ docker/docker-compose.yml | 22 ++++++++++++++++++++++ docker/redis/redis.conf | 2 ++ 4 files changed, 28 insertions(+) create mode 100644 docker/database.env.example create mode 100644 docker/docker-compose.yml create mode 100644 docker/redis/redis.conf diff --git a/.gitignore b/.gitignore index 7b3c05c..040d4e9 100644 --- a/.gitignore +++ b/.gitignore @@ -100,6 +100,7 @@ test/databases/private.db # Config files config.json +docker/database.env # Mac files .DS_Store diff --git a/docker/database.env.example b/docker/database.env.example new file mode 100644 index 0000000..dba3119 --- /dev/null +++ b/docker/database.env.example @@ -0,0 +1,3 @@ +POSTGRES_USER=sponsorblock +POSTGRES_PASSWORD= +POSTGRES_DB=sponsorTimes \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000..224055a --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3' +services: + database: + container_name: database + image: postgres + env_file: + - database.env + volumes: + - database-data:/var/lib/postgresql/data + ports: + - 5432:5432 + redis: + container_name: redis + image: redis + command: /usr/local/etc/redis/redis.conf + volumes: + - ./redis/redis.conf:/usr/local/etc/redis/redis.conf + ports: + - 32773:6379 + +volumes: + database-data: \ No newline at end of file diff --git a/docker/redis/redis.conf b/docker/redis/redis.conf new file mode 100644 index 0000000..023731b --- /dev/null +++ b/docker/redis/redis.conf @@ -0,0 +1,2 @@ +maxmemory-policy allkeys-lru +maxmemory 1000mb \ No newline at end of file From 9a9038d5e0d7741c4805b463b986fb91a1c7cb17 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 1 Mar 2021 19:21:59 -0500 Subject: [PATCH 02/16] Update gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 040d4e9..3cff924 100644 --- a/.gitignore +++ b/.gitignore @@ -92,6 +92,8 @@ databases/sponsorTimes.db databases/sponsorTimes.db-shm databases/sponsorTimes.db-wal databases/private.db +databases/private.db-shm +databases/private.db-wal databases/sponsorTimesReal.db test/databases/sponsorTimes.db test/databases/sponsorTimes.db-shm From ff4af3786e807e5147b3cb6f45f8c37c4efb529e Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 1 Mar 2021 20:40:31 -0500 Subject: [PATCH 03/16] Switch to postgres + promises --- package-lock.json | 129 +++++++++++++++++++++++++++ package.json | 2 + src/databases/IDatabase.ts | 8 +- src/databases/Mysql.ts | 17 +--- src/databases/Postgres.ts | 31 +++++++ src/databases/Sqlite.ts | 16 +--- src/databases/databases.ts | 3 +- src/middleware/requestRateLimit.ts | 4 +- src/routes/addUserAsVIP.ts | 6 +- src/routes/deleteNoSegments.ts | 12 +-- src/routes/getDaysSavedFormatted.ts | 4 +- src/routes/getIsUserVIP.ts | 4 +- src/routes/getSavedTimeForUser.ts | 4 +- src/routes/getSkipSegments.ts | 28 +++--- src/routes/getTopUsers.ts | 98 ++++++++++---------- src/routes/getTotalStats.ts | 4 +- src/routes/getUserInfo.ts | 42 +++++---- src/routes/getUsername.ts | 4 +- src/routes/getViewsForUser.ts | 4 +- src/routes/postNoSegments.ts | 12 +-- src/routes/postSegmentShift.ts | 15 ++-- src/routes/postSkipSegments.ts | 34 +++---- src/routes/postWarning.ts | 10 +-- src/routes/setUsername.ts | 8 +- src/routes/shadowBanUser.ts | 26 +++--- src/routes/viewedVideoSponsorTime.ts | 4 +- src/routes/voteOnSponsorTime.ts | 82 ++++++++--------- src/utils/isUserTrustworthy.ts | 4 +- src/utils/isUserVIP.ts | 4 +- test/cases/dbUpgrade.ts | 6 +- test/cases/noSegmentRecords.ts | 14 +-- test/cases/oldSubmitSponsorTimes.ts | 4 +- test/cases/postSkipSegments.ts | 14 +-- test/cases/postWarning.ts | 6 +- test/cases/segmentShift.ts | 4 +- test/cases/unBan.ts | 6 +- test/cases/voteOnSponsorTime.ts | 42 ++++----- 37 files changed, 424 insertions(+), 291 deletions(-) create mode 100644 src/databases/Postgres.ts diff --git a/package-lock.json b/package-lock.json index 158f360..3f790d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -149,6 +149,17 @@ "form-data": "^3.0.0" } }, + "@types/pg": { + "version": "7.14.10", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-7.14.10.tgz", + "integrity": "sha512-m6G0mrpj71YgVgHJF0cIHC3OZTKiQSUzTkMj869a+YWXF2tdbmO2PmIpNnDkiFPhHWcoGq2bk5P2e0CZX0F9Mg==", + "dev": true, + "requires": { + "@types/node": "*", + "pg-protocol": "^1.2.0", + "pg-types": "^2.2.0" + } + }, "@types/qs": { "version": "6.9.5", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.5.tgz", @@ -480,6 +491,11 @@ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" }, + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, "bytes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", @@ -2011,6 +2027,11 @@ "semver": "^5.1.0" } }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -2045,6 +2066,60 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "pg": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.5.1.tgz", + "integrity": "sha512-9wm3yX9lCfjvA98ybCyw2pADUivyNWT/yIP4ZcDVpMN0og70BUWYEGXPCTAQdGTAqnytfRADb7NERrY1qxhIqw==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.4.0", + "pg-pool": "^3.2.2", + "pg-protocol": "^1.4.0", + "pg-types": "^2.1.0", + "pgpass": "1.x" + } + }, + "pg-connection-string": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.4.0.tgz", + "integrity": "sha512-3iBXuv7XKvxeMrIgym7njT+HlZkwZqqGX4Bu9cci8xHZNT+Um1gWKqCsAzcC0d95rcKMU5WBg6YRUcHyV0HZKQ==" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-pool": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.2.tgz", + "integrity": "sha512-ORJoFxAlmmros8igi608iVEbQNNZlp89diFVx6yV5v+ehmpMY9sK6QgpmgoXbmkNaBAx8cOOZh9g80kJv1ooyA==" + }, + "pg-protocol": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.4.0.tgz", + "integrity": "sha512-El+aXWcwG/8wuFICMQjM5ZSAm6OWiJicFdNYo+VY3QP+8vI4SvLIWVe51PppTzMhikUJR+PsyIFKqfdXPz/yxA==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.4.tgz", + "integrity": "sha512-YmuA56alyBq7M59vxVBfPJrGSozru8QAdoNlWuW3cz8l+UX3cWge0vTvjKhsSHSJpo3Bom8/Mm6hf0TR5GY0+w==", + "requires": { + "split2": "^3.1.1" + } + }, "picomatch": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", @@ -2057,6 +2132,29 @@ "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=", "dev": true }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, "prepend-http": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-1.0.4.tgz", @@ -2132,6 +2230,16 @@ "strip-json-comments": "~2.0.1" } }, + "readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + }, "readdirp": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.2.0.tgz", @@ -2340,6 +2448,14 @@ "source-map": "^0.6.0" } }, + "split2": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/split2/-/split2-3.2.2.tgz", + "integrity": "sha512-9NThjpgZnifTkJpzTZ7Eue85S49QwpNhZTq6GRJwObb6jnLFNGB7Qm73V5HewTROPyxD0C29xqmaI68bQtV+hg==", + "requires": { + "readable-stream": "^3.0.0" + } + }, "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -2425,6 +2541,14 @@ "es-abstract": "^1.17.5" } }, + "string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "requires": { + "safe-buffer": "~5.2.0" + } + }, "strip-eof": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", @@ -2854,6 +2978,11 @@ "integrity": "sha1-SWsswQnsqNus/i3HK2A8F8WHCtQ=", "dev": true }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + }, "y18n": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", diff --git a/package.json b/package.json index b1deca9..1cd2362 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "http": "0.0.0", "iso8601-duration": "^1.2.0", "node-fetch": "^2.6.0", + "pg": "^8.5.1", "redis": "^3.0.2", "sync-mysql": "^3.0.1", "uuid": "^3.3.2", @@ -31,6 +32,7 @@ "@types/mocha": "^8.0.3", "@types/node": "^14.11.9", "@types/node-fetch": "^2.5.7", + "@types/pg": "^7.14.10", "@types/redis": "^2.8.28", "@types/request": "^2.48.5", "mocha": "^7.1.1", diff --git a/src/databases/IDatabase.ts b/src/databases/IDatabase.ts index f30a532..4bac1e3 100644 --- a/src/databases/IDatabase.ts +++ b/src/databases/IDatabase.ts @@ -1,13 +1,7 @@ export interface IDatabase { init(): void; - prepare(type: QueryType, query: string, params: any[]): any; - - get(query: string, params: any[]): TModel; - getAll(query: string, params: any[]): TModel[]; - run(query: string, params: any[]): void; - - exec(query: string): any; + prepare(type: QueryType, query: string, params: any[]): Promise; } export type QueryType = 'get' | 'all' | 'run'; diff --git a/src/databases/Mysql.ts b/src/databases/Mysql.ts index 5001a4c..4d9c675 100644 --- a/src/databases/Mysql.ts +++ b/src/databases/Mysql.ts @@ -9,14 +9,10 @@ export class Mysql implements IDatabase { constructor(private config: any) { } - init() { + init(): void { this.connection = new MysqlInterface(this.config); } - exec(query: string) { - this.prepare('run', query, []); - } - prepare(type: QueryType, query: string, params: any[]) { Logger.debug(`prepare (mysql): type: ${type}, query: ${query}, params: ${params}`); const queryResult = this.connection.query(query, params); @@ -34,16 +30,5 @@ export class Mysql implements IDatabase { } } - public get(query: string, params: any[]): TModel { - return this.prepare('get', query, params); - } - - public getAll(query: string, params: any[]): TModel[] { - return this.prepare('all', query, params); - } - - public run(query: string, params: any[]): void { - this.prepare('run', query, params); - } } diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts new file mode 100644 index 0000000..2b6685a --- /dev/null +++ b/src/databases/Postgres.ts @@ -0,0 +1,31 @@ +import { Logger } from '../utils/logger'; +import { IDatabase, QueryType } from './IDatabase'; +import { Pool } from 'pg'; + +export class Mysql implements IDatabase { + private pool: Pool; + + constructor(private config: any) {} + + init(): void { + this.pool = new Pool(); + } + + async prepare(type: QueryType, query: string, params: any[]) { + Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`); + const queryResult = await this.pool.query(query, params); + + switch (type) { + case 'get': { + return queryResult.rows[0]; + } + case 'all': { + return queryResult.rows; + } + case 'run': { + break; + } + } + } +} + diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index 9a3b1fd..5cf0104 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -12,7 +12,7 @@ export class Sqlite implements IDatabase { { } - prepare(type: QueryType, query: string, params: any[]) { + async prepare(type: QueryType, query: string, params: any[]) { const preparedQuery = this.db.prepare(query); switch (type) { @@ -29,20 +29,6 @@ export class Sqlite implements IDatabase { } } - get(query: string, params: any[]): TModel { - return this.prepare('get', query, params); - } - getAll(query: string, params: any[]): TModel[] { - return this.prepare('all', query, params); - } - run(query: string, params: any[]): void { - this.prepare('run', query, params); - } - - exec(query: string) { - return this.db.exec(query); - } - init() { // Make dirs if required if (!fs.existsSync(path.join(this.config.dbPath, "../"))) { diff --git a/src/databases/databases.ts b/src/databases/databases.ts index 3b9e719..ef75d0a 100644 --- a/src/databases/databases.ts +++ b/src/databases/databases.ts @@ -9,8 +9,7 @@ let privateDB: IDatabase; if (config.mysql) { db = new Mysql(config.mysql); privateDB = new Mysql(config.privateMysql); -} -else { +} else { db = new Sqlite({ dbPath: config.db, dbSchemaFileName: config.dbSchema, diff --git a/src/middleware/requestRateLimit.ts b/src/middleware/requestRateLimit.ts index 807d291..c99cc5a 100644 --- a/src/middleware/requestRateLimit.ts +++ b/src/middleware/requestRateLimit.ts @@ -16,8 +16,8 @@ export function rateLimitMiddleware(limitConfig: RateLimitConfig, getUserID?: (r keyGenerator: (req) => { return getHash(getIP(req), 1); }, - handler: (req, res, next) => { - if (getUserID === undefined || !isUserVIP(getHash(getUserID(req)))) { + handler: async (req, res, next) => { + if (getUserID === undefined || !await isUserVIP(getHash(getUserID(req)))) { return res.status(limitConfig.statusCode).send(limitConfig.message); } else { return next(); diff --git a/src/routes/addUserAsVIP.ts b/src/routes/addUserAsVIP.ts index a30493f..346ab98 100644 --- a/src/routes/addUserAsVIP.ts +++ b/src/routes/addUserAsVIP.ts @@ -27,14 +27,14 @@ export async function addUserAsVIP(req: Request, res: Response) { } //check to see if this user is already a vip - const row = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]); + const row = await db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]); if (enabled && row.userCount == 0) { //add them to the vip list - db.prepare('run', "INSERT INTO vipUsers VALUES(?)", [userID]); + await db.prepare('run', "INSERT INTO vipUsers VALUES(?)", [userID]); } else if (!enabled && row.userCount > 0) { //remove them from the shadow ban list - db.prepare('run', "DELETE FROM vipUsers WHERE userID = ?", [userID]); + await db.prepare('run', "DELETE FROM vipUsers WHERE userID = ?", [userID]); } res.sendStatus(200); diff --git a/src/routes/deleteNoSegments.ts b/src/routes/deleteNoSegments.ts index 5fa694e..6451e85 100644 --- a/src/routes/deleteNoSegments.ts +++ b/src/routes/deleteNoSegments.ts @@ -3,7 +3,7 @@ import {isUserVIP} from '../utils/isUserVIP'; import {getHash} from '../utils/getHash'; import {db} from '../databases/databases'; -export function deleteNoSegments(req: Request, res: Response) { +export async function deleteNoSegments(req: Request, res: Response) { // Collect user input data const videoID = req.body.videoID; let userID = req.body.userID; @@ -24,7 +24,7 @@ export function deleteNoSegments(req: Request, res: Response) { // Check if user is VIP userID = getHash(userID); - const userIsVIP = isUserVIP(userID); + const userIsVIP = await isUserVIP(userID); if (!userIsVIP) { res.status(403).json({ @@ -33,11 +33,13 @@ export function deleteNoSegments(req: Request, res: Response) { return; } - db.prepare("all", 'SELECT * FROM noSegments WHERE videoID = ?', [videoID]).filter((entry: any) => { + const entries = (await db.prepare("all", 'SELECT * FROM noSegments WHERE videoID = ?', [videoID])).filter((entry: any) => { return (categories.indexOf(entry.category) !== -1); - }).forEach((entry: any) => { - db.prepare('run', 'DELETE FROM noSegments WHERE videoID = ? AND category = ?', [videoID, entry.category]); }); + for (const entry of entries) { + await db.prepare('run', 'DELETE FROM noSegments WHERE videoID = ? AND category = ?', [videoID, entry.category]); + } + res.status(200).json({message: 'Removed no segments entrys for video ' + videoID}); } diff --git a/src/routes/getDaysSavedFormatted.ts b/src/routes/getDaysSavedFormatted.ts index 0179425..699e48d 100644 --- a/src/routes/getDaysSavedFormatted.ts +++ b/src/routes/getDaysSavedFormatted.ts @@ -1,8 +1,8 @@ import {db} from '../databases/databases'; import {Request, Response} from 'express'; -export function getDaysSavedFormatted(req: Request, res: Response) { - let row = db.prepare('get', "SELECT SUM((endTime - startTime) / 60 / 60 / 24 * views) as daysSaved from sponsorTimes where shadowHidden != 1", []); +export async function getDaysSavedFormatted(req: Request, res: Response) { + let row = await db.prepare('get', "SELECT SUM((endTime - startTime) / 60 / 60 / 24 * views) as daysSaved from sponsorTimes where shadowHidden != 1", []); if (row !== undefined) { //send this result diff --git a/src/routes/getIsUserVIP.ts b/src/routes/getIsUserVIP.ts index 597de25..927ca1f 100644 --- a/src/routes/getIsUserVIP.ts +++ b/src/routes/getIsUserVIP.ts @@ -4,7 +4,7 @@ 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 { +export async function getIsUserVIP(req: Request, res: Response): Promise { const userID = req.query.userID as UserID; if (userID == undefined) { @@ -17,7 +17,7 @@ export function getIsUserVIP(req: Request, res: Response): void { const hashedUserID: HashedUserID = getHash(userID); try { - let vipState = isUserVIP(hashedUserID); + let vipState = await isUserVIP(hashedUserID); res.status(200).json({ hashedUserID: hashedUserID, vip: vipState, diff --git a/src/routes/getSavedTimeForUser.ts b/src/routes/getSavedTimeForUser.ts index fbf28dc..0d54f59 100644 --- a/src/routes/getSavedTimeForUser.ts +++ b/src/routes/getSavedTimeForUser.ts @@ -2,7 +2,7 @@ import {db} from '../databases/databases'; import {Request, Response} from 'express'; import {getHash} from '../utils/getHash'; -export function getSavedTimeForUser(req: Request, res: Response) { +export async function getSavedTimeForUser(req: Request, res: Response) { let userID = req.query.userID as string; if (userID == undefined) { @@ -15,7 +15,7 @@ export function getSavedTimeForUser(req: Request, res: Response) { userID = getHash(userID); try { - let row = db.prepare("get", "SELECT SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE userID = ? AND votes > -1 AND shadowHidden != 1 ", [userID]); + let row = await db.prepare("get", "SELECT SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE userID = ? AND votes > -1 AND shadowHidden != 1 ", [userID]); if (row.minutesSaved != null) { res.send({ diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index 6b69e58..98d7178 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -11,8 +11,8 @@ import { Logger } from '../utils/logger'; import redis from '../utils/redis'; -function prepareCategorySegments(req: Request, videoID: VideoID, category: Category, segments: DBSegment[], cache: SegmentCache = {shadowHiddenSegmentIPs: {}}): Segment[] { - const filteredSegments = segments.filter((segment) => { +async function prepareCategorySegments(req: Request, videoID: VideoID, category: Category, segments: DBSegment[], cache: SegmentCache = {shadowHiddenSegmentIPs: {}}): Promise { + const shouldFilter: boolean[] = await Promise.all(segments.map(async (segment) => { if (segment.votes < -1) { return false; //too untrustworthy, just ignore it } @@ -24,7 +24,7 @@ function prepareCategorySegments(req: Request, videoID: VideoID, category: Categ } if (cache.shadowHiddenSegmentIPs[videoID] === undefined) { - cache.shadowHiddenSegmentIPs[videoID] = privateDB.prepare('all', 'SELECT hashedIP FROM sponsorTimes WHERE videoID = ?', [videoID]) as { hashedIP: HashedIP }[]; + cache.shadowHiddenSegmentIPs[videoID] = await privateDB.prepare('all', 'SELECT hashedIP FROM sponsorTimes WHERE videoID = ?', [videoID]) as { hashedIP: HashedIP }[]; } //if this isn't their ip, don't send it to them @@ -36,7 +36,9 @@ function prepareCategorySegments(req: Request, videoID: VideoID, category: Categ return shadowHiddenSegment.hashedIP === cache.userHashedIP; }); - }); + })); + + const filteredSegments = segments.filter((_, index) => shouldFilter[index]); return chooseSegments(filteredSegments).map((chosenSegment) => ({ category, @@ -45,17 +47,17 @@ function prepareCategorySegments(req: Request, videoID: VideoID, category: Categ })); } -function getSegmentsByVideoID(req: Request, videoID: string, categories: Category[]): Segment[] { +async function getSegmentsByVideoID(req: Request, videoID: string, categories: Category[]): Promise { const cache: SegmentCache = {shadowHiddenSegmentIPs: {}}; const segments: Segment[] = []; try { - const segmentsByCategory: SBRecord = db + const segmentsByCategory: SBRecord = (await db .prepare( 'all', `SELECT startTime, endTime, votes, locked, UUID, category, shadowHidden FROM sponsorTimes WHERE videoID = ? AND category IN (${Array(categories.length).fill('?').join()}) ORDER BY startTime`, [videoID, categories] - ).reduce((acc: SBRecord, segment: DBSegment) => { + )).reduce((acc: SBRecord, segment: DBSegment) => { acc[segment.category] = acc[segment.category] || []; acc[segment.category].push(segment); @@ -63,7 +65,7 @@ function getSegmentsByVideoID(req: Request, videoID: string, categories: Categor }, {}); for (const [category, categorySegments] of Object.entries(segmentsByCategory)) { - segments.push(...prepareCategorySegments(req, videoID as VideoID, category as Category, categorySegments, cache)); + segments.push(...(await prepareCategorySegments(req, videoID as VideoID, category as Category, categorySegments, cache))); } return segments; @@ -75,19 +77,19 @@ function getSegmentsByVideoID(req: Request, videoID: string, categories: Categor } } -function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, categories: Category[]): SBRecord { +async function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, categories: Category[]): Promise> { const cache: SegmentCache = {shadowHiddenSegmentIPs: {}}; const segments: SBRecord = {}; try { type SegmentWithHashPerVideoID = SBRecord}>; - const segmentPerVideoID: SegmentWithHashPerVideoID = db + const segmentPerVideoID: SegmentWithHashPerVideoID = (await db .prepare( 'all', `SELECT videoID, startTime, endTime, votes, locked, UUID, category, shadowHidden, hashedVideoID FROM sponsorTimes WHERE hashedVideoID LIKE ? AND category IN (${Array(categories.length).fill('?').join()}) ORDER BY startTime`, [hashedVideoIDPrefix + '%', categories] - ).reduce((acc: SegmentWithHashPerVideoID, segment: DBSegment) => { + )).reduce((acc: SegmentWithHashPerVideoID, segment: DBSegment) => { acc[segment.videoID] = acc[segment.videoID] || { hash: segment.hashedVideoID, segmentPerCategory: {}, @@ -107,7 +109,7 @@ function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, categ }; for (const [category, segmentPerCategory] of Object.entries(videoData.segmentPerCategory)) { - segments[videoID].segments.push(...prepareCategorySegments(req, videoID as VideoID, category as Category, segmentPerCategory, cache)); + segments[videoID].segments.push(...(await prepareCategorySegments(req, videoID as VideoID, category as Category, segmentPerCategory, cache))); } } @@ -241,7 +243,7 @@ async function handleGetSegments(req: Request, res: Response): Promise { - const userNames = []; - const viewCounts = []; - const totalSubmissions = []; - const minutesSaved = []; - const categoryStats: any[] = categoryStatsEnabled ? [] : undefined; +async function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boolean = false) { + const userNames = []; + const viewCounts = []; + const totalSubmissions = []; + const minutesSaved = []; + const categoryStats: any[] = categoryStatsEnabled ? [] : undefined; - let additionalFields = ''; + let additionalFields = ''; + if (categoryStatsEnabled) { + additionalFields += "SUM(CASE WHEN category = 'sponsor' THEN 1 ELSE 0 END) as categorySponsor, " + + "SUM(CASE WHEN category = 'intro' THEN 1 ELSE 0 END) as categorySumIntro, " + + "SUM(CASE WHEN category = 'outro' THEN 1 ELSE 0 END) as categorySumOutro, " + + "SUM(CASE WHEN category = 'interaction' THEN 1 ELSE 0 END) as categorySumInteraction, " + + "SUM(CASE WHEN category = 'selfpromo' THEN 1 ELSE 0 END) as categorySelfpromo, " + + "SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, "; + } + + const rows = await db.prepare('all', "SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," + + "SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, " + + "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 " + + "GROUP BY IFNULL(userName, sponsorTimes.userID) HAVING userVotes > 20 " + + "ORDER BY " + sortBy + " DESC LIMIT 100", []); + + for (let i = 0; i < rows.length; i++) { + userNames[i] = rows[i].userName; + + viewCounts[i] = rows[i].viewCount; + totalSubmissions[i] = rows[i].totalSubmissions; + minutesSaved[i] = rows[i].minutesSaved; if (categoryStatsEnabled) { - additionalFields += "SUM(CASE WHEN category = 'sponsor' THEN 1 ELSE 0 END) as categorySponsor, " + - "SUM(CASE WHEN category = 'intro' THEN 1 ELSE 0 END) as categorySumIntro, " + - "SUM(CASE WHEN category = 'outro' THEN 1 ELSE 0 END) as categorySumOutro, " + - "SUM(CASE WHEN category = 'interaction' THEN 1 ELSE 0 END) as categorySumInteraction, " + - "SUM(CASE WHEN category = 'selfpromo' THEN 1 ELSE 0 END) as categorySelfpromo, " + - "SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, "; + categoryStats[i] = [ + rows[i].categorySponsor, + rows[i].categorySumIntro, + rows[i].categorySumOutro, + rows[i].categorySumInteraction, + rows[i].categorySelfpromo, + rows[i].categoryMusicOfftopic, + ]; } + } - const rows = db.prepare('all', "SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," + - "SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, " + - "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 " + - "GROUP BY IFNULL(userName, sponsorTimes.userID) HAVING userVotes > 20 " + - "ORDER BY " + sortBy + " DESC LIMIT 100", []); - - for (let i = 0; i < rows.length; i++) { - userNames[i] = rows[i].userName; - - viewCounts[i] = rows[i].viewCount; - totalSubmissions[i] = rows[i].totalSubmissions; - minutesSaved[i] = rows[i].minutesSaved; - if (categoryStatsEnabled) { - categoryStats[i] = [ - rows[i].categorySponsor, - rows[i].categorySumIntro, - rows[i].categorySumOutro, - rows[i].categorySumInteraction, - rows[i].categorySelfpromo, - rows[i].categoryMusicOfftopic, - ]; - } - } - - resolve({ - userNames, - viewCounts, - totalSubmissions, - minutesSaved, - categoryStats, - }); - }); + return { + userNames, + viewCounts, + totalSubmissions, + minutesSaved, + categoryStats, + }; } export async function getTopUsers(req: Request, res: Response) { diff --git a/src/routes/getTotalStats.ts b/src/routes/getTotalStats.ts index b5e6bf8..dcc94e6 100644 --- a/src/routes/getTotalStats.ts +++ b/src/routes/getTotalStats.ts @@ -13,8 +13,8 @@ let apiUsersCache = 0; let lastUserCountCheck = 0; -export function getTotalStats(req: Request, res: Response) { - let row = db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " + +export async function getTotalStats(req: Request, res: Response) { + let row = await db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " + "SUM(views) as viewCount, SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE shadowHidden != 1 AND votes >= 0", []); if (row !== undefined) { diff --git a/src/routes/getUserInfo.ts b/src/routes/getUserInfo.ts index 99c7694..a8d92c7 100644 --- a/src/routes/getUserInfo.ts +++ b/src/routes/getUserInfo.ts @@ -3,9 +3,9 @@ import {getHash} from '../utils/getHash'; import {Request, Response} from 'express'; import {Logger} from '../utils/logger' -function dbGetSubmittedSegmentSummary(userID: string): any { +async function dbGetSubmittedSegmentSummary(userID: string): Promise<{ minutesSaved: number, segmentCount: number }> { try { - let row = db.prepare("get", "SELECT SUM(((endTime - startTime) / 60) * views) as minutesSaved, count(*) as segmentCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]); + let row = await db.prepare("get", "SELECT SUM(((endTime - startTime) / 60) * views) as minutesSaved, count(*) as segmentCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]); if (row.minutesSaved != null) { return { minutesSaved: row.minutesSaved, @@ -18,13 +18,13 @@ function dbGetSubmittedSegmentSummary(userID: string): any { }; } } catch (err) { - return false; + return null; } } -function dbGetUsername(userID: string) { +async function dbGetUsername(userID: string) { try { - let row = db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); + let row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); if (row !== undefined) { return row.userName; } else { @@ -36,9 +36,9 @@ function dbGetUsername(userID: string) { } } -function dbGetViewsForUser(userID: string) { +async function dbGetViewsForUser(userID: string) { try { - let row = db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]); + let row = await db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]); //increase the view count by one if (row.viewCount != null) { return row.viewCount; @@ -50,9 +50,9 @@ function dbGetViewsForUser(userID: string) { } } -function dbGetWarningsForUser(userID: string): number { +async function dbGetWarningsForUser(userID: string): Promise { try { - let rows = db.prepare('all', "SELECT * FROM warnings WHERE userID = ?", [userID]); + let rows = await db.prepare('all', "SELECT * FROM warnings WHERE userID = ?", [userID]); return rows.length; } catch (err) { Logger.error('Couldn\'t get warnings for user ' + userID + '. returning 0'); @@ -60,7 +60,7 @@ function dbGetWarningsForUser(userID: string): number { } } -export function getUserInfo(req: Request, res: Response) { +export async function getUserInfo(req: Request, res: Response) { let userID = req.query.userID as string; if (userID == undefined) { @@ -72,13 +72,17 @@ export function getUserInfo(req: Request, res: Response) { //hash the userID userID = getHash(userID); - const segmentsSummary = dbGetSubmittedSegmentSummary(userID); - res.send({ - userID, - userName: dbGetUsername(userID), - minutesSaved: segmentsSummary.minutesSaved, - segmentCount: segmentsSummary.segmentCount, - viewCount: dbGetViewsForUser(userID), - warnings: dbGetWarningsForUser(userID), - }); + const segmentsSummary = await dbGetSubmittedSegmentSummary(userID); + if (segmentsSummary) { + res.send({ + userID, + userName: await dbGetUsername(userID), + minutesSaved: segmentsSummary.minutesSaved, + segmentCount: segmentsSummary.segmentCount, + viewCount: await dbGetViewsForUser(userID), + warnings: await dbGetWarningsForUser(userID), + }); + } else { + res.status(400).send(); + } } diff --git a/src/routes/getUsername.ts b/src/routes/getUsername.ts index 3d78de9..ef8966a 100644 --- a/src/routes/getUsername.ts +++ b/src/routes/getUsername.ts @@ -3,7 +3,7 @@ import {getHash} from '../utils/getHash'; import {Logger} from '../utils/logger'; import {Request, Response} from 'express'; -export function getUsername(req: Request, res: Response) { +export async function getUsername(req: Request, res: Response) { let userID = req.query.userID as string; if (userID == undefined) { @@ -16,7 +16,7 @@ export function getUsername(req: Request, res: Response) { userID = getHash(userID); try { - let row = db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); + let row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); if (row !== undefined) { res.send({ diff --git a/src/routes/getViewsForUser.ts b/src/routes/getViewsForUser.ts index 1237100..2daba71 100644 --- a/src/routes/getViewsForUser.ts +++ b/src/routes/getViewsForUser.ts @@ -3,7 +3,7 @@ import {Request, Response} from 'express'; import {getHash} from '../utils/getHash'; import {Logger} from '../utils/logger'; -export function getViewsForUser(req: Request, res: Response) { +export async function getViewsForUser(req: Request, res: Response) { let userID = req.query.userID as string; if (userID == undefined) { @@ -16,7 +16,7 @@ export function getViewsForUser(req: Request, res: Response) { userID = getHash(userID); try { - let row = db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ?", [userID]); + let row = await db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ?", [userID]); //increase the view count by one if (row.viewCount != null) { diff --git a/src/routes/postNoSegments.ts b/src/routes/postNoSegments.ts index 04f6989..c92a19b 100644 --- a/src/routes/postNoSegments.ts +++ b/src/routes/postNoSegments.ts @@ -4,7 +4,7 @@ import {isUserVIP} from '../utils/isUserVIP'; import {db} from '../databases/databases'; import {Request, Response} from 'express'; -export function postNoSegments(req: Request, res: Response) { +export async function postNoSegments(req: Request, res: Response) { // Collect user input data let videoID = req.body.videoID; let userID = req.body.userID; @@ -25,7 +25,7 @@ export function postNoSegments(req: Request, res: Response) { // Check if user is VIP userID = getHash(userID); - let userIsVIP = isUserVIP(userID); + let userIsVIP = await isUserVIP(userID); if (!userIsVIP) { res.status(403).json({ @@ -35,7 +35,7 @@ export function postNoSegments(req: Request, res: Response) { } // Get existing no segment markers - let noSegmentList = db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID]); + let noSegmentList = await db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID]); if (!noSegmentList || noSegmentList.length === 0) { noSegmentList = []; } else { @@ -57,9 +57,9 @@ export function postNoSegments(req: Request, res: Response) { }); // create database entry - categoriesToMark.forEach((category) => { + for (const category of categoriesToMark) { try { - db.prepare('run', "INSERT INTO noSegments (videoID, userID, category) VALUES(?, ?, ?)", [videoID, userID, category]); + await db.prepare('run', "INSERT INTO noSegments (videoID, userID, category) VALUES(?, ?, ?)", [videoID, userID, category]); } catch (err) { Logger.error("Error submitting 'noSegment' marker for category '" + category + "' for video '" + videoID + "'"); Logger.error(err); @@ -67,7 +67,7 @@ export function postNoSegments(req: Request, res: Response) { message: "Internal Server Error: Could not write marker to the database.", }); } - }); + }; res.status(200).json({ submitted: categoriesToMark, diff --git a/src/routes/postSegmentShift.ts b/src/routes/postSegmentShift.ts index a885b06..47e0f85 100644 --- a/src/routes/postSegmentShift.ts +++ b/src/routes/postSegmentShift.ts @@ -45,7 +45,7 @@ function shiftSegment(segment: any, shift: { startTime: any; endTime: any }) { return {action: ACTION_NONE, segment}; } -export function postSegmentShift(req: Request, res: Response): Response { +export async function postSegmentShift(req: Request, res: Response): Promise { // Collect user input data const videoID = req.body.videoID; const startTime = req.body.startTime; @@ -66,7 +66,7 @@ export function postSegmentShift(req: Request, res: Response): Response { // Check if user is VIP userID = getHash(userID); - const userIsVIP = isUserVIP(userID); + const userIsVIP = await isUserVIP(userID); if (!userIsVIP) { res.status(403).json({ @@ -76,22 +76,23 @@ export function postSegmentShift(req: Request, res: Response): Response { } try { - const segments = db.prepare('all', 'SELECT startTime, endTime, UUID FROM sponsorTimes WHERE videoID = ?', [videoID]); + const segments = await db.prepare('all', 'SELECT startTime, endTime, UUID FROM sponsorTimes WHERE videoID = ?', [videoID]); const shift = { startTime, endTime, }; - segments.forEach((segment: any) => { + + for (const segment of segments) { const result = shiftSegment(segment, shift); switch (result.action) { case ACTION_UPDATE: - db.prepare('run', 'UPDATE sponsorTimes SET startTime = ?, endTime = ? WHERE UUID = ?', [result.segment.startTime, result.segment.endTime, result.segment.UUID]); + await db.prepare('run', 'UPDATE sponsorTimes SET startTime = ?, endTime = ? WHERE UUID = ?', [result.segment.startTime, result.segment.endTime, result.segment.UUID]); break; case ACTION_REMOVE: - db.prepare('run', 'UPDATE sponsorTimes SET startTime = ?, endTime = ?, votes = -2 WHERE UUID = ?', [result.segment.startTime, result.segment.endTime, result.segment.UUID]); + await db.prepare('run', 'UPDATE sponsorTimes SET startTime = ?, endTime = ?, votes = -2 WHERE UUID = ?', [result.segment.startTime, result.segment.endTime, result.segment.UUID]); break; } - }); + }; } catch (err) { Logger.error(err); res.sendStatus(500); diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index 1029f3f..d17f4f0 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -15,8 +15,8 @@ import { skipSegmentsKey } from '../middleware/redisKeys'; import redis from '../utils/redis'; -function sendWebhookNotification(userID: string, videoID: string, UUID: string, submissionCount: number, youtubeData: any, {submissionStart, submissionEnd}: { submissionStart: number; submissionEnd: number; }, segmentInfo: any) { - const row = db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); +async function sendWebhookNotification(userID: string, videoID: string, UUID: string, submissionCount: number, youtubeData: any, {submissionStart, submissionEnd}: { submissionStart: number; submissionEnd: number; }, segmentInfo: any) { + const row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); const userName = row !== undefined ? row.userName : null; const video = youtubeData.items[0]; @@ -45,9 +45,9 @@ function sendWebhookNotification(userID: string, videoID: string, UUID: string, }); } -function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo: any) { +async function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo: any) { if (config.youtubeAPIKey !== null) { - const userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]); + const userSubmissionCountRow = await db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]); YouTubeAPI.listVideos(videoID, (err: any, data: any) => { if (err || data.items.length === 0) { @@ -105,8 +105,8 @@ function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo } } -function sendWebhooksNB(userID: string, videoID: string, UUID: string, startTime: number, endTime: number, category: string, probability: number, ytData: any) { - const submissionInfoRow = db.prepare('get', "SELECT " + +async function sendWebhooksNB(userID: string, videoID: string, UUID: string, startTime: number, endTime: number, category: string, probability: number, ytData: any) { + const submissionInfoRow = await db.prepare('get', "SELECT " + "(select count(1) from sponsorTimes where userID = ?) count, " + "(select count(1) from sponsorTimes where userID = ? and votes <= -2) disregarded, " + "coalesce((select userName FROM userNames WHERE userID = ?), ?) userName", @@ -304,20 +304,20 @@ export async function postSkipSegments(req: Request, res: Response) { const MILLISECONDS_IN_HOUR = 3600000; const now = Date.now(); - const warningsCount = db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1", + const warningsCount = (await db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1", [userID, Math.floor(now - (config.hoursAfterWarningExpires * MILLISECONDS_IN_HOUR))], - ).count; + )).count; if (warningsCount >= config.maxNumberOfActiveWarnings) { return res.status(403).send('Submission rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes that are not malicious, and we just want to clarify the rules. Could you please send a message in Discord or Matrix so we can further help you?'); } - const noSegmentList = db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID]).map((list: any) => { + const noSegmentList = (await db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID])).map((list: any) => { return list.category; }); //check if this user is on the vip list - const isVIP = db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]).userCount > 0; + const isVIP = (await db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID])).userCount > 0; const decreaseVotes = 0; @@ -366,7 +366,7 @@ export async function postSkipSegments(req: Request, res: Response) { } //check if this info has already been submitted before - const duplicateCheck2Row = db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE startTime = ? " + + const duplicateCheck2Row = await db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE startTime = ? " + "and endTime = ? and category = ? and videoID = ?", [startTime, endTime, segments[i].category, videoID]); if (duplicateCheck2Row.count > 0) { res.sendStatus(409); @@ -401,7 +401,7 @@ export async function postSkipSegments(req: Request, res: Response) { // Disable IP ratelimiting for now if (false) { //check to see if this ip has submitted too many sponsors today - const rateLimitCheckRow = privateDB.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE hashedIP = ? AND videoID = ? AND timeSubmitted > ?", [hashedIP, videoID, yesterday]); + const rateLimitCheckRow = await privateDB.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE hashedIP = ? AND videoID = ? AND timeSubmitted > ?", [hashedIP, videoID, yesterday]); if (rateLimitCheckRow.count >= 10) { //too many sponsors for the same video from the same ip address @@ -414,7 +414,7 @@ export async function postSkipSegments(req: Request, res: Response) { // Disable max submissions for now if (false) { //check to see if the user has already submitted sponsors for this video - const duplicateCheckRow = db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE userID = ? and videoID = ?", [userID, videoID]); + const duplicateCheckRow = await db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE userID = ? and videoID = ?", [userID, videoID]); if (duplicateCheckRow.count >= 16) { //too many sponsors for the same video from the same user @@ -425,7 +425,7 @@ export async function postSkipSegments(req: Request, res: Response) { } //check to see if this user is shadowbanned - const shadowBanRow = privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?", [userID]); + const shadowBanRow = await privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?", [userID]); let shadowBanned = shadowBanRow.userCount; @@ -445,7 +445,7 @@ export async function postSkipSegments(req: Request, res: Response) { Logger.error("Error while submitting when connecting to YouTube API: " + err); } else { //get all segments for this video and user - const allSubmittedByUser = db.prepare('all', "SELECT startTime, endTime FROM sponsorTimes WHERE userID = ? and videoID = ? and votes > -1", [userID, videoID]); + const allSubmittedByUser = await db.prepare('all', "SELECT startTime, endTime FROM sponsorTimes WHERE userID = ? and videoID = ? and votes > -1", [userID, videoID]); const allSegmentTimes = []; if (allSubmittedByUser !== undefined) { //add segments the user has previously submitted @@ -489,7 +489,7 @@ export async function postSkipSegments(req: Request, res: Response) { const startingLocked = isVIP ? 1 : 0; try { - db.prepare('run', "INSERT INTO sponsorTimes " + + await db.prepare('run', "INSERT INTO sponsorTimes " + "(videoID, startTime, endTime, votes, locked, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID)" + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [ videoID, segmentInfo.segment[0], segmentInfo.segment[1], startingVotes, startingLocked, UUID, userID, timeSubmitted, 0, segmentInfo.category, shadowBanned, getHash(videoID, 1), @@ -497,7 +497,7 @@ export async function postSkipSegments(req: Request, res: Response) { ); //add to private db as well - privateDB.prepare('run', "INSERT INTO sponsorTimes VALUES(?, ?, ?)", [videoID, hashedIP, timeSubmitted]); + await privateDB.prepare('run', "INSERT INTO sponsorTimes VALUES(?, ?, ?)", [videoID, hashedIP, timeSubmitted]); // Clear redis cache for this video redis.delAsync(skipSegmentsKey(videoID)); diff --git a/src/routes/postWarning.ts b/src/routes/postWarning.ts index 6feedef..1d3a362 100644 --- a/src/routes/postWarning.ts +++ b/src/routes/postWarning.ts @@ -5,7 +5,7 @@ import {isUserVIP} from '../utils/isUserVIP'; import {getHash} from '../utils/getHash'; import { HashedUserID, UserID } from '../types/user.model'; -export function postWarning(req: Request, res: Response) { +export async function postWarning(req: Request, res: Response) { // Collect user input data let issuerUserID: HashedUserID = getHash( req.body.issuerUserID); let userID: UserID = req.body.userID; @@ -13,7 +13,7 @@ export function postWarning(req: Request, res: Response) { let enabled: boolean = req.body.enabled ?? true; // Ensure user is a VIP - if (!isUserVIP(issuerUserID)) { + if (!await isUserVIP(issuerUserID)) { Logger.warn("Permission violation: User " + issuerUserID + " attempted to warn user " + userID + "."); res.status(403).json({"message": "Not a VIP"}); return; @@ -22,17 +22,17 @@ export function postWarning(req: Request, res: Response) { let resultStatus = ""; if (enabled) { - let previousWarning = db.prepare('get', 'SELECT * FROM warnings WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]); + let previousWarning = await db.prepare('get', 'SELECT * FROM warnings WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]); if (!previousWarning) { - db.prepare('run', 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES (?, ?, ?, 1)', [userID, issueTime, issuerUserID]); + await db.prepare('run', 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES (?, ?, ?, 1)', [userID, issueTime, issuerUserID]); resultStatus = "issued to"; } else { res.status(409).send(); return; } } else { - db.prepare('run', 'UPDATE warnings SET enabled = 0 WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]); + await db.prepare('run', 'UPDATE warnings SET enabled = 0 WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]); resultStatus = "removed from"; } diff --git a/src/routes/setUsername.ts b/src/routes/setUsername.ts index 228a1b1..02ae415 100644 --- a/src/routes/setUsername.ts +++ b/src/routes/setUsername.ts @@ -4,7 +4,7 @@ import {db} from '../databases/databases'; import {getHash} from '../utils/getHash'; import {Request, Response} from 'express'; -export function setUsername(req: Request, res: Response) { +export async function setUsername(req: Request, res: Response) { let userID = req.query.userID as string; let userName = req.query.username as string; @@ -38,14 +38,14 @@ export function setUsername(req: Request, res: Response) { try { //check if username is already set - let row = db.prepare('get', "SELECT count(*) as count FROM userNames WHERE userID = ?", [userID]); + let row = await db.prepare('get', "SELECT count(*) as count FROM userNames WHERE userID = ?", [userID]); if (row.count > 0) { //already exists, update this row - db.prepare('run', "UPDATE userNames SET userName = ? WHERE userID = ?", [userName, userID]); + await db.prepare('run', "UPDATE userNames SET userName = ? WHERE userID = ?", [userName, userID]); } else { //add to the db - db.prepare('run', "INSERT INTO userNames VALUES(?, ?)", [userID, userName]); + await db.prepare('run', "INSERT INTO userNames VALUES(?, ?)", [userID, userName]); } res.sendStatus(200); diff --git a/src/routes/shadowBanUser.ts b/src/routes/shadowBanUser.ts index d782990..a115c7c 100644 --- a/src/routes/shadowBanUser.ts +++ b/src/routes/shadowBanUser.ts @@ -23,7 +23,7 @@ export async function shadowBanUser(req: Request, res: Response) { //hash the userID adminUserIDInput = getHash(adminUserIDInput); - const isVIP = db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [adminUserIDInput]).userCount > 0; + const isVIP = (await db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [adminUserIDInput])).userCount > 0; if (!isVIP) { //not authorized res.sendStatus(403); @@ -32,30 +32,30 @@ export async function shadowBanUser(req: Request, res: Response) { if (userID) { //check to see if this user is already shadowbanned - const row = privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?", [userID]); + const row = await privateDB.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 - privateDB.prepare('run', "INSERT INTO shadowBannedUsers VALUES(?)", [userID]); + await privateDB.prepare('run', "INSERT INTO shadowBannedUsers VALUES(?)", [userID]); //find all previous submissions and hide them if (unHideOldSubmissions) { - db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?" + await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?" + " AND NOT EXISTS ( SELECT videoID, category FROM noSegments WHERE" + " sponsorTimes.videoID = noSegments.videoID AND sponsorTimes.category = noSegments.category)", [userID]); } } else if (!enabled && row.userCount > 0) { //remove them from the shadow ban list - privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]); + await privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]); //find all previous submissions and unhide them if (unHideOldSubmissions) { - let segmentsToIgnore = db.prepare('all', "SELECT UUID FROM sponsorTimes st " + let segmentsToIgnore = (await db.prepare('all', "SELECT UUID FROM sponsorTimes st " + "JOIN noSegments ns on st.videoID = ns.videoID AND st.category = ns.category WHERE st.userID = ?" - , [userID]).map((item: {UUID: string}) => item.UUID); - let allSegments = db.prepare('all', "SELECT UUID FROM sponsorTimes st WHERE st.userID = ?", [userID]) + , [userID])).map((item: {UUID: string}) => item.UUID); + let allSegments = (await db.prepare('all', "SELECT UUID FROM sponsorTimes st WHERE st.userID = ?", [userID])) .map((item: {UUID: string}) => item.UUID); allSegments.filter((item: {uuid: string}) => { @@ -68,29 +68,29 @@ export async function shadowBanUser(req: Request, res: Response) { } else if (hashedIP) { //check to see if this user is already shadowbanned - // let row = privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedIPs WHERE hashedIP = ?", [hashedIP]); + // let row = await privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedIPs WHERE hashedIP = ?", [hashedIP]); // if (enabled && row.userCount == 0) { if (enabled) { //add them to the shadow ban list //add it to the table - // privateDB.prepare('run', "INSERT INTO shadowBannedIPs VALUES(?)", [hashedIP]); + // await privateDB.prepare('run', "INSERT INTO shadowBannedIPs VALUES(?)", [hashedIP]); //find all previous submissions and hide them if (unHideOldSubmissions) { - db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE timeSubmitted IN " + + await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE timeSubmitted IN " + "(SELECT privateDB.timeSubmitted FROM sponsorTimes LEFT JOIN privateDB.sponsorTimes as privateDB ON sponsorTimes.timeSubmitted=privateDB.timeSubmitted " + "WHERE privateDB.hashedIP = ?)", [hashedIP]); } } /*else if (!enabled && row.userCount > 0) { // //remove them from the shadow ban list - // privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]); + // await privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]); // //find all previous submissions and unhide them // if (unHideOldSubmissions) { - // db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE userID = ?", [userID]); + // await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE userID = ?", [userID]); // } }*/ } diff --git a/src/routes/viewedVideoSponsorTime.ts b/src/routes/viewedVideoSponsorTime.ts index 5d9dda6..41c6484 100644 --- a/src/routes/viewedVideoSponsorTime.ts +++ b/src/routes/viewedVideoSponsorTime.ts @@ -1,7 +1,7 @@ import {db} from '../databases/databases'; import {Request, Response} from 'express'; -export function viewedVideoSponsorTime(req: Request, res: Response): Response { +export async function viewedVideoSponsorTime(req: Request, res: Response): Promise { let UUID = req.query.UUID; if (UUID == undefined) { @@ -10,7 +10,7 @@ export function viewedVideoSponsorTime(req: Request, res: Response): Response { } //up the view count by one - db.prepare('run', "UPDATE sponsorTimes SET views = views + 1 WHERE UUID = ?", [UUID]); + await db.prepare('run', "UPDATE sponsorTimes SET views = views + 1 WHERE UUID = ?", [UUID]); return res.sendStatus(200); } diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 5fedb8e..41ca838 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -35,14 +35,14 @@ interface VoteData { oldIncrementAmount: number; } -function sendWebhooks(voteData: VoteData) { - const submissionInfoRow = db.prepare('get', "SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " + +async function sendWebhooks(voteData: VoteData) { + const submissionInfoRow = await db.prepare('get', "SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " + "(select count(1) from sponsorTimes where userID = s.userID) count, " + "(select count(1) from sponsorTimes where userID = s.userID and votes <= -2) disregarded " + "FROM sponsorTimes s left join userNames u on s.userID = u.userID where s.UUID=?", [voteData.UUID]); - const userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [voteData.nonAnonUserID]); + const userSubmissionCountRow = await db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [voteData.nonAnonUserID]); if (submissionInfoRow !== undefined && userSubmissionCountRow != undefined) { let webhookURL: string = null; @@ -141,9 +141,9 @@ function sendWebhooks(voteData: VoteData) { } } -function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string, hashedIP: string, res: Response) { +async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string, hashedIP: string, res: Response) { // Check if they've already made a vote - const usersLastVoteInfo = privateDB.prepare('get', "select count(*) as votes, category from categoryVotes where UUID = ? and userID = ?", [UUID, userID]); + const usersLastVoteInfo = await privateDB.prepare('get', "select count(*) as votes, category from categoryVotes where UUID = ? and userID = ?", [UUID, userID]); if (usersLastVoteInfo?.category === category) { // Double vote, ignore @@ -151,7 +151,7 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmiss return; } - const currentCategory = db.prepare('get', "select category from sponsorTimes where UUID = ?", [UUID]); + const currentCategory = await db.prepare('get', "select category from sponsorTimes where UUID = ?", [UUID]); if (!currentCategory) { // Submission doesn't exist res.status(400).send("Submission doesn't exist."); @@ -163,36 +163,36 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmiss return; } - const nextCategoryInfo = db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, category]); + const nextCategoryInfo = await db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, category]); const timeSubmitted = Date.now(); const voteAmount = isVIP ? 500 : 1; // Add the vote - if (db.prepare('get', "select count(*) as count from categoryVotes where UUID = ? and category = ?", [UUID, category]).count > 0) { + if ((await db.prepare('get', "select count(*) as count from categoryVotes where UUID = ? and category = ?", [UUID, category])).count > 0) { // Update the already existing db entry - db.prepare('run', "update categoryVotes set votes = votes + ? where UUID = ? and category = ?", [voteAmount, UUID, category]); + await db.prepare('run', "update categoryVotes set votes = votes + ? where UUID = ? and category = ?", [voteAmount, UUID, category]); } else { // Add a db entry - db.prepare('run', "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, category, voteAmount]); + await db.prepare('run', "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, category, voteAmount]); } // Add the info into the private db if (usersLastVoteInfo?.votes > 0) { // Reverse the previous vote - db.prepare('run', "update categoryVotes set votes = votes - ? where UUID = ? and category = ?", [voteAmount, UUID, usersLastVoteInfo.category]); + await db.prepare('run', "update categoryVotes set votes = votes - ? where UUID = ? and category = ?", [voteAmount, UUID, usersLastVoteInfo.category]); - privateDB.prepare('run', "update categoryVotes set category = ?, timeSubmitted = ?, hashedIP = ? where userID = ? and UUID = ?", [category, timeSubmitted, hashedIP, userID, UUID]); + await privateDB.prepare('run', "update categoryVotes set category = ?, timeSubmitted = ?, hashedIP = ? where userID = ? and UUID = ?", [category, timeSubmitted, hashedIP, userID, UUID]); } else { - privateDB.prepare('run', "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, userID, hashedIP, category, timeSubmitted]); + await privateDB.prepare('run', "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, userID, hashedIP, category, timeSubmitted]); } // See if the submissions category is ready to change - const currentCategoryInfo = db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, currentCategory.category]); + const currentCategoryInfo = await db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, currentCategory.category]); - const submissionInfo = db.prepare("get", "SELECT userID, timeSubmitted, votes FROM sponsorTimes WHERE UUID = ?", [UUID]); - const isSubmissionVIP = submissionInfo && isUserVIP(submissionInfo.userID); + const submissionInfo = await db.prepare("get", "SELECT userID, timeSubmitted, votes FROM sponsorTimes WHERE UUID = ?", [UUID]); + const isSubmissionVIP = submissionInfo && await isUserVIP(submissionInfo.userID); const startingVotes = isSubmissionVIP ? 10000 : 1; // Change this value from 1 in the future to make it harder to change categories @@ -201,9 +201,9 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmiss // Add submission as vote if (!currentCategoryInfo && submissionInfo) { - db.prepare("run", "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, currentCategory.category, currentCategoryCount]); + await db.prepare("run", "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, currentCategory.category, currentCategoryCount]); - privateDB.prepare("run", "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, submissionInfo.userID, "unknown", currentCategory.category, submissionInfo.timeSubmitted]); + await privateDB.prepare("run", "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, submissionInfo.userID, "unknown", currentCategory.category, submissionInfo.timeSubmitted]); } const nextCategoryCount = (nextCategoryInfo?.votes || 0) + voteAmount; @@ -212,7 +212,7 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmiss // VIPs change it every time if (nextCategoryCount - currentCategoryCount >= Math.max(Math.ceil(submissionInfo?.votes / 2), 2) || isVIP || isOwnSubmission) { // Replace the category - db.prepare('run', "update sponsorTimes set category = ? where UUID = ?", [category, UUID]); + await db.prepare('run', "update sponsorTimes set category = ? where UUID = ?", [category, UUID]); } res.sendStatus(200); @@ -245,20 +245,20 @@ export async function voteOnSponsorTime(req: Request, res: Response) { const hashedIP = getHash(ip + config.globalSalt); //check if this user is on the vip list - const isVIP = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [nonAnonUserID]).userCount > 0; + const isVIP = (await db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [nonAnonUserID])).userCount > 0; //check if user voting on own submission - const isOwnSubmission = db.prepare("get", "SELECT UUID as submissionCount FROM sponsorTimes where userID = ? AND UUID = ?", [nonAnonUserID, UUID]) !== undefined; + const isOwnSubmission = (await db.prepare("get", "SELECT UUID as submissionCount FROM sponsorTimes where userID = ? AND UUID = ?", [nonAnonUserID, UUID])) !== undefined; // If not upvote if (!isVIP && type !== 1) { - const isSegmentLocked = () => !!db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", [UUID])?.locked; - const isVideoLocked = () => !!db.prepare('get', 'SELECT noSegments.category from noSegments left join sponsorTimes' + + const isSegmentLocked = async () => !!(await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", [UUID]))?.locked; + const isVideoLocked = async () => !!(await db.prepare('get', 'SELECT noSegments.category from noSegments left join sponsorTimes' + ' on (noSegments.videoID = sponsorTimes.videoID and noSegments.category = sponsorTimes.category)' + - ' where UUID = ?', [UUID]); + ' where UUID = ?', [UUID])); - if (isSegmentLocked() || isVideoLocked()) { + if (await isSegmentLocked() || await isVideoLocked()) { res.status(403).send("Vote rejected: A moderator has decided that this segment is correct"); return; } @@ -270,7 +270,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { if (type == 1 && !isVIP && !isOwnSubmission) { // Check if upvoting hidden segment - const voteInfo = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", [UUID]); + const voteInfo = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", [UUID]); if (voteInfo && voteInfo.votes <= -2) { res.status(403).send("Not allowed to upvote segment with too many downvotes unless you are VIP."); @@ -280,9 +280,9 @@ export async function voteOnSponsorTime(req: Request, res: Response) { const MILLISECONDS_IN_HOUR = 3600000; const now = Date.now(); - const warningsCount = db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1", + const warningsCount = (await db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1", [nonAnonUserID, Math.floor(now - (config.hoursAfterWarningExpires * MILLISECONDS_IN_HOUR))], - ).count; + )).count; if (warningsCount >= config.maxNumberOfActiveWarnings) { return res.status(403).send('Vote rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes that are not malicious, and we just want to clarify the rules. Could you please send a message in Discord or Matrix so we can further help you?'); @@ -292,7 +292,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { try { //check if vote has already happened - const votesRow = privateDB.prepare('get', "SELECT type FROM votes WHERE userID = ? AND UUID = ?", [userID, UUID]); + const votesRow = await privateDB.prepare('get', "SELECT type FROM votes WHERE userID = ? AND UUID = ?", [userID, UUID]); //-1 for downvote, 1 for upvote. Maybe more depending on reputation in the future let incrementAmount = 0; @@ -338,7 +338,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { } //check if the increment amount should be multiplied (downvotes have more power if there have been many views) - const row = db.prepare('get', "SELECT videoID, votes, views FROM sponsorTimes WHERE UUID = ?", [UUID]) as + const row = await db.prepare('get', "SELECT videoID, votes, views FROM sponsorTimes WHERE UUID = ?", [UUID]) as {videoID: VideoID, votes: number, views: number}; if (voteTypeEnum === voteTypes.normal) { @@ -357,16 +357,16 @@ export async function voteOnSponsorTime(req: Request, res: Response) { // Only change the database if they have made a submission before and haven't voted recently const ableToVote = isVIP - || (db.prepare("get", "SELECT userID FROM sponsorTimes WHERE userID = ?", [nonAnonUserID]) !== undefined - && privateDB.prepare("get", "SELECT userID FROM shadowBannedUsers WHERE userID = ?", [nonAnonUserID]) === undefined - && privateDB.prepare("get", "SELECT UUID FROM votes WHERE UUID = ? AND hashedIP = ? AND userID != ?", [UUID, hashedIP, userID]) === undefined); + || ((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 privateDB.prepare("get", "SELECT UUID FROM votes WHERE UUID = ? AND hashedIP = ? AND userID != ?", [UUID, hashedIP, userID])) === undefined); if (ableToVote) { //update the votes table if (votesRow != undefined) { - privateDB.prepare('run', "UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?", [type, userID, UUID]); + await privateDB.prepare('run', "UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?", [type, userID, UUID]); } else { - privateDB.prepare('run', "INSERT INTO votes VALUES(?, ?, ?, ?)", [UUID, userID, hashedIP, type]); + await privateDB.prepare('run', "INSERT INTO votes VALUES(?, ?, ?, ?)", [UUID, userID, hashedIP, type]); } let columnName = ""; @@ -378,13 +378,13 @@ export async function voteOnSponsorTime(req: Request, res: Response) { //update the vote count on this sponsorTime //oldIncrementAmount will be zero is row is null - db.prepare('run', "UPDATE sponsorTimes SET " + columnName + " = " + columnName + " + ? WHERE UUID = ?", [incrementAmount - oldIncrementAmount, UUID]); + await db.prepare('run', "UPDATE sponsorTimes SET " + columnName + " = " + columnName + " + ? WHERE UUID = ?", [incrementAmount - oldIncrementAmount, UUID]); if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) { // Lock this submission - db.prepare('run', "UPDATE sponsorTimes SET locked = 1 WHERE UUID = ?", [UUID]); + await db.prepare('run', "UPDATE sponsorTimes SET locked = 1 WHERE UUID = ?", [UUID]); } else if (isVIP && incrementAmount < 0 && voteTypeEnum === voteTypes.normal) { // Unlock if a VIP downvotes it - db.prepare('run', "UPDATE sponsorTimes SET locked = 0 WHERE UUID = ?", [UUID]); + await db.prepare('run', "UPDATE sponsorTimes SET locked = 0 WHERE UUID = ?", [UUID]); } // Clear redis cache for this video @@ -393,7 +393,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { //for each positive vote, see if a hidden submission can be shown again if (incrementAmount > 0 && voteTypeEnum === voteTypes.normal) { //find the UUID that submitted the submission that was voted on - const submissionUserIDInfo = db.prepare('get', "SELECT userID FROM sponsorTimes WHERE UUID = ?", [UUID]); + const submissionUserIDInfo = await db.prepare('get', "SELECT userID FROM sponsorTimes WHERE UUID = ?", [UUID]); if (!submissionUserIDInfo) { // They are voting on a non-existent submission res.status(400).send("Voting on a non-existent submission"); @@ -403,14 +403,14 @@ export async function voteOnSponsorTime(req: Request, res: Response) { const submissionUserID = submissionUserIDInfo.userID; //check if any submissions are hidden - const hiddenSubmissionsRow = db.prepare('get', "SELECT count(*) as hiddenSubmissions FROM sponsorTimes WHERE userID = ? AND shadowHidden > 0", [submissionUserID]); + const hiddenSubmissionsRow = await db.prepare('get', "SELECT count(*) as hiddenSubmissions FROM sponsorTimes WHERE userID = ? AND shadowHidden > 0", [submissionUserID]); if (hiddenSubmissionsRow.hiddenSubmissions > 0) { //see if some of this users submissions should be visible again if (await isUserTrustworthy(submissionUserID)) { //they are trustworthy again, show 2 of their submissions again, if there are two to show - db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE ROWID IN (SELECT ROWID FROM sponsorTimes WHERE userID = ? AND shadowHidden = 1 LIMIT 2)", [submissionUserID]); + await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE ROWID IN (SELECT ROWID FROM sponsorTimes WHERE userID = ? AND shadowHidden = 1 LIMIT 2)", [submissionUserID]); } } } diff --git a/src/utils/isUserTrustworthy.ts b/src/utils/isUserTrustworthy.ts index 884ea2e..93bb6f0 100644 --- a/src/utils/isUserTrustworthy.ts +++ b/src/utils/isUserTrustworthy.ts @@ -6,11 +6,11 @@ import {db} from '../databases/databases'; */ export async function isUserTrustworthy(userID: string): Promise { //check to see if this user how many submissions this user has submitted - const totalSubmissionsRow = db.prepare('get', "SELECT count(*) as totalSubmissions, sum(votes) as voteSum FROM sponsorTimes WHERE userID = ?", [userID]); + const totalSubmissionsRow = await db.prepare('get', "SELECT count(*) as totalSubmissions, sum(votes) as voteSum FROM sponsorTimes WHERE userID = ?", [userID]); if (totalSubmissionsRow.totalSubmissions > 5) { //check if they have a high downvote ratio - const downvotedSubmissionsRow = db.prepare('get', "SELECT count(*) as downvotedSubmissions FROM sponsorTimes WHERE userID = ? AND (votes < 0 OR shadowHidden > 0)", [userID]); + const downvotedSubmissionsRow = await db.prepare('get', "SELECT count(*) as downvotedSubmissions FROM sponsorTimes WHERE userID = ? AND (votes < 0 OR shadowHidden > 0)", [userID]); return (downvotedSubmissionsRow.downvotedSubmissions / totalSubmissionsRow.totalSubmissions) < 0.6 || (totalSubmissionsRow.voteSum > downvotedSubmissionsRow.downvotedSubmissions); diff --git a/src/utils/isUserVIP.ts b/src/utils/isUserVIP.ts index 3704791..596ac62 100644 --- a/src/utils/isUserVIP.ts +++ b/src/utils/isUserVIP.ts @@ -1,8 +1,8 @@ import {db} from '../databases/databases'; import { HashedUserID } from '../types/user.model'; -export function isUserVIP(userID: HashedUserID): boolean { - return db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]).userCount > 0; +export async function isUserVIP(userID: HashedUserID): Promise { + return (await db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID])).userCount > 0; } diff --git a/test/cases/dbUpgrade.ts b/test/cases/dbUpgrade.ts index 03fd6c5..8762b5d 100644 --- a/test/cases/dbUpgrade.ts +++ b/test/cases/dbUpgrade.ts @@ -2,9 +2,9 @@ import {db, privateDB} from '../../src/databases/databases'; import {Done} from '../utils'; describe('dbUpgrade', () => { - it('Should update the database version when starting the application', (done: Done) => { - let dbVersion = db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version']).value; - let privateVersion = privateDB.prepare('get', 'SELECT key, value FROM config where key = ?', ['version']).value; + it('Should update the database version when starting the application', async (done: Done) => { + let dbVersion = (await db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version'])).value; + let privateVersion = (await privateDB.prepare('get', 'SELECT key, value FROM config where key = ?', ['version'])).value; if (dbVersion >= 1 && privateVersion >= 1) done(); else done('Versions are not at least 1. db is ' + dbVersion + ', private is ' + privateVersion); }); diff --git a/test/cases/noSegmentRecords.ts b/test/cases/noSegmentRecords.ts index 9437295..172227f 100644 --- a/test/cases/noSegmentRecords.ts +++ b/test/cases/noSegmentRecords.ts @@ -22,7 +22,7 @@ describe('noSegmentRecords', () => { }); it('Should update the database version when starting the application', (done: Done) => { - let version = db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version']).value; + let version = await db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version']).value; if (version > 1) done(); else done('Version isn\'t greater than 1. Version is ' + version); }); @@ -95,7 +95,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['no-segments-video-id-1']); + let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['no-segments-video-id-1']); if (result.length !== 4) { console.log(result); done("Expected 4 entrys in db, got " + result.length); @@ -129,7 +129,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['underscore']); + let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['underscore']); if (result.length !== 1) { console.log(result); done("Expected 1 entrys in db, got " + result.length); @@ -163,7 +163,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['bothCases']); + let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['bothCases']); if (result.length !== 1) { console.log(result); done("Expected 1 entrys in db, got " + result.length); @@ -197,7 +197,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['specialChar']); + let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['specialChar']); if (result.length !== 0) { console.log(result); done("Expected 0 entrys in db, got " + result.length); @@ -395,7 +395,7 @@ describe('noSegmentRecords', () => { }) .then(res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['delete-record']); + let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['delete-record']); if (result.length === 0) { done(); } else { @@ -426,7 +426,7 @@ describe('noSegmentRecords', () => { }) .then(res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['delete-record-1']); + let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['delete-record-1']); if (result.length === 1) { done(); } else { diff --git a/test/cases/oldSubmitSponsorTimes.ts b/test/cases/oldSubmitSponsorTimes.ts index 68a3242..616dec7 100644 --- a/test/cases/oldSubmitSponsorTimes.ts +++ b/test/cases/oldSubmitSponsorTimes.ts @@ -9,7 +9,7 @@ describe('postVideoSponsorTime (Old submission method)', () => { + "/api/postVideoSponsorTimes?videoID=dQw4w9WgXcQ&startTime=1&endTime=10&userID=test") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcQ"]); + let row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcQ"]); if (row.startTime === 1 && row.endTime === 10 && row.category === "sponsor") { done(); } else { @@ -32,7 +32,7 @@ describe('postVideoSponsorTime (Old submission method)', () => { }) .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcE"]); + let row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcE"]); if (row.startTime === 1 && row.endTime === 11 && row.category === "sponsor") { done(); } else { diff --git a/test/cases/postSkipSegments.ts b/test/cases/postSkipSegments.ts index 29f6ee8..9f1058a 100644 --- a/test/cases/postSkipSegments.ts +++ b/test/cases/postSkipSegments.ts @@ -53,7 +53,7 @@ describe('postSkipSegments', () => { }) .then(res => { if (res.status === 200) { - const row = db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]); + const row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]); if (row.startTime === 2 && row.endTime === 10 && row.category === "sponsor") { done(); } else { @@ -84,7 +84,7 @@ describe('postSkipSegments', () => { }) .then(res => { if (res.status === 200) { - const row = db.prepare('get', "SELECT startTime, endTime, locked, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcF"]); + const row = await db.prepare('get', "SELECT startTime, endTime, locked, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcF"]); if (row.startTime === 0 && row.endTime === 10 && row.locked === 0 && row.category === "sponsor") { done(); } else { @@ -115,7 +115,7 @@ describe('postSkipSegments', () => { }) .then(res => { if (res.status === 200) { - const row = db.prepare('get', "SELECT startTime, endTime, locked, category FROM sponsorTimes WHERE videoID = ?", ["vipuserIDSubmission"]); + const row = await db.prepare('get', "SELECT startTime, endTime, locked, category FROM sponsorTimes WHERE videoID = ?", ["vipuserIDSubmission"]); if (row.startTime === 0 && row.endTime === 10 && row.locked === 1 && row.category === "sponsor") { done(); } else { @@ -149,7 +149,7 @@ describe('postSkipSegments', () => { }) .then(res => { if (res.status === 200) { - const rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]); + const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]); let success = true; if (rows.length === 2) { for (const row of rows) { @@ -196,7 +196,7 @@ describe('postSkipSegments', () => { }) .then(res => { if (res.status === 200) { - const rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["L_jWHffIx5E"]); + const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["L_jWHffIx5E"]); let success = true; if (rows.length === 4) { for (const row of rows) { @@ -245,7 +245,7 @@ describe('postSkipSegments', () => { }) .then(res => { if (res.status === 400) { - const rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["n9rIGdXnSJc"]); + const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["n9rIGdXnSJc"]); let success = true; if (rows.length === 4) { for (const row of rows) { @@ -293,7 +293,7 @@ describe('postSkipSegments', () => { }) .then(res => { if (res.status === 400) { - const rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["80percent_video"]); + const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["80percent_video"]); let success = rows.length == 2; for (const row of rows) { if ((row.startTime === 2000 || row.endTime === 4000 || row.category === "sponsor") || diff --git a/test/cases/postWarning.ts b/test/cases/postWarning.ts index fa766ff..eda2761 100644 --- a/test/cases/postWarning.ts +++ b/test/cases/postWarning.ts @@ -23,7 +23,7 @@ describe('postWarning', () => { }) .then(async res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); + let row = await db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) { done(); } else { @@ -54,7 +54,7 @@ describe('postWarning', () => { }) .then(async res => { if (res.status === 409) { - let row = db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); + let row = await db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) { done(); } else { @@ -86,7 +86,7 @@ describe('postWarning', () => { }) .then(async res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); + let row = await db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); if (row?.enabled == 0) { done(); } else { diff --git a/test/cases/segmentShift.ts b/test/cases/segmentShift.ts index af87638..3b9caeb 100644 --- a/test/cases/segmentShift.ts +++ b/test/cases/segmentShift.ts @@ -21,13 +21,13 @@ function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, en } function dbSponsorTimesSetByUUID(db: IDatabase, UUID: string, startTime: number, endTime: number) { - db.prepare('run', `UPDATE sponsorTimes SET startTime = ?, endTime = ? WHERE UUID = ?`, [startTime, endTime, UUID]); + await db.prepare('run', `UPDATE sponsorTimes SET startTime = ?, endTime = ? WHERE UUID = ?`, [startTime, endTime, UUID]); } function dbSponsorTimesCompareExpect(db: IDatabase, expect: any) { for (let i = 0, len = expect.length; i < len; i++) { const expectSeg = expect[i]; - let seg = db.prepare('get', "SELECT startTime, endTime FROM sponsorTimes WHERE UUID = ?", [expectSeg.UUID]); + let seg = await db.prepare('get', "SELECT startTime, endTime FROM sponsorTimes WHERE UUID = ?", [expectSeg.UUID]); if ('removed' in expect) { if (expect.removed === true && seg.votes === -2) { return; diff --git a/test/cases/unBan.ts b/test/cases/unBan.ts index a9bc1da..5324c49 100644 --- a/test/cases/unBan.ts +++ b/test/cases/unBan.ts @@ -31,7 +31,7 @@ describe('unBan', () => { }) .then(async res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM sponsorTimes WHERE videoID = ? AND userID = ? AND shadowHidden = ?', ['unBan-videoID-0', 'testMan-unBan', 1]); + let result = await db.prepare('all', 'SELECT * FROM sponsorTimes WHERE videoID = ? AND userID = ? AND shadowHidden = ?', ['unBan-videoID-0', 'testMan-unBan', 1]); if (result.length !== 0) { console.log(result); done("Expected 0 banned entrys in db, got " + result.length); @@ -56,7 +56,7 @@ describe('unBan', () => { }) .then(async res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM sponsorTimes WHERE videoID = ? AND userID = ? AND shadowHidden = ?', ['unBan-videoID-1', 'testWoman-unBan', 1]); + let result = await db.prepare('all', 'SELECT * FROM sponsorTimes WHERE videoID = ? AND userID = ? AND shadowHidden = ?', ['unBan-videoID-1', 'testWoman-unBan', 1]); if (result.length !== 1) { console.log(result); done("Expected 1 banned entry1 in db, got " + result.length); @@ -81,7 +81,7 @@ describe('unBan', () => { }) .then(async res => { if (res.status === 200) { - let result = db.prepare('all', 'SELECT * FROM sponsorTimes WHERE userID = ? AND shadowHidden = ?', ['testEntity-unBan', 1]); + let result = await db.prepare('all', 'SELECT * FROM sponsorTimes WHERE userID = ? AND shadowHidden = ?', ['testEntity-unBan', 1]); if (result.length !== 1) { console.log(result); done("Expected 1 banned entry1 in db, got " + result.length); diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 8345448..c0d1a65 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -68,7 +68,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID&UUID=vote-uuid-0&type=1") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-0"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-0"]); if (row.votes === 3) { done(); } else { @@ -86,7 +86,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-2&type=0") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]); if (row.votes < 10) { done(); } else { @@ -104,7 +104,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID3&UUID=vote-uuid-2&type=0") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]); if (row.votes === 9) { done(); } else { @@ -122,7 +122,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID4&UUID=vote-uuid-1.6&type=0") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1.6"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1.6"]); if (row.votes === 10) { done(); } else { @@ -140,7 +140,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=hasNotSubmittedID&UUID=vote-uuid-1&type=1") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1"]); if (row.votes === 2) { done(); } else { @@ -158,7 +158,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=hasNotSubmittedID&UUID=vote-uuid-1.5&type=0") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1.5"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1.5"]); if (row.votes === 10) { done(); } else { @@ -176,7 +176,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-3&type=0") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-3"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-3"]); if (row.votes <= -2) { done(); } else { @@ -194,7 +194,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=own-submission-id&UUID=own-submission-uuid&type=0") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["own-submission-uuid"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["own-submission-uuid"]); if (row.votes <= -2) { done(); } else { @@ -212,7 +212,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=not-own-submission-uuid&type=0") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["not-own-submission-uuid"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["not-own-submission-uuid"]); if (row.votes === 499) { done(); } else { @@ -230,8 +230,8 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=intro") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]); - let categoryRows = db.prepare('all', "SELECT votes, category FROM categoryVotes WHERE UUID = ?", ["vote-uuid-4"]); + let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]); + let categoryRows = await db.prepare('all', "SELECT votes, category FROM categoryVotes WHERE UUID = ?", ["vote-uuid-4"]); if (row.category === "sponsor" && categoryRows.length === 2 && categoryRows[0]?.votes === 1 && categoryRows[0]?.category === "intro" && categoryRows[1]?.votes === 1 && categoryRows[1]?.category === "sponsor") { @@ -251,7 +251,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=incorrect-category&category=fakecategory") .then(res => { if (res.status === 400) { - let row = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["incorrect-category"]); + let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["incorrect-category"]); if (row.category === "sponsor") { done(); } else { @@ -269,8 +269,8 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=outro") .then(res => { if (res.status === 200) { - let submissionRow = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]); - let categoryRows = db.prepare('all', "SELECT votes, category FROM categoryVotes WHERE UUID = ?", ["vote-uuid-4"]); + let submissionRow = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]); + let categoryRows = await db.prepare('all', "SELECT votes, category FROM categoryVotes WHERE UUID = ?", ["vote-uuid-4"]); let introVotes = 0; let outroVotes = 0; let sponsorVotes = 0; @@ -299,7 +299,7 @@ describe('voteOnSponsorTime', () => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=incorrect-category-change&category=" + inputCat) .then(res => { - let row = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["incorrect-category-change"]); + let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["incorrect-category-change"]); if (row.category === assertCat) { callback(); } else { @@ -319,8 +319,8 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-5&category=outro") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); - let row2 = db.prepare('get', "SELECT votes FROM categoryVotes WHERE UUID = ? and category = ?", ["vote-uuid-5", "outro"]); + let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); + let row2 = await db.prepare('get', "SELECT votes FROM categoryVotes WHERE UUID = ? and category = ?", ["vote-uuid-5", "outro"]); if (row.category === "outro" && row2.votes === 500) { done(); } else { @@ -338,7 +338,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=testman&UUID=vote-uuid-5_1&category=outro") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); + let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); if (row.category === "outro") { done(); } else { @@ -382,7 +382,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-5&type=1") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); + let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); if (row.votes > -3) { done(); } else { @@ -452,7 +452,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=segment-locking-uuid-1&type=1") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", ["segment-locking-uuid-1"]); + let row = await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", ["segment-locking-uuid-1"]); if (row?.locked) { done(); } else { @@ -470,7 +470,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=segment-locking-uuid-1&type=0") .then(res => { if (res.status === 200) { - let row = db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", ["segment-locking-uuid-1"]); + let row = await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", ["segment-locking-uuid-1"]); if (!row?.locked) { done(); } else { From 46b42da5bdf7bae321533391befbab0b0cadb1cd Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 1 Mar 2021 21:37:35 -0500 Subject: [PATCH 04/16] Update tests to use promises --- src/databases/IDatabase.ts | 2 +- src/databases/Mysql.ts | 2 +- src/databases/Postgres.ts | 2 +- src/databases/Sqlite.ts | 20 ++++- src/routes/getSkipSegmentsByHash.ts | 2 +- test/cases/dbUpgrade.ts | 6 +- test/cases/getIsUserVIP.ts | 4 +- test/cases/getSavedTimeForUser.ts | 6 +- test/cases/getSegmentsByHash.ts | 11 ++- test/cases/getSkipSegments.ts | 134 ++++++++++++++-------------- test/cases/getUserInfo.ts | 46 +++++----- test/cases/noSegmentRecords.ts | 62 ++++++------- test/cases/oldGetSponsorTime.ts | 4 +- test/cases/oldSubmitSponsorTimes.ts | 6 +- test/cases/postSkipSegments.ts | 60 ++++++------- test/cases/postWarning.ts | 2 +- test/cases/segmentShift.ts | 30 +++---- test/cases/unBan.ts | 18 ++-- test/cases/voteOnSponsorTime.ts | 116 ++++++++++++------------ 19 files changed, 274 insertions(+), 259 deletions(-) diff --git a/src/databases/IDatabase.ts b/src/databases/IDatabase.ts index 4bac1e3..6001cdd 100644 --- a/src/databases/IDatabase.ts +++ b/src/databases/IDatabase.ts @@ -1,7 +1,7 @@ export interface IDatabase { init(): void; - prepare(type: QueryType, query: string, params: any[]): Promise; + prepare(type: QueryType, query: string, params?: any[]): Promise; } export type QueryType = 'get' | 'all' | 'run'; diff --git a/src/databases/Mysql.ts b/src/databases/Mysql.ts index 4d9c675..9ec78c7 100644 --- a/src/databases/Mysql.ts +++ b/src/databases/Mysql.ts @@ -13,7 +13,7 @@ export class Mysql implements IDatabase { this.connection = new MysqlInterface(this.config); } - prepare(type: QueryType, query: string, params: any[]) { + prepare(type: QueryType, query: string, params?: any[]) { Logger.debug(`prepare (mysql): type: ${type}, query: ${query}, params: ${params}`); const queryResult = this.connection.query(query, params); diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 2b6685a..3f63707 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -11,7 +11,7 @@ export class Mysql implements IDatabase { this.pool = new Pool(); } - async prepare(type: QueryType, query: string, params: any[]) { + async prepare(type: QueryType, query: string, params?: any[]) { Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`); const queryResult = await this.pool.query(query, params); diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index 5cf0104..ec3055d 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -12,18 +12,30 @@ export class Sqlite implements IDatabase { { } - async prepare(type: QueryType, query: string, params: any[]) { + async prepare(type: QueryType, query: string, params?: any[]) { const preparedQuery = this.db.prepare(query); switch (type) { case 'get': { - return preparedQuery.get(...params); + if (params) { + return preparedQuery.get(...params); + } else { + return preparedQuery.get(); + } } case 'all': { - return preparedQuery.all(...params); + if (params) { + return preparedQuery.all(...params); + } else { + return preparedQuery.all(); + } } case 'run': { - preparedQuery.run(...params); + if (params) { + preparedQuery.run(...params); + } else { + preparedQuery.run(); + } break; } } diff --git a/src/routes/getSkipSegmentsByHash.ts b/src/routes/getSkipSegmentsByHash.ts index 399400b..7313af0 100644 --- a/src/routes/getSkipSegmentsByHash.ts +++ b/src/routes/getSkipSegmentsByHash.ts @@ -29,7 +29,7 @@ export async function getSkipSegmentsByHash(req: Request, res: Response) { categories = categories.filter((item: any) => typeof item === "string"); // Get all video id's that match hash prefix - const segments = getSegmentsByHash(req, hashPrefix, categories); + const segments = await getSegmentsByHash(req, hashPrefix, categories); if (!segments) return res.status(404).json([]); diff --git a/test/cases/dbUpgrade.ts b/test/cases/dbUpgrade.ts index 8762b5d..12bfe92 100644 --- a/test/cases/dbUpgrade.ts +++ b/test/cases/dbUpgrade.ts @@ -2,10 +2,10 @@ import {db, privateDB} from '../../src/databases/databases'; import {Done} from '../utils'; describe('dbUpgrade', () => { - it('Should update the database version when starting the application', async (done: Done) => { + it('Should update the database version when starting the application', async () => { let dbVersion = (await db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version'])).value; let privateVersion = (await privateDB.prepare('get', 'SELECT key, value FROM config where key = ?', ['version'])).value; - if (dbVersion >= 1 && privateVersion >= 1) done(); - else done('Versions are not at least 1. db is ' + dbVersion + ', private is ' + privateVersion); + if (dbVersion >= 1 && privateVersion >= 1) return; + else return 'Versions are not at least 1. db is ' + dbVersion + ', private is ' + privateVersion; }); }); diff --git a/test/cases/getIsUserVIP.ts b/test/cases/getIsUserVIP.ts index 4f923e0..7610e4a 100644 --- a/test/cases/getIsUserVIP.ts +++ b/test/cases/getIsUserVIP.ts @@ -4,8 +4,8 @@ import {db} from '../../src/databases/databases'; import {getHash} from '../../src/utils/getHash'; describe('getIsUserVIP', () => { - before(() => { - db.exec("INSERT INTO vipUsers (userID) VALUES ('" + getHash("supertestman") + "')"); + before((done: Done) => { + db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("supertestman") + "')").then(done); }); it('Should be able to get a 200', (done: Done) => { diff --git a/test/cases/getSavedTimeForUser.ts b/test/cases/getSavedTimeForUser.ts index 4c96687..795f41b 100644 --- a/test/cases/getSavedTimeForUser.ts +++ b/test/cases/getSavedTimeForUser.ts @@ -4,9 +4,11 @@ import {db} from '../../src/databases/databases'; import {getHash} from '../../src/utils/getHash'; describe('getSavedTimeForUser', () => { - before(() => { + before(async () => { let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - db.exec(startOfQuery + "('getSavedTimeForUser', 1, 11, 2, 'abc1239999', '" + getHash("testman") + "', 0, 50, 'sponsor', 0, '" + getHash('getSavedTimeForUser', 0) + "')"); + await db.prepare("run", startOfQuery + "('getSavedTimeForUser', 1, 11, 2, 'abc1239999', '" + getHash("testman") + "', 0, 50, 'sponsor', 0, '" + getHash('getSavedTimeForUser', 0) + "')"); + + return; }); it('Should be able to get a 200', (done: Done) => { diff --git a/test/cases/getSegmentsByHash.ts b/test/cases/getSegmentsByHash.ts index 1d0975f..1f5597f 100644 --- a/test/cases/getSegmentsByHash.ts +++ b/test/cases/getSegmentsByHash.ts @@ -11,13 +11,12 @@ const sinonStub = mockManager.mock('listVideos'); sinonStub.callsFake(YouTubeApiMock.listVideos); describe('getSegmentsByHash', () => { - before(() => { + before(async () => { let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - db.exec(startOfQuery + "('getSegmentsByHash-0', 1, 10, 2, 'getSegmentsByHash-0-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('getSegmentsByHash-0', 1) + "')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 - db.exec(startOfQuery + "('getSegmentsByHash-0', 20, 30, 2, 'getSegmentsByHash-0-1', 'testman', 100, 150, 'intro', 0, '" + getHash('getSegmentsByHash-0', 1) + "')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 - db.exec(startOfQuery + "('getSegmentsByHash-noMatchHash', 40, 50, 2, 'getSegmentsByHash-noMatchHash', 'testman', 0, 50, 'sponsor', 0, 'fdaffnoMatchHash')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 - db.exec(startOfQuery + "('getSegmentsByHash-1', 60, 70, 2, 'getSegmentsByHash-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('getSegmentsByHash-1', 1) + "')"); // hash = 3272fa85ee0927f6073ef6f07ad5f3146047c1abba794cfa364d65ab9921692b - + await db.prepare("run", startOfQuery + "('getSegmentsByHash-0', 1, 10, 2, 'getSegmentsByHash-0-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('getSegmentsByHash-0', 1) + "')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 + await db.prepare("run", startOfQuery + "('getSegmentsByHash-0', 20, 30, 2, 'getSegmentsByHash-0-1', 'testman', 100, 150, 'intro', 0, '" + getHash('getSegmentsByHash-0', 1) + "')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 + await db.prepare("run", startOfQuery + "('getSegmentsByHash-noMatchHash', 40, 50, 2, 'getSegmentsByHash-noMatchHash', 'testman', 0, 50, 'sponsor', 0, 'fdaffnoMatchHash')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 + await db.prepare("run", startOfQuery + "('getSegmentsByHash-1', 60, 70, 2, 'getSegmentsByHash-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('getSegmentsByHash-1', 1) + "')"); // hash = 3272fa85ee0927f6073ef6f07ad5f3146047c1abba794cfa364d65ab9921692b }); it('Should be able to get a 200', (done: Done) => { diff --git a/test/cases/getSkipSegments.ts b/test/cases/getSkipSegments.ts index a68fdce..ae562e5 100644 --- a/test/cases/getSkipSegments.ts +++ b/test/cases/getSkipSegments.ts @@ -4,92 +4,94 @@ import {Done, getbaseURL} from '../utils'; import {getHash} from '../../src/utils/getHash'; describe('getSkipSegments', () => { - before(() => { + before(async () => { let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, locked, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - db.exec(startOfQuery + "('testtesttest', 1, 11, 2, 0, '1-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest', 1) + "')"); - db.exec(startOfQuery + "('testtesttest', 20, 33, 2, 0, '1-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('testtesttest', 1) + "')"); - db.exec(startOfQuery + "('testtesttest,test', 1, 11, 2, 0, '1-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest,test', 1) + "')"); - db.exec(startOfQuery + "('test3', 1, 11, 2, 0, '1-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')"); - db.exec(startOfQuery + "('test3', 7, 22, -3, 0, '1-uuid-5', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')"); - db.exec(startOfQuery + "('multiple', 1, 11, 2, 0, '1-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')"); - db.exec(startOfQuery + "('multiple', 20, 33, 2, 0, '1-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')"); - db.exec(startOfQuery + "('locked', 20, 33, 2, 1, '1-uuid-locked-8', 'testman', 0, 50, 'intro', 0, '" + getHash('locked', 1) + "')"); - db.exec(startOfQuery + "('locked', 20, 34, 100000, 0, '1-uuid-9', 'testman', 0, 50, 'intro', 0, '" + getHash('locked', 1) + "')"); + await db.prepare("run", startOfQuery + "('testtesttest', 1, 11, 2, 0, '1-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest', 1) + "')"); + await db.prepare("run", startOfQuery + "('testtesttest', 20, 33, 2, 0, '1-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('testtesttest', 1) + "')"); + await db.prepare("run", startOfQuery + "('testtesttest,test', 1, 11, 2, 0, '1-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest,test', 1) + "')"); + await db.prepare("run", startOfQuery + "('test3', 1, 11, 2, 0, '1-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')"); + await db.prepare("run", startOfQuery + "('test3', 7, 22, -3, 0, '1-uuid-5', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')"); + await db.prepare("run", startOfQuery + "('multiple', 1, 11, 2, 0, '1-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')"); + await db.prepare("run", startOfQuery + "('multiple', 20, 33, 2, 0, '1-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')"); + await db.prepare("run", startOfQuery + "('locked', 20, 33, 2, 1, '1-uuid-locked-8', 'testman', 0, 50, 'intro', 0, '" + getHash('locked', 1) + "')"); + await db.prepare("run", startOfQuery + "('locked', 20, 34, 100000, 0, '1-uuid-9', 'testman', 0, 50, 'intro', 0, '" + getHash('locked', 1) + "')"); + + return; }); - it('Should be able to get a time by category 1', (done: Done) => { + it('Should be able to get a time by category 1', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&category=sponsor") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const data = await res.json(); if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11 && data[0].category === "sponsor" && data[0].UUID === "1-uuid-0") { - done(); + return; } else { - done("Received incorrect body: " + (await res.text())); + return ("Received incorrect body: " + (await res.text())); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => "Couldn't call endpoint"); }); - it('Should be able to get a time by category 2', (done: Done) => { + it('Should be able to get a time by category 2', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&category=intro") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const data = await res.json(); if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33 && data[0].category === "intro" && data[0].UUID === "1-uuid-2") { - done(); + return; } else { - done("Received incorrect body: " + (await res.text())); + return ("Received incorrect body: " + (await res.text())); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => ("Couldn't call endpoint")); }); - it('Should be able to get a time by categories array', (done: Done) => { + it('Should be able to get a time by categories array', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"sponsor\"]") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const data = await res.json(); if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11 && data[0].category === "sponsor" && data[0].UUID === "1-uuid-0") { - done(); + return; } else { - done("Received incorrect body: " + (await res.text())); + return ("Received incorrect body: " + (await res.text())); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => ("Couldn't call endpoint")); }); - it('Should be able to get a time by categories array 2', (done: Done) => { + it('Should be able to get a time by categories array 2', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"intro\"]") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const data = await res.json(); if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33 && data[0].category === "intro" && data[0].UUID === "1-uuid-2") { - done(); + return; } else { - done("Received incorrect body: " + (await res.text())); + return ("Received incorrect body: " + (await res.text())); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => ("Couldn't call endpoint")); }); - it('Should be able to get multiple times by category', (done: Done) => { + it('Should be able to get multiple times by category', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=multiple&categories=[\"intro\"]") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const body = await res.text(); const data = JSON.parse(body); @@ -105,20 +107,20 @@ describe('getSkipSegments', () => { } } - if (success) done(); - else done("Received incorrect body: " + body); + if (success) return; + else return ("Received incorrect body: " + body); } else { - done("Received incorrect body: " + body); + return ("Received incorrect body: " + body); } } }) - .catch(err => done("Couldn't call endpoint\n\n" + err)); + .catch(err => ("Couldn't call endpoint\n\n" + err)); }); - it('Should be able to get multiple times by multiple categories', (done: Done) => { + it('Should be able to get multiple times by multiple categories', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"sponsor\", \"intro\"]") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const body = await res.text(); const data = JSON.parse(body); @@ -135,95 +137,95 @@ describe('getSkipSegments', () => { } } - if (success) done(); - else done("Received incorrect body: " + body); + if (success) return; + else return ("Received incorrect body: " + body); } else { - done("Received incorrect body: " + body); + return ("Received incorrect body: " + body); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => ("Couldn't call endpoint")); }); - it('Should be possible to send unexpected query parameters', (done: Done) => { + it('Should be possible to send unexpected query parameters', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&fakeparam=hello&category=sponsor") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const body = await res.text(); const data = JSON.parse(body); if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11 && data[0].category === "sponsor" && data[0].UUID === "1-uuid-0") { - done(); + return; } else { - done("Received incorrect body: " + body); + return ("Received incorrect body: " + body); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => ("Couldn't call endpoint")); }); - it('Low voted submissions should be hidden', (done: Done) => { + it('Low voted submissions should be hidden', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=test3&category=sponsor") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const body = await res.text(); const data = JSON.parse(body); if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11 && data[0].category === "sponsor" && data[0].UUID === "1-uuid-4") { - done(); + return; } else { - done("Received incorrect body: " + body); + return ("Received incorrect body: " + body); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => ("Couldn't call endpoint")); }); - it('Should return 404 if no segment found', (done: Done) => { + it('Should return 404 if no segment found', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=notarealvideo") .then(res => { - if (res.status !== 404) done("non 404 respone code: " + res.status); - else done(); // pass + if (res.status !== 404) return ("non 404 respone code: " + res.status); + else return; // pass }) - .catch(err => done("couldn't call endpoint")); + .catch(err => ("couldn't call endpoint")); }); - it('Should be able send a comma in a query param', (done: Done) => { + it('Should be able send a comma in a query param', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest,test&category=sponsor") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const body = await res.text(); const data = JSON.parse(body); if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11 && data[0].category === "sponsor" && data[0].UUID === "1-uuid-1") { - done(); + return; } else { - done("Received incorrect body: " + body); + return ("Received incorrect body: " + body); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => ("Couldn't call endpoint")); }); - it('Should always get locked segment', (done: Done) => { + it('Should always get locked segment', () => { fetch(getbaseURL() + "/api/skipSegments?videoID=locked&category=intro") .then(async res => { - if (res.status !== 200) done("Status code was: " + res.status); + if (res.status !== 200) return ("Status code was: " + res.status); else { const data = await res.json(); if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33 && data[0].category === "intro" && data[0].UUID === "1-uuid-locked-8") { - done(); + return; } else { - done("Received incorrect body: " + (await res.text())); + return ("Received incorrect body: " + (await res.text())); } } }) - .catch(err => done("Couldn't call endpoint")); + .catch(err => ("Couldn't call endpoint")); }); }); diff --git a/test/cases/getUserInfo.ts b/test/cases/getUserInfo.ts index 673364f..318f28b 100644 --- a/test/cases/getUserInfo.ts +++ b/test/cases/getUserInfo.ts @@ -4,30 +4,30 @@ import {db} from '../../src/databases/databases'; import {getHash} from '../../src/utils/getHash'; describe('getUserInfo', () => { - before(() => { + before(async () => { let startOfUserNamesQuery = "INSERT INTO userNames (userID, userName) VALUES"; - db.exec(startOfUserNamesQuery + "('" + getHash("getuserinfo_user_01") + "', 'Username user 01')"); + await db.prepare("run", startOfUserNamesQuery + "('" + getHash("getuserinfo_user_01") + "', 'Username user 01')"); let startOfSponsorTimesQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden) VALUES"; - db.exec(startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000001', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); - db.exec(startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000002', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); - db.exec(startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -1, 'uuid000003', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); - db.exec(startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -2, 'uuid000004', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 1)"); - db.exec(startOfSponsorTimesQuery + "('xzzzxxyyy', 1, 11, -5, 'uuid000005', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 1)"); - db.exec(startOfSponsorTimesQuery + "('zzzxxxyyy', 1, 11, 2, 'uuid000006', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 0)"); - db.exec(startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000007', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)"); - db.exec(startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000008', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000001', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000002', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); + await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -1, 'uuid000003', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); + await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -2, 'uuid000004', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 1)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xzzzxxyyy', 1, 11, -5, 'uuid000005', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 1)"); + await db.prepare("run", startOfSponsorTimesQuery + "('zzzxxxyyy', 1, 11, 2, 'uuid000006', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 0)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000007', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000008', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)"); - - db.exec("INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_0') + "', 10, 'getuserinfo_vip', 1)"); - db.exec("INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)"); - db.exec("INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)"); + + await db.prepare("run", "INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_0') + "', 10, 'getuserinfo_vip', 1)"); + await db.prepare("run", "INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)"); + await db.prepare("run", "INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)"); }); it('Should be able to get a 200', (done: Done) => { fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_01') .then(res => { if (res.status !== 200) done('non 200 (' + res.status + ')'); - else done(); // pass + else done(); // pass }) .catch(err => done('couldn\'t call endpoint')); }); @@ -41,7 +41,7 @@ describe('getUserInfo', () => { .catch(err => done('couldn\'t call endpoint')); }); - it('Should return info', (done: Done) => { + it('Should done(info', (done: Done) => { fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_01') .then(async res => { if (res.status !== 200) { @@ -61,7 +61,7 @@ describe('getUserInfo', () => { } } }) - .catch(err => done("couldn't call endpoint")); + .catch(err => ("couldn't call endpoint")); }); it('Should get warning data', (done: Done) => { @@ -75,7 +75,7 @@ describe('getUserInfo', () => { else done(); // pass } }) - .catch(err => done("couldn't call endpoint")); + .catch(err => ("couldn't call endpoint")); }); it('Should get multiple warnings', (done: Done) => { @@ -89,7 +89,7 @@ describe('getUserInfo', () => { else done(); // pass } }) - .catch(err => done("couldn't call endpoint")); + .catch(err => ("couldn't call endpoint")); }); it('Should not get warnings if noe', (done: Done) => { @@ -103,10 +103,10 @@ describe('getUserInfo', () => { else done(); // pass } }) - .catch(err => done("couldn't call endpoint")); + .catch(err => ("couldn't call endpoint")); }); - it('Should return userID for userName (No userName set)', (done: Done) => { + it('Should done(userID for userName (No userName set)', (done: Done) => { fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_02') .then(async res => { if (res.status !== 200) { @@ -114,11 +114,11 @@ describe('getUserInfo', () => { } else { const data = await res.json(); if (data.userName !== 'c2a28fd225e88f74945794ae85aef96001d4a1aaa1022c656f0dd48ac0a3ea0f') { - return done('Did not return userID for userName'); + done('Did not done(userID for userName'); } done(); // pass } }) - .catch(err => done('couldn\'t call endpoint')); + .catch(err => ('couldn\'t call endpoint')); }); }); diff --git a/test/cases/noSegmentRecords.ts b/test/cases/noSegmentRecords.ts index 172227f..a789b5d 100644 --- a/test/cases/noSegmentRecords.ts +++ b/test/cases/noSegmentRecords.ts @@ -5,26 +5,26 @@ import {db} from '../../src/databases/databases'; describe('noSegmentRecords', () => { - before(() => { - db.exec("INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser-noSegments") + "')"); - - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id', 'sponsor')"); - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id', 'intro')"); - - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id-1', 'sponsor')"); - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id-1', 'intro')"); - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'noSubmitVideo', 'sponsor')"); - - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record', 'sponsor')"); - - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record-1', 'sponsor')"); - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record-1', 'intro')"); + before(async () => { + await db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser-noSegments") + "')"); + + await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id', 'sponsor')"); + await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id', 'intro')"); + + await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id-1', 'sponsor')"); + await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id-1', 'intro')"); + await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'noSubmitVideo', 'sponsor')"); + + await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record', 'sponsor')"); + + await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record-1', 'sponsor')"); + await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record-1', 'intro')"); }); - it('Should update the database version when starting the application', (done: Done) => { - let version = await db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version']).value; - if (version > 1) done(); - else done('Version isn\'t greater than 1. Version is ' + version); + it('Should update the database version when starting the application', async () => { + let version = (await db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version'])).value; + if (version > 1) return; + else return 'Version isn\'t greater than 1. Version is ' + version; }); it('Should be able to submit categories not in video (http response)', (done: Done) => { @@ -221,7 +221,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify({}), }) - .then(res => { + .then(async res => { if (res.status === 400) { done(); } else { @@ -245,7 +245,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify(json), }) - .then(res => { + .then(async res => { if (res.status === 400) { done(); } else { @@ -269,7 +269,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify(json), }) - .then(res => { + .then(async res => { if (res.status === 400) { done(); } else { @@ -293,7 +293,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify(json), }) - .then(res => { + .then(async res => { if (res.status === 400) { done(); } else { @@ -317,7 +317,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify(json), }) - .then(res => { + .then(async res => { if (res.status === 400) { done(); } else { @@ -341,7 +341,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify(json), }) - .then(res => { + .then(async res => { if (res.status === 400) { done(); } else { @@ -367,7 +367,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify(json), }) - .then(res => { + .then(async res => { if (res.status === 403) { done(); } else { @@ -393,7 +393,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify(json), }) - .then(res => { + .then(async res => { if (res.status === 200) { let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['delete-record']); if (result.length === 0) { @@ -424,7 +424,7 @@ describe('noSegmentRecords', () => { }, body: JSON.stringify(json), }) - .then(res => { + .then(async res => { if (res.status === 200) { let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['delete-record-1']); if (result.length === 1) { @@ -460,7 +460,7 @@ describe('noSegmentRecords', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 403) { done(); } else { @@ -488,7 +488,7 @@ describe('noSegmentRecords', () => { }], },), }) - .then(res => { + .then(async res => { if (res.status === 403) { done(); } else { @@ -514,7 +514,7 @@ describe('noSegmentRecords', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 200) { done(); } else { @@ -539,7 +539,7 @@ describe('noSegmentRecords', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 200) { done(); } else { diff --git a/test/cases/oldGetSponsorTime.ts b/test/cases/oldGetSponsorTime.ts index 68482a0..067ea7d 100644 --- a/test/cases/oldGetSponsorTime.ts +++ b/test/cases/oldGetSponsorTime.ts @@ -19,8 +19,8 @@ import {getHash} from '../../src/utils/getHash'; describe('getVideoSponsorTime (Old get method)', () => { before(() => { let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - db.exec(startOfQuery + "('old-testtesttest', 1, 11, 2, 'uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('old-testtesttest', 1) + "')"); - db.exec(startOfQuery + "('old-testtesttest,test', 1, 11, 2, 'uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('old-testtesttest,test', 1) + "')"); + db.prepare("run", startOfQuery + "('old-testtesttest', 1, 11, 2, 'uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('old-testtesttest', 1) + "')"); + db.prepare("run", startOfQuery + "('old-testtesttest,test', 1, 11, 2, 'uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('old-testtesttest,test', 1) + "')"); }); it('Should be able to get a time', (done: Done) => { diff --git a/test/cases/oldSubmitSponsorTimes.ts b/test/cases/oldSubmitSponsorTimes.ts index 616dec7..96c4292 100644 --- a/test/cases/oldSubmitSponsorTimes.ts +++ b/test/cases/oldSubmitSponsorTimes.ts @@ -7,7 +7,7 @@ describe('postVideoSponsorTime (Old submission method)', () => { it('Should be able to submit a time (GET)', (done: Done) => { fetch(getbaseURL() + "/api/postVideoSponsorTimes?videoID=dQw4w9WgXcQ&startTime=1&endTime=10&userID=test") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcQ"]); if (row.startTime === 1 && row.endTime === 10 && row.category === "sponsor") { @@ -30,7 +30,7 @@ describe('postVideoSponsorTime (Old submission method)', () => { 'Content-Type': 'application/json', }, }) - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcE"]); if (row.startTime === 1 && row.endTime === 11 && row.category === "sponsor") { @@ -48,7 +48,7 @@ describe('postVideoSponsorTime (Old submission method)', () => { it('Should return 400 for missing params', (done: Done) => { fetch(getbaseURL() + "/api/postVideoSponsorTimes?startTime=1&endTime=10&userID=test") - .then(res => { + .then(async res => { if (res.status === 400) done(); else done("Status code was: " + res.status); }) diff --git a/test/cases/postSkipSegments.ts b/test/cases/postSkipSegments.ts index 9f1058a..faebba7 100644 --- a/test/cases/postSkipSegments.ts +++ b/test/cases/postSkipSegments.ts @@ -15,9 +15,9 @@ describe('postSkipSegments', () => { before(() => { let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - db.exec(startOfQuery + "('80percent_video', 0, 1000, 0, '80percent-uuid-0', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); - db.exec(startOfQuery + "('80percent_video', 1001, 1005, 0, '80percent-uuid-1', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); - db.exec(startOfQuery + "('80percent_video', 0, 5000, -2, '80percent-uuid-2', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); + db.prepare("run", startOfQuery + "('80percent_video', 0, 1000, 0, '80percent-uuid-0', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); + db.prepare("run", startOfQuery + "('80percent_video', 1001, 1005, 0, '80percent-uuid-1', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); + db.prepare("run", startOfQuery + "('80percent_video', 0, 5000, -2, '80percent-uuid-2', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); const now = Date.now(); const warnVip01Hash = getHash("warn-vip01"); @@ -27,20 +27,20 @@ describe('postSkipSegments', () => { const MILLISECONDS_IN_HOUR = 3600000; const warningExpireTime = MILLISECONDS_IN_HOUR * config.hoursAfterWarningExpires; const startOfWarningQuery = 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES'; - db.exec(startOfWarningQuery + "('" + warnUser01Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 1000)) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 2000)) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser03Hash + "', '" + now + "', '" + warnVip01Hash + "', 0)"); - db.exec(startOfWarningQuery + "('" + warnUser03Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 0)"); - db.exec(startOfWarningQuery + "('" + warnUser03Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser03Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 1000)) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 2000)) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser03Hash + "', '" + now + "', '" + warnVip01Hash + "', 0)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser03Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 0)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser03Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser03Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); - db.exec("INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUserSubmission") + "')"); + db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUserSubmission") + "')"); }); it('Should be able to submit a single time (Params method)', (done: Done) => { @@ -51,7 +51,7 @@ describe('postSkipSegments', () => { 'Content-Type': 'application/json', }, }) - .then(res => { + .then(async res => { if (res.status === 200) { const row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]); if (row.startTime === 2 && row.endTime === 10 && row.category === "sponsor") { @@ -82,7 +82,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 200) { const row = await db.prepare('get', "SELECT startTime, endTime, locked, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcF"]); if (row.startTime === 0 && row.endTime === 10 && row.locked === 0 && row.category === "sponsor") { @@ -113,7 +113,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 200) { const row = await db.prepare('get', "SELECT startTime, endTime, locked, category FROM sponsorTimes WHERE videoID = ?", ["vipuserIDSubmission"]); if (row.startTime === 0 && row.endTime === 10 && row.locked === 1 && row.category === "sponsor") { @@ -147,7 +147,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 200) { const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]); let success = true; @@ -194,7 +194,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 200) { const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["L_jWHffIx5E"]); let success = true; @@ -243,7 +243,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 400) { const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["n9rIGdXnSJc"]); let success = true; @@ -291,7 +291,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 400) { const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["80percent_video"]); let success = rows.length == 2; @@ -385,7 +385,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 403) { done(); // success } else { @@ -482,7 +482,7 @@ describe('postSkipSegments', () => { + "/api/postVideoSponsorTimes?startTime=9&endTime=10&userID=test", { method: 'POST', }) - .then(res => { + .then(async res => { if (res.status === 400) done(); else done(true); }) @@ -507,7 +507,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 400) done(); else done(true); }) @@ -525,7 +525,7 @@ describe('postSkipSegments', () => { videoID: "dQw4w9WgXcQ", }), }) - .then(res => { + .then(async res => { if (res.status === 400) done(); else done(true); }) @@ -550,7 +550,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 400) done(); else done(true); }) @@ -574,7 +574,7 @@ describe('postSkipSegments', () => { }], }), }) - .then(res => { + .then(async res => { if (res.status === 400) done(); else done(true); }) @@ -592,7 +592,7 @@ describe('postSkipSegments', () => { videoID: "dQw4w9WgXcQ", }), }) - .then(res => { + .then(async res => { if (res.status === 400) done(); else done(true); }) diff --git a/test/cases/postWarning.ts b/test/cases/postWarning.ts index eda2761..43627eb 100644 --- a/test/cases/postWarning.ts +++ b/test/cases/postWarning.ts @@ -5,7 +5,7 @@ import {getHash} from '../../src/utils/getHash'; describe('postWarning', () => { before(() => { - db.exec("INSERT INTO vipUsers (userID) VALUES ('" + getHash("warning-vip") + "')"); + db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("warning-vip") + "')"); }); it('Should be able to create warning if vip (exp 200)', (done: Done) => { diff --git a/test/cases/segmentShift.ts b/test/cases/segmentShift.ts index 3b9caeb..a8b7d08 100644 --- a/test/cases/segmentShift.ts +++ b/test/cases/segmentShift.ts @@ -11,7 +11,7 @@ function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, en views = 0, shadowHidden = 0, hashedVideoID = `hash_${UUID}`; - db.exec(`INSERT INTO + db.prepare("run", `INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES @@ -20,11 +20,11 @@ function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, en `); } -function dbSponsorTimesSetByUUID(db: IDatabase, UUID: string, startTime: number, endTime: number) { +async function dbSponsorTimesSetByUUID(db: IDatabase, UUID: string, startTime: number, endTime: number) { await db.prepare('run', `UPDATE sponsorTimes SET startTime = ?, endTime = ? WHERE UUID = ?`, [startTime, endTime, UUID]); } -function dbSponsorTimesCompareExpect(db: IDatabase, expect: any) { +async function dbSponsorTimesCompareExpect(db: IDatabase, expect: any) { for (let i = 0, len = expect.length; i < len; i++) { const expectSeg = expect[i]; let seg = await db.prepare('get', "SELECT startTime, endTime FROM sponsorTimes WHERE UUID = ?", [expectSeg.UUID]); @@ -56,7 +56,7 @@ describe('segmentShift', function () { dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid02', 'sponsor'); dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid03', 'interaction'); dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid04', 'outro'); - db.exec(`INSERT INTO vipUsers (userID) VALUES ('${vipUserID}')`); + db.prepare("run", `INSERT INTO vipUsers (userID) VALUES ('${vipUserID}')`); done(); }); @@ -82,7 +82,7 @@ describe('segmentShift', function () { endTime: 30, }), }) - .then(res => { + .then(async res => { done(res.status === 403 ? undefined : res.status); }) .catch(err => done(err)); @@ -101,7 +101,7 @@ describe('segmentShift', function () { endTime: 30, }), }) - .then(res => { + .then(async res => { if (res.status !== 200) return done(`Status code was ${res.status}`); const expect = [ { @@ -125,7 +125,7 @@ describe('segmentShift', function () { endTime: 130, }, ]; - done(dbSponsorTimesCompareExpect(db, expect)); + done(await dbSponsorTimesCompareExpect(db, expect)); }) .catch(err => done(err)); }); @@ -143,7 +143,7 @@ describe('segmentShift', function () { endTime: 75, }), }) - .then(res => { + .then(async res => { if (res.status !== 200) return done(`Status code was ${res.status}`); const expect = [ { @@ -167,7 +167,7 @@ describe('segmentShift', function () { endTime: 130, }, ]; - done(dbSponsorTimesCompareExpect(db, expect)); + done(await dbSponsorTimesCompareExpect(db, expect)); }) .catch(err => done(err)); }); @@ -185,7 +185,7 @@ describe('segmentShift', function () { endTime: 42, }), }) - .then(res => { + .then(async res => { if (res.status !== 200) return done(`Status code was ${res.status}`); const expect = [ { @@ -209,7 +209,7 @@ describe('segmentShift', function () { endTime: 130, }, ]; - done(dbSponsorTimesCompareExpect(db, expect)); + done(await dbSponsorTimesCompareExpect(db, expect)); }) .catch(err => done(err)); }); @@ -227,7 +227,7 @@ describe('segmentShift', function () { endTime: 95, }), }) - .then(res => { + .then(async res => { if (res.status !== 200) return done(`Status code was ${res.status}`); const expect = [ { @@ -251,7 +251,7 @@ describe('segmentShift', function () { endTime: 130, }, ]; - done(dbSponsorTimesCompareExpect(db, expect)); + done(await dbSponsorTimesCompareExpect(db, expect)); }) .catch(err => done(err)); }); @@ -269,7 +269,7 @@ describe('segmentShift', function () { endTime: 55, }), }) - .then(res => { + .then(async res => { if (res.status !== 200) return done(`Status code was ${res.status}`); const expect = [ { @@ -294,7 +294,7 @@ describe('segmentShift', function () { endTime: 120, }, ]; - done(dbSponsorTimesCompareExpect(db, expect)); + done(await dbSponsorTimesCompareExpect(db, expect)); }) .catch(err => done(err)); }); diff --git a/test/cases/unBan.ts b/test/cases/unBan.ts index 5324c49..fc0010c 100644 --- a/test/cases/unBan.ts +++ b/test/cases/unBan.ts @@ -8,18 +8,18 @@ import { Logger } from '../../src/utils/logger.js'; describe('unBan', () => { before(() => { - db.exec("INSERT INTO shadowBannedUsers VALUES('testMan-unBan')"); - db.exec("INSERT INTO shadowBannedUsers VALUES('testWoman-unBan')"); - db.exec("INSERT INTO shadowBannedUsers VALUES('testEntity-unBan')"); + db.prepare("run", "INSERT INTO shadowBannedUsers VALUES('testMan-unBan')"); + db.prepare("run", "INSERT INTO shadowBannedUsers VALUES('testWoman-unBan')"); + db.prepare("run", "INSERT INTO shadowBannedUsers VALUES('testEntity-unBan')"); - db.exec("INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser-unBan") + "')"); - db.exec("INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-unBan") + "', 'unBan-videoID-1', 'sponsor')"); + db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser-unBan") + "')"); + db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-unBan") + "', 'unBan-videoID-1', 'sponsor')"); let startOfInsertSegmentQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - db.exec(startOfInsertSegmentQuery + "('unBan-videoID-0', 1, 11, 2, 'unBan-uuid-0', 'testMan-unBan', 0, 50, 'sponsor', 1, '" + getHash('unBan-videoID-0', 1) + "')"); - db.exec(startOfInsertSegmentQuery + "('unBan-videoID-1', 1, 11, 2, 'unBan-uuid-1', 'testWoman-unBan', 0, 50, 'sponsor', 1, '" + getHash('unBan-videoID-1', 1) + "')"); - db.exec(startOfInsertSegmentQuery + "('unBan-videoID-1', 1, 11, 2, 'unBan-uuid-2', 'testEntity-unBan', 0, 60, 'sponsor', 1, '" + getHash('unBan-videoID-1', 1) + "')"); - db.exec(startOfInsertSegmentQuery + "('unBan-videoID-2', 1, 11, 2, 'unBan-uuid-3', 'testEntity-unBan', 0, 60, 'sponsor', 1, '" + getHash('unBan-videoID-2', 1) + "')"); + db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-0', 1, 11, 2, 'unBan-uuid-0', 'testMan-unBan', 0, 50, 'sponsor', 1, '" + getHash('unBan-videoID-0', 1) + "')"); + db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-1', 1, 11, 2, 'unBan-uuid-1', 'testWoman-unBan', 0, 50, 'sponsor', 1, '" + getHash('unBan-videoID-1', 1) + "')"); + db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-1', 1, 11, 2, 'unBan-uuid-2', 'testEntity-unBan', 0, 60, 'sponsor', 1, '" + getHash('unBan-videoID-1', 1) + "')"); + db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-2', 1, 11, 2, 'unBan-uuid-3', 'testEntity-unBan', 0, 60, 'sponsor', 1, '" + getHash('unBan-videoID-2', 1) + "')"); }); it('Should be able to unban a user and re-enable shadow banned segments', (done) => { diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index c0d1a65..0b7eae2 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -22,51 +22,51 @@ describe('voteOnSponsorTime', () => { let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; const startOfWarningQuery = 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES'; - db.exec(startOfQuery + "('vote-testtesttest', 1, 11, 2, 'vote-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest', 1) + "')"); - db.exec(startOfQuery + "('vote-testtesttest2', 1, 11, 2, 'vote-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest2', 1) + "')"); - db.exec(startOfQuery + "('vote-testtesttest2', 1, 11, 10, 'vote-uuid-1.5', 'testman', 0, 50, 'outro', 0, '" + getHash('vote-testtesttest2', 1) + "')"); - db.exec(startOfQuery + "('vote-testtesttest2', 1, 11, 10, 'vote-uuid-1.6', 'testman', 0, 50, 'interaction', 0, '" + getHash('vote-testtesttest2', 1) + "')"); - db.exec(startOfQuery + "('vote-testtesttest3', 20, 33, 10, 'vote-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-testtesttest3', 1) + "')"); - db.exec(startOfQuery + "('vote-testtesttest,test', 1, 11, 100, 'vote-uuid-3', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest,test', 1) + "')"); - db.exec(startOfQuery + "('vote-test3', 1, 11, 2, 'vote-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-test3', 1) + "')"); - db.exec(startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); - db.exec(startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5_1', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); - db.exec(startOfQuery + "('vote-multiple', 1, 11, 2, 'vote-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); - db.exec(startOfQuery + "('vote-multiple', 20, 33, 2, 'vote-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); - db.exec(startOfQuery + "('voter-submitter', 1, 11, 2, 'vote-uuid-8', '" + getHash("randomID") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter', 1) + "')"); - db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-9', '" + getHash("randomID2") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); - db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-10', '" + getHash("randomID3") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); - db.exec(startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-11', '" + getHash("randomID4") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); - db.exec(startOfQuery + "('own-submission-video', 1, 11, 500, 'own-submission-uuid', '" + getHash('own-submission-id') + "', 0, 50, 'sponsor', 0, '" + getHash('own-submission-video', 1) + "')"); - db.exec(startOfQuery + "('not-own-submission-video', 1, 11, 500, 'not-own-submission-uuid', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('not-own-submission-video', 1) + "')"); - db.exec(startOfQuery + "('incorrect-category', 1, 11, 500, 'incorrect-category', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('incorrect-category', 1) + "')"); - db.exec(startOfQuery + "('incorrect-category-change', 1, 11, 500, 'incorrect-category-change', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('incorrect-category-change', 1) + "')"); - db.exec(startOfQuery + "('vote-testtesttest', 1, 11, 2, 'warnvote-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest', 1) + "')"); - db.exec(startOfQuery + "('no-sponsor-segments-video', 1, 11, 2, 'no-sponsor-segments-uuid-0', 'no-sponsor-segments', 0, 50, 'sponsor', 0, '" + getHash('no-sponsor-segments-video', 1) + "')"); - db.exec(startOfQuery + "('no-sponsor-segments-video', 1, 11, 2, 'no-sponsor-segments-uuid-1', 'no-sponsor-segments', 0, 50, 'intro', 0, '" + getHash('no-sponsor-segments-video', 1) + "')"); - db.exec(startOfQuery + "('segment-locking-video', 1, 11, 2, 'segment-locking-uuid-1', 'segment-locking-user', 0, 50, 'intro', 0, '" + getHash('segment-locking-video', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-testtesttest', 1, 11, 2, 'vote-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 2, 'vote-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest2', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 10, 'vote-uuid-1.5', 'testman', 0, 50, 'outro', 0, '" + getHash('vote-testtesttest2', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 10, 'vote-uuid-1.6', 'testman', 0, 50, 'interaction', 0, '" + getHash('vote-testtesttest2', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-testtesttest3', 20, 33, 10, 'vote-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-testtesttest3', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-testtesttest,test', 1, 11, 100, 'vote-uuid-3', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest,test', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-test3', 1, 11, 2, 'vote-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-test3', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5_1', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-multiple', 1, 11, 2, 'vote-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-multiple', 20, 33, 2, 'vote-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); + db.prepare("run", startOfQuery + "('voter-submitter', 1, 11, 2, 'vote-uuid-8', '" + getHash("randomID") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter', 1) + "')"); + db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-9', '" + getHash("randomID2") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); + db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-10', '" + getHash("randomID3") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); + db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-11', '" + getHash("randomID4") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); + db.prepare("run", startOfQuery + "('own-submission-video', 1, 11, 500, 'own-submission-uuid', '" + getHash('own-submission-id') + "', 0, 50, 'sponsor', 0, '" + getHash('own-submission-video', 1) + "')"); + db.prepare("run", startOfQuery + "('not-own-submission-video', 1, 11, 500, 'not-own-submission-uuid', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('not-own-submission-video', 1) + "')"); + db.prepare("run", startOfQuery + "('incorrect-category', 1, 11, 500, 'incorrect-category', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('incorrect-category', 1) + "')"); + db.prepare("run", startOfQuery + "('incorrect-category-change', 1, 11, 500, 'incorrect-category-change', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('incorrect-category-change', 1) + "')"); + db.prepare("run", startOfQuery + "('vote-testtesttest', 1, 11, 2, 'warnvote-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest', 1) + "')"); + db.prepare("run", startOfQuery + "('no-sponsor-segments-video', 1, 11, 2, 'no-sponsor-segments-uuid-0', 'no-sponsor-segments', 0, 50, 'sponsor', 0, '" + getHash('no-sponsor-segments-video', 1) + "')"); + db.prepare("run", startOfQuery + "('no-sponsor-segments-video', 1, 11, 2, 'no-sponsor-segments-uuid-1', 'no-sponsor-segments', 0, 50, 'intro', 0, '" + getHash('no-sponsor-segments-video', 1) + "')"); + db.prepare("run", startOfQuery + "('segment-locking-video', 1, 11, 2, 'segment-locking-uuid-1', 'segment-locking-user', 0, 50, 'intro', 0, '" + getHash('segment-locking-video', 1) + "')"); - db.exec(startOfWarningQuery + "('" + warnUser01Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 1000)) + "', '" + warnVip01Hash + "', 1)"); - db.exec(startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 2000)) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 1000)) + "', '" + warnVip01Hash + "', 1)"); + db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 2000)) + "', '" + warnVip01Hash + "', 1)"); - db.exec("INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser") + "')"); - privateDB.exec("INSERT INTO shadowBannedUsers (userID) VALUES ('" + getHash("randomID4") + "')"); + db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser") + "')"); + privateDB.prepare("run", "INSERT INTO shadowBannedUsers (userID) VALUES ('" + getHash("randomID4") + "')"); - db.exec("INSERT INTO noSegments (videoID, userID, category) VALUES ('no-sponsor-segments-video', 'someUser', 'sponsor')"); + db.prepare("run", "INSERT INTO noSegments (videoID, userID, category) VALUES ('no-sponsor-segments-video', 'someUser', 'sponsor')"); }); it('Should be able to upvote a segment', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID&UUID=vote-uuid-0&type=1") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-0"]); if (row.votes === 3) { @@ -84,7 +84,7 @@ describe('voteOnSponsorTime', () => { it('Should be able to downvote a segment', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-2&type=0") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]); if (row.votes < 10) { @@ -102,7 +102,7 @@ describe('voteOnSponsorTime', () => { it('Should not be able to downvote the same segment when voting from a different user on the same IP', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID3&UUID=vote-uuid-2&type=0") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]); if (row.votes === 9) { @@ -120,7 +120,7 @@ describe('voteOnSponsorTime', () => { it("Should not be able to downvote a segment if the user is shadow banned", (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID4&UUID=vote-uuid-1.6&type=0") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1.6"]); if (row.votes === 10) { @@ -138,7 +138,7 @@ describe('voteOnSponsorTime', () => { it("Should not be able to upvote a segment if the user hasn't submitted yet", (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=hasNotSubmittedID&UUID=vote-uuid-1&type=1") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1"]); if (row.votes === 2) { @@ -156,7 +156,7 @@ describe('voteOnSponsorTime', () => { it("Should not be able to downvote a segment if the user hasn't submitted yet", (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=hasNotSubmittedID&UUID=vote-uuid-1.5&type=0") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1.5"]); if (row.votes === 10) { @@ -174,7 +174,7 @@ describe('voteOnSponsorTime', () => { it('VIP should be able to completely downvote a segment', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-3&type=0") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-3"]); if (row.votes <= -2) { @@ -192,7 +192,7 @@ describe('voteOnSponsorTime', () => { it('should be able to completely downvote your own segment', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=own-submission-id&UUID=own-submission-uuid&type=0") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["own-submission-uuid"]); if (row.votes <= -2) { @@ -210,7 +210,7 @@ describe('voteOnSponsorTime', () => { it('should not be able to completely downvote somebody elses segment', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=not-own-submission-uuid&type=0") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["not-own-submission-uuid"]); if (row.votes === 499) { @@ -228,7 +228,7 @@ describe('voteOnSponsorTime', () => { it('Should be able to vote for a category and it should add your vote to the database', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=intro") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]); let categoryRows = await db.prepare('all', "SELECT votes, category FROM categoryVotes WHERE UUID = ?", ["vote-uuid-4"]); @@ -249,7 +249,7 @@ describe('voteOnSponsorTime', () => { it('Should not able to change to an invalid category', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=incorrect-category&category=fakecategory") - .then(res => { + .then(async res => { if (res.status === 400) { let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["incorrect-category"]); if (row.category === "sponsor") { @@ -267,7 +267,7 @@ describe('voteOnSponsorTime', () => { it('Should be able to change your vote for a category and it should add your vote to the database', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=outro") - .then(res => { + .then(async res => { if (res.status === 200) { let submissionRow = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]); let categoryRows = await db.prepare('all', "SELECT votes, category FROM categoryVotes WHERE UUID = ?", ["vote-uuid-4"]); @@ -298,7 +298,7 @@ describe('voteOnSponsorTime', () => { const vote = (inputCat: string, assertCat: string, callback: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=incorrect-category-change&category=" + inputCat) - .then(res => { + .then(async res => { let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["incorrect-category-change"]); if (row.category === assertCat) { callback(); @@ -317,7 +317,7 @@ describe('voteOnSponsorTime', () => { it('VIP should be able to vote for a category and it should immediately change', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-5&category=outro") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); let row2 = await db.prepare('get', "SELECT votes FROM categoryVotes WHERE UUID = ? and category = ?", ["vote-uuid-5", "outro"]); @@ -336,7 +336,7 @@ describe('voteOnSponsorTime', () => { it('Submitter should be able to vote for a category and it should immediately change', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=testman&UUID=vote-uuid-5_1&category=outro") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); if (row.category === "outro") { @@ -354,7 +354,7 @@ describe('voteOnSponsorTime', () => { it('Should not be able to category-vote on an invalid UUID submission', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID3&UUID=invalid-uuid&category=intro") - .then(res => { + .then(async res => { if (res.status === 400) { done(); } else { @@ -367,7 +367,7 @@ describe('voteOnSponsorTime', () => { it('Non-VIP should not be able to upvote "dead" submission', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-5&type=1") - .then(res => { + .then(async res => { if (res.status === 403) { done(); } else { @@ -380,7 +380,7 @@ describe('voteOnSponsorTime', () => { it('VIP should be able to upvote "dead" submission', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-5&type=1") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); if (row.votes > -3) { @@ -398,7 +398,7 @@ describe('voteOnSponsorTime', () => { it('Should not be able to upvote a segment (Too many warning)', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=warn-voteuser01&UUID=warnvote-uuid-0&type=1") - .then(res => { + .then(async res => { if (res.status === 403) { done(); // success } else { @@ -411,7 +411,7 @@ describe('voteOnSponsorTime', () => { it('Non-VIP should not be able to downvote on a segment with no-segments category', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=no-segments-voter&UUID=no-sponsor-segments-uuid-0&type=0") - .then(res => { + .then(async res => { if (res.status === 403) { done(); } else { @@ -424,7 +424,7 @@ describe('voteOnSponsorTime', () => { it('Non-VIP should be able to upvote on a segment with no-segments category', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=no-segments-voter&UUID=no-sponsor-segments-uuid-0&type=1") - .then(res => { + .then(async res => { if (res.status === 200) { done(); } else { @@ -437,7 +437,7 @@ describe('voteOnSponsorTime', () => { it('Non-VIP should not be able to category vote on a segment with no-segments category', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=no-segments-voter&UUID=no-sponsor-segments-uuid-0&category=outro") - .then(res => { + .then(async res => { if (res.status === 403) { done(); } else { @@ -450,7 +450,7 @@ describe('voteOnSponsorTime', () => { it('VIP upvote should lock segment', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=VIPUser&UUID=segment-locking-uuid-1&type=1") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", ["segment-locking-uuid-1"]); if (row?.locked) { @@ -468,7 +468,7 @@ describe('voteOnSponsorTime', () => { it('VIP downvote should unlock segment', (done: Done) => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=VIPUser&UUID=segment-locking-uuid-1&type=0") - .then(res => { + .then(async res => { if (res.status === 200) { let row = await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", ["segment-locking-uuid-1"]); if (!row?.locked) { From 88855ab695d86cbb523e2a341c1b9aa5260dd0b9 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 1 Mar 2021 22:20:44 -0500 Subject: [PATCH 05/16] Support schema upgrading with postgres --- databases/_sponsorTimes.db.sql | 3 +++ src/databases/Postgres.ts | 40 ++++++++++++++++++++++++++++++++++ src/databases/Sqlite.ts | 8 ++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/databases/_sponsorTimes.db.sql b/databases/_sponsorTimes.db.sql index 03d02f1..9856d28 100644 --- a/databases/_sponsorTimes.db.sql +++ b/databases/_sponsorTimes.db.sql @@ -33,4 +33,7 @@ CREATE TABLE IF NOT EXISTS "config" ( CREATE INDEX IF NOT EXISTS sponsorTimes_videoID on sponsorTimes(videoID); CREATE INDEX IF NOT EXISTS sponsorTimes_UUID on sponsorTimes(UUID); + +CREATE EXTENSION IF NOT EXISTS pgcrypto; --!sqlite-ignore + COMMIT; \ No newline at end of file diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 3f63707..6001a38 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -2,6 +2,8 @@ import { Logger } from '../utils/logger'; import { IDatabase, QueryType } from './IDatabase'; import { Pool } from 'pg'; +import fs from "fs"; + export class Mysql implements IDatabase { private pool: Pool; @@ -9,10 +11,25 @@ export class Mysql implements IDatabase { init(): void { this.pool = new Pool(); + + if (!this.config.readOnly) { + // Upgrade database if required + this.upgradeDB(this.config.fileNamePrefix, this.config.dbSchemaFolder); + } } async prepare(type: QueryType, query: string, params?: any[]) { Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`); + + // Convert query to use numbered parameters + let count = 1; + for (let char = 0; char < query.length; char++) { + if (query.charAt(char) === '?') { + query = query.slice(0, char) + "$" + count + query.slice(char + 1); + count++; + } + } + const queryResult = await this.pool.query(query, params); switch (type) { @@ -27,5 +44,28 @@ export class Mysql implements IDatabase { } } } + + private async upgradeDB(fileNamePrefix: string, schemaFolder: string) { + const versionCodeInfo = await this.pool.query("SELECT value FROM config WHERE key = 'version'"); + let versionCode = versionCodeInfo ? versionCodeInfo.rows[0].value : 0; + + let path = schemaFolder + "/_upgrade_" + fileNamePrefix + "_" + (parseInt(versionCode) + 1) + ".sql"; + Logger.debug('db update: trying ' + path); + while (fs.existsSync(path)) { + Logger.debug('db update: updating ' + path); + await this.pool.query(this.processUpgradeQuery(fs.readFileSync(path).toString())); + + versionCode = (await this.pool.query("SELECT value FROM config WHERE key = 'version'"))?.rows[0]?.value; + path = schemaFolder + "/_upgrade_" + fileNamePrefix + "_" + (parseInt(versionCode) + 1) + ".sql"; + Logger.debug('db update: trying ' + path); + } + Logger.debug('db update: no file ' + path); + } + + private processUpgradeQuery(query: string): string { + let result = query.replace(/sha256\((.*?)\)/gm, "digest($1, 'sha256')"); + + return result; + } } diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index ec3055d..1ef1823 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -84,7 +84,7 @@ export class Sqlite implements IDatabase { Logger.debug('db update: trying ' + path); while (fs.existsSync(path)) { Logger.debug('db update: updating ' + path); - db.exec(fs.readFileSync(path).toString()); + db.exec(this.processUpgradeQuery(fs.readFileSync(path).toString())); versionCode = db.prepare("SELECT value FROM config WHERE key = ?").get("version").value; path = schemaFolder + "/_upgrade_" + fileNamePrefix + "_" + (parseInt(versionCode) + 1) + ".sql"; @@ -92,6 +92,12 @@ export class Sqlite implements IDatabase { } Logger.debug('db update: no file ' + path); } + + private static processUpgradeQuery(query: string): string { + let result = query.replace(/^.*--!sqlite-ignore/gm, ""); + + return result; + } } export interface SqliteConfig { From 2c211d4730494018e7c2e755fbd2aeb4b5515c8d Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 1 Mar 2021 22:40:13 -0500 Subject: [PATCH 06/16] Fix preprocessor not being used for initial schema --- src/databases/Postgres.ts | 4 ++++ src/databases/Sqlite.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 6001a38..1bb4e08 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -15,6 +15,10 @@ export class Mysql implements IDatabase { if (!this.config.readOnly) { // Upgrade database if required this.upgradeDB(this.config.fileNamePrefix, this.config.dbSchemaFolder); + + if (this.config.createDbIfNotExists && !this.config.readOnly && fs.existsSync(this.config.dbSchemaFileName)) { + this.pool.query(this.processUpgradeQuery(fs.readFileSync(this.config.dbSchemaFileName).toString())); + } } } diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index 1ef1823..b2880a2 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -50,7 +50,7 @@ export class Sqlite implements IDatabase { this.db = new Sqlite3(this.config.dbPath, {readonly: this.config.readOnly, fileMustExist: !this.config.createDbIfNotExists}); if (this.config.createDbIfNotExists && !this.config.readOnly && fs.existsSync(this.config.dbSchemaFileName)) { - this.db.exec(fs.readFileSync(this.config.dbSchemaFileName).toString()); + this.db.exec(Sqlite.processUpgradeQuery(fs.readFileSync(this.config.dbSchemaFileName).toString())); } if (!this.config.readOnly) { From 1a66be8665d75d022aebcfebbeb11f1320304ab8 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 4 Mar 2021 19:44:54 -0500 Subject: [PATCH 07/16] Make schemas work with postgres --- databases/_sponsorTimes.db.sql | 58 ++++++++++++++------------- databases/_upgrade_sponsorTimes_1.sql | 8 ++-- databases/_upgrade_sponsorTimes_3.sql | 6 +-- databases/_upgrade_sponsorTimes_4.sql | 2 +- databases/_upgrade_sponsorTimes_5.sql | 4 +- databases/_upgrade_sponsorTimes_6.sql | 8 ++-- package-lock.json | 5 +++ package.json | 1 + src/config.ts | 1 + src/databases/Postgres.ts | 26 ++++++------ src/databases/databases.ts | 21 ++++++++++ src/routes/getSkipSegments.ts | 19 +++++++-- src/types/config.model.ts | 12 ++++++ 13 files changed, 114 insertions(+), 57 deletions(-) diff --git a/databases/_sponsorTimes.db.sql b/databases/_sponsorTimes.db.sql index 9856d28..4ea56af 100644 --- a/databases/_sponsorTimes.db.sql +++ b/databases/_sponsorTimes.db.sql @@ -1,39 +1,43 @@ BEGIN TRANSACTION; -CREATE TABLE IF NOT EXISTS "vipUsers" ( - "userID" TEXT NOT NULL -); -CREATE TABLE IF NOT EXISTS "sponsorTimes" ( - "videoID" TEXT NOT NULL, - "startTime" REAL NOT NULL, - "endTime" REAL NOT NULL, - "votes" INTEGER NOT NULL, - "UUID" TEXT NOT NULL UNIQUE, - "userID" TEXT NOT NULL, - "timeSubmitted" INTEGER NOT NULL, - "views" INTEGER NOT NULL, - "category" TEXT NOT NULL, - "shadowHidden" INTEGER NOT NULL -); -CREATE TABLE IF NOT EXISTS "userNames" ( - "userID" TEXT NOT NULL, - "userName" TEXT NOT NULL -); -CREATE TABLE IF NOT EXISTS "categoryVotes" ( - "UUID" TEXT NOT NULL, - "category" TEXT NOT NULL, - "votes" INTEGER NOT NULL default '0' +CREATE TABLE IF NOT EXISTS vipUsers ( + userID TEXT NOT NULL ); -CREATE TABLE IF NOT EXISTS "config" ( - "key" TEXT NOT NULL UNIQUE, - "value" TEXT NOT NULL +COMMIT; + +BEGIN TRANSACTION; + +CREATE TABLE IF NOT EXISTS sponsorTimes ( + videoID TEXT NOT NULL, + startTime REAL NOT NULL, + endTime REAL NOT NULL, + votes INTEGER NOT NULL, + UUID TEXT NOT NULL UNIQUE, + userID TEXT NOT NULL, + timeSubmitted INTEGER NOT NULL, + views INTEGER NOT NULL, + category TEXT NOT NULL, + shadowHidden INTEGER NOT NULL +); +CREATE TABLE IF NOT EXISTS userNames ( + userID TEXT NOT NULL, + userName TEXT NOT NULL +); +CREATE TABLE IF NOT EXISTS categoryVotes ( + UUID TEXT NOT NULL, + category TEXT NOT NULL, + votes INTEGER NOT NULL default 0 +); + +CREATE TABLE IF NOT EXISTS config ( + key TEXT NOT NULL UNIQUE, + value TEXT NOT NULL ); CREATE INDEX IF NOT EXISTS sponsorTimes_videoID on sponsorTimes(videoID); CREATE INDEX IF NOT EXISTS sponsorTimes_UUID on sponsorTimes(UUID); - CREATE EXTENSION IF NOT EXISTS pgcrypto; --!sqlite-ignore COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_1.sql b/databases/_upgrade_sponsorTimes_1.sql index 5629cc8..c950d08 100644 --- a/databases/_upgrade_sponsorTimes_1.sql +++ b/databases/_upgrade_sponsorTimes_1.sql @@ -6,20 +6,20 @@ CREATE TABLE "sqlb_temp_table_1" ( "startTime" REAL NOT NULL, "endTime" REAL NOT NULL, "votes" INTEGER NOT NULL, - "incorrectVotes" INTEGER NOT NULL default '1', + "incorrectVotes" INTEGER NOT NULL default 1, "UUID" TEXT NOT NULL UNIQUE, "userID" TEXT NOT NULL, "timeSubmitted" INTEGER NOT NULL, "views" INTEGER NOT NULL, - "category" TEXT NOT NULL DEFAULT "sponsor", + "category" TEXT NOT NULL DEFAULT 'sponsor', "shadowHidden" INTEGER NOT NULL ); -INSERT INTO sqlb_temp_table_1 SELECT videoID,startTime,endTime,votes,"1",UUID,userID,timeSubmitted,views,category,shadowHidden FROM sponsorTimes; +INSERT INTO sqlb_temp_table_1 SELECT videoID,startTime,endTime,votes,'1',UUID,userID,timeSubmitted,views,category,shadowHidden FROM sponsorTimes; DROP TABLE sponsorTimes; ALTER TABLE sqlb_temp_table_1 RENAME TO "sponsorTimes"; /* Add version to config */ -INSERT INTO config (key, value) VALUES("version", 1); +INSERT INTO config (key, value) VALUES('version', 1); COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_3.sql b/databases/_upgrade_sponsorTimes_3.sql index 5480ec2..ae3ac7e 100644 --- a/databases/_upgrade_sponsorTimes_3.sql +++ b/databases/_upgrade_sponsorTimes_3.sql @@ -1,13 +1,13 @@ BEGIN TRANSACTION; -/* hash upgrade test sha256('vid') = '1ff838dc6ca9680d88455341118157d59a055fe6d0e3870f9c002847bebe4663' +/* hash upgrade test sha256('vid') = '1ff838dc6ca9680d88455341118157d59a055fe6d0e3870f9c002847bebe4663' */ /* Add hash field */ -ALTER TABLE sponsorTimes ADD hashedVideoID TEXT NOT NULL default ""; +ALTER TABLE sponsorTimes ADD hashedVideoID TEXT NOT NULL default ''; UPDATE sponsorTimes SET hashedVideoID = sha256(videoID); CREATE INDEX IF NOT EXISTS sponsorTimes_hashedVideoID on sponsorTimes(hashedVideoID); /* Bump version in config */ -UPDATE config SET value = 3 WHERE key = "version"; +UPDATE config SET value = 3 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_4.sql b/databases/_upgrade_sponsorTimes_4.sql index 91e51e2..09948a5 100644 --- a/databases/_upgrade_sponsorTimes_4.sql +++ b/databases/_upgrade_sponsorTimes_4.sql @@ -7,6 +7,6 @@ CREATE TABLE "warnings" ( issuerUserID TEXT NOT NULL ); -UPDATE config SET value = 4 WHERE key = "version"; +UPDATE config SET value = 4 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_5.sql b/databases/_upgrade_sponsorTimes_5.sql index 1132af2..cc85949 100644 --- a/databases/_upgrade_sponsorTimes_5.sql +++ b/databases/_upgrade_sponsorTimes_5.sql @@ -10,8 +10,8 @@ CREATE TABLE "sqlb_temp_table_5" ( INSERT INTO sqlb_temp_table_5 SELECT userID,issueTime,issuerUserID,1 FROM warnings; DROP TABLE warnings; -ALTER TABLE sqlb_temp_table_5 RENAME TO "warnings"; +ALTER TABLE sqlb_temp_table_5 RENAME TO "warnings";; -UPDATE config SET value = 5 WHERE key = "version"; +UPDATE config SET value = 5 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_6.sql b/databases/_upgrade_sponsorTimes_6.sql index 3901602..1a1afbb 100644 --- a/databases/_upgrade_sponsorTimes_6.sql +++ b/databases/_upgrade_sponsorTimes_6.sql @@ -12,16 +12,16 @@ CREATE TABLE "sqlb_temp_table_6" ( "userID" TEXT NOT NULL, "timeSubmitted" INTEGER NOT NULL, "views" INTEGER NOT NULL, - "category" TEXT NOT NULL DEFAULT "sponsor", + "category" TEXT NOT NULL DEFAULT 'sponsor', "shadowHidden" INTEGER NOT NULL, - "hashedVideoID" TEXT NOT NULL default "" + "hashedVideoID" TEXT NOT NULL default '' ); -INSERT INTO sqlb_temp_table_6 SELECT videoID,startTime,endTime,votes,"0",incorrectVotes,UUID,userID,timeSubmitted,views,category,shadowHidden,hashedVideoID FROM sponsorTimes; +INSERT INTO sqlb_temp_table_6 SELECT videoID,startTime,endTime,votes,'0',incorrectVotes,UUID,userID,timeSubmitted,views,category,shadowHidden,hashedVideoID FROM sponsorTimes; DROP TABLE sponsorTimes; ALTER TABLE sqlb_temp_table_6 RENAME TO "sponsorTimes"; -UPDATE config SET value = 6 WHERE key = "version"; +UPDATE config SET value = 6 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 3f790d1..dd69f2e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -754,6 +754,11 @@ "is-obj": "^1.0.0" } }, + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + }, "duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", diff --git a/package.json b/package.json index 1cd2362..af27bfc 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "license": "MIT", "dependencies": { "better-sqlite3": "^5.4.3", + "dotenv": "^8.2.0", "express": "^4.17.1", "express-rate-limit": "^5.1.3", "http": "0.0.0", diff --git a/src/config.ts b/src/config.ts index 7a172cc..93c6a43 100644 --- a/src/config.ts +++ b/src/config.ts @@ -45,6 +45,7 @@ addDefaults(config, { }, userCounterURL: null, youtubeAPIKey: null, + postgres: null }); // Add defaults diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 1bb4e08..5e76cda 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -4,27 +4,25 @@ import { Pool } from 'pg'; import fs from "fs"; -export class Mysql implements IDatabase { +export class Postgres implements IDatabase { private pool: Pool; constructor(private config: any) {} - init(): void { - this.pool = new Pool(); + async init(): Promise { + this.pool = new Pool(this.config.postgres); if (!this.config.readOnly) { - // Upgrade database if required - this.upgradeDB(this.config.fileNamePrefix, this.config.dbSchemaFolder); - if (this.config.createDbIfNotExists && !this.config.readOnly && fs.existsSync(this.config.dbSchemaFileName)) { - this.pool.query(this.processUpgradeQuery(fs.readFileSync(this.config.dbSchemaFileName).toString())); + await this.pool.query(this.processUpgradeQuery(fs.readFileSync(this.config.dbSchemaFileName).toString())); } + + // Upgrade database if required + await this.upgradeDB(this.config.fileNamePrefix, this.config.dbSchemaFolder); } } async prepare(type: QueryType, query: string, params?: any[]) { - Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`); - // Convert query to use numbered parameters let count = 1; for (let char = 0; char < query.length; char++) { @@ -34,7 +32,9 @@ export class Mysql implements IDatabase { } } - const queryResult = await this.pool.query(query, params); + Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`); + + const queryResult = await this.pool.query({text: query, values: params}); switch (type) { case 'get': { @@ -51,7 +51,7 @@ export class Mysql implements IDatabase { private async upgradeDB(fileNamePrefix: string, schemaFolder: string) { const versionCodeInfo = await this.pool.query("SELECT value FROM config WHERE key = 'version'"); - let versionCode = versionCodeInfo ? versionCodeInfo.rows[0].value : 0; + let versionCode = versionCodeInfo.rows[0] ? versionCodeInfo.rows[0].value : 0; let path = schemaFolder + "/_upgrade_" + fileNamePrefix + "_" + (parseInt(versionCode) + 1) + ".sql"; Logger.debug('db update: trying ' + path); @@ -67,7 +67,9 @@ export class Mysql implements IDatabase { } private processUpgradeQuery(query: string): string { - let result = query.replace(/sha256\((.*?)\)/gm, "digest($1, 'sha256')"); + let result = query.toLocaleLowerCase(); + result = result.replace(/sha256\((.*?)\)/gm, "digest($1, 'sha256')"); + result = result.replace(/integer/gm, "numeric"); return result; } diff --git a/src/databases/databases.ts b/src/databases/databases.ts index ef75d0a..41c6b9e 100644 --- a/src/databases/databases.ts +++ b/src/databases/databases.ts @@ -1,6 +1,7 @@ import {config} from '../config'; import {Sqlite} from './Sqlite'; import {Mysql} from './Mysql'; +import {Postgres} from './Postgres'; import {IDatabase} from './IDatabase'; @@ -9,6 +10,26 @@ let privateDB: IDatabase; if (config.mysql) { db = new Mysql(config.mysql); privateDB = new Mysql(config.privateMysql); +} else if (config.postgres) { + db = new Postgres({ + dbSchemaFileName: config.dbSchema, + dbSchemaFolder: config.schemaFolder, + fileNamePrefix: 'sponsorTimes', + readOnly: config.readOnly, + createDbIfNotExists: config.createDatabaseIfNotExist, + enableWalCheckpointNumber: !config.readOnly && config.mode === "production", + postgres: config.postgres + }); + + privateDB = new Sqlite({ + dbPath: config.privateDB, + dbSchemaFileName: config.privateDBSchema, + dbSchemaFolder: config.schemaFolder, + fileNamePrefix: 'private', + readOnly: config.readOnly, + createDbIfNotExists: config.createDatabaseIfNotExist, + enableWalCheckpointNumber: false + }); } else { db = new Sqlite({ dbPath: config.db, diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index 98d7178..f3e9665 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -52,11 +52,14 @@ async function getSegmentsByVideoID(req: Request, videoID: string, categories: C const segments: Segment[] = []; try { + categories.filter((category) => !/[^a-z|_|-]/.test(category)); + const segmentsByCategory: SBRecord = (await db .prepare( 'all', - `SELECT startTime, endTime, votes, locked, UUID, category, shadowHidden FROM sponsorTimes WHERE videoID = ? AND category IN (${Array(categories.length).fill('?').join()}) ORDER BY startTime`, - [videoID, categories] + `SELECT startTime, endTime, votes, locked, UUID, category, shadowHidden FROM sponsorTimes + WHERE videoID = ? AND category IN (${categories.map((c) => "'" + c + "'")}) ORDER BY startTime`, + [videoID] )).reduce((acc: SBRecord, segment: DBSegment) => { acc[segment.category] = acc[segment.category] || []; acc[segment.category].push(segment); @@ -64,10 +67,15 @@ async function getSegmentsByVideoID(req: Request, videoID: string, categories: C return acc; }, {}); + console.log(segmentsByCategory) + + for (const [category, categorySegments] of Object.entries(segmentsByCategory)) { segments.push(...(await prepareCategorySegments(req, videoID as VideoID, category as Category, categorySegments, cache))); } + console.log(segments) + return segments; } catch (err) { if (err) { @@ -84,11 +92,14 @@ async function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, try { type SegmentWithHashPerVideoID = SBRecord}>; + categories.filter((category) => !/[^a-z|_|-]/.test(category)); + const segmentPerVideoID: SegmentWithHashPerVideoID = (await db .prepare( 'all', - `SELECT videoID, startTime, endTime, votes, locked, UUID, category, shadowHidden, hashedVideoID FROM sponsorTimes WHERE hashedVideoID LIKE ? AND category IN (${Array(categories.length).fill('?').join()}) ORDER BY startTime`, - [hashedVideoIDPrefix + '%', categories] + `SELECT videoID, startTime, endTime, votes, locked, UUID, category, shadowHidden, hashedVideoID FROM sponsorTimes + WHERE hashedVideoID LIKE ? AND category IN (${categories.map((c) => "'" + c + "'")}) ORDER BY startTime`, + [hashedVideoIDPrefix + '%'] )).reduce((acc: SegmentWithHashPerVideoID, segment: DBSegment) => { acc[segment.videoID] = acc[segment.videoID] || { hash: segment.hashedVideoID, diff --git a/src/types/config.model.ts b/src/types/config.model.ts index 88af3d2..b27cfaa 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -1,3 +1,4 @@ +import { PoolConfig } from 'pg'; import * as redis from 'redis'; export interface SBSConfig { @@ -36,6 +37,7 @@ export interface SBSConfig { minimumPrefix?: string; maximumPrefix?: string; redis?: redis.ClientOpts; + postgres?: PoolConfig; } export interface WebhookConfig { @@ -50,3 +52,13 @@ export interface RateLimitConfig { message: string; statusCode: number; } + +export interface PostgresConfig { + dbSchemaFileName: string; + dbSchemaFolder: string; + fileNamePrefix: string; + readOnly: boolean; + createDbIfNotExists: boolean; + enableWalCheckpointNumber: boolean; + postgres: PoolConfig; +} \ No newline at end of file From 2772a9dcc608aa3dd8f0ddfbd95f58fcdb6b2fb8 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 4 Mar 2021 20:23:05 -0500 Subject: [PATCH 08/16] Switch to case sensitive and get submitting + getting working --- databases/_sponsorTimes.db.sql | 50 +++++++++++++-------------- databases/_upgrade_sponsorTimes_1.sql | 4 +-- databases/_upgrade_sponsorTimes_2.sql | 2 +- databases/_upgrade_sponsorTimes_3.sql | 8 ++--- databases/_upgrade_sponsorTimes_4.sql | 8 ++--- databases/_upgrade_sponsorTimes_5.sql | 10 +++--- databases/_upgrade_sponsorTimes_6.sql | 6 ++-- src/databases/Postgres.ts | 4 +-- src/routes/addUserAsVIP.ts | 6 ++-- src/routes/deleteNoSegments.ts | 4 +-- src/routes/getDaysSavedFormatted.ts | 2 +- src/routes/getSavedTimeForUser.ts | 2 +- src/routes/getSkipSegments.ts | 10 +++--- src/routes/getTopUsers.ts | 16 ++++----- src/routes/postSkipSegments.ts | 38 ++++++++++---------- src/utils/isUserTrustworthy.ts | 4 +-- 16 files changed, 87 insertions(+), 87 deletions(-) diff --git a/databases/_sponsorTimes.db.sql b/databases/_sponsorTimes.db.sql index 4ea56af..0f7da3a 100644 --- a/databases/_sponsorTimes.db.sql +++ b/databases/_sponsorTimes.db.sql @@ -1,42 +1,42 @@ BEGIN TRANSACTION; -CREATE TABLE IF NOT EXISTS vipUsers ( - userID TEXT NOT NULL +CREATE TABLE IF NOT EXISTS "vipUsers" ( + "userID" TEXT NOT NULL ); COMMIT; BEGIN TRANSACTION; -CREATE TABLE IF NOT EXISTS sponsorTimes ( - videoID TEXT NOT NULL, - startTime REAL NOT NULL, - endTime REAL NOT NULL, - votes INTEGER NOT NULL, - UUID TEXT NOT NULL UNIQUE, - userID TEXT NOT NULL, - timeSubmitted INTEGER NOT NULL, - views INTEGER NOT NULL, - category TEXT NOT NULL, - shadowHidden INTEGER NOT NULL +CREATE TABLE IF NOT EXISTS "sponsorTimes" ( + "videoID" TEXT NOT NULL, + "startTime" REAL NOT NULL, + "endTime" REAL NOT NULL, + "votes" INTEGER NOT NULL, + "UUID" TEXT NOT NULL UNIQUE, + "userID" TEXT NOT NULL, + "timeSubmitted" INTEGER NOT NULL, + "views" INTEGER NOT NULL, + "category" TEXT NOT NULL, + "shadowHidden" INTEGER NOT NULL ); -CREATE TABLE IF NOT EXISTS userNames ( - userID TEXT NOT NULL, - userName TEXT NOT NULL +CREATE TABLE IF NOT EXISTS "userNames" ( + "userID" TEXT NOT NULL, + "userName" TEXT NOT NULL ); -CREATE TABLE IF NOT EXISTS categoryVotes ( - UUID TEXT NOT NULL, - category TEXT NOT NULL, - votes INTEGER NOT NULL default 0 +CREATE TABLE IF NOT EXISTS "categoryVotes" ( + "UUID" TEXT NOT NULL, + "category" TEXT NOT NULL, + "votes" INTEGER NOT NULL default 0 ); -CREATE TABLE IF NOT EXISTS config ( - key TEXT NOT NULL UNIQUE, - value TEXT NOT NULL +CREATE TABLE IF NOT EXISTS "config" ( + "key" TEXT NOT NULL UNIQUE, + "value" TEXT NOT NULL ); -CREATE INDEX IF NOT EXISTS sponsorTimes_videoID on sponsorTimes(videoID); -CREATE INDEX IF NOT EXISTS sponsorTimes_UUID on sponsorTimes(UUID); +CREATE INDEX IF NOT EXISTS "sponsorTimes_videoID" on "sponsorTimes"("videoID"); +CREATE INDEX IF NOT EXISTS "sponsorTimes_UUID" on "sponsorTimes"("UUID"); CREATE EXTENSION IF NOT EXISTS pgcrypto; --!sqlite-ignore diff --git a/databases/_upgrade_sponsorTimes_1.sql b/databases/_upgrade_sponsorTimes_1.sql index c950d08..e394605 100644 --- a/databases/_upgrade_sponsorTimes_1.sql +++ b/databases/_upgrade_sponsorTimes_1.sql @@ -14,9 +14,9 @@ CREATE TABLE "sqlb_temp_table_1" ( "category" TEXT NOT NULL DEFAULT 'sponsor', "shadowHidden" INTEGER NOT NULL ); -INSERT INTO sqlb_temp_table_1 SELECT videoID,startTime,endTime,votes,'1',UUID,userID,timeSubmitted,views,category,shadowHidden FROM sponsorTimes; +INSERT INTO sqlb_temp_table_1 SELECT "videoID","startTime","endTime","votes",'1',"UUID","userID","timeSubmitted","views","category","shadowHidden" FROM "sponsorTimes"; -DROP TABLE sponsorTimes; +DROP TABLE "sponsorTimes"; ALTER TABLE sqlb_temp_table_1 RENAME TO "sponsorTimes"; /* Add version to config */ diff --git a/databases/_upgrade_sponsorTimes_2.sql b/databases/_upgrade_sponsorTimes_2.sql index f6816bc..6bb8d8e 100644 --- a/databases/_upgrade_sponsorTimes_2.sql +++ b/databases/_upgrade_sponsorTimes_2.sql @@ -8,6 +8,6 @@ CREATE TABLE "noSegments" ( ); /* Add version to config */ -UPDATE config SET value = 2 WHERE key = 'version'; +UPDATE "config" SET value = 2 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_3.sql b/databases/_upgrade_sponsorTimes_3.sql index ae3ac7e..fec45c9 100644 --- a/databases/_upgrade_sponsorTimes_3.sql +++ b/databases/_upgrade_sponsorTimes_3.sql @@ -2,12 +2,12 @@ BEGIN TRANSACTION; /* hash upgrade test sha256('vid') = '1ff838dc6ca9680d88455341118157d59a055fe6d0e3870f9c002847bebe4663' */ /* Add hash field */ -ALTER TABLE sponsorTimes ADD hashedVideoID TEXT NOT NULL default ''; -UPDATE sponsorTimes SET hashedVideoID = sha256(videoID); +ALTER TABLE "sponsorTimes" ADD "hashedVideoID" TEXT NOT NULL default ''; +UPDATE "sponsorTimes" SET "hashedVideoID" = sha256("videoID"); -CREATE INDEX IF NOT EXISTS sponsorTimes_hashedVideoID on sponsorTimes(hashedVideoID); +CREATE INDEX IF NOT EXISTS "sponsorTimes_hashedVideoID" on "sponsorTimes"("hashedVideoID"); /* Bump version in config */ -UPDATE config SET value = 3 WHERE key = 'version'; +UPDATE "config" SET value = 3 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_4.sql b/databases/_upgrade_sponsorTimes_4.sql index 09948a5..4e04abf 100644 --- a/databases/_upgrade_sponsorTimes_4.sql +++ b/databases/_upgrade_sponsorTimes_4.sql @@ -2,11 +2,11 @@ BEGIN TRANSACTION; /* Create warnings table */ CREATE TABLE "warnings" ( - userID TEXT NOT NULL, - issueTime INTEGER NOT NULL, - issuerUserID TEXT NOT NULL + "userID" TEXT NOT NULL, + "issueTime" INTEGER NOT NULL, + "issuerUserID" TEXT NOT NULL ); -UPDATE config SET value = 4 WHERE key = 'version'; +UPDATE "config" SET value = 4 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_5.sql b/databases/_upgrade_sponsorTimes_5.sql index cc85949..4607dae 100644 --- a/databases/_upgrade_sponsorTimes_5.sql +++ b/databases/_upgrade_sponsorTimes_5.sql @@ -2,16 +2,16 @@ BEGIN TRANSACTION; /* Add enabled field */ CREATE TABLE "sqlb_temp_table_5" ( - userID TEXT NOT NULL, - issueTime INTEGER NOT NULL, - issuerUserID TEXT NOT NULL, + "userID" TEXT NOT NULL, + "issueTime" INTEGER NOT NULL, + "issuerUserID" TEXT NOT NULL, enabled INTEGER NOT NULL ); -INSERT INTO sqlb_temp_table_5 SELECT userID,issueTime,issuerUserID,1 FROM warnings; +INSERT INTO sqlb_temp_table_5 SELECT "userID","issueTime","issuerUserID",1 FROM "warnings"; DROP TABLE warnings; ALTER TABLE sqlb_temp_table_5 RENAME TO "warnings";; -UPDATE config SET value = 5 WHERE key = 'version'; +UPDATE "config" SET value = 5 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/databases/_upgrade_sponsorTimes_6.sql b/databases/_upgrade_sponsorTimes_6.sql index 1a1afbb..14bd646 100644 --- a/databases/_upgrade_sponsorTimes_6.sql +++ b/databases/_upgrade_sponsorTimes_6.sql @@ -17,11 +17,11 @@ CREATE TABLE "sqlb_temp_table_6" ( "hashedVideoID" TEXT NOT NULL default '' ); -INSERT INTO sqlb_temp_table_6 SELECT videoID,startTime,endTime,votes,'0',incorrectVotes,UUID,userID,timeSubmitted,views,category,shadowHidden,hashedVideoID FROM sponsorTimes; +INSERT INTO sqlb_temp_table_6 SELECT "videoID","startTime","endTime","votes",'0',"incorrectVotes","UUID","userID","timeSubmitted","views","category","shadowHidden","hashedVideoID" FROM "sponsorTimes"; -DROP TABLE sponsorTimes; +DROP TABLE "sponsorTimes"; ALTER TABLE sqlb_temp_table_6 RENAME TO "sponsorTimes"; -UPDATE config SET value = 6 WHERE key = 'version'; +UPDATE "config" SET value = 6 WHERE key = 'version'; COMMIT; \ No newline at end of file diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 5e76cda..8e004ac 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -67,9 +67,9 @@ export class Postgres implements IDatabase { } private processUpgradeQuery(query: string): string { - let result = query.toLocaleLowerCase(); + let result = query; result = result.replace(/sha256\((.*?)\)/gm, "digest($1, 'sha256')"); - result = result.replace(/integer/gm, "numeric"); + result = result.replace(/integer/gmi, "NUMERIC"); return result; } diff --git a/src/routes/addUserAsVIP.ts b/src/routes/addUserAsVIP.ts index 346ab98..8b7c957 100644 --- a/src/routes/addUserAsVIP.ts +++ b/src/routes/addUserAsVIP.ts @@ -27,14 +27,14 @@ export async function addUserAsVIP(req: Request, res: Response) { } //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 row = await db.prepare('get', 'SELECT count(*) as "userCount" FROM vipUsers WHERE userID = ?', [userID]); if (enabled && row.userCount == 0) { //add them to the vip list - await db.prepare('run', "INSERT INTO vipUsers VALUES(?)", [userID]); + await db.prepare('run', 'INSERT INTO "vipUsers" VALUES(?)', [userID]); } else if (!enabled && row.userCount > 0) { //remove them from the shadow ban list - await db.prepare('run', "DELETE FROM vipUsers WHERE userID = ?", [userID]); + await db.prepare('run', 'DELETE FROM "vipUsers" WHERE "userID" = ?', [userID]); } res.sendStatus(200); diff --git a/src/routes/deleteNoSegments.ts b/src/routes/deleteNoSegments.ts index 6451e85..cfa6c78 100644 --- a/src/routes/deleteNoSegments.ts +++ b/src/routes/deleteNoSegments.ts @@ -33,12 +33,12 @@ export async function deleteNoSegments(req: Request, res: Response) { return; } - const entries = (await db.prepare("all", 'SELECT * FROM noSegments WHERE videoID = ?', [videoID])).filter((entry: any) => { + const entries = (await db.prepare("all", 'SELECT * FROM "noSegments" WHERE "videoID" = ?', [videoID])).filter((entry: any) => { return (categories.indexOf(entry.category) !== -1); }); for (const entry of entries) { - await db.prepare('run', 'DELETE FROM noSegments WHERE videoID = ? AND category = ?', [videoID, entry.category]); + await db.prepare('run', 'DELETE FROM "noSegments" WHERE "videoID" = ? AND "category" = ?', [videoID, entry.category]); } res.status(200).json({message: 'Removed no segments entrys for video ' + videoID}); diff --git a/src/routes/getDaysSavedFormatted.ts b/src/routes/getDaysSavedFormatted.ts index 699e48d..22872b4 100644 --- a/src/routes/getDaysSavedFormatted.ts +++ b/src/routes/getDaysSavedFormatted.ts @@ -2,7 +2,7 @@ import {db} from '../databases/databases'; import {Request, Response} from 'express'; export async function getDaysSavedFormatted(req: Request, res: Response) { - let row = await db.prepare('get', "SELECT SUM((endTime - startTime) / 60 / 60 / 24 * views) as daysSaved from sponsorTimes where shadowHidden != 1", []); + let row = await db.prepare('get', 'SELECT SUM(("endTime" - "startTime") / 60 / 60 / 24 * "views") as "daysSaved" from "sponsorTimes" where "shadowHidden" != 1', []); if (row !== undefined) { //send this result diff --git a/src/routes/getSavedTimeForUser.ts b/src/routes/getSavedTimeForUser.ts index 0d54f59..24fbbda 100644 --- a/src/routes/getSavedTimeForUser.ts +++ b/src/routes/getSavedTimeForUser.ts @@ -15,7 +15,7 @@ export async function getSavedTimeForUser(req: Request, res: Response) { userID = getHash(userID); try { - let row = await db.prepare("get", "SELECT SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE userID = ? AND votes > -1 AND shadowHidden != 1 ", [userID]); + let row = await db.prepare("get", 'SELECT SUM(("endTime" - "startTime") / 60 * "views") as "minutesSaved" FROM "sponsorTimes" WHERE "userID" = ? AND "votes" > -1 AND "shadowHidden" != 1 ', [userID]); if (row.minutesSaved != null) { res.send({ diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index f3e9665..e77c844 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -24,7 +24,7 @@ async function prepareCategorySegments(req: Request, videoID: VideoID, category: } if (cache.shadowHiddenSegmentIPs[videoID] === undefined) { - cache.shadowHiddenSegmentIPs[videoID] = await privateDB.prepare('all', 'SELECT hashedIP FROM sponsorTimes WHERE videoID = ?', [videoID]) as { hashedIP: HashedIP }[]; + cache.shadowHiddenSegmentIPs[videoID] = await privateDB.prepare('all', 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ?', [videoID]) as { hashedIP: HashedIP }[]; } //if this isn't their ip, don't send it to them @@ -57,8 +57,8 @@ async function getSegmentsByVideoID(req: Request, videoID: string, categories: C const segmentsByCategory: SBRecord = (await db .prepare( 'all', - `SELECT startTime, endTime, votes, locked, UUID, category, shadowHidden FROM sponsorTimes - WHERE videoID = ? AND category IN (${categories.map((c) => "'" + c + "'")}) ORDER BY startTime`, + `SELECT "startTime", "endTime", "votes", "locked", "UUID", "category", "shadowHidden" FROM "sponsorTimes" + WHERE "videoID" = ? AND "category" IN (${categories.map((c) => "'" + c + "'")}) ORDER BY "startTime"`, [videoID] )).reduce((acc: SBRecord, segment: DBSegment) => { acc[segment.category] = acc[segment.category] || []; @@ -97,8 +97,8 @@ async function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, const segmentPerVideoID: SegmentWithHashPerVideoID = (await db .prepare( 'all', - `SELECT videoID, startTime, endTime, votes, locked, UUID, category, shadowHidden, hashedVideoID FROM sponsorTimes - WHERE hashedVideoID LIKE ? AND category IN (${categories.map((c) => "'" + c + "'")}) ORDER BY startTime`, + `SELECT "startTime", "endTime", "votes", "locked", "UUID", "category", "shadowHidden", "hashedVideoID" FROM "sponsorTimes" + WHERE "hashedVideoID" LIKE ? AND "category" IN (${categories.map((c) => "'" + c + "'")}) ORDER BY "startTime"`, [hashedVideoIDPrefix + '%'] )).reduce((acc: SegmentWithHashPerVideoID, segment: DBSegment) => { acc[segment.videoID] = acc[segment.videoID] || { diff --git a/src/routes/getTopUsers.ts b/src/routes/getTopUsers.ts index 328ce09..47cd95b 100644 --- a/src/routes/getTopUsers.ts +++ b/src/routes/getTopUsers.ts @@ -23,15 +23,15 @@ async function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boole "SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, "; } - const rows = await db.prepare('all', "SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," + - "SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, " + - "SUM(votes) as userVotes, " + + const rows = await db.prepare('all', `SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount, + SUM(("sponsorTimes.endTime" - "sponsorTimes.startTime") / 60 * "sponsorTimes.views") as minutesSaved, + 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 " + - "GROUP BY IFNULL(userName, sponsorTimes.userID) HAVING userVotes > 20 " + - "ORDER BY " + sortBy + " DESC LIMIT 100", []); + `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 + GROUP BY IFNULL("userName", "sponsorTimes.userID") HAVING "userVotes" > 20 + ORDER BY "` + sortBy + `" DESC LIMIT 100`, []); for (let i = 0; i < rows.length; i++) { userNames[i] = rows[i].userName; diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index d17f4f0..c19ee17 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -16,7 +16,7 @@ import redis from '../utils/redis'; async function sendWebhookNotification(userID: string, videoID: string, UUID: string, submissionCount: number, youtubeData: any, {submissionStart, submissionEnd}: { submissionStart: number; submissionEnd: number; }, segmentInfo: any) { - const row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); + const row = await db.prepare('get', `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [userID]); const userName = row !== undefined ? row.userName : null; const video = youtubeData.items[0]; @@ -47,7 +47,7 @@ async function sendWebhookNotification(userID: string, videoID: string, UUID: st async function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo: any) { if (config.youtubeAPIKey !== null) { - const userSubmissionCountRow = await db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]); + const userSubmissionCountRow = await db.prepare('get', `SELECT count(*) as "submissionCount" FROM "sponsorTimes" WHERE "userID" = ?`, [userID]); YouTubeAPI.listVideos(videoID, (err: any, data: any) => { if (err || data.items.length === 0) { @@ -106,10 +106,10 @@ async function sendWebhooks(userID: string, videoID: string, UUID: string, segme } async function sendWebhooksNB(userID: string, videoID: string, UUID: string, startTime: number, endTime: number, category: string, probability: number, ytData: any) { - const submissionInfoRow = await db.prepare('get', "SELECT " + - "(select count(1) from sponsorTimes where userID = ?) count, " + - "(select count(1) from sponsorTimes where userID = ? and votes <= -2) disregarded, " + - "coalesce((select userName FROM userNames WHERE userID = ?), ?) userName", + const submissionInfoRow = await db.prepare('get', `SELECT + (select count(1) from "sponsorTimes" where "userID" = ?) count, + (select count(1) from "sponsorTimes" where "userID" = ? and "votes" <= -2) disregarded, + coalesce((select "userName" FROM "userNames" WHERE "userID" = ?), ?) "userName"`, [userID, userID, userID, userID]); let submittedBy: string; @@ -304,7 +304,7 @@ export async function postSkipSegments(req: Request, res: Response) { const MILLISECONDS_IN_HOUR = 3600000; const now = Date.now(); - const warningsCount = (await db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1", + const warningsCount = (await db.prepare('get', `SELECT count(1) as count FROM warnings WHERE "userID" = ? AND "issueTime" > ? AND enabled = 1`, [userID, Math.floor(now - (config.hoursAfterWarningExpires * MILLISECONDS_IN_HOUR))], )).count; @@ -312,12 +312,12 @@ export async function postSkipSegments(req: Request, res: Response) { return res.status(403).send('Submission rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes that are not malicious, and we just want to clarify the rules. Could you please send a message in Discord or Matrix so we can further help you?'); } - const noSegmentList = (await db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID])).map((list: any) => { + const noSegmentList = (await db.prepare('all', 'SELECT category from "noSegments" where "videoID" = ?', [videoID])).map((list: any) => { return list.category; }); //check if this user is on the vip list - const isVIP = (await db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID])).userCount > 0; + const isVIP = (await db.prepare("get", `SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?`, [userID])).userCount > 0; const decreaseVotes = 0; @@ -366,8 +366,8 @@ export async function postSkipSegments(req: Request, res: Response) { } //check if this info has already been submitted before - const duplicateCheck2Row = await db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE startTime = ? " + - "and endTime = ? and category = ? and videoID = ?", [startTime, endTime, segments[i].category, videoID]); + const duplicateCheck2Row = await db.prepare('get', `SELECT COUNT(*) as count FROM "sponsorTimes" WHERE "startTime" = ? + and "endTime" = ? and "category" = ? and "videoID" = ?`, [startTime, endTime, segments[i].category, videoID]); if (duplicateCheck2Row.count > 0) { res.sendStatus(409); return; @@ -401,7 +401,7 @@ export async function postSkipSegments(req: Request, res: Response) { // Disable IP ratelimiting for now if (false) { //check to see if this ip has submitted too many sponsors today - const rateLimitCheckRow = await privateDB.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE hashedIP = ? AND videoID = ? AND timeSubmitted > ?", [hashedIP, videoID, yesterday]); + const rateLimitCheckRow = await privateDB.prepare('get', `SELECT COUNT(*) as count FROM "sponsorTimes" WHERE "hashedIP" = ? AND "videoID" = ? AND "timeSubmitted" > ?`, [hashedIP, videoID, yesterday]); if (rateLimitCheckRow.count >= 10) { //too many sponsors for the same video from the same ip address @@ -414,7 +414,7 @@ export async function postSkipSegments(req: Request, res: Response) { // Disable max submissions for now if (false) { //check to see if the user has already submitted sponsors for this video - const duplicateCheckRow = await db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE userID = ? and videoID = ?", [userID, videoID]); + const duplicateCheckRow = await db.prepare('get', `SELECT COUNT(*) as count FROM "sponsorTimes" WHERE "userID" = ? and "videoID" = ?`, [userID, videoID]); if (duplicateCheckRow.count >= 16) { //too many sponsors for the same video from the same user @@ -425,7 +425,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 privateDB.prepare('get', `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); let shadowBanned = shadowBanRow.userCount; @@ -445,7 +445,7 @@ export async function postSkipSegments(req: Request, res: Response) { Logger.error("Error while submitting when connecting to YouTube API: " + err); } else { //get all segments for this video and user - const allSubmittedByUser = await db.prepare('all', "SELECT startTime, endTime FROM sponsorTimes WHERE userID = ? and videoID = ? and votes > -1", [userID, videoID]); + const allSubmittedByUser = await db.prepare('all', `SELECT "startTime", "endTime" FROM "sponsorTimes" WHERE "userID" = ? and "videoID" = ? and "votes" > -1`, [userID, videoID]); const allSegmentTimes = []; if (allSubmittedByUser !== undefined) { //add segments the user has previously submitted @@ -489,15 +489,15 @@ export async function postSkipSegments(req: Request, res: Response) { const startingLocked = isVIP ? 1 : 0; try { - await db.prepare('run', "INSERT INTO sponsorTimes " + - "(videoID, startTime, endTime, votes, locked, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID)" + - "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [ + await db.prepare('run', `INSERT INTO "sponsorTimes" + ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "shadowHidden", "hashedVideoID") + VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [ videoID, segmentInfo.segment[0], segmentInfo.segment[1], startingVotes, startingLocked, UUID, userID, timeSubmitted, 0, segmentInfo.category, shadowBanned, getHash(videoID, 1), ], ); //add to private db as well - await privateDB.prepare('run', "INSERT INTO sponsorTimes VALUES(?, ?, ?)", [videoID, hashedIP, timeSubmitted]); + await privateDB.prepare('run', `INSERT INTO "sponsorTimes" VALUES(?, ?, ?)`, [videoID, hashedIP, timeSubmitted]); // Clear redis cache for this video redis.delAsync(skipSegmentsKey(videoID)); diff --git a/src/utils/isUserTrustworthy.ts b/src/utils/isUserTrustworthy.ts index 93bb6f0..e43bfe8 100644 --- a/src/utils/isUserTrustworthy.ts +++ b/src/utils/isUserTrustworthy.ts @@ -6,11 +6,11 @@ import {db} from '../databases/databases'; */ export async function isUserTrustworthy(userID: string): Promise { //check to see if this user how many submissions this user has submitted - const totalSubmissionsRow = await db.prepare('get', "SELECT count(*) as totalSubmissions, sum(votes) as voteSum FROM sponsorTimes WHERE userID = ?", [userID]); + const totalSubmissionsRow = await db.prepare('get', `SELECT count(*) as "totalSubmissions", sum(votes) as "voteSum" FROM "sponsorTimes" WHERE "userID" = ?`, [userID]); if (totalSubmissionsRow.totalSubmissions > 5) { //check if they have a high downvote ratio - const downvotedSubmissionsRow = await db.prepare('get', "SELECT count(*) as downvotedSubmissions FROM sponsorTimes WHERE userID = ? AND (votes < 0 OR shadowHidden > 0)", [userID]); + const downvotedSubmissionsRow = await db.prepare('get', `SELECT count(*) as "downvotedSubmissions" FROM "sponsorTimes" WHERE "userID" = ? AND (votes < 0 OR "shadowHidden" > 0)`, [userID]); return (downvotedSubmissionsRow.downvotedSubmissions / totalSubmissionsRow.totalSubmissions) < 0.6 || (totalSubmissionsRow.voteSum > downvotedSubmissionsRow.downvotedSubmissions); From e9b7eac2894a13f42469568a4f8f1b091690e42b Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 4 Mar 2021 23:29:01 -0500 Subject: [PATCH 09/16] Finish up conversion to use case-sensitive column names --- src/databases/Postgres.ts | 29 +++++++++- src/routes/addUserAsVIP.ts | 2 +- src/routes/getTopUsers.ts | 6 +-- src/routes/getTotalStats.ts | 4 +- src/routes/getUserInfo.ts | 8 +-- src/routes/getUsername.ts | 2 +- src/routes/getViewsForUser.ts | 2 +- src/routes/postNoSegments.ts | 4 +- src/routes/postSegmentShift.ts | 6 +-- src/routes/postWarning.ts | 6 +-- src/routes/setUsername.ts | 6 +-- src/routes/shadowBanUser.ts | 28 +++++----- src/routes/viewedVideoSponsorTime.ts | 2 +- src/routes/voteOnSponsorTime.ts | 80 ++++++++++++++-------------- src/utils/isUserVIP.ts | 2 +- 15 files changed, 106 insertions(+), 81 deletions(-) diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 8e004ac..e5e31d3 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -38,10 +38,35 @@ export class Postgres implements IDatabase { switch (type) { case 'get': { - return queryResult.rows[0]; + const value = queryResult.rows[0]; + Logger.debug(`result (postgres): ${JSON.stringify(value)}`); + if (value) { + for (const [key, v] of Object.entries(value)) { + if (!isNaN(v as any)) { + value[key] = parseFloat(v as string) + } + } + } + + Logger.debug(`result (postgres): ${value}`); + return value; } case 'all': { - return queryResult.rows; + let values = queryResult.rows; + if (values) { + values = values.map((row) => { + for (const [key, v] of Object.entries(row)) { + if (!isNaN(v as any)) { + row[key] = parseFloat(v as string) + } + } + + return row; + }); + } + + Logger.debug(`result (postgres): ${values}`); + return values; } case 'run': { break; diff --git a/src/routes/addUserAsVIP.ts b/src/routes/addUserAsVIP.ts index 8b7c957..71ab186 100644 --- a/src/routes/addUserAsVIP.ts +++ b/src/routes/addUserAsVIP.ts @@ -27,7 +27,7 @@ export async function addUserAsVIP(req: Request, res: Response) { } //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 row = await db.prepare('get', 'SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?', [userID]); if (enabled && row.userCount == 0) { //add them to the vip list diff --git a/src/routes/getTopUsers.ts b/src/routes/getTopUsers.ts index 47cd95b..15ff3ae 100644 --- a/src/routes/getTopUsers.ts +++ b/src/routes/getTopUsers.ts @@ -23,9 +23,9 @@ async function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boole "SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, "; } - const rows = await db.prepare('all', `SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount, - SUM(("sponsorTimes.endTime" - "sponsorTimes.startTime") / 60 * "sponsorTimes.views") as minutesSaved, - SUM("votes") as userVotes, ` + + const rows = await db.prepare('all', `SELECT COUNT(*) as "totalSubmissions", SUM(views) as "viewCount", + SUM(("sponsorTimes.endTime" - "sponsorTimes.startTime") / 60 * "sponsorTimes.views") as "minutesSaved", + 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" diff --git a/src/routes/getTotalStats.ts b/src/routes/getTotalStats.ts index dcc94e6..b851df2 100644 --- a/src/routes/getTotalStats.ts +++ b/src/routes/getTotalStats.ts @@ -14,8 +14,8 @@ let apiUsersCache = 0; let lastUserCountCheck = 0; export async function getTotalStats(req: Request, res: Response) { - let row = await db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " + - "SUM(views) as viewCount, SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE shadowHidden != 1 AND votes >= 0", []); + let row = await db.prepare('get', `SELECT COUNT(DISTINCT "userID") as "userCount", COUNT(*) as "totalSubmissions", + SUM("views") as "viewCount", SUM(("endTime" - "startTime") / 60 * "views") as "minutesSaved" FROM "sponsorTimes" WHERE "shadowHidden" != 1 AND "votes" >= 0`, []); if (row !== undefined) { let extensionUsers = chromeUsersCache + firefoxUsersCache; diff --git a/src/routes/getUserInfo.ts b/src/routes/getUserInfo.ts index a8d92c7..ca1066c 100644 --- a/src/routes/getUserInfo.ts +++ b/src/routes/getUserInfo.ts @@ -5,7 +5,7 @@ import {Logger} from '../utils/logger' async function dbGetSubmittedSegmentSummary(userID: string): Promise<{ minutesSaved: number, segmentCount: number }> { try { - let row = await db.prepare("get", "SELECT SUM(((endTime - startTime) / 60) * views) as minutesSaved, count(*) as segmentCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]); + let row = await db.prepare("get", `SELECT SUM((("endTime" - "startTime") / 60) * "views") as "minutesSaved", count(*) as "segmentCount" FROM "sponsorTimes" WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [userID]); if (row.minutesSaved != null) { return { minutesSaved: row.minutesSaved, @@ -24,7 +24,7 @@ async function dbGetSubmittedSegmentSummary(userID: string): Promise<{ minutesSa async function dbGetUsername(userID: string) { try { - let row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); + let row = await db.prepare('get', `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [userID]); if (row !== undefined) { return row.userName; } else { @@ -38,7 +38,7 @@ async function dbGetUsername(userID: string) { async function dbGetViewsForUser(userID: string) { try { - let row = await db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]); + let row = await db.prepare('get', `SELECT SUM("views") as "viewCount" FROM "sponsorTimes" WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [userID]); //increase the view count by one if (row.viewCount != null) { return row.viewCount; @@ -52,7 +52,7 @@ async function dbGetViewsForUser(userID: string) { async function dbGetWarningsForUser(userID: string): Promise { try { - let rows = await db.prepare('all', "SELECT * FROM warnings WHERE userID = ?", [userID]); + let rows = await db.prepare('all', `SELECT * FROM "warnings" WHERE "userID" = ?`, [userID]); return rows.length; } catch (err) { Logger.error('Couldn\'t get warnings for user ' + userID + '. returning 0'); diff --git a/src/routes/getUsername.ts b/src/routes/getUsername.ts index ef8966a..f8f7e1a 100644 --- a/src/routes/getUsername.ts +++ b/src/routes/getUsername.ts @@ -16,7 +16,7 @@ export async function getUsername(req: Request, res: Response) { userID = getHash(userID); try { - let row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]); + let row = await db.prepare('get', `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [userID]); if (row !== undefined) { res.send({ diff --git a/src/routes/getViewsForUser.ts b/src/routes/getViewsForUser.ts index 2daba71..ccc0344 100644 --- a/src/routes/getViewsForUser.ts +++ b/src/routes/getViewsForUser.ts @@ -16,7 +16,7 @@ export async function getViewsForUser(req: Request, res: Response) { userID = getHash(userID); try { - let row = await db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ?", [userID]); + let row = await db.prepare('get', `SELECT SUM("views") as "viewCount" FROM "sponsorTimes" WHERE "userID" = ?`, [userID]); //increase the view count by one if (row.viewCount != null) { diff --git a/src/routes/postNoSegments.ts b/src/routes/postNoSegments.ts index c92a19b..6ab49a7 100644 --- a/src/routes/postNoSegments.ts +++ b/src/routes/postNoSegments.ts @@ -35,7 +35,7 @@ export async function postNoSegments(req: Request, res: Response) { } // Get existing no segment markers - let noSegmentList = await db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID]); + let noSegmentList = await db.prepare('all', 'SELECT "category" from "noSegments" where "videoID" = ?', [videoID]); if (!noSegmentList || noSegmentList.length === 0) { noSegmentList = []; } else { @@ -59,7 +59,7 @@ export async function postNoSegments(req: Request, res: Response) { // create database entry for (const category of categoriesToMark) { try { - await db.prepare('run', "INSERT INTO noSegments (videoID, userID, category) VALUES(?, ?, ?)", [videoID, userID, category]); + await db.prepare('run', `INSERT INTO "noSegments" ("videoID", "userID", "category") VALUES(?, ?, ?)`, [videoID, userID, category]); } catch (err) { Logger.error("Error submitting 'noSegment' marker for category '" + category + "' for video '" + videoID + "'"); Logger.error(err); diff --git a/src/routes/postSegmentShift.ts b/src/routes/postSegmentShift.ts index 47e0f85..0b7f9ab 100644 --- a/src/routes/postSegmentShift.ts +++ b/src/routes/postSegmentShift.ts @@ -76,7 +76,7 @@ export async function postSegmentShift(req: Request, res: Response): Promise 0) { //already exists, update this row - await db.prepare('run', "UPDATE userNames SET userName = ? WHERE userID = ?", [userName, userID]); + await db.prepare('run', `UPDATE "userNames" SET "userName" = ? WHERE "userID" = ?`, [userName, userID]); } else { //add to the db - await db.prepare('run', "INSERT INTO userNames VALUES(?, ?)", [userID, userName]); + await db.prepare('run', `INSERT INTO "userNames" VALUES(?, ?)`, [userID, userName]); } res.sendStatus(200); diff --git a/src/routes/shadowBanUser.ts b/src/routes/shadowBanUser.ts index a115c7c..4007f7b 100644 --- a/src/routes/shadowBanUser.ts +++ b/src/routes/shadowBanUser.ts @@ -23,7 +23,7 @@ export async function shadowBanUser(req: Request, res: Response) { //hash the userID adminUserIDInput = getHash(adminUserIDInput); - const isVIP = (await db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [adminUserIDInput])).userCount > 0; + const isVIP = (await db.prepare("get", `SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?`, [adminUserIDInput])).userCount > 0; if (!isVIP) { //not authorized res.sendStatus(403); @@ -32,36 +32,36 @@ 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 privateDB.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 privateDB.prepare('run', `INSERT INTO "shadowBannedUsers" VALUES(?)`, [userID]); //find all previous submissions and hide them if (unHideOldSubmissions) { - await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?" - + " AND NOT EXISTS ( SELECT videoID, category FROM noSegments WHERE" - + " sponsorTimes.videoID = noSegments.videoID AND sponsorTimes.category = noSegments.category)", [userID]); + await db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 1 WHERE "userID" = ? + AND NOT EXISTS ( SELECT "videoID", "category" FROM "noSegments" WHERE + "sponsorTimes.videoID" = "noSegments.videoID" AND "sponsorTimes.category" = "noSegments.category")`, [userID]); } } else if (!enabled && row.userCount > 0) { //remove them from the shadow ban list - await privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]); + await privateDB.prepare('run', `DELETE FROM "shadowBannedUsers" WHERE userID = ?`, [userID]); //find all previous submissions and unhide them if (unHideOldSubmissions) { - let segmentsToIgnore = (await db.prepare('all', "SELECT UUID FROM sponsorTimes st " - + "JOIN noSegments ns on st.videoID = ns.videoID AND st.category = ns.category WHERE st.userID = ?" + let segmentsToIgnore = (await db.prepare('all', `SELECT UUID FROM "sponsorTimes" st + JOIN "noSegments" ns on "st.videoID" = "ns.videoID" AND st.category = ns.category WHERE "st.userID" = ?` , [userID])).map((item: {UUID: string}) => item.UUID); - let allSegments = (await db.prepare('all', "SELECT UUID FROM sponsorTimes st WHERE st.userID = ?", [userID])) + let allSegments = (await db.prepare('all', `SELECT "UUID" FROM "sponsorTimes" st WHERE "st.userID" = ?`, [userID])) .map((item: {UUID: string}) => item.UUID); allSegments.filter((item: {uuid: string}) => { return segmentsToIgnore.indexOf(item) === -1; }).forEach((UUID: string) => { - db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE UUID = ?", [UUID]); + db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 0 WHERE "UUID" = ?`, [UUID]); }); } } @@ -80,9 +80,9 @@ export async function shadowBanUser(req: Request, res: Response) { //find all previous submissions and hide them if (unHideOldSubmissions) { - await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE timeSubmitted IN " + - "(SELECT privateDB.timeSubmitted FROM sponsorTimes LEFT JOIN privateDB.sponsorTimes as privateDB ON sponsorTimes.timeSubmitted=privateDB.timeSubmitted " + - "WHERE privateDB.hashedIP = ?)", [hashedIP]); + await db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 1 WHERE "timeSubmitted" IN + (SELECT "privateDB.timeSubmitted" FROM "sponsorTimes" LEFT JOIN "privateDB.sponsorTimes" as "privateDB" ON "sponsorTimes.timeSubmitted"="privateDB.timeSubmitted" + WHERE "privateDB.hashedIP" = ?)`, [hashedIP]); } } /*else if (!enabled && row.userCount > 0) { // //remove them from the shadow ban list diff --git a/src/routes/viewedVideoSponsorTime.ts b/src/routes/viewedVideoSponsorTime.ts index 41c6484..59ae2ae 100644 --- a/src/routes/viewedVideoSponsorTime.ts +++ b/src/routes/viewedVideoSponsorTime.ts @@ -10,7 +10,7 @@ export async function viewedVideoSponsorTime(req: Request, res: Response): Promi } //up the view count by one - await db.prepare('run', "UPDATE sponsorTimes SET views = views + 1 WHERE UUID = ?", [UUID]); + await db.prepare('run', `UPDATE "sponsorTimes" SET views = views + 1 WHERE "UUID" = ?`, [UUID]); return res.sendStatus(200); } diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 41ca838..8ab4354 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -36,13 +36,13 @@ interface VoteData { } async function sendWebhooks(voteData: VoteData) { - const submissionInfoRow = await db.prepare('get', "SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " + - "(select count(1) from sponsorTimes where userID = s.userID) count, " + - "(select count(1) from sponsorTimes where userID = s.userID and votes <= -2) disregarded " + - "FROM sponsorTimes s left join userNames u on s.userID = u.userID where s.UUID=?", + const submissionInfoRow = await db.prepare('get', `SELECT "s.videoID", "s.userID", "s.startTime", "s.endTime", "s.category", "u.userName", + (select count(1) from "sponsorTimes" where "userID" = "s.userID") count, + (select count(1) from "sponsorTimes" where "userID" = "s.userID" and votes <= -2) disregarded + FROM "sponsorTimes" s left join "userNames" u on "s.userID" = "u.userID" where "s.UUID"=?`, [voteData.UUID]); - const userSubmissionCountRow = await db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [voteData.nonAnonUserID]); + const userSubmissionCountRow = await db.prepare('get', `SELECT count(*) as "submissionCount" FROM "sponsorTimes" WHERE "userID" = ?`, [voteData.nonAnonUserID]); if (submissionInfoRow !== undefined && userSubmissionCountRow != undefined) { let webhookURL: string = null; @@ -143,7 +143,7 @@ async function sendWebhooks(voteData: VoteData) { async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string, hashedIP: string, res: Response) { // Check if they've already made a vote - const usersLastVoteInfo = await privateDB.prepare('get', "select count(*) as votes, category from categoryVotes where UUID = ? and userID = ?", [UUID, userID]); + const usersLastVoteInfo = await privateDB.prepare('get', `select count(*) as votes, category from "categoryVotes" where "UUID" = ? and "userID" = ?`, [UUID, userID]); if (usersLastVoteInfo?.category === category) { // Double vote, ignore @@ -151,7 +151,7 @@ async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnS return; } - const currentCategory = await db.prepare('get', "select category from sponsorTimes where UUID = ?", [UUID]); + const currentCategory = await db.prepare('get', `select category from "sponsorTimes" where "UUID" = ?`, [UUID]); if (!currentCategory) { // Submission doesn't exist res.status(400).send("Submission doesn't exist."); @@ -163,35 +163,35 @@ async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnS return; } - const nextCategoryInfo = await db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, category]); + const nextCategoryInfo = await db.prepare("get", `select votes from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, category]); const timeSubmitted = Date.now(); const voteAmount = isVIP ? 500 : 1; // Add the vote - if ((await db.prepare('get', "select count(*) as count from categoryVotes where UUID = ? and category = ?", [UUID, category])).count > 0) { + if ((await db.prepare('get', `select count(*) as count from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, category])).count > 0) { // Update the already existing db entry - await db.prepare('run', "update categoryVotes set votes = votes + ? where UUID = ? and category = ?", [voteAmount, UUID, category]); + await db.prepare('run', `update "categoryVotes" set "votes" = "votes" + ? where "UUID" = ? and "category" = ?`, [voteAmount, UUID, category]); } else { // Add a db entry - await db.prepare('run', "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, category, voteAmount]); + await db.prepare('run', `insert into "categoryVotes" ("UUID", "category", "votes") values (?, ?, ?)`, [UUID, category, voteAmount]); } // Add the info into the private db if (usersLastVoteInfo?.votes > 0) { // Reverse the previous vote - await db.prepare('run', "update categoryVotes set votes = votes - ? where UUID = ? and category = ?", [voteAmount, UUID, usersLastVoteInfo.category]); + await db.prepare('run', `update "categoryVotes" set "votes" = "votes" - ? where "UUID" = ? and "category" = ?`, [voteAmount, UUID, usersLastVoteInfo.category]); - await privateDB.prepare('run', "update categoryVotes set category = ?, timeSubmitted = ?, hashedIP = ? where userID = ? and UUID = ?", [category, timeSubmitted, hashedIP, userID, UUID]); + await privateDB.prepare('run', `update "categoryVotes" set "category" = ?, "timeSubmitted" = ?, "hashedIP" = ? where "userID" = ? and "UUID" = ?`, [category, timeSubmitted, hashedIP, userID, UUID]); } else { - await privateDB.prepare('run', "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, userID, hashedIP, category, timeSubmitted]); + await privateDB.prepare('run', `insert into "categoryVotes" ("UUID", "userID", "hashedIP", "category", "timeSubmitted") values (?, ?, ?, ?, ?)`, [UUID, userID, hashedIP, category, timeSubmitted]); } // See if the submissions category is ready to change - const currentCategoryInfo = await db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, currentCategory.category]); + const currentCategoryInfo = await db.prepare("get", `select votes from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, currentCategory.category]); - const submissionInfo = await db.prepare("get", "SELECT userID, timeSubmitted, votes FROM sponsorTimes WHERE UUID = ?", [UUID]); + const submissionInfo = await db.prepare("get", `SELECT "userID", "timeSubmitted", "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); const isSubmissionVIP = submissionInfo && await isUserVIP(submissionInfo.userID); const startingVotes = isSubmissionVIP ? 10000 : 1; @@ -201,9 +201,9 @@ async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnS // Add submission as vote if (!currentCategoryInfo && submissionInfo) { - await db.prepare("run", "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, currentCategory.category, currentCategoryCount]); + await db.prepare("run", `insert into "categoryVotes" ("UUID", "category", "votes") values (?, ?, ?)`, [UUID, currentCategory.category, currentCategoryCount]); - await privateDB.prepare("run", "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, submissionInfo.userID, "unknown", currentCategory.category, submissionInfo.timeSubmitted]); + await privateDB.prepare("run", `insert into "categoryVotes" ("UUID", "userID", "hashedIP", "category", "timeSubmitted") values (?, ?, ?, ?, ?)`, [UUID, submissionInfo.userID, "unknown", currentCategory.category, submissionInfo.timeSubmitted]); } const nextCategoryCount = (nextCategoryInfo?.votes || 0) + voteAmount; @@ -212,7 +212,7 @@ async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnS // VIPs change it every time if (nextCategoryCount - currentCategoryCount >= Math.max(Math.ceil(submissionInfo?.votes / 2), 2) || isVIP || isOwnSubmission) { // Replace the category - await db.prepare('run', "update sponsorTimes set category = ? where UUID = ?", [category, UUID]); + await db.prepare('run', `update "sponsorTimes" set "category" = ? where "UUID" = ?`, [category, UUID]); } res.sendStatus(200); @@ -245,18 +245,18 @@ export async function voteOnSponsorTime(req: Request, res: Response) { const hashedIP = getHash(ip + config.globalSalt); //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 db.prepare('get', `SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?`, [nonAnonUserID])).userCount > 0; //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; + const isOwnSubmission = (await db.prepare("get", `SELECT "UUID" as "submissionCount" FROM "sponsorTimes" where "userID" = ? AND "UUID" = ?`, [nonAnonUserID, UUID])) !== undefined; // If not upvote if (!isVIP && type !== 1) { - const isSegmentLocked = async () => !!(await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", [UUID]))?.locked; - const isVideoLocked = async () => !!(await db.prepare('get', 'SELECT noSegments.category from noSegments left join sponsorTimes' + - ' on (noSegments.videoID = sponsorTimes.videoID and noSegments.category = sponsorTimes.category)' + - ' where UUID = ?', [UUID])); + const isSegmentLocked = async () => !!(await db.prepare('get', `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]))?.locked; + const isVideoLocked = async () => !!(await db.prepare('get', 'SELECT "noSegments.category" from "noSegments" left join "sponsorTimes"' + + ' on ("noSegments.videoID" = "sponsorTimes.videoID" and "noSegments.category" = "sponsorTimes.category")' + + ' where "UUID" = ?', [UUID])); if (await isSegmentLocked() || await isVideoLocked()) { res.status(403).send("Vote rejected: A moderator has decided that this segment is correct"); @@ -270,7 +270,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { if (type == 1 && !isVIP && !isOwnSubmission) { // Check if upvoting hidden segment - const voteInfo = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", [UUID]); + const voteInfo = await db.prepare('get', `SELECT votes FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); if (voteInfo && voteInfo.votes <= -2) { res.status(403).send("Not allowed to upvote segment with too many downvotes unless you are VIP."); @@ -280,7 +280,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { const MILLISECONDS_IN_HOUR = 3600000; const now = Date.now(); - const warningsCount = (await db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1", + const warningsCount = (await db.prepare('get', `SELECT count(1) as count FROM warnings WHERE "userID" = ? AND "issueTime" > ? AND enabled = 1`, [nonAnonUserID, Math.floor(now - (config.hoursAfterWarningExpires * MILLISECONDS_IN_HOUR))], )).count; @@ -292,7 +292,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { try { //check if vote has already happened - const votesRow = await privateDB.prepare('get', "SELECT type FROM votes WHERE userID = ? AND UUID = ?", [userID, UUID]); + const votesRow = await privateDB.prepare('get', `SELECT "type" FROM "votes" WHERE "userID" = ? AND "UUID" = ?`, [userID, UUID]); //-1 for downvote, 1 for upvote. Maybe more depending on reputation in the future let incrementAmount = 0; @@ -338,7 +338,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { } //check if the increment amount should be multiplied (downvotes have more power if there have been many views) - const row = await db.prepare('get', "SELECT videoID, votes, views FROM sponsorTimes WHERE UUID = ?", [UUID]) as + const row = await db.prepare('get', `SELECT "videoID", votes, views FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) as {videoID: VideoID, votes: number, views: number}; if (voteTypeEnum === voteTypes.normal) { @@ -357,16 +357,16 @@ export async function voteOnSponsorTime(req: Request, res: Response) { // Only change the database if they have made a submission before and haven't voted recently const ableToVote = isVIP - || ((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 privateDB.prepare("get", "SELECT UUID FROM votes WHERE UUID = ? AND hashedIP = ? AND userID != ?", [UUID, hashedIP, userID])) === undefined); + || ((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 privateDB.prepare("get", `SELECT "UUID" FROM "votes" WHERE "UUID" = ? AND "hashedIP" = ? AND "userID" != ?`, [UUID, hashedIP, userID])) === undefined); if (ableToVote) { //update the votes table if (votesRow != undefined) { - await privateDB.prepare('run', "UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?", [type, userID, UUID]); + await privateDB.prepare('run', `UPDATE "votes" SET "type" = ? WHERE "userID" = ? AND "UUID" = ?`, [type, userID, UUID]); } else { - await privateDB.prepare('run', "INSERT INTO votes VALUES(?, ?, ?, ?)", [UUID, userID, hashedIP, type]); + await privateDB.prepare('run', `INSERT INTO "votes" VALUES(?, ?, ?, ?)`, [UUID, userID, hashedIP, type]); } let columnName = ""; @@ -378,13 +378,13 @@ export async function voteOnSponsorTime(req: Request, res: Response) { //update the vote count on this sponsorTime //oldIncrementAmount will be zero is row is null - await db.prepare('run', "UPDATE sponsorTimes SET " + columnName + " = " + columnName + " + ? WHERE UUID = ?", [incrementAmount - oldIncrementAmount, UUID]); + await db.prepare('run', 'UPDATE "sponsorTimes" SET ' + columnName + ' = ' + columnName + ' + ? WHERE "UUID" = ?', [incrementAmount - oldIncrementAmount, UUID]); if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) { // Lock this submission - await db.prepare('run', "UPDATE sponsorTimes SET locked = 1 WHERE UUID = ?", [UUID]); + await db.prepare('run', 'UPDATE "sponsorTimes" SET locked = 1 WHERE "UUID" = ?', [UUID]); } else if (isVIP && incrementAmount < 0 && voteTypeEnum === voteTypes.normal) { // Unlock if a VIP downvotes it - await db.prepare('run', "UPDATE sponsorTimes SET locked = 0 WHERE UUID = ?", [UUID]); + await db.prepare('run', 'UPDATE "sponsorTimes" SET locked = 0 WHERE "UUID" = ?', [UUID]); } // Clear redis cache for this video @@ -393,7 +393,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { //for each positive vote, see if a hidden submission can be shown again if (incrementAmount > 0 && voteTypeEnum === voteTypes.normal) { //find the UUID that submitted the submission that was voted on - const submissionUserIDInfo = await db.prepare('get', "SELECT userID FROM sponsorTimes WHERE UUID = ?", [UUID]); + const submissionUserIDInfo = await db.prepare('get', 'SELECT "userID" FROM "sponsorTimes" WHERE "UUID" = ?', [UUID]); if (!submissionUserIDInfo) { // They are voting on a non-existent submission res.status(400).send("Voting on a non-existent submission"); @@ -403,14 +403,14 @@ export async function voteOnSponsorTime(req: Request, res: Response) { const submissionUserID = submissionUserIDInfo.userID; //check if any submissions are hidden - const hiddenSubmissionsRow = await db.prepare('get', "SELECT count(*) as hiddenSubmissions FROM sponsorTimes WHERE userID = ? AND shadowHidden > 0", [submissionUserID]); + const hiddenSubmissionsRow = await db.prepare('get', 'SELECT count(*) as "hiddenSubmissions" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" > 0', [submissionUserID]); if (hiddenSubmissionsRow.hiddenSubmissions > 0) { //see if some of this users submissions should be visible again if (await isUserTrustworthy(submissionUserID)) { //they are trustworthy again, show 2 of their submissions again, if there are two to show - await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE ROWID IN (SELECT ROWID FROM sponsorTimes WHERE userID = ? AND shadowHidden = 1 LIMIT 2)", [submissionUserID]); + await db.prepare('run', 'UPDATE "sponsorTimes" SET "shadowHidden" = 0 WHERE ROWID IN (SELECT ROWID FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = 1 LIMIT 2)', [submissionUserID]); } } } diff --git a/src/utils/isUserVIP.ts b/src/utils/isUserVIP.ts index 596ac62..36388b5 100644 --- a/src/utils/isUserVIP.ts +++ b/src/utils/isUserVIP.ts @@ -2,7 +2,7 @@ import {db} from '../databases/databases'; import { HashedUserID } from '../types/user.model'; export async function isUserVIP(userID: HashedUserID): Promise { - return (await db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID])).userCount > 0; + return (await db.prepare('get', `SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?`, [userID])).userCount > 0; } From 44f10b9ff995994ba3ee830b663b382b70e5b29e Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 4 Mar 2021 23:30:30 -0500 Subject: [PATCH 10/16] Removed some unused logs --- src/routes/getSavedTimeForUser.ts | 3 ++- src/routes/getSkipSegments.ts | 5 ----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/routes/getSavedTimeForUser.ts b/src/routes/getSavedTimeForUser.ts index 24fbbda..2d6c1c1 100644 --- a/src/routes/getSavedTimeForUser.ts +++ b/src/routes/getSavedTimeForUser.ts @@ -1,6 +1,7 @@ import {db} from '../databases/databases'; import {Request, Response} from 'express'; import {getHash} from '../utils/getHash'; +import { Logger } from '../utils/logger'; export async function getSavedTimeForUser(req: Request, res: Response) { let userID = req.query.userID as string; @@ -25,7 +26,7 @@ export async function getSavedTimeForUser(req: Request, res: Response) { res.sendStatus(404); } } catch (err) { - console.log(err); + Logger.error("getSavedTimeForUser " + err); res.sendStatus(500); return; diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index e77c844..78ec5f3 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -67,15 +67,10 @@ async function getSegmentsByVideoID(req: Request, videoID: string, categories: C return acc; }, {}); - console.log(segmentsByCategory) - - for (const [category, categorySegments] of Object.entries(segmentsByCategory)) { segments.push(...(await prepareCategorySegments(req, videoID as VideoID, category as Category, categorySegments, cache))); } - console.log(segments) - return segments; } catch (err) { if (err) { From 54e69b266d69e6ce2e1ae804e71fe70b04407d64 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 6 Mar 2021 00:25:18 -0500 Subject: [PATCH 11/16] Fix tests --- src/databases/Sqlite.ts | 21 +++++---------------- src/routes/getSkipSegments.ts | 6 +++--- src/routes/getTopUsers.ts | 10 +++++----- src/routes/shadowBanUser.ts | 23 +++++++++++------------ src/routes/voteOnSponsorTime.ts | 12 ++++++------ test/cases/unBan.ts | 2 +- 6 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index b2880a2..ba80b0b 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -12,30 +12,19 @@ export class Sqlite implements IDatabase { { } - async prepare(type: QueryType, query: string, params?: any[]) { + async prepare(type: QueryType, query: string, params: any[] = []) { + // Logger.debug(`prepare (sqlite): type: ${type}, query: ${query}, params: ${params}`); const preparedQuery = this.db.prepare(query); switch (type) { case 'get': { - if (params) { - return preparedQuery.get(...params); - } else { - return preparedQuery.get(); - } + return preparedQuery.get(...params); } case 'all': { - if (params) { - return preparedQuery.all(...params); - } else { - return preparedQuery.all(); - } + return preparedQuery.all(...params); } case 'run': { - if (params) { - preparedQuery.run(...params); - } else { - preparedQuery.run(); - } + preparedQuery.run(...params); break; } } diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index 78ec5f3..54e74e2 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -52,7 +52,7 @@ async function getSegmentsByVideoID(req: Request, videoID: string, categories: C const segments: Segment[] = []; try { - categories.filter((category) => !/[^a-z|_|-]/.test(category)); + categories = categories.filter((category) => !/[^a-z|_|-]/.test(category)); const segmentsByCategory: SBRecord = (await db .prepare( @@ -87,12 +87,12 @@ async function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, try { type SegmentWithHashPerVideoID = SBRecord}>; - categories.filter((category) => !/[^a-z|_|-]/.test(category)); + categories = categories.filter((category) => !(/[^a-z|_|-]/.test(category))); const segmentPerVideoID: SegmentWithHashPerVideoID = (await db .prepare( 'all', - `SELECT "startTime", "endTime", "votes", "locked", "UUID", "category", "shadowHidden", "hashedVideoID" FROM "sponsorTimes" + `SELECT "videoID", "startTime", "endTime", "votes", "locked", "UUID", "category", "shadowHidden", "hashedVideoID" FROM "sponsorTimes" WHERE "hashedVideoID" LIKE ? AND "category" IN (${categories.map((c) => "'" + c + "'")}) ORDER BY "startTime"`, [hashedVideoIDPrefix + '%'] )).reduce((acc: SegmentWithHashPerVideoID, segment: DBSegment) => { diff --git a/src/routes/getTopUsers.ts b/src/routes/getTopUsers.ts index 15ff3ae..0e3acb2 100644 --- a/src/routes/getTopUsers.ts +++ b/src/routes/getTopUsers.ts @@ -24,13 +24,13 @@ async function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boole } const rows = await db.prepare('all', `SELECT COUNT(*) as "totalSubmissions", SUM(views) as "viewCount", - SUM(("sponsorTimes.endTime" - "sponsorTimes.startTime") / 60 * "sponsorTimes.views") as "minutesSaved", + SUM(("sponsorTimes"."endTime" - "sponsorTimes"."startTime") / 60 * "sponsorTimes"."views") as "minutesSaved", 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 - GROUP BY IFNULL("userName", "sponsorTimes.userID") HAVING "userVotes" > 20 + `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 + GROUP BY IFNULL("userName", "sponsorTimes"."userID") HAVING "userVotes" > 20 ORDER BY "` + sortBy + `" DESC LIMIT 100`, []); for (let i = 0; i < rows.length; i++) { diff --git a/src/routes/shadowBanUser.ts b/src/routes/shadowBanUser.ts index 4007f7b..5ee22ee 100644 --- a/src/routes/shadowBanUser.ts +++ b/src/routes/shadowBanUser.ts @@ -44,29 +44,28 @@ export async function shadowBanUser(req: Request, res: Response) { if (unHideOldSubmissions) { await db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 1 WHERE "userID" = ? AND NOT EXISTS ( SELECT "videoID", "category" FROM "noSegments" WHERE - "sponsorTimes.videoID" = "noSegments.videoID" AND "sponsorTimes.category" = "noSegments.category")`, [userID]); + "sponsorTimes"."videoID" = "noSegments"."videoID" AND "sponsorTimes"."category" = "noSegments"."category")`, [userID]); } } else if (!enabled && row.userCount > 0) { //remove them from the shadow ban list - await privateDB.prepare('run', `DELETE FROM "shadowBannedUsers" WHERE userID = ?`, [userID]); + await privateDB.prepare('run', `DELETE FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); //find all previous submissions and unhide them if (unHideOldSubmissions) { let segmentsToIgnore = (await db.prepare('all', `SELECT UUID FROM "sponsorTimes" st - JOIN "noSegments" ns on "st.videoID" = "ns.videoID" AND st.category = ns.category WHERE "st.userID" = ?` + JOIN "noSegments" ns on "st"."videoID" = "ns"."videoID" AND st.category = ns.category WHERE "st"."userID" = ?` , [userID])).map((item: {UUID: string}) => item.UUID); - let allSegments = (await db.prepare('all', `SELECT "UUID" FROM "sponsorTimes" st WHERE "st.userID" = ?`, [userID])) + let allSegments = (await db.prepare('all', `SELECT "UUID" FROM "sponsorTimes" st WHERE "st"."userID" = ?`, [userID])) .map((item: {UUID: string}) => item.UUID); - allSegments.filter((item: {uuid: string}) => { + await Promise.all(allSegments.filter((item: {uuid: string}) => { return segmentsToIgnore.indexOf(item) === -1; - }).forEach((UUID: string) => { - db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 0 WHERE "UUID" = ?`, [UUID]); - }); + }).map((UUID: string) => { + return db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 0 WHERE "UUID" = ?`, [UUID]); + })); } } - } - else if (hashedIP) { + } else if (hashedIP) { //check to see if this user is already shadowbanned // let row = await privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedIPs WHERE hashedIP = ?", [hashedIP]); @@ -81,8 +80,8 @@ export async function shadowBanUser(req: Request, res: Response) { //find all previous submissions and hide them if (unHideOldSubmissions) { await db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 1 WHERE "timeSubmitted" IN - (SELECT "privateDB.timeSubmitted" FROM "sponsorTimes" LEFT JOIN "privateDB.sponsorTimes" as "privateDB" ON "sponsorTimes.timeSubmitted"="privateDB.timeSubmitted" - WHERE "privateDB.hashedIP" = ?)`, [hashedIP]); + (SELECT "privateDB"."timeSubmitted" FROM "sponsorTimes" LEFT JOIN "privateDB"."sponsorTimes" as "privateDB" ON "sponsorTimes"."timeSubmitted"="privateDB"."timeSubmitted" + WHERE "privateDB"."hashedIP" = ?)`, [hashedIP]); } } /*else if (!enabled && row.userCount > 0) { // //remove them from the shadow ban list diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 8ab4354..d74bc9b 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -36,10 +36,10 @@ interface VoteData { } async function sendWebhooks(voteData: VoteData) { - const submissionInfoRow = await db.prepare('get', `SELECT "s.videoID", "s.userID", "s.startTime", "s.endTime", "s.category", "u.userName", - (select count(1) from "sponsorTimes" where "userID" = "s.userID") count, - (select count(1) from "sponsorTimes" where "userID" = "s.userID" and votes <= -2) disregarded - FROM "sponsorTimes" s left join "userNames" u on "s.userID" = "u.userID" where "s.UUID"=?`, + const submissionInfoRow = await db.prepare('get', `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName", + (select count(1) from "sponsorTimes" where "userID" = s."userID") count, + (select count(1) from "sponsorTimes" where "userID" = s."userID" and votes <= -2) disregarded + FROM "sponsorTimes" s left join "userNames" u on s."userID" = u."userID" where s."UUID"=?`, [voteData.UUID]); const userSubmissionCountRow = await db.prepare('get', `SELECT count(*) as "submissionCount" FROM "sponsorTimes" WHERE "userID" = ?`, [voteData.nonAnonUserID]); @@ -254,8 +254,8 @@ export async function voteOnSponsorTime(req: Request, res: Response) { // If not upvote if (!isVIP && type !== 1) { const isSegmentLocked = async () => !!(await db.prepare('get', `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]))?.locked; - const isVideoLocked = async () => !!(await db.prepare('get', 'SELECT "noSegments.category" from "noSegments" left join "sponsorTimes"' + - ' on ("noSegments.videoID" = "sponsorTimes.videoID" and "noSegments.category" = "sponsorTimes.category")' + + const isVideoLocked = async () => !!(await db.prepare('get', 'SELECT "noSegments".category from "noSegments" left join "sponsorTimes"' + + ' on ("noSegments"."videoID" = "sponsorTimes"."videoID" and "noSegments".category = "sponsorTimes".category)' + ' where "UUID" = ?', [UUID])); if (await isSegmentLocked() || await isVideoLocked()) { diff --git a/test/cases/unBan.ts b/test/cases/unBan.ts index fc0010c..99d3e87 100644 --- a/test/cases/unBan.ts +++ b/test/cases/unBan.ts @@ -31,7 +31,7 @@ describe('unBan', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM sponsorTimes WHERE videoID = ? AND userID = ? AND shadowHidden = ?', ['unBan-videoID-0', 'testMan-unBan', 1]); + let result = await db.prepare('all', 'SELECT * FROM "sponsorTimes" WHERE "videoID" = ? AND "userID" = ? AND "shadowHidden" = ?', ['unBan-videoID-0', 'testMan-unBan', 1]); if (result.length !== 0) { console.log(result); done("Expected 0 banned entrys in db, got " + result.length); From 3fe7501802ffddbc2719d223d4340b0a22701119 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 6 Mar 2021 19:29:03 -0500 Subject: [PATCH 12/16] Support private db with postgres --- databases/_private.db.sql | 4 ++-- databases/_upgrade_private_1.sql | 2 +- src/databases/databases.ts | 20 +++++++++++++++----- src/routes/voteOnSponsorTime.ts | 3 +-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/databases/_private.db.sql b/databases/_private.db.sql index 4517c71..7e41b16 100644 --- a/databases/_private.db.sql +++ b/databases/_private.db.sql @@ -29,7 +29,7 @@ CREATE TABLE IF NOT EXISTS "config" ( "value" TEXT NOT NULL ); -CREATE INDEX IF NOT EXISTS sponsorTimes_hashedIP on sponsorTimes(hashedIP); -CREATE INDEX IF NOT EXISTS votes_userID on votes(UUID); +CREATE INDEX IF NOT EXISTS "sponsorTimes_hashedIP" on "sponsorTimes"("hashedIP"); +CREATE INDEX IF NOT EXISTS "votes_userID" on "votes"("UUID"); COMMIT; diff --git a/databases/_upgrade_private_1.sql b/databases/_upgrade_private_1.sql index c4de7e9..b4d7fec 100644 --- a/databases/_upgrade_private_1.sql +++ b/databases/_upgrade_private_1.sql @@ -3,6 +3,6 @@ BEGIN TRANSACTION; /* for testing the db upgrade, don't remove because it looks empty */ /* Add version to config */ -INSERT INTO config (key, value) VALUES("version", 1); +INSERT INTO config (key, value) VALUES('version', 1); COMMIT; \ No newline at end of file diff --git a/src/databases/databases.ts b/src/databases/databases.ts index 41c6b9e..255840b 100644 --- a/src/databases/databases.ts +++ b/src/databases/databases.ts @@ -17,18 +17,28 @@ if (config.mysql) { fileNamePrefix: 'sponsorTimes', readOnly: config.readOnly, createDbIfNotExists: config.createDatabaseIfNotExist, - enableWalCheckpointNumber: !config.readOnly && config.mode === "production", - postgres: config.postgres + postgres: { + user: config.postgres?.user, + host: config.postgres?.host, + database: "sponsorTimes", + password: config.postgres?.password, + port: config.postgres?.port, + } }); - privateDB = new Sqlite({ - dbPath: config.privateDB, + privateDB = new Postgres({ dbSchemaFileName: config.privateDBSchema, dbSchemaFolder: config.schemaFolder, fileNamePrefix: 'private', readOnly: config.readOnly, createDbIfNotExists: config.createDatabaseIfNotExist, - enableWalCheckpointNumber: false + postgres: { + user: config.postgres?.user, + host: config.postgres?.host, + database: "privateDB", + password: config.postgres?.password, + port: config.postgres?.port, + } }); } else { db = new Sqlite({ diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index d74bc9b..bfe9110 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -143,7 +143,7 @@ async function sendWebhooks(voteData: VoteData) { async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string, hashedIP: string, res: Response) { // Check if they've already made a vote - const usersLastVoteInfo = await privateDB.prepare('get', `select count(*) as votes, category from "categoryVotes" where "UUID" = ? and "userID" = ?`, [UUID, userID]); + const usersLastVoteInfo = await privateDB.prepare('get', `select count(*) as votes, category from "categoryVotes" where "UUID" = ? and "userID" = ? group by category`, [UUID, userID]); if (usersLastVoteInfo?.category === category) { // Double vote, ignore @@ -250,7 +250,6 @@ export async function voteOnSponsorTime(req: Request, res: Response) { //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; - // If not upvote if (!isVIP && type !== 1) { const isSegmentLocked = async () => !!(await db.prepare('get', `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]))?.locked; From 8729796e8704b9b2f855e2ba9090d6ba8ac65ae0 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 7 Mar 2021 00:21:56 -0500 Subject: [PATCH 13/16] Make tests pass running with postgres --- src/databases/IDatabase.ts | 2 +- src/databases/Mysql.ts | 2 +- src/databases/Postgres.ts | 29 ++++++- src/databases/Sqlite.ts | 2 +- src/databases/databases.ts | 6 +- src/index.ts | 13 +++- src/routes/shadowBanUser.ts | 2 +- src/routes/voteOnSponsorTime.ts | 2 +- test/cases/getIsUserVIP.ts | 2 +- test/cases/getSavedTimeForUser.ts | 2 +- test/cases/getSegmentsByHash.ts | 2 +- test/cases/getSkipSegments.ts | 2 +- test/cases/getUserInfo.ts | 10 +-- test/cases/noSegmentRecords.ts | 30 +++---- test/cases/oldGetSponsorTime.ts | 2 +- test/cases/oldSubmitSponsorTimes.ts | 4 +- test/cases/postSkipSegments.ts | 20 ++--- test/cases/postWarning.ts | 10 +-- test/cases/segmentShift.ts | 25 +++--- test/cases/unBan.ts | 28 +++---- test/cases/voteOnSponsorTime.ts | 116 ++++++++++++++-------------- test/test.ts | 70 +++++++++-------- 22 files changed, 208 insertions(+), 173 deletions(-) diff --git a/src/databases/IDatabase.ts b/src/databases/IDatabase.ts index 6001cdd..33244b0 100644 --- a/src/databases/IDatabase.ts +++ b/src/databases/IDatabase.ts @@ -1,5 +1,5 @@ export interface IDatabase { - init(): void; + async init(): Promise; prepare(type: QueryType, query: string, params?: any[]): Promise; } diff --git a/src/databases/Mysql.ts b/src/databases/Mysql.ts index 9ec78c7..079395d 100644 --- a/src/databases/Mysql.ts +++ b/src/databases/Mysql.ts @@ -9,7 +9,7 @@ export class Mysql implements IDatabase { constructor(private config: any) { } - init(): void { + async init(): Promise { this.connection = new MysqlInterface(this.config); } diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index e5e31d3..bae2da6 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -1,6 +1,6 @@ import { Logger } from '../utils/logger'; import { IDatabase, QueryType } from './IDatabase'; -import { Pool } from 'pg'; +import { Client, Pool } from 'pg'; import fs from "fs"; @@ -13,6 +13,10 @@ export class Postgres implements IDatabase { this.pool = new Pool(this.config.postgres); if (!this.config.readOnly) { + if (this.config.createDbIfNotExists) { + await this.createDB(); + } + if (this.config.createDbIfNotExists && !this.config.readOnly && fs.existsSync(this.config.dbSchemaFileName)) { await this.pool.query(this.processUpgradeQuery(fs.readFileSync(this.config.dbSchemaFileName).toString())); } @@ -74,6 +78,29 @@ export class Postgres implements IDatabase { } } + private async createDB() { + const client = new Client({ + ...this.config.postgres, + database: "postgres" + }); + + await client.connect(); + + if ((await client.query(`SELECT * FROM pg_database WHERE datname = '${this.config.postgres.database}'`)).rowCount == 0) { + await client.query(`CREATE DATABASE "${this.config.postgres.database}" + WITH + OWNER = ${this.config.postgres.user} + ENCODING = 'UTF8' + LC_COLLATE = 'en_US.utf8' + LC_CTYPE = 'en_US.utf8' + TABLESPACE = pg_default + CONNECTION LIMIT = -1;` + ); + } + + client.end(); + } + private async upgradeDB(fileNamePrefix: string, schemaFolder: string) { const versionCodeInfo = await this.pool.query("SELECT value FROM config WHERE key = 'version'"); let versionCode = versionCodeInfo.rows[0] ? versionCodeInfo.rows[0].value : 0; diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index ba80b0b..98422d3 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -30,7 +30,7 @@ export class Sqlite implements IDatabase { } } - init() { + async init() { // Make dirs if required if (!fs.existsSync(path.join(this.config.dbPath, "../"))) { fs.mkdirSync(path.join(this.config.dbPath, "../")); diff --git a/src/databases/databases.ts b/src/databases/databases.ts index 255840b..06f53ed 100644 --- a/src/databases/databases.ts +++ b/src/databases/databases.ts @@ -60,9 +60,9 @@ if (config.mysql) { enableWalCheckpointNumber: false }); } -function initDb() { - db.init(); - privateDB.init(); +async function initDb() { + await db.init(); + await privateDB.init(); if (db instanceof Sqlite) { // Attach private db to main db diff --git a/src/index.ts b/src/index.ts index 22bd0ac..13576a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,12 @@ import {initDb} from './databases/databases'; import {createServer} from "./app"; import {Logger} from "./utils/logger"; -initDb(); -createServer(() => { - Logger.info("Server started on port " + config.port + "."); -}); +async function init() { + await initDb(); + + createServer(() => { + Logger.info("Server started on port " + config.port + "."); + }); +} + +init(); \ No newline at end of file diff --git a/src/routes/shadowBanUser.ts b/src/routes/shadowBanUser.ts index 5ee22ee..92b1f02 100644 --- a/src/routes/shadowBanUser.ts +++ b/src/routes/shadowBanUser.ts @@ -52,7 +52,7 @@ export async function shadowBanUser(req: Request, res: Response) { //find all previous submissions and unhide them if (unHideOldSubmissions) { - let segmentsToIgnore = (await db.prepare('all', `SELECT UUID FROM "sponsorTimes" st + let segmentsToIgnore = (await db.prepare('all', `SELECT "UUID" FROM "sponsorTimes" st JOIN "noSegments" ns on "st"."videoID" = "ns"."videoID" AND st.category = ns.category WHERE "st"."userID" = ?` , [userID])).map((item: {UUID: string}) => item.UUID); let allSegments = (await db.prepare('all', `SELECT "UUID" FROM "sponsorTimes" st WHERE "st"."userID" = ?`, [userID])) diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index bfe9110..fca1ffa 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -357,7 +357,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) { // Only change the database if they have made a submission before and haven't voted recently const ableToVote = isVIP || ((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 privateDB.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); if (ableToVote) { diff --git a/test/cases/getIsUserVIP.ts b/test/cases/getIsUserVIP.ts index 7610e4a..8cb21a5 100644 --- a/test/cases/getIsUserVIP.ts +++ b/test/cases/getIsUserVIP.ts @@ -5,7 +5,7 @@ import {getHash} from '../../src/utils/getHash'; describe('getIsUserVIP', () => { before((done: Done) => { - db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("supertestman") + "')").then(done); + db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("supertestman") + "')").then(done); }); it('Should be able to get a 200', (done: Done) => { diff --git a/test/cases/getSavedTimeForUser.ts b/test/cases/getSavedTimeForUser.ts index 795f41b..7db6de9 100644 --- a/test/cases/getSavedTimeForUser.ts +++ b/test/cases/getSavedTimeForUser.ts @@ -5,7 +5,7 @@ import {getHash} from '../../src/utils/getHash'; describe('getSavedTimeForUser', () => { before(async () => { - let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; + let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES'; await db.prepare("run", startOfQuery + "('getSavedTimeForUser', 1, 11, 2, 'abc1239999', '" + getHash("testman") + "', 0, 50, 'sponsor', 0, '" + getHash('getSavedTimeForUser', 0) + "')"); return; diff --git a/test/cases/getSegmentsByHash.ts b/test/cases/getSegmentsByHash.ts index 1f5597f..d957e2d 100644 --- a/test/cases/getSegmentsByHash.ts +++ b/test/cases/getSegmentsByHash.ts @@ -12,7 +12,7 @@ sinonStub.callsFake(YouTubeApiMock.listVideos); describe('getSegmentsByHash', () => { before(async () => { - let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; + let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES'; await db.prepare("run", startOfQuery + "('getSegmentsByHash-0', 1, 10, 2, 'getSegmentsByHash-0-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('getSegmentsByHash-0', 1) + "')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 await db.prepare("run", startOfQuery + "('getSegmentsByHash-0', 20, 30, 2, 'getSegmentsByHash-0-1', 'testman', 100, 150, 'intro', 0, '" + getHash('getSegmentsByHash-0', 1) + "')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 await db.prepare("run", startOfQuery + "('getSegmentsByHash-noMatchHash', 40, 50, 2, 'getSegmentsByHash-noMatchHash', 'testman', 0, 50, 'sponsor', 0, 'fdaffnoMatchHash')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910 diff --git a/test/cases/getSkipSegments.ts b/test/cases/getSkipSegments.ts index ae562e5..85dcfbb 100644 --- a/test/cases/getSkipSegments.ts +++ b/test/cases/getSkipSegments.ts @@ -5,7 +5,7 @@ import {getHash} from '../../src/utils/getHash'; describe('getSkipSegments', () => { before(async () => { - let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, locked, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; + let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES'; await db.prepare("run", startOfQuery + "('testtesttest', 1, 11, 2, 0, '1-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest', 1) + "')"); await db.prepare("run", startOfQuery + "('testtesttest', 20, 33, 2, 0, '1-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('testtesttest', 1) + "')"); await db.prepare("run", startOfQuery + "('testtesttest,test', 1, 11, 2, 0, '1-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest,test', 1) + "')"); diff --git a/test/cases/getUserInfo.ts b/test/cases/getUserInfo.ts index 318f28b..0e369e4 100644 --- a/test/cases/getUserInfo.ts +++ b/test/cases/getUserInfo.ts @@ -5,9 +5,9 @@ import {getHash} from '../../src/utils/getHash'; describe('getUserInfo', () => { before(async () => { - let startOfUserNamesQuery = "INSERT INTO userNames (userID, userName) VALUES"; + let startOfUserNamesQuery = `INSERT INTO "userNames" ("userID", "userName") VALUES`; await db.prepare("run", startOfUserNamesQuery + "('" + getHash("getuserinfo_user_01") + "', 'Username user 01')"); - let startOfSponsorTimesQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden) VALUES"; + let startOfSponsorTimesQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden") VALUES'; await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000001', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000002', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -1, 'uuid000003', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); @@ -18,9 +18,9 @@ describe('getUserInfo', () => { await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000008', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)"); - await db.prepare("run", "INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_0') + "', 10, 'getuserinfo_vip', 1)"); - await db.prepare("run", "INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)"); - await db.prepare("run", "INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES ('" + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)"); + await db.prepare("run", `INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES ('` + getHash('getuserinfo_warning_0') + "', 10, 'getuserinfo_vip', 1)"); + await db.prepare("run", `INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES ('` + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)"); + await db.prepare("run", `INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES ('` + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)"); }); it('Should be able to get a 200', (done: Done) => { diff --git a/test/cases/noSegmentRecords.ts b/test/cases/noSegmentRecords.ts index a789b5d..0d551aa 100644 --- a/test/cases/noSegmentRecords.ts +++ b/test/cases/noSegmentRecords.ts @@ -6,19 +6,19 @@ import {db} from '../../src/databases/databases'; describe('noSegmentRecords', () => { before(async () => { - await db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser-noSegments") + "')"); + await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("VIPUser-noSegments") + "')"); - await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id', 'sponsor')"); - await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id', 'intro')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-noSegments") + "', 'no-segments-video-id', 'sponsor')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-noSegments") + "', 'no-segments-video-id', 'intro')"); - await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id-1', 'sponsor')"); - await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'no-segments-video-id-1', 'intro')"); - await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'noSubmitVideo', 'sponsor')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-noSegments") + "', 'no-segments-video-id-1', 'sponsor')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-noSegments") + "', 'no-segments-video-id-1', 'intro')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-noSegments") + "', 'noSubmitVideo', 'sponsor')"); - await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record', 'sponsor')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-noSegments") + "', 'delete-record', 'sponsor')"); - await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record-1', 'sponsor')"); - await db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-noSegments") + "', 'delete-record-1', 'intro')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-noSegments") + "', 'delete-record-1', 'sponsor')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-noSegments") + "', 'delete-record-1', 'intro')"); }); it('Should update the database version when starting the application', async () => { @@ -95,7 +95,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['no-segments-video-id-1']); + let result = await db.prepare('all', 'SELECT * FROM "noSegments" WHERE "videoID" = ?', ['no-segments-video-id-1']); if (result.length !== 4) { console.log(result); done("Expected 4 entrys in db, got " + result.length); @@ -129,7 +129,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['underscore']); + let result = await db.prepare('all', 'SELECT * FROM "noSegments" WHERE "videoID" = ?', ['underscore']); if (result.length !== 1) { console.log(result); done("Expected 1 entrys in db, got " + result.length); @@ -163,7 +163,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['bothCases']); + let result = await db.prepare('all', 'SELECT * FROM "noSegments" WHERE "videoID" = ?', ['bothCases']); if (result.length !== 1) { console.log(result); done("Expected 1 entrys in db, got " + result.length); @@ -197,7 +197,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['specialChar']); + let result = await db.prepare('all', 'SELECT * FROM "noSegments" WHERE "videoID" = ?', ['specialChar']); if (result.length !== 0) { console.log(result); done("Expected 0 entrys in db, got " + result.length); @@ -395,7 +395,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['delete-record']); + let result = await db.prepare('all', 'SELECT * FROM "noSegments" WHERE "videoID" = ?', ['delete-record']); if (result.length === 0) { done(); } else { @@ -426,7 +426,7 @@ describe('noSegmentRecords', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM noSegments WHERE videoID = ?', ['delete-record-1']); + let result = await db.prepare('all', 'SELECT * FROM "noSegments" WHERE "videoID" = ?', ['delete-record-1']); if (result.length === 1) { done(); } else { diff --git a/test/cases/oldGetSponsorTime.ts b/test/cases/oldGetSponsorTime.ts index 067ea7d..fb044ea 100644 --- a/test/cases/oldGetSponsorTime.ts +++ b/test/cases/oldGetSponsorTime.ts @@ -18,7 +18,7 @@ import {getHash} from '../../src/utils/getHash'; describe('getVideoSponsorTime (Old get method)', () => { before(() => { - let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; + let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES'; db.prepare("run", startOfQuery + "('old-testtesttest', 1, 11, 2, 'uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('old-testtesttest', 1) + "')"); db.prepare("run", startOfQuery + "('old-testtesttest,test', 1, 11, 2, 'uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('old-testtesttest,test', 1) + "')"); }); diff --git a/test/cases/oldSubmitSponsorTimes.ts b/test/cases/oldSubmitSponsorTimes.ts index 96c4292..90e0449 100644 --- a/test/cases/oldSubmitSponsorTimes.ts +++ b/test/cases/oldSubmitSponsorTimes.ts @@ -9,7 +9,7 @@ describe('postVideoSponsorTime (Old submission method)', () => { + "/api/postVideoSponsorTimes?videoID=dQw4w9WgXcQ&startTime=1&endTime=10&userID=test") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcQ"]); + let row = await db.prepare('get', `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcQ"]); if (row.startTime === 1 && row.endTime === 10 && row.category === "sponsor") { done(); } else { @@ -32,7 +32,7 @@ describe('postVideoSponsorTime (Old submission method)', () => { }) .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcE"]); + let row = await db.prepare('get', `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcE"]); if (row.startTime === 1 && row.endTime === 11 && row.category === "sponsor") { done(); } else { diff --git a/test/cases/postSkipSegments.ts b/test/cases/postSkipSegments.ts index faebba7..e31326a 100644 --- a/test/cases/postSkipSegments.ts +++ b/test/cases/postSkipSegments.ts @@ -13,7 +13,7 @@ sinonStub.callsFake(YouTubeApiMock.listVideos); describe('postSkipSegments', () => { before(() => { - let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; + let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES'; db.prepare("run", startOfQuery + "('80percent_video', 0, 1000, 0, '80percent-uuid-0', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); db.prepare("run", startOfQuery + "('80percent_video', 1001, 1005, 0, '80percent-uuid-1', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); @@ -26,7 +26,7 @@ describe('postSkipSegments', () => { const warnUser03Hash = getHash("warn-user03"); const MILLISECONDS_IN_HOUR = 3600000; const warningExpireTime = MILLISECONDS_IN_HOUR * config.hoursAfterWarningExpires; - const startOfWarningQuery = 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES'; + const startOfWarningQuery = 'INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES'; db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 1)"); db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); @@ -40,7 +40,7 @@ describe('postSkipSegments', () => { db.prepare("run", startOfWarningQuery + "('" + warnUser03Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); db.prepare("run", startOfWarningQuery + "('" + warnUser03Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUserSubmission") + "')"); + db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("VIPUserSubmission") + "')"); }); it('Should be able to submit a single time (Params method)', (done: Done) => { @@ -53,7 +53,7 @@ describe('postSkipSegments', () => { }) .then(async res => { if (res.status === 200) { - const row = await db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]); + const row = await db.prepare('get', `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcR"]); if (row.startTime === 2 && row.endTime === 10 && row.category === "sponsor") { done(); } else { @@ -84,7 +84,7 @@ describe('postSkipSegments', () => { }) .then(async res => { if (res.status === 200) { - const row = await db.prepare('get', "SELECT startTime, endTime, locked, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcF"]); + const row = await db.prepare('get', `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcF"]); if (row.startTime === 0 && row.endTime === 10 && row.locked === 0 && row.category === "sponsor") { done(); } else { @@ -115,7 +115,7 @@ describe('postSkipSegments', () => { }) .then(async res => { if (res.status === 200) { - const row = await db.prepare('get', "SELECT startTime, endTime, locked, category FROM sponsorTimes WHERE videoID = ?", ["vipuserIDSubmission"]); + const row = await db.prepare('get', `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["vipuserIDSubmission"]); if (row.startTime === 0 && row.endTime === 10 && row.locked === 1 && row.category === "sponsor") { done(); } else { @@ -149,7 +149,7 @@ describe('postSkipSegments', () => { }) .then(async res => { if (res.status === 200) { - const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]); + const rows = await db.prepare('all', `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcR"]); let success = true; if (rows.length === 2) { for (const row of rows) { @@ -196,7 +196,7 @@ describe('postSkipSegments', () => { }) .then(async res => { if (res.status === 200) { - const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["L_jWHffIx5E"]); + const rows = await db.prepare('all', `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ? and "votes" > -1`, ["L_jWHffIx5E"]); let success = true; if (rows.length === 4) { for (const row of rows) { @@ -245,7 +245,7 @@ describe('postSkipSegments', () => { }) .then(async res => { if (res.status === 400) { - const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["n9rIGdXnSJc"]); + const rows = await db.prepare('all', `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ? and "votes" > -1`, ["n9rIGdXnSJc"]); let success = true; if (rows.length === 4) { for (const row of rows) { @@ -293,7 +293,7 @@ describe('postSkipSegments', () => { }) .then(async res => { if (res.status === 400) { - const rows = await db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["80percent_video"]); + const rows = await db.prepare('all', `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ? and "votes" > -1`, ["80percent_video"]); let success = rows.length == 2; for (const row of rows) { if ((row.startTime === 2000 || row.endTime === 4000 || row.category === "sponsor") || diff --git a/test/cases/postWarning.ts b/test/cases/postWarning.ts index 43627eb..aaa5664 100644 --- a/test/cases/postWarning.ts +++ b/test/cases/postWarning.ts @@ -4,8 +4,8 @@ import {db} from '../../src/databases/databases'; import {getHash} from '../../src/utils/getHash'; describe('postWarning', () => { - before(() => { - db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("warning-vip") + "')"); + before(async () => { + await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("warning-vip") + "')"); }); it('Should be able to create warning if vip (exp 200)', (done: Done) => { @@ -23,7 +23,7 @@ describe('postWarning', () => { }) .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); + let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) { done(); } else { @@ -54,7 +54,7 @@ describe('postWarning', () => { }) .then(async res => { if (res.status === 409) { - let row = await db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); + let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) { done(); } else { @@ -86,7 +86,7 @@ describe('postWarning', () => { }) .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]); + let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); if (row?.enabled == 0) { done(); } else { diff --git a/test/cases/segmentShift.ts b/test/cases/segmentShift.ts index a8b7d08..2030ec7 100644 --- a/test/cases/segmentShift.ts +++ b/test/cases/segmentShift.ts @@ -4,16 +4,16 @@ import {db} from '../../src/databases/databases'; import {getHash} from '../../src/utils/getHash'; import {IDatabase} from '../../src/databases/IDatabase'; -function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, endTime: number, UUID: string, category: string) { +async function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, endTime: number, UUID: string, category: string) { const votes = 0, userID = 0, timeSubmitted = 0, views = 0, shadowHidden = 0, hashedVideoID = `hash_${UUID}`; - db.prepare("run", `INSERT INTO - sponsorTimes (videoID, startTime, endTime, votes, UUID, - userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) + await db.prepare("run", `INSERT INTO + "sponsorTimes" ("videoID", "startTime", "endTime", votes, "UUID", + "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES ('${videoID}', ${startTime}, ${endTime}, ${votes}, '${UUID}', '${userID}', ${timeSubmitted}, ${views}, '${category}', ${shadowHidden}, '${hashedVideoID}') @@ -21,13 +21,13 @@ function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, en } async function dbSponsorTimesSetByUUID(db: IDatabase, UUID: string, startTime: number, endTime: number) { - await db.prepare('run', `UPDATE sponsorTimes SET startTime = ?, endTime = ? WHERE UUID = ?`, [startTime, endTime, UUID]); + await db.prepare('run', `UPDATE "sponsorTimes" SET "startTime" = ?, "endTime" = ? WHERE "UUID" = ?`, [startTime, endTime, UUID]); } async function dbSponsorTimesCompareExpect(db: IDatabase, expect: any) { for (let i = 0, len = expect.length; i < len; i++) { const expectSeg = expect[i]; - let seg = await db.prepare('get', "SELECT startTime, endTime FROM sponsorTimes WHERE UUID = ?", [expectSeg.UUID]); + let seg = await db.prepare('get', `SELECT "startTime", "endTime" FROM "sponsorTimes" WHERE "UUID" = ?`, [expectSeg.UUID]); if ('removed' in expect) { if (expect.removed === true && seg.votes === -2) { return; @@ -50,14 +50,13 @@ describe('segmentShift', function () { const vipUserID = getHash(privateVipUserID); const baseURL = getbaseURL(); - before(function (done: Done) { + before(async function () { // startTime and endTime get set in beforeEach for consistency - dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid01', 'intro'); - dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid02', 'sponsor'); - dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid03', 'interaction'); - dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid04', 'outro'); - db.prepare("run", `INSERT INTO vipUsers (userID) VALUES ('${vipUserID}')`); - done(); + await dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid01', 'intro'); + await dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid02', 'sponsor'); + await dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid03', 'interaction'); + await dbSponsorTimesAdd(db, 'vsegshift01', 0, 0, 'vsegshifttest01uuid04', 'outro'); + await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('${vipUserID}')`); }); beforeEach(function (done: Done) { diff --git a/test/cases/unBan.ts b/test/cases/unBan.ts index 99d3e87..bcf2936 100644 --- a/test/cases/unBan.ts +++ b/test/cases/unBan.ts @@ -3,23 +3,23 @@ import fetch from 'node-fetch'; import * as utils from '../utils'; import { getHash } from '../../src/utils/getHash'; -import { db } from '../../src/databases/databases'; +import { db, privateDB } from '../../src/databases/databases'; import { Logger } from '../../src/utils/logger.js'; describe('unBan', () => { - before(() => { - db.prepare("run", "INSERT INTO shadowBannedUsers VALUES('testMan-unBan')"); - db.prepare("run", "INSERT INTO shadowBannedUsers VALUES('testWoman-unBan')"); - db.prepare("run", "INSERT INTO shadowBannedUsers VALUES('testEntity-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')`); - db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser-unBan") + "')"); - db.prepare("run", "INSERT INTO noSegments (userID, videoID, category) VALUES ('" + getHash("VIPUser-unBan") + "', 'unBan-videoID-1', 'sponsor')"); + await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("VIPUser-unBan") + "')"); + await db.prepare("run", `INSERT INTO "noSegments" ("userID", "videoID", "category") VALUES ('` + getHash("VIPUser-unBan") + "', 'unBan-videoID-1', 'sponsor')"); - let startOfInsertSegmentQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-0', 1, 11, 2, 'unBan-uuid-0', 'testMan-unBan', 0, 50, 'sponsor', 1, '" + getHash('unBan-videoID-0', 1) + "')"); - db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-1', 1, 11, 2, 'unBan-uuid-1', 'testWoman-unBan', 0, 50, 'sponsor', 1, '" + getHash('unBan-videoID-1', 1) + "')"); - db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-1', 1, 11, 2, 'unBan-uuid-2', 'testEntity-unBan', 0, 60, 'sponsor', 1, '" + getHash('unBan-videoID-1', 1) + "')"); - db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-2', 1, 11, 2, 'unBan-uuid-3', 'testEntity-unBan', 0, 60, 'sponsor', 1, '" + getHash('unBan-videoID-2', 1) + "')"); + let startOfInsertSegmentQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES'; + await db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-0', 1, 11, 2, 'unBan-uuid-0', 'testMan-unBan', 0, 50, 'sponsor', 1, '" + getHash('unBan-videoID-0', 1) + "')"); + await db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-1', 1, 11, 2, 'unBan-uuid-1', 'testWoman-unBan', 0, 50, 'sponsor', 1, '" + getHash('unBan-videoID-1', 1) + "')"); + await db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-1', 1, 11, 2, 'unBan-uuid-2', 'testEntity-unBan', 0, 60, 'sponsor', 1, '" + getHash('unBan-videoID-1', 1) + "')"); + await db.prepare("run", startOfInsertSegmentQuery + "('unBan-videoID-2', 1, 11, 2, 'unBan-uuid-3', 'testEntity-unBan', 0, 60, 'sponsor', 1, '" + getHash('unBan-videoID-2', 1) + "')"); }); it('Should be able to unban a user and re-enable shadow banned segments', (done) => { @@ -56,7 +56,7 @@ describe('unBan', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM sponsorTimes WHERE videoID = ? AND userID = ? AND shadowHidden = ?', ['unBan-videoID-1', 'testWoman-unBan', 1]); + let result = await db.prepare('all', 'SELECT * FROM "sponsorTimes" WHERE "videoID" = ? AND "userID" = ? AND "shadowHidden" = ?', ['unBan-videoID-1', 'testWoman-unBan', 1]); if (result.length !== 1) { console.log(result); done("Expected 1 banned entry1 in db, got " + result.length); @@ -81,7 +81,7 @@ describe('unBan', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM sponsorTimes WHERE userID = ? AND shadowHidden = ?', ['testEntity-unBan', 1]); + let result = await db.prepare('all', 'SELECT * FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?', ['testEntity-unBan', 1]); if (result.length !== 1) { console.log(result); done("Expected 1 banned entry1 in db, got " + result.length); diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 0b7eae2..47a1f19 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -12,54 +12,54 @@ const sinonStub = mockManager.mock('listVideos'); sinonStub.callsFake(YouTubeApiMock.listVideos); describe('voteOnSponsorTime', () => { - before(() => { + before(async () => { const now = Date.now(); const warnVip01Hash = getHash("warn-vip01"); const warnUser01Hash = getHash("warn-voteuser01"); const warnUser02Hash = getHash("warn-voteuser02"); const MILLISECONDS_IN_HOUR = 3600000; const warningExpireTime = MILLISECONDS_IN_HOUR * config.hoursAfterWarningExpires; - let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - const startOfWarningQuery = 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES'; + let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES'; + const startOfWarningQuery = 'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled") VALUES'; - db.prepare("run", startOfQuery + "('vote-testtesttest', 1, 11, 2, 'vote-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 2, 'vote-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest2', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 10, 'vote-uuid-1.5', 'testman', 0, 50, 'outro', 0, '" + getHash('vote-testtesttest2', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 10, 'vote-uuid-1.6', 'testman', 0, 50, 'interaction', 0, '" + getHash('vote-testtesttest2', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-testtesttest3', 20, 33, 10, 'vote-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-testtesttest3', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-testtesttest,test', 1, 11, 100, 'vote-uuid-3', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest,test', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-test3', 1, 11, 2, 'vote-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-test3', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5_1', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-multiple', 1, 11, 2, 'vote-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-multiple', 20, 33, 2, 'vote-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); - db.prepare("run", startOfQuery + "('voter-submitter', 1, 11, 2, 'vote-uuid-8', '" + getHash("randomID") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter', 1) + "')"); - db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-9', '" + getHash("randomID2") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); - db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-10', '" + getHash("randomID3") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); - db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-11', '" + getHash("randomID4") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); - db.prepare("run", startOfQuery + "('own-submission-video', 1, 11, 500, 'own-submission-uuid', '" + getHash('own-submission-id') + "', 0, 50, 'sponsor', 0, '" + getHash('own-submission-video', 1) + "')"); - db.prepare("run", startOfQuery + "('not-own-submission-video', 1, 11, 500, 'not-own-submission-uuid', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('not-own-submission-video', 1) + "')"); - db.prepare("run", startOfQuery + "('incorrect-category', 1, 11, 500, 'incorrect-category', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('incorrect-category', 1) + "')"); - db.prepare("run", startOfQuery + "('incorrect-category-change', 1, 11, 500, 'incorrect-category-change', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('incorrect-category-change', 1) + "')"); - db.prepare("run", startOfQuery + "('vote-testtesttest', 1, 11, 2, 'warnvote-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest', 1) + "')"); - db.prepare("run", startOfQuery + "('no-sponsor-segments-video', 1, 11, 2, 'no-sponsor-segments-uuid-0', 'no-sponsor-segments', 0, 50, 'sponsor', 0, '" + getHash('no-sponsor-segments-video', 1) + "')"); - db.prepare("run", startOfQuery + "('no-sponsor-segments-video', 1, 11, 2, 'no-sponsor-segments-uuid-1', 'no-sponsor-segments', 0, 50, 'intro', 0, '" + getHash('no-sponsor-segments-video', 1) + "')"); - db.prepare("run", startOfQuery + "('segment-locking-video', 1, 11, 2, 'segment-locking-uuid-1', 'segment-locking-user', 0, 50, 'intro', 0, '" + getHash('segment-locking-video', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-testtesttest', 1, 11, 2, 'vote-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 2, 'vote-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest2', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 10, 'vote-uuid-1.5', 'testman', 0, 50, 'outro', 0, '" + getHash('vote-testtesttest2', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-testtesttest2', 1, 11, 10, 'vote-uuid-1.6', 'testman', 0, 50, 'interaction', 0, '" + getHash('vote-testtesttest2', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-testtesttest3', 20, 33, 10, 'vote-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-testtesttest3', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-testtesttest,test', 1, 11, 100, 'vote-uuid-3', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest,test', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-test3', 1, 11, 2, 'vote-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-test3', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5_1', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-multiple', 1, 11, 2, 'vote-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-multiple', 20, 33, 2, 'vote-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); + await db.prepare("run", startOfQuery + "('voter-submitter', 1, 11, 2, 'vote-uuid-8', '" + getHash("randomID") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter', 1) + "')"); + await db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-9', '" + getHash("randomID2") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); + await db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-10', '" + getHash("randomID3") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); + await db.prepare("run", startOfQuery + "('voter-submitter2', 1, 11, 2, 'vote-uuid-11', '" + getHash("randomID4") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter2', 1) + "')"); + await db.prepare("run", startOfQuery + "('own-submission-video', 1, 11, 500, 'own-submission-uuid', '" + getHash('own-submission-id') + "', 0, 50, 'sponsor', 0, '" + getHash('own-submission-video', 1) + "')"); + await db.prepare("run", startOfQuery + "('not-own-submission-video', 1, 11, 500, 'not-own-submission-uuid', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('not-own-submission-video', 1) + "')"); + await db.prepare("run", startOfQuery + "('incorrect-category', 1, 11, 500, 'incorrect-category', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('incorrect-category', 1) + "')"); + await db.prepare("run", startOfQuery + "('incorrect-category-change', 1, 11, 500, 'incorrect-category-change', '" + getHash('somebody-else-id') + "', 0, 50, 'sponsor', 0, '" + getHash('incorrect-category-change', 1) + "')"); + await db.prepare("run", startOfQuery + "('vote-testtesttest', 1, 11, 2, 'warnvote-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest', 1) + "')"); + await db.prepare("run", startOfQuery + "('no-sponsor-segments-video', 1, 11, 2, 'no-sponsor-segments-uuid-0', 'no-sponsor-segments', 0, 50, 'sponsor', 0, '" + getHash('no-sponsor-segments-video', 1) + "')"); + await db.prepare("run", startOfQuery + "('no-sponsor-segments-video', 1, 11, 2, 'no-sponsor-segments-uuid-1', 'no-sponsor-segments', 0, 50, 'intro', 0, '" + getHash('no-sponsor-segments-video', 1) + "')"); + await db.prepare("run", startOfQuery + "('segment-locking-video', 1, 11, 2, 'segment-locking-uuid-1', 'segment-locking-user', 0, 50, 'intro', 0, '" + getHash('segment-locking-video', 1) + "')"); - db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 1000)) + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 2000)) + "', '" + warnVip01Hash + "', 1)"); + await db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + await db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 1000) + "', '" + warnVip01Hash + "', 1)"); + await db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 2000) + "', '" + warnVip01Hash + "', 1)"); + await db.prepare("run", startOfWarningQuery + "('" + warnUser01Hash + "', '" + (now - 3601000) + "', '" + warnVip01Hash + "', 1)"); + await db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + await db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)"); + await db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 1000)) + "', '" + warnVip01Hash + "', 1)"); + await db.prepare("run", startOfWarningQuery + "('" + warnUser02Hash + "', '" + (now - (warningExpireTime + 2000)) + "', '" + warnVip01Hash + "', 1)"); - db.prepare("run", "INSERT INTO vipUsers (userID) VALUES ('" + getHash("VIPUser") + "')"); - privateDB.prepare("run", "INSERT INTO shadowBannedUsers (userID) VALUES ('" + getHash("randomID4") + "')"); + await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("VIPUser") + "')"); + await privateDB.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES ('` + getHash("randomID4") + "')"); - db.prepare("run", "INSERT INTO noSegments (videoID, userID, category) VALUES ('no-sponsor-segments-video', 'someUser', 'sponsor')"); + await db.prepare("run", `INSERT INTO "noSegments" ("videoID", "userID", "category") VALUES ('no-sponsor-segments-video', 'someUser', 'sponsor')`); }); @@ -68,7 +68,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID&UUID=vote-uuid-0&type=1") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-0"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-0"]); if (row.votes === 3) { done(); } else { @@ -86,7 +86,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-2&type=0") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-2"]); if (row.votes < 10) { done(); } else { @@ -104,7 +104,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID3&UUID=vote-uuid-2&type=0") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-2"]); if (row.votes === 9) { done(); } else { @@ -122,7 +122,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID4&UUID=vote-uuid-1.6&type=0") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1.6"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-1.6"]); if (row.votes === 10) { done(); } else { @@ -140,7 +140,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=hasNotSubmittedID&UUID=vote-uuid-1&type=1") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-1"]); if (row.votes === 2) { done(); } else { @@ -158,7 +158,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=hasNotSubmittedID&UUID=vote-uuid-1.5&type=0") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-1.5"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-1.5"]); if (row.votes === 10) { done(); } else { @@ -176,7 +176,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-3&type=0") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-3"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-3"]); if (row.votes <= -2) { done(); } else { @@ -194,7 +194,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=own-submission-id&UUID=own-submission-uuid&type=0") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["own-submission-uuid"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["own-submission-uuid"]); if (row.votes <= -2) { done(); } else { @@ -212,7 +212,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=not-own-submission-uuid&type=0") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["not-own-submission-uuid"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["not-own-submission-uuid"]); if (row.votes === 499) { done(); } else { @@ -230,8 +230,8 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=intro") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]); - let categoryRows = await db.prepare('all', "SELECT votes, category FROM categoryVotes WHERE UUID = ?", ["vote-uuid-4"]); + let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-4"]); + let categoryRows = await db.prepare('all', `SELECT votes, category FROM "categoryVotes" WHERE "UUID" = ?`, ["vote-uuid-4"]); if (row.category === "sponsor" && categoryRows.length === 2 && categoryRows[0]?.votes === 1 && categoryRows[0]?.category === "intro" && categoryRows[1]?.votes === 1 && categoryRows[1]?.category === "sponsor") { @@ -251,7 +251,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=incorrect-category&category=fakecategory") .then(async res => { if (res.status === 400) { - let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["incorrect-category"]); + let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["incorrect-category"]); if (row.category === "sponsor") { done(); } else { @@ -269,8 +269,8 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=outro") .then(async res => { if (res.status === 200) { - let submissionRow = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]); - let categoryRows = await db.prepare('all', "SELECT votes, category FROM categoryVotes WHERE UUID = ?", ["vote-uuid-4"]); + let submissionRow = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-4"]); + let categoryRows = await db.prepare('all', `SELECT votes, category FROM "categoryVotes" WHERE "UUID" = ?`, ["vote-uuid-4"]); let introVotes = 0; let outroVotes = 0; let sponsorVotes = 0; @@ -299,7 +299,7 @@ describe('voteOnSponsorTime', () => { fetch(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=incorrect-category-change&category=" + inputCat) .then(async res => { - let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["incorrect-category-change"]); + let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["incorrect-category-change"]); if (row.category === assertCat) { callback(); } else { @@ -319,8 +319,8 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-5&category=outro") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); - let row2 = await db.prepare('get', "SELECT votes FROM categoryVotes WHERE UUID = ? and category = ?", ["vote-uuid-5", "outro"]); + let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-5"]); + let row2 = await db.prepare('get', `SELECT votes FROM "categoryVotes" WHERE "UUID" = ? and category = ?`, ["vote-uuid-5", "outro"]); if (row.category === "outro" && row2.votes === 500) { done(); } else { @@ -338,7 +338,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=testman&UUID=vote-uuid-5_1&category=outro") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); + let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-5"]); if (row.category === "outro") { done(); } else { @@ -382,7 +382,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=vote-uuid-5&type=1") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); + let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["vote-uuid-5"]); if (row.votes > -3) { done(); } else { @@ -452,7 +452,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=segment-locking-uuid-1&type=1") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", ["segment-locking-uuid-1"]); + let row = await db.prepare('get', `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, ["segment-locking-uuid-1"]); if (row?.locked) { done(); } else { @@ -470,7 +470,7 @@ describe('voteOnSponsorTime', () => { + "/api/voteOnSponsorTime?userID=VIPUser&UUID=segment-locking-uuid-1&type=0") .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", ["segment-locking-uuid-1"]); + let row = await db.prepare('get', `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, ["segment-locking-uuid-1"]); if (!row?.locked) { done(); } else { diff --git a/test/test.ts b/test/test.ts index 7102f1e..8697652 100644 --- a/test/test.ts +++ b/test/test.ts @@ -10,44 +10,48 @@ import {ImportMock} from 'ts-mock-imports'; import * as rateLimitMiddlewareModule from '../src/middleware/requestRateLimit'; import rateLimit from 'express-rate-limit'; -ImportMock.mockFunction(rateLimitMiddlewareModule, 'rateLimitMiddleware', rateLimit({ - skip: () => { - return true; - } -})); +async function init() { + ImportMock.mockFunction(rateLimitMiddlewareModule, 'rateLimitMiddleware', rateLimit({ + skip: () => { + return true; + } + })); -// delete old test database -if (fs.existsSync(config.db)) fs.unlinkSync(config.db) -if (fs.existsSync(config.privateDB)) fs.unlinkSync(config.privateDB); + // delete old test database + if (fs.existsSync(config.db)) fs.unlinkSync(config.db) + if (fs.existsSync(config.privateDB)) fs.unlinkSync(config.privateDB); -initDb(); + await initDb(); -// Instantiate a Mocha instance. -const mocha = new Mocha(); + // Instantiate a Mocha instance. + const mocha = new Mocha(); -const testDir = './test/cases'; + const testDir = './test/cases'; -// Add each .ts file to the mocha instance -fs.readdirSync(testDir) - .filter(function(file) { - // Only keep the .ts files - return file.substr(-3) === '.ts'; - }) - .forEach(function(file) { - mocha.addFile( - path.join(testDir, file) - ); - }); + // Add each .ts file to the mocha instance + fs.readdirSync(testDir) + .filter(function(file) { + // Only keep the .ts files + return file.substr(-3) === '.ts'; + }) + .forEach(function(file) { + mocha.addFile( + path.join(testDir, file) + ); + }); -const mockServer = createMockServer(() => { - Logger.info("Started mock HTTP Server"); - const server = createServer(() => { - Logger.info("Started main HTTP server"); - // Run the tests. - mocha.run((failures) => { - mockServer.close(); - server.close(); - process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures + const mockServer = createMockServer(() => { + Logger.info("Started mock HTTP Server"); + const server = createServer(() => { + Logger.info("Started main HTTP server"); + // Run the tests. + mocha.run((failures) => { + mockServer.close(); + server.close(); + process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures + }); }); }); -}); +} + +init(); \ No newline at end of file From eacd9cb6e8a9f36da51ccde48201dee42b23e4e7 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 7 Mar 2021 00:37:47 -0500 Subject: [PATCH 14/16] Secure docker containers to local network --- docker/docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 224055a..a54a3c0 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -8,7 +8,7 @@ services: volumes: - database-data:/var/lib/postgresql/data ports: - - 5432:5432 + - 127.0.0.1:5432:5432 redis: container_name: redis image: redis @@ -16,7 +16,7 @@ services: volumes: - ./redis/redis.conf:/usr/local/etc/redis/redis.conf ports: - - 32773:6379 + - 127.0.0.1:32773:6379 volumes: database-data: \ No newline at end of file From b0c7a6c537a3d22817d91fcfef9660f040c4b507 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 8 Mar 2021 18:28:41 -0500 Subject: [PATCH 15/16] Partially fix getTopUsers --- src/routes/getTopUsers.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/routes/getTopUsers.ts b/src/routes/getTopUsers.ts index 0e3acb2..279253f 100644 --- a/src/routes/getTopUsers.ts +++ b/src/routes/getTopUsers.ts @@ -15,12 +15,12 @@ async function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boole let additionalFields = ''; if (categoryStatsEnabled) { - additionalFields += "SUM(CASE WHEN category = 'sponsor' THEN 1 ELSE 0 END) as categorySponsor, " + - "SUM(CASE WHEN category = 'intro' THEN 1 ELSE 0 END) as categorySumIntro, " + - "SUM(CASE WHEN category = 'outro' THEN 1 ELSE 0 END) as categorySumOutro, " + - "SUM(CASE WHEN category = 'interaction' THEN 1 ELSE 0 END) as categorySumInteraction, " + - "SUM(CASE WHEN category = 'selfpromo' THEN 1 ELSE 0 END) as categorySelfpromo, " + - "SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, "; + additionalFields += `SUM(CASE WHEN category = 'sponsor' THEN 1 ELSE 0 END) as "categorySponsor", + SUM(CASE WHEN category = 'intro' THEN 1 ELSE 0 END) as "categorySumIntro", + SUM(CASE WHEN category = 'outro' THEN 1 ELSE 0 END) as "categorySumOutro", + SUM(CASE WHEN category = 'interaction' THEN 1 ELSE 0 END) as "categorySumInteraction", + SUM(CASE WHEN category = 'selfpromo' THEN 1 ELSE 0 END) as "categorySelfpromo", + SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as "categoryMusicOfftopic", `; } const rows = await db.prepare('all', `SELECT COUNT(*) as "totalSubmissions", SUM(views) as "viewCount", From a4bbc9f2ba7c7dd9b429603956874e39800cb1d9 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 8 Mar 2021 19:25:20 -0500 Subject: [PATCH 16/16] Add pg load file --- docker/migrate/database.pgload.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docker/migrate/database.pgload.txt diff --git a/docker/migrate/database.pgload.txt b/docker/migrate/database.pgload.txt new file mode 100644 index 0000000..d738d93 --- /dev/null +++ b/docker/migrate/database.pgload.txt @@ -0,0 +1,5 @@ +load database + from ./database.db + into postgresql://sponsorblock:pw@127.0.0.1:5432/sponsorTimes + +with quote identifiers; \ No newline at end of file