diff --git a/src/middleware/userCounter.js b/src/middleware/userCounter.js index 183e1b4..85be545 100644 --- a/src/middleware/userCounter.js +++ b/src/middleware/userCounter.js @@ -1,11 +1,16 @@ var request = require('request'); -var config = require('../config.js'); -var getIP = require('../utils/getIP.js'); +const config = require('../config.js'); +const getIP = require('../utils/getIP.js'); const getHash = require('../utils/getHash.js'); +const logger = require('../utils/logger.js'); module.exports = function userCounter(req, res, next) { - request.post(config.userCounterURL + "/api/v1/addIP?hashedIP=" + getHash(getIP(req), 1)); + try { + request.post(config.userCounterURL + "/api/v1/addIP?hashedIP=" + getHash(getIP(req), 1)); + } catch(e) { + logger.debug("Failing to connect to user counter at: " + config.userCounterURL); + } next(); } \ No newline at end of file diff --git a/src/routes/getTotalStats.js b/src/routes/getTotalStats.js index d2be079..6d3841e 100644 --- a/src/routes/getTotalStats.js +++ b/src/routes/getTotalStats.js @@ -12,28 +12,30 @@ let apiUsersCache = null; let lastUserCountCheck = 0; module.exports = function getTotalStats (req, res) { - let row = db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " + + 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 AND votes >= 0", []); - if (row !== undefined) { - //send this result - res.send({ - userCount: row.userCount, - activeUsers: chromeUsersCache + firefoxUsersCache, - apiUsers: apiUsersCache, - viewCount: row.viewCount, - totalSubmissions: row.totalSubmissions, - minutesSaved: row.minutesSaved - }); + if (row !== undefined) { + let extensionUsers = chromeUsersCache + firefoxUsersCache; - // Check if the cache should be updated (every ~14 hours) - let now = Date.now(); - if (now - lastUserCountCheck > 5000000) { - lastUserCountCheck = now; + //send this result + res.send({ + userCount: row.userCount, + activeUsers: extensionUsers, + apiUsers: Math.max(apiUsersCache, extensionUsers), + viewCount: row.viewCount, + totalSubmissions: row.totalSubmissions, + minutesSaved: row.minutesSaved + }); - updateExtensionUsers(); - } - } + // Check if the cache should be updated (every ~14 hours) + let now = Date.now(); + if (now - lastUserCountCheck > 5000000) { + lastUserCountCheck = now; + + updateExtensionUsers(); + } + } } function updateExtensionUsers() { diff --git a/src/routes/postSkipSegments.js b/src/routes/postSkipSegments.js index d9408b6..48fbb76 100644 --- a/src/routes/postSkipSegments.js +++ b/src/routes/postSkipSegments.js @@ -63,7 +63,7 @@ function sendWebhooks(userID, videoID, UUID, segmentInfo) { // If it is a first time submission // Then send a notification to discord - if (config.discordFirstTimeSubmissionsWebhookURL === null) return; + if (config.discordFirstTimeSubmissionsWebhookURL === null || userSubmissionCountRow.submissionCount > 1) return; request.post(config.discordFirstTimeSubmissionsWebhookURL, { json: { "embeds": [{ diff --git a/src/routes/shadowBanUser.js b/src/routes/shadowBanUser.js index ba5052e..3617a94 100644 --- a/src/routes/shadowBanUser.js +++ b/src/routes/shadowBanUser.js @@ -1,5 +1,3 @@ -var config = require('../config.js'); - var databases = require('../databases/databases.js'); var db = databases.db; var privateDB = databases.privateDB; @@ -30,7 +28,8 @@ module.exports = async function shadowBanUser(req, res) { //hash the userID adminUserIDInput = getHash(adminUserIDInput); - if (adminUserIDInput !== config.adminUserID) { + let isVIP = db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [adminUserIDInput]).userCount > 0; + if (!isVIP) { //not authorized res.sendStatus(403); return;