From bcf90e8094f8933637168392545a57f8b55718a8 Mon Sep 17 00:00:00 2001 From: James Robinson Date: Wed, 14 Oct 2020 00:43:29 -0500 Subject: [PATCH] fix formatting and add more comments --- src/routes/postSkipSegments.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/routes/postSkipSegments.js b/src/routes/postSkipSegments.js index 27bfdde..092b7f8 100644 --- a/src/routes/postSkipSegments.js +++ b/src/routes/postSkipSegments.js @@ -404,23 +404,28 @@ module.exports = async function postSkipSegments(req, res) { //get all segments for this video and user let allSubmittedByUser = db.prepare('all', "SELECT startTime, endTime FROM sponsorTimes WHERE userID = ? and videoID = ? and votes > -1", [userID, videoID]); let allSegmentTimes = []; - if (allSubmittedByUser !== undefined) - { + if (allSubmittedByUser !== undefined) { + //add segments the user has previously submitted for (const segmentInfo of allSubmittedByUser) { allSegmentTimes.push([parseFloat(segmentInfo.startTime), parseFloat(segmentInfo.endTime)]) } } + + //add segments they are trying to add in this submission for (let i = 0; i < segments.length; i++) { let startTime = parseFloat(segments[i].segment[0]); let endTime = parseFloat(segments[i].segment[1]); allSegmentTimes.push([startTime, endTime]); } + + //merge all the times into non-overlapping arrays const allSegmentsSorted = mergeTimeSegments(allSegmentTimes.sort(function(a, b) { return a[0]-b[0] || a[1]-b[1] })); let videoDuration = data.items[0].contentDetails.duration; videoDuration = isoDurations.toSeconds(isoDurations.parse(videoDuration)); if (videoDuration != 0) { let allSegmentDuration = 0; + //sum all segment times together allSegmentsSorted.forEach(segmentInfo => allSegmentDuration += segmentInfo[1] - segmentInfo[0]); if (allSegmentDuration > (videoDuration/100)*80) { // Reject submission if all segments combine are over 80% of the video