Merge pull request #92 from Joe-Dowd/mysql-conv

Added ability to query a mysql database
This commit is contained in:
Ajay Ramachandran
2020-07-25 19:01:57 -04:00
committed by GitHub
21 changed files with 180 additions and 113 deletions

View File

@@ -16,6 +16,7 @@
"express": "^4.17.1",
"http": "0.0.0",
"iso8601-duration": "^1.2.0",
"sync-mysql": "^3.0.1",
"uuid": "^3.3.2",
"youtube-api": "^2.0.10"
},

27
src/databases/Mysql.js Normal file
View File

@@ -0,0 +1,27 @@
var MysqlInterface = require('sync-mysql');
class Mysql {
constructor(config) {
this.connection = new MysqlInterface(config);
}
exec(query) {
this.prepare('run', query, []);
}
prepare (type, query, params) {
(config.mode === "development") && console.log("prepare (mysql): type: " + type + ", query: " + query + ", params: " + params);
if (type === 'get') {
return this.connection.query(query, params)[0];
} else if (type === 'run') {
this.connection.query(query, params);
} else if (type === 'all') {
return this.connection.query(query, params);
} else {
console.log('returning undefined...')
return undefined;
}
}
}
module.exports = Mysql;

31
src/databases/Sqlite.js Normal file
View File

@@ -0,0 +1,31 @@
const { db } = require("./databases");
class Sqlite {
constructor(connection) {
this.connection = connection;
}
getConnection() {
return this.connection;
}
prepare(type, query, params) {
if (type === 'get') {
return this.connection.prepare(query).get(...params);
} else if (type === 'run') {
this.connection.prepare(query).run(...params);
} else if (type === 'all') {
return this.connection.prepare(query).all(...params);
} else {
(config.mode === "development") && console.log('returning undefined...')
(config.mode === "development") && console.log("prepare: type: " + type + ", query: " + query + ", params: " + params);
return undefined;
}
}
exec(query) {
return this.connection.exec(query);
}
}
module.exports = Sqlite;

View File

@@ -2,58 +2,67 @@ var config = require('../config.js');
var Sqlite3 = require('better-sqlite3');
var fs = require('fs');
var path = require('path');
var Sqlite = require('./Sqlite.js')
var Mysql = require('./Mysql.js')
let options = {
readonly: config.readOnly,
fileMustExist: !config.createDatabaseIfNotExist
};
// Make dirs if required
if (!fs.existsSync(path.join(config.db, "../"))) {
fs.mkdirSync(path.join(config.db, "../"));
}
if (!fs.existsSync(path.join(config.privateDB, "../"))) {
fs.mkdirSync(path.join(config.privateDB, "../"));
}
if (config.mysql) {
module.exports = {
db: new Mysql(config.mysql),
privateDB: new Mysql(config.privateMysql)
};
} else {
// Make dirs if required
if (!fs.existsSync(path.join(config.db, "../"))) {
fs.mkdirSync(path.join(config.db, "../"));
}
if (!fs.existsSync(path.join(config.privateDB, "../"))) {
fs.mkdirSync(path.join(config.privateDB, "../"));
}
var db = new Sqlite3(config.db, options);
var privateDB = new Sqlite3(config.privateDB, options);
var db = new Sqlite3(config.db, options);
var privateDB = new Sqlite3(config.privateDB, options);
if (config.createDatabaseIfNotExist && !config.readOnly) {
if (fs.existsSync(config.dbSchema)) db.exec(fs.readFileSync(config.dbSchema).toString());
if (fs.existsSync(config.privateDBSchema)) privateDB.exec(fs.readFileSync(config.privateDBSchema).toString());
}
if (config.createDatabaseIfNotExist && !config.readOnly) {
if (fs.existsSync(config.dbSchema)) db.exec(fs.readFileSync(config.dbSchema).toString());
if (fs.existsSync(config.privateDBSchema)) privateDB.exec(fs.readFileSync(config.privateDBSchema).toString());
}
// Upgrade database if required
if (!config.readOnly) {
ugradeDB(db, "sponsorTimes");
ugradeDB(privateDB, "private")
}
// Upgrade database if required
if (!config.readOnly) {
ugradeDB(db, "sponsorTimes");
ugradeDB(privateDB, "private")
}
// Enable WAL mode checkpoint number
if (!config.readOnly && config.mode === "production") {
db.exec("PRAGMA journal_mode=WAL;");
db.exec("PRAGMA wal_autocheckpoint=1;");
}
// Enable WAL mode checkpoint number
if (!config.readOnly && config.mode === "production") {
db.exec("PRAGMA journal_mode=WAL;");
db.exec("PRAGMA wal_autocheckpoint=1;");
}
// Enable Memory-Mapped IO
db.exec("pragma mmap_size= 500000000;");
privateDB.exec("pragma mmap_size= 500000000;");
// Enable Memory-Mapped IO
db.exec("pragma mmap_size= 500000000;");
privateDB.exec("pragma mmap_size= 500000000;");
module.exports = {
db: db,
privateDB: privateDB
};
module.exports = {
db: new Sqlite(db),
privateDB: new Sqlite(privateDB)
};
function ugradeDB(db, prefix) {
let versionCodeInfo = db.prepare("SELECT value FROM config WHERE key = ?").get("version");
let versionCode = versionCodeInfo ? versionCodeInfo.value : 0;
function ugradeDB(db, prefix) {
let versionCodeInfo = db.prepare("SELECT value FROM config WHERE key = ?").get("version");
let versionCode = versionCodeInfo ? versionCodeInfo.value : 0;
let path = config.schemaFolder + "/_upgrade_" + prefix + "_" + (versionCode + 1) + ".sql";
while (fs.existsSync(path)) {
db.exec(fs.readFileSync(path).toString());
let path = config.schemaFolder + "/_upgrade_" + prefix + "_" + (versionCode + 1) + ".sql";
while (fs.existsSync(path)) {
db.exec(fs.readFileSync(path).toString());
versionCode = db.prepare("SELECT value FROM config WHERE key = ?").get("version").value;
path = config.schemaFolder + "/_upgrade_" + prefix + "_" + (versionCode + 1) + ".sql";
versionCode = db.prepare("SELECT value FROM config WHERE key = ?").get("version").value;
path = config.schemaFolder + "/_upgrade_" + prefix + "_" + (versionCode + 1) + ".sql";
}
}
}

View File

@@ -31,14 +31,14 @@ module.exports = async function addUserAsVIP (req, res) {
}
//check to see if this user is already a vip
let row = db.prepare("SELECT count(*) as userCount FROM vipUsers WHERE userID = ?").get(userID);
let row = 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("INSERT INTO vipUsers VALUES(?)").run(userID);
db.prepare('run', "INSERT INTO vipUsers VALUES(?)", [userID]);
} else if (!enabled && row.userCount > 0) {
//remove them from the shadow ban list
db.prepare("DELETE FROM vipUsers WHERE userID = ?").run(userID);
db.prepare('run', "DELETE FROM vipUsers WHERE userID = ?", [userID]);
}
res.sendStatus(200);

View File

@@ -1,7 +1,7 @@
var db = require('../databases/databases.js').db;
module.exports = function getDaysSavedFormatted (req, res) {
let row = db.prepare("SELECT SUM((endTime - startTime) / 60 / 60 / 24 * views) as daysSaved from sponsorTimes where shadowHidden != 1").get();
let row = db.prepare('get', "SELECT SUM((endTime - startTime) / 60 / 60 / 24 * views) as daysSaved from sponsorTimes where shadowHidden != 1", []);
if (row !== undefined) {
//send this result

View File

@@ -14,7 +14,7 @@ module.exports = function getSavedTimeForUser (req, res) {
userID = getHash(userID);
try {
let row = db.prepare("SELECT SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE userID = ? AND votes > -1 AND shadowHidden != 1 ").get(userID);
let row = 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({

View File

@@ -121,9 +121,10 @@ function handleGetSegments(req, res) {
for (const category of categories) {
const categorySegments = db
.prepare(
'SELECT startTime, endTime, votes, UUID, shadowHidden FROM sponsorTimes WHERE videoID = ? and category = ? ORDER BY startTime'
'all',
'SELECT startTime, endTime, votes, UUID, shadowHidden FROM sponsorTimes WHERE videoID = ? and category = ? ORDER BY startTime',
[videoID, category]
)
.all(videoID, category)
.filter(segment => {
if (segment.votes < -1) {
return false; //too untrustworthy, just ignore it
@@ -136,9 +137,7 @@ function handleGetSegments(req, res) {
}
if (shadowHiddenSegments === undefined) {
shadowHiddenSegments = privateDB
.prepare('SELECT hashedIP FROM sponsorTimes WHERE videoID = ?')
.all(videoID);
shadowHiddenSegments = privateDB.prepare('all', 'SELECT hashedIP FROM sponsorTimes WHERE videoID = ?', [videoID]);
}
//if this isn't their ip, don't send it to them

View File

@@ -40,11 +40,11 @@ module.exports = function getTopUsers (req, res) {
"SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, ";
}
let rows = db.prepare("SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," +
let rows = db.prepare('all', "SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," +
"SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, " +
additionalFields +
"IFNULL(userNames.userName, sponsorTimes.userID) as userName FROM sponsorTimes LEFT JOIN userNames ON sponsorTimes.userID=userNames.userID " +
"WHERE sponsorTimes.votes > -1 AND sponsorTimes.shadowHidden != 1 GROUP BY IFNULL(userName, sponsorTimes.userID) ORDER BY " + sortBy + " DESC LIMIT 100").all();
"WHERE sponsorTimes.votes > -1 AND sponsorTimes.shadowHidden != 1 GROUP BY IFNULL(userName, sponsorTimes.userID) ORDER BY " + sortBy + " DESC LIMIT 100", []);
for (let i = 0; i < rows.length; i++) {
userNames[i] = rows[i].userName;

View File

@@ -8,8 +8,8 @@ var lastUserCountCheck = 0;
module.exports = function getTotalStats (req, res) {
let row = db.prepare("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").get();
let row = 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", []);
if (row !== undefined) {
//send this result

View File

@@ -15,7 +15,7 @@ module.exports = function getUsername (req, res) {
userID = getHash(userID);
try {
let row = db.prepare("SELECT userName FROM userNames WHERE userID = ?").get(userID);
let row = db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]);
if (row !== undefined) {
res.send({

View File

@@ -14,7 +14,7 @@ module.exports = function getViewsForUser(req, res) {
userID = getHash(userID);
try {
let row = db.prepare("SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ?").get(userID);
let row = db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ?", [userID]);
//increase the view count by one
if (row.viewCount != null) {

View File

@@ -16,7 +16,7 @@ function sendDiscordNotification(userID, videoID, UUID, segmentInfo) {
//check if they are a first time user
//if so, send a notification to discord
if (config.youtubeAPIKey !== null && config.discordFirstTimeSubmissionsWebhookURL !== null) {
let userSubmissionCountRow = db.prepare("SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?").get(userID);
let userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]);
// If it is a first time submission
if (userSubmissionCountRow.submissionCount <= 1) {
@@ -161,8 +161,8 @@ module.exports = async function postSkipSegments(req, res) {
}
//check if this info has already been submitted before
let duplicateCheck2Row = db.prepare("SELECT COUNT(*) as count FROM sponsorTimes WHERE startTime = ? " +
"and endTime = ? and category = ? and videoID = ?").get(startTime, endTime, segments[i].category, videoID);
let duplicateCheck2Row = 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;
@@ -177,7 +177,7 @@ module.exports = async function postSkipSegments(req, res) {
try {
//check if this user is on the vip list
let vipRow = db.prepare("SELECT count(*) as userCount FROM vipUsers WHERE userID = ?").get(userID);
let vipRow = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]);
//get current time
let timeSubmitted = Date.now();
@@ -187,7 +187,7 @@ module.exports = async function postSkipSegments(req, res) {
// Disable IP ratelimiting for now
if (false) {
//check to see if this ip has submitted too many sponsors today
let rateLimitCheckRow = privateDB.prepare("SELECT COUNT(*) as count FROM sponsorTimes WHERE hashedIP = ? AND videoID = ? AND timeSubmitted > ?").get([hashedIP, videoID, yesterday]);
let rateLimitCheckRow = 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
@@ -200,7 +200,7 @@ module.exports = async function postSkipSegments(req, res) {
// Disable max submissions for now
if (false) {
//check to see if the user has already submitted sponsors for this video
let duplicateCheckRow = db.prepare("SELECT COUNT(*) as count FROM sponsorTimes WHERE userID = ? and videoID = ?").get([userID, videoID]);
let duplicateCheckRow = 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
@@ -211,7 +211,7 @@ module.exports = async function postSkipSegments(req, res) {
}
//check to see if this user is shadowbanned
let shadowBanRow = privateDB.prepare("SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?").get(userID);
let shadowBanRow = privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?", [userID]);
let shadowBanned = shadowBanRow.userCount;
@@ -234,13 +234,13 @@ module.exports = async function postSkipSegments(req, res) {
segmentInfo.segment[1] + segmentInfo.category + userID, 1);
try {
db.prepare("INSERT INTO sponsorTimes " +
db.prepare('run', "INSERT INTO sponsorTimes " +
"(videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden)" +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").run(videoID, segmentInfo.segment[0],
segmentInfo.segment[1], startingVotes, UUID, userID, timeSubmitted, 0, segmentInfo.category, shadowBanned);
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [videoID, segmentInfo.segment[0],
segmentInfo.segment[1], startingVotes, UUID, userID, timeSubmitted, 0, segmentInfo.category, shadowBanned]);
//add to private db as well
privateDB.prepare("INSERT INTO sponsorTimes VALUES(?, ?, ?)").run(videoID, hashedIP, timeSubmitted);
privateDB.prepare('run', "INSERT INTO sponsorTimes VALUES(?, ?, ?)", [videoID, hashedIP, timeSubmitted]);
} catch (err) {
//a DB change probably occurred
res.sendStatus(502);

View File

@@ -33,14 +33,14 @@ module.exports = function setUsername(req, res) {
try {
//check if username is already set
let row = db.prepare("SELECT count(*) as count FROM userNames WHERE userID = ?").get(userID);
let row = db.prepare('get', "SELECT count(*) as count FROM userNames WHERE userID = ?", [userID]);
if (row.count > 0) {
//already exists, update this row
db.prepare("UPDATE userNames SET userName = ? WHERE userID = ?").run(userName, userID);
db.prepare('run', "UPDATE userNames SET userName = ? WHERE userID = ?", [userName, userID]);
} else {
//add to the db
db.prepare("INSERT INTO userNames VALUES(?, ?)").run(userID, userName);
db.prepare('run', "INSERT INTO userNames VALUES(?, ?)", [userID, userName]);
}
res.sendStatus(200);

View File

@@ -41,23 +41,23 @@ module.exports = async function shadowBanUser(req, res) {
}
//check to see if this user is already shadowbanned
let row = privateDB.prepare("SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?").get(userID);
let row = 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("INSERT INTO shadowBannedUsers VALUES(?)").run(userID);
privateDB.prepare('run', "INSERT INTO shadowBannedUsers VALUES(?)", [userID]);
//find all previous submissions and hide them
db.prepare("UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?").run(userID);
db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?", [userID]);
} else if (!enabled && row.userCount > 0) {
//remove them from the shadow ban list
privateDB.prepare("DELETE FROM shadowBannedUsers WHERE userID = ?").run(userID);
privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]);
//find all previous submissions and unhide them
if (unHideOldSubmissions) {
db.prepare("UPDATE sponsorTimes SET shadowHidden = 0 WHERE userID = ?").run(userID);
db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE userID = ?", [userID]);
}
}

View File

@@ -10,7 +10,7 @@ module.exports = function viewedVideoSponsorTime(req, res) {
}
//up the view count by one
db.prepare("UPDATE sponsorTimes SET views = views + 1 WHERE UUID = ?").run(UUID);
db.prepare('run', "UPDATE sponsorTimes SET views = views + 1 WHERE UUID = ?", [UUID]);
res.sendStatus(200);
}

View File

@@ -14,7 +14,7 @@ var request = require('request');
function categoryVote(UUID, userID, isVIP, category, hashedIP, res) {
// Check if they've already made a vote
let previousVoteInfo = privateDB.prepare("select count(*) as votes, category from categoryVotes where UUID = ? and userID = ?").get(UUID, userID);
let previousVoteInfo = privateDB.prepare('get', "select count(*) as votes, category from categoryVotes where UUID = ? and userID = ?", [UUID, userID]);
if (previousVoteInfo > 0 && previousVoteInfo.category === category) {
// Double vote, ignore
@@ -22,7 +22,7 @@ function categoryVote(UUID, userID, isVIP, category, hashedIP, res) {
return;
}
let currentCategory = db.prepare("select category from sponsorTimes where UUID = ?").get(UUID);
let currentCategory = db.prepare('get', "select category from sponsorTimes where UUID = ?", [UUID]);
if (!currentCategory) {
// Submission doesn't exist
res.status("400").send("Submission doesn't exist.");
@@ -34,26 +34,26 @@ function categoryVote(UUID, userID, isVIP, category, hashedIP, res) {
let voteAmount = isVIP ? 500 : 1;
// Add the vote
if (db.prepare("select count(*) as count from categoryVotes where UUID = ? and category = ?").get(UUID, category).count > 0) {
if (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("update categoryVotes set votes = votes + ? where UUID = ? and category = ?").run(voteAmount, UUID, category);
db.prepare('run', "update categoryVotes set votes = votes + ? where UUID = ? and category = ?", [voteAmount, UUID, category]);
} else {
// Add a db entry
db.prepare("insert into categoryVotes (UUID, category, votes) values (?, ?, ?)").run(UUID, category, voteAmount);
db.prepare('run', "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, category, voteAmount]);
}
// Add the info into the private db
if (previousVoteInfo > 0) {
// Reverse the previous vote
db.prepare("update categoryVotes set votes -= 1 where UUID = ? and category = ?").run(UUID, previousVoteInfo.category);
db.prepare('run', "update categoryVotes set votes -= 1 where UUID = ? and category = ?", [UUID, previousVoteInfo.category]);
privateDB.prepare("update categoryVotes set category = ?, timeSubmitted = ?, hashedIP = ?").run(category, timeSubmitted, hashedIP)
privateDB.prepare('run', "update categoryVotes set category = ?, timeSubmitted = ?, hashedIP = ?", [category, timeSubmitted, hashedIP]);
} else {
privateDB.prepare("insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)").run(UUID, userID, hashedIP, category, timeSubmitted);
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
let currentCategoryInfo = db.prepare("select votes from categoryVotes where UUID = ? and category = ?").get(UUID, currentCategory.category);
let currentCategoryInfo = db.prepare('get', "select votes from categoryVotes where UUID = ? and category = ?", [UUID, currentCategory.category]);
// Change this value from 1 in the future to make it harder to change categories
// Done this way without ORs incase the value is zero
@@ -65,7 +65,7 @@ function categoryVote(UUID, userID, isVIP, category, hashedIP, res) {
// VIPs change it every time
if (nextCategoryCount - currentCategoryCount >= 0 || isVIP) {
// Replace the category
db.prepare("update sponsorTimes set category = ? where UUID = ?").run(category, UUID);
db.prepare('run', "update sponsorTimes set category = ? where UUID = ?", [category, UUID]);
}
res.sendStatus(200);
@@ -94,7 +94,7 @@ module.exports = async function voteOnSponsorTime(req, res) {
let hashedIP = getHash(ip + config.globalSalt);
//check if this user is on the vip list
let isVIP = db.prepare("SELECT count(*) as userCount FROM vipUsers WHERE userID = ?").get(nonAnonUserID).userCount > 0;
let isVIP = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [nonAnonUserID]).userCount > 0;
if (type === undefined && category !== undefined) {
return categoryVote(UUID, userID, isVIP, category, hashedIP, res);
@@ -102,7 +102,7 @@ module.exports = async function voteOnSponsorTime(req, res) {
if (type == 1 && !isVIP) {
// Check if upvoting hidden segment
let voteInfo = db.prepare("SELECT votes FROM sponsorTimes WHERE UUID = ?").get(UUID);
let voteInfo = 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.")
@@ -119,7 +119,7 @@ module.exports = async function voteOnSponsorTime(req, res) {
try {
//check if vote has already happened
let votesRow = privateDB.prepare("SELECT type FROM votes WHERE userID = ? AND UUID = ?").get(userID, UUID);
let votesRow = 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;
@@ -159,7 +159,7 @@ module.exports = async function voteOnSponsorTime(req, res) {
}
//check if the increment amount should be multiplied (downvotes have more power if there have been many views)
let row = db.prepare("SELECT votes, views FROM sponsorTimes WHERE UUID = ?").get(UUID);
let row = db.prepare('get', "SELECT votes, views FROM sponsorTimes WHERE UUID = ?", [UUID]);
if (voteTypeEnum === voteTypes.normal) {
if (isVIP && incrementAmount < 0) {
@@ -178,13 +178,13 @@ module.exports = async function voteOnSponsorTime(req, res) {
// Send discord message
if (incrementAmount < 0) {
// Get video ID
let submissionInfoRow = db.prepare("SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " +
let submissionInfoRow = 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=?"
).get(UUID);
"FROM sponsorTimes s left join userNames u on s.userID = u.userID where s.UUID=?",
[UUID]);
let userSubmissionCountRow = db.prepare("SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?").get(nonAnonUserID);
let userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [nonAnonUserID]);
if (submissionInfoRow !== undefined && userSubmissionCountRow != undefined) {
let webhookURL = null;
@@ -245,9 +245,9 @@ module.exports = async function voteOnSponsorTime(req, res) {
//update the votes table
if (votesRow != undefined) {
privateDB.prepare("UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?").run(type, userID, UUID);
privateDB.prepare('run', "UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?", [type, userID, UUID]);
} else {
privateDB.prepare("INSERT INTO votes VALUES(?, ?, ?, ?)").run(UUID, userID, hashedIP, type);
privateDB.prepare('run', "INSERT INTO votes VALUES(?, ?, ?, ?)", [UUID, userID, hashedIP, type]);
}
let columnName = "";
@@ -259,12 +259,12 @@ module.exports = async function voteOnSponsorTime(req, res) {
//update the vote count on this sponsorTime
//oldIncrementAmount will be zero is row is null
db.prepare("UPDATE sponsorTimes SET " + columnName + " = " + columnName + " + ? WHERE UUID = ?").run(incrementAmount - oldIncrementAmount, UUID);
db.prepare('run', "UPDATE sponsorTimes SET " + columnName + " = " + columnName + " + ? WHERE UUID = ?", [incrementAmount - oldIncrementAmount, UUID]);
//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
let submissionUserIDInfo = db.prepare("SELECT userID FROM sponsorTimes WHERE UUID = ?").get(UUID);
let submissionUserIDInfo = 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");
@@ -274,14 +274,14 @@ module.exports = async function voteOnSponsorTime(req, res) {
let submissionUserID = submissionUserIDInfo.userID;
//check if any submissions are hidden
let hiddenSubmissionsRow = db.prepare("SELECT count(*) as hiddenSubmissions FROM sponsorTimes WHERE userID = ? AND shadowHidden > 0").get(submissionUserID);
let hiddenSubmissionsRow = 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("UPDATE sponsorTimes SET shadowHidden = 0 WHERE ROWID IN (SELECT ROWID FROM sponsorTimes WHERE userID = ? AND shadowHidden = 1 LIMIT 2)").run(submissionUserID)
db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE ROWID IN (SELECT ROWID FROM sponsorTimes WHERE userID = ? AND shadowHidden = 1 LIMIT 2)", [submissionUserID]);
}
}
}

View File

@@ -6,11 +6,11 @@ var db = databases.db;
module.exports = async (userID) => {
//check to see if this user how many submissions this user has submitted
let totalSubmissionsRow = db.prepare("SELECT count(*) as totalSubmissions, sum(votes) as voteSum FROM sponsorTimes WHERE userID = ?").get(userID);
let totalSubmissionsRow = 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
let downvotedSubmissionsRow = db.prepare("SELECT count(*) as downvotedSubmissions FROM sponsorTimes WHERE userID = ? AND (votes < 0 OR shadowHidden > 0)").get(userID);
let downvotedSubmissionsRow = 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);

View File

@@ -13,7 +13,7 @@ describe('postVideoSponsorTime (Old submission method)', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?").get("dQw4w9WgXcQ");
let row = db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcQ"]);
if (row.startTime === 1 && row.endTime === 10 && row.category === "sponsor") {
done()
} else {
@@ -31,7 +31,7 @@ describe('postVideoSponsorTime (Old submission method)', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?").get("dQw4w9WgXcE");
let row = db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcE"]);
if (row.startTime === 1 && row.endTime === 11 && row.category === "sponsor") {
done()
} else {

View File

@@ -13,7 +13,7 @@ describe('postSkipSegments', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?").get("dQw4w9WgXcR");
let row = db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]);
if (row.startTime === 2 && row.endTime === 10 && row.category === "sponsor") {
done()
} else {
@@ -40,7 +40,7 @@ describe('postSkipSegments', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?").get("dQw4w9WgXcF");
let row = db.prepare('get', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcF"]);
if (row.startTime === 0 && row.endTime === 10 && row.category === "sponsor") {
done()
} else {
@@ -70,7 +70,7 @@ describe('postSkipSegments', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let rows = db.prepare("SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?").all("dQw4w9WgXcR");
let rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ?", ["dQw4w9WgXcR"]);
let success = true;
if (rows.length === 2) {
for (const row of rows) {

View File

@@ -24,7 +24,7 @@ describe('voteOnSponsorTime', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT votes FROM sponsorTimes WHERE UUID = ?").get("vote-uuid-0");
let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-0"]);
if (row.votes === 3) {
done()
} else {
@@ -42,7 +42,7 @@ describe('voteOnSponsorTime', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT votes FROM sponsorTimes WHERE UUID = ?").get("vote-uuid-2");
let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-2"]);
if (row.votes < 10) {
done()
} else {
@@ -60,7 +60,7 @@ describe('voteOnSponsorTime', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT votes FROM sponsorTimes WHERE UUID = ?").get("vote-uuid-3");
let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-3"]);
if (row.votes <= -2) {
done()
} else {
@@ -78,7 +78,7 @@ describe('voteOnSponsorTime', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT category FROM sponsorTimes WHERE UUID = ?").get("vote-uuid-4");
let row = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]);
if (row.category === "intro") {
done()
} else {
@@ -96,7 +96,7 @@ describe('voteOnSponsorTime', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT category FROM sponsorTimes WHERE UUID = ?").get("vote-uuid-4");
let row = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-4"]);
if (row.category === "outro") {
done()
} else {
@@ -114,8 +114,8 @@ describe('voteOnSponsorTime', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT category FROM sponsorTimes WHERE UUID = ?").get("vote-uuid-5");
let row2 = db.prepare("SELECT votes FROM categoryVotes WHERE UUID = ? and category = ?").get("vote-uuid-5", "outro");
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"]);
if (row.category === "outro" && row2.votes === 500) {
done()
} else {
@@ -159,7 +159,7 @@ describe('voteOnSponsorTime', () => {
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 200) {
let row = db.prepare("SELECT votes FROM sponsorTimes WHERE UUID = ?").get("vote-uuid-5");
let row = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]);
if (row.votes > -3) {
done()
} else {