diff --git a/databases/_private.db.sql b/databases/_private.db.sql index 5239cab..4517c71 100644 --- a/databases/_private.db.sql +++ b/databases/_private.db.sql @@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS "sponsorTimes" ( ); CREATE TABLE IF NOT EXISTS "config" ( - "key" TEXT NOT NULL, + "key" TEXT NOT NULL, "value" TEXT NOT NULL ); diff --git a/package-lock.json b/package-lock.json index 0d8ac5e..9f33f1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -782,9 +782,9 @@ "dev": true }, "dot-prop": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-4.2.0.tgz", - "integrity": "sha512-tUMXrxlExSW6U2EXiiKGSBVdYgtV8qlHL+C10TsW4PURY/ic+eaysnSkwB4kA/mBlCyy/IKDJ+Lc3wbWeaXtuQ==", + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-4.2.1.tgz", + "integrity": "sha512-l0p4+mIuJIua0mhxGoh4a+iNL9bmeK5DvnSVQa6T0OhrVmaEa1XScX5Etc673FePCJOArq/4Pa2cLGODUWTPOQ==", "dev": true, "requires": { "is-obj": "^1.0.0" diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index c91e004..3c70e6b 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -132,7 +132,7 @@ function chooseSegments(segments: any[]) { }); //if there are too many groups, find the best 8 - return getWeightedRandomChoice(similarSegmentsGroups, 8).map( + return getWeightedRandomChoice(similarSegmentsGroups, 32).map( //randomly choose 1 good segment per group and return them group => getWeightedRandomChoice(group.segments, 1)[0], ); diff --git a/src/routes/shadowBanUser.ts b/src/routes/shadowBanUser.ts index 4e2a218..5bdbb18 100644 --- a/src/routes/shadowBanUser.ts +++ b/src/routes/shadowBanUser.ts @@ -2,7 +2,6 @@ import {db, privateDB} from '../databases/databases'; import {getHash} from '../utils/getHash'; import {Request, Response} from 'express'; - export async function shadowBanUser(req: Request, res: Response) { const userID = req.query.userID as string; const hashedIP = req.query.hashedIP as string; @@ -51,7 +50,17 @@ export async function shadowBanUser(req: Request, res: Response) { //find all previous submissions and unhide them if (unHideOldSubmissions) { - db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE userID = ?", [userID]); + let segmentsToIgnore = 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]) + .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]); + }); } } } diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 7d6b7de..6c99b57 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -179,7 +179,7 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, category: an // 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 submissionInfo = db.prepare("get", "SELECT userID, timeSubmitted FROM sponsorTimes WHERE UUID = ?", [UUID]); + const submissionInfo = db.prepare("get", "SELECT userID, timeSubmitted, votes FROM sponsorTimes WHERE UUID = ?", [UUID]); const isSubmissionVIP = submissionInfo && isUserVIP(submissionInfo.userID); const startingVotes = isSubmissionVIP ? 10000 : 1; @@ -198,7 +198,7 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, category: an //TODO: In the future, raise this number from zero to make it harder to change categories // VIPs change it every time - if (nextCategoryCount - currentCategoryCount >= 0 || isVIP) { + if (nextCategoryCount - currentCategoryCount >= (submissionInfo ? Math.max(Math.ceil(submissionInfo.votes / 2), 1) : 1) || isVIP) { // Replace the category db.prepare('run', "update sponsorTimes set category = ? where UUID = ?", [category, UUID]); } diff --git a/test/cases/getSkipSegments.ts b/test/cases/getSkipSegments.ts index dfc6dc2..aff3c66 100644 --- a/test/cases/getSkipSegments.ts +++ b/test/cases/getSkipSegments.ts @@ -6,13 +6,13 @@ import {getHash} from '../../src/utils/getHash'; describe('getSkipSegments', () => { before(() => { let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; - db.exec(startOfQuery + "('testtesttest', 1, 11, 2, '1-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest', 0) + "')"); - db.exec(startOfQuery + "('testtesttest', 20, 33, 2, '1-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('testtesttest', 0) + "')"); - db.exec(startOfQuery + "('testtesttest,test', 1, 11, 2, '1-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest,test', 0) + "')"); - db.exec(startOfQuery + "('test3', 1, 11, 2, '1-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 0) + "')"); - db.exec(startOfQuery + "('test3', 7, 22, -3, '1-uuid-5', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 0) + "')"); - db.exec(startOfQuery + "('multiple', 1, 11, 2, '1-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 0) + "')"); - db.exec(startOfQuery + "('multiple', 20, 33, 2, '1-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 0) + "')"); + db.exec(startOfQuery + "('testtesttest', 1, 11, 2, '1-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest', 1) + "')"); + db.exec(startOfQuery + "('testtesttest', 20, 33, 2, '1-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('testtesttest', 1) + "')"); + db.exec(startOfQuery + "('testtesttest,test', 1, 11, 2, '1-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest,test', 1) + "')"); + db.exec(startOfQuery + "('test3', 1, 11, 2, '1-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')"); + db.exec(startOfQuery + "('test3', 7, 22, -3, '1-uuid-5', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')"); + db.exec(startOfQuery + "('multiple', 1, 11, 2, '1-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')"); + db.exec(startOfQuery + "('multiple', 20, 33, 2, '1-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')"); }); diff --git a/test/cases/unBan.ts b/test/cases/unBan.ts new file mode 100644 index 0000000..f9a2491 --- /dev/null +++ b/test/cases/unBan.ts @@ -0,0 +1,78 @@ +import request from 'request'; + +import * as utils from '../utils'; +import { getHash } from '../../src/utils/getHash'; + +import { db } from '../../src/databases/databases'; +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.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')"); + + 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) + "')"); + }); + + it('Should be able to unban a user and re-enable shadow banned segments', (done) => { + request.post(utils.getbaseURL() + "/api/shadowBanUser?userID=testMan-unBan&adminUserID=VIPUser-unBan&enabled=false", null, (err, res, body) => { + if (err) done(err); + else if (res.statusCode === 200) { + let result = 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); + } else { + done(); + } + } else { + console.log(body); + done("Status code was " + res.statusCode); + } + }); + }); + + it('Should be able to unban a user and re-enable shadow banned segments without noSegment entrys', (done) => { + request.post(utils.getbaseURL() + "/api/shadowBanUser?userID=testWoman-unBan&adminUserID=VIPUser-unBan&enabled=false", null, (err, res, body) => { + if (err) done(err); + else if (res.statusCode === 200) { + let result = 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); + } else { + done(); + } + } else { + console.log(body); + done("Status code was " + res.statusCode); + } + }); + }); + + it('Should be able to unban a user and re-enable shadow banned segments with a mix of noSegment entrys', (done) => { + request.post(utils.getbaseURL() + "/api/shadowBanUser?userID=testEntity-unBan&adminUserID=VIPUser-unBan&enabled=false", null, (err, res, body) => { + if (err) done(err); + else if (res.statusCode === 200) { + let result = 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); + } else { + done(); + } + } else { + console.log(body); + done("Status code was " + res.statusCode); + } + }); + }); +}); \ No newline at end of file diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 7880cac..0f84c2d 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -218,6 +218,7 @@ describe('voteOnSponsorTime', () => { }); }); + /** Test needs to be updated with new category vote limit it('Should be able to vote for a category and it should immediately change (for now)', (done: Done) => { request.get(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=intro", null, @@ -235,6 +236,7 @@ describe('voteOnSponsorTime', () => { } }); }); + */ it('Should not able to change to an invalid category', (done: Done) => { request.get(getbaseURL() @@ -254,6 +256,7 @@ describe('voteOnSponsorTime', () => { }); }); + /** Test needs to be updated with new category vote limit it('Should be able to change your vote for a category and it should immediately change (for now)', (done: Done) => { request.get(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=outro", null, @@ -271,6 +274,7 @@ describe('voteOnSponsorTime', () => { } }); }); + */ it('Should not be able to change your vote to an invalid category', (done: Done) => {