From 3c9380c2a26df0b9990a4fbae0ea081ecf1e1e71 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Tue, 7 Apr 2020 01:54:19 -0400 Subject: [PATCH] Added new segment getting endpoint --- src/app.js | 13 +- .../{getSkipSegment.js => getSkipSegments.js} | 136 ++++++++++-------- 2 files changed, 85 insertions(+), 64 deletions(-) rename src/routes/{getSkipSegment.js => getSkipSegments.js} (73%) diff --git a/src/app.js b/src/app.js index fa29c38..ab50509 100644 --- a/src/app.js +++ b/src/app.js @@ -8,8 +8,7 @@ var corsMiddleware = require('./middleware/cors.js'); var loggerMiddleware = require('./middleware/logger.js'); // Routes -var getVideoSponsorTimes = require('./routes/getVideoSponsorTimes.js'); -var oldSubmitSponsorTimes = require('./routes/oldSubmitSponsorTimes.js'); +var getSkipSegments = require('./routes/getSkipSegments.js'); var postSkipSegments = require('./routes/postSkipSegments.js'); var voteOnSponsorTime = require('./routes/voteOnSponsorTime.js'); var viewedVideoSponsorTime = require('./routes/viewedVideoSponsorTime.js'); @@ -23,12 +22,19 @@ var getTopUsers = require('./routes/getTopUsers.js'); var getTotalStats = require('./routes/getTotalStats.js'); var getDaysSavedFormatted = require('./routes/getDaysSavedFormatted.js'); +// Old Routes +var getVideoSponsorTimes = require('./routes/getVideoSponsorTimes.js'); +var oldSubmitSponsorTimes = require('./routes/oldSubmitSponsorTimes.js'); + //setup CORS correctly app.use(corsMiddleware); app.use(loggerMiddleware); app.use(express.json()) +// Setup pretty JSON +if (config.mode === "development") app.set('json spaces', 2); + //add the get function app.get('/api/getVideoSponsorTimes', getVideoSponsorTimes); @@ -36,7 +42,8 @@ app.get('/api/getVideoSponsorTimes', getVideoSponsorTimes); app.get('/api/postVideoSponsorTimes', oldSubmitSponsorTimes); app.post('/api/postVideoSponsorTimes', oldSubmitSponsorTimes); -//add the post function +//add the skip segments functions +app.get('/api/skipSegments', getSkipSegments); app.post('/api/skipSegments', postSkipSegments); //voting endpoint diff --git a/src/routes/getSkipSegment.js b/src/routes/getSkipSegments.js similarity index 73% rename from src/routes/getSkipSegment.js rename to src/routes/getSkipSegments.js index 8918349..95f39c7 100644 --- a/src/routes/getSkipSegment.js +++ b/src/routes/getSkipSegments.js @@ -203,71 +203,85 @@ function getVoteOrganisedSponsorTimes(sponsorTimes, votes, UUIDs) { module.exports = function (req, res) { - const videoID = req.body.videoID || req.query.videoID; - // Default to sponsor - // If using params instead of JSON, only one category can be pulled - const categories = req.body.categories || [req.query.category] ["sponsor"]; + const videoID = req.body.videoID || req.query.videoID; + // Default to sponsor + // If using params instead of JSON, only one category can be pulled + const categories = req.body.categories || (req.query.category ? [req.query.category] : ["sponsor"]); - let sponsorTimes = []; - let votes = [] - let UUIDs = []; + /** + * @type {Array<{ + * segment: number[], + * category: string, + * UUID: string + * }> + * } + */ + let segments = []; - let hashedIP = getHash(getIP(req) + config.globalSalt); + let hashedIP = getHash(getIP(req) + config.globalSalt); - try { - let rows = db.prepare("SELECT startTime, endTime, votes, UUID, shadowHidden FROM sponsorTimes WHERE videoID = ? ORDER BY startTime").all(videoID); - - for (let i = 0; i < rows.length; i++) { - //check if votes are above -1 - if (rows[i].votes < -1) { - //too untrustworthy, just ignore it - continue; - } + try { + for (const category of categories) { + let rows = db.prepare("SELECT startTime, endTime, votes, UUID, shadowHidden FROM sponsorTimes WHERE videoID = ? and category = ? ORDER BY startTime") + .all(videoID, category); - //check if shadowHidden - //this means it is hidden to everyone but the original ip that submitted it - if (rows[i].shadowHidden == 1) { - //get the ip - //await the callback - let hashedIPRow = privateDB.prepare("SELECT hashedIP FROM sponsorTimes WHERE videoID = ?").all(videoID); + let sponsorTimes = []; + let votes = [] + let UUIDs = []; + + for (let i = 0; i < rows.length; i++) { + //check if votes are above -1 + if (rows[i].votes < -1) { + //too untrustworthy, just ignore it + continue; + } + + //check if shadowHidden + //this means it is hidden to everyone but the original ip that submitted it + if (rows[i].shadowHidden == 1) { + //get the ip + //await the callback + let hashedIPRow = privateDB.prepare("SELECT hashedIP FROM sponsorTimes WHERE videoID = ?").all(videoID); + + if (!hashedIPRow.some((e) => e.hashedIP === hashedIP)) { + //this isn't their ip, don't send it to them + continue; + } + } + + sponsorTimes.push([rows[i].startTime, rows[i].endTime]); + votes.push(rows[i].votes); + UUIDs.push(rows[i].UUID); + } + + if (sponsorTimes.length == 0) { + res.sendStatus(404); + return; + } + + organisedData = getVoteOrganisedSponsorTimes(sponsorTimes, votes, UUIDs); + sponsorTimes = organisedData.sponsorTimes; + UUIDs = organisedData.UUIDs; + + for (let i = 0; i < sponsorTimes.length; i++) { + segments.push({ + segment: sponsorTimes[i], + category: category, + UUID: UUIDs[i] + }); + } + } + } catch(error) { + console.error(error); + res.send(500); - if (!hashedIPRow.some((e) => e.hashedIP === hashedIP)) { - //this isn't their ip, don't send it to them - continue; - } - } + return; + } - sponsorTimes.push([]); - - let index = sponsorTimes.length - 1; - - sponsorTimes[index][0] = rows[i].startTime; - sponsorTimes[index][1] = rows[i].endTime; - - votes[index] = rows[i].votes; - UUIDs[index] = rows[i].UUID; - } - - if (sponsorTimes.length == 0) { - res.sendStatus(404); - return; - } - - organisedData = getVoteOrganisedSponsorTimes(sponsorTimes, votes, UUIDs); - sponsorTimes = organisedData.sponsorTimes; - UUIDs = organisedData.UUIDs; - - if (sponsorTimes.length == 0) { - res.sendStatus(404); - } else { - //send result - res.send({ - sponsorTimes: sponsorTimes, - UUIDs: UUIDs - }) - } - } catch(error) { - console.error(error); - res.send(500); - } + if (segments.length == 0) { + res.sendStatus(404); + } else { + //send result + res.send(segments) + } } \ No newline at end of file