diff --git a/src/routes/postSkipSegments.js b/src/routes/postSkipSegments.js index b361cb8..8ffb9a2 100644 --- a/src/routes/postSkipSegments.js +++ b/src/routes/postSkipSegments.js @@ -50,7 +50,7 @@ function sendWebhooks(userID, videoID, UUID, segmentInfo) { if (config.youtubeAPIKey !== null) { let userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]); - YouTubeAPI.listVideos(videoID, "snippet", (err, data) => { + YouTubeAPI.listVideos(videoID, (err, data) => { if (err || data.items.length === 0) { err && logger.error(err); return; @@ -154,7 +154,7 @@ async function autoModerateSubmission(submission) { // Get the video information from the youtube API if (config.youtubeAPIKey !== null) { let {err, data} = await new Promise((resolve, reject) => { - YouTubeAPI.listVideos(submission.videoID, "contentDetails,snippet", (err, data) => resolve({err, data})); + YouTubeAPI.listVideos(submission.videoID, (err, data) => resolve({err, data})); }); if (err) { @@ -406,6 +406,45 @@ module.exports = async function postSkipSegments(req, res) { startingVotes = 10000; } + if (config.youtubeAPIKey !== null) { + let {err, data} = await new Promise((resolve, reject) => { + YouTubeAPI.listVideos(videoID, (err, data) => resolve({err, data})); + }); + + //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) { + //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 + res.status(400).send("Total length of your submitted segments are over 80% of the video."); + return; + } + } + } + for (const segmentInfo of segments) { //this can just be a hash of the data //it's better than generating an actual UUID like what was used before @@ -447,3 +486,30 @@ module.exports = async function postSkipSegments(req, res) { sendWebhooks(userID, videoID, UUIDs[i], segments[i]); } } + +// Takes an array of arrays: +// ex) +// [ +// [3, 40], +// [50, 70], +// [60, 80], +// [100, 150] +// ] +// => transforms to combining overlapping segments +// [ +// [3, 40], +// [50, 80], +// [100, 150] +// ] +function mergeTimeSegments(ranges) { + var result = [], last; + + ranges.forEach(function (r) { + if (!last || r[0] > last[1]) + result.push(last = r); + else if (r[1] > last[1]) + last[1] = r[1]; + }); + + return result; +} \ No newline at end of file diff --git a/src/routes/voteOnSponsorTime.js b/src/routes/voteOnSponsorTime.js index 10cae90..bfe5e0a 100644 --- a/src/routes/voteOnSponsorTime.js +++ b/src/routes/voteOnSponsorTime.js @@ -49,7 +49,7 @@ function sendWebhooks(voteData) { } if (config.youtubeAPIKey !== null) { - YouTubeAPI.listVideos(submissionInfoRow.videoID, "snippet", (err, data) => { + YouTubeAPI.listVideos(submissionInfoRow.videoID, (err, data) => { if (err || data.items.length === 0) { err && logger.error(err); return; diff --git a/src/utils/youtubeAPI.js b/src/utils/youtubeAPI.js index 1deb3b2..96e718e 100644 --- a/src/utils/youtubeAPI.js +++ b/src/utils/youtubeAPI.js @@ -18,13 +18,14 @@ if (config.mode === "test") { exportObject = YouTubeAPI; // YouTubeAPI.videos.list wrapper with cacheing - exportObject.listVideos = (videoID, part, callback) => { + exportObject.listVideos = (videoID, callback) => { + let part = 'contentDetails,snippet'; if (videoID.length !== 11 || videoID.includes(".")) { callback("Invalid video ID"); return; } - let redisKey = "youtube.video." + videoID + "." + part; + let redisKey = "youtube.video." + videoID; redis.get(redisKey, (getErr, result) => { if (getErr || !result) { logger.debug("redis: no cache for video information: " + videoID); diff --git a/test/cases/postSkipSegments.js b/test/cases/postSkipSegments.js index b8966b1..0a37aaa 100644 --- a/test/cases/postSkipSegments.js +++ b/test/cases/postSkipSegments.js @@ -10,6 +10,12 @@ var db = databases.db; describe('postSkipSegments', () => { before(() => { + let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES"; + + db.exec(startOfQuery + "('80percent_video', 0, 1000, 0, '80percent-uuid-0', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); + db.exec(startOfQuery + "('80percent_video', 1001, 1005, 0, '80percent-uuid-1', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); + db.exec(startOfQuery + "('80percent_video', 0, 5000, -2, '80percent-uuid-2', '" + getHash("test") + "', 0, 0, 'interaction', 0, '80percent_video')"); + const now = Date.now(); const warnVip01Hash = getHash("warn-vip01"); const warnUser01Hash = getHash("warn-user01"); @@ -110,6 +116,139 @@ describe('postSkipSegments', () => { }); }).timeout(5000); + it('Should allow multiple times if total is under 80% of video(JSON method)', (done) => { + request.post(utils.getbaseURL() + + "/api/postVideoSponsorTimes", { + json: { + userID: "test", + videoID: "L_jWHffIx5E", + segments: [{ + segment: [3, 3000], + category: "sponsor" + },{ + segment: [3002, 3050], + category: "intro" + },{ + segment: [45, 100], + category: "interaction" + },{ + segment: [99, 170], + category: "sponsor" + }] + } + }, + (err, res, body) => { + if (err) done(err); + else if (res.statusCode === 200) { + let rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["L_jWHffIx5E"]); + let success = true; + if (rows.length === 4) { + for (const row of rows) { + if ((row.startTime !== 3 || row.endTime !== 3000 || row.category !== "sponsor") && + (row.startTime !== 3002 || row.endTime !== 3050 || row.category !== "intro") && + (row.startTime !== 45 || row.endTime !== 100 || row.category !== "interaction") && + (row.startTime !== 99 || row.endTime !== 170 || row.category !== "sponsor")) { + success = false; + break; + } + } + } + + if (success) done(); + else done("Submitted times were not saved. Actual submissions: " + JSON.stringify(rows)); + } else { + done("Status code was " + res.statusCode); + } + }); + }).timeout(5000); + + it('Should reject multiple times if total is over 80% of video (JSON method)', (done) => { + request.post(utils.getbaseURL() + + "/api/postVideoSponsorTimes", { + json: { + userID: "test", + videoID: "n9rIGdXnSJc", + segments: [{ + segment: [0, 2000], + category: "interaction" + },{ + segment: [3000, 4000], + category: "sponsor" + },{ + segment: [1500, 2750], + category: "sponsor" + },{ + segment: [4050, 4750], + category: "intro" + }] + } + }, + (err, res, body) => { + if (err) done(err); + else if (res.statusCode === 400) { + let rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["n9rIGdXnSJc"]); + let success = true; + if (rows.length === 4) { + for (const row of rows) { + if ((row.startTime === 0 || row.endTime === 2000 || row.category === "interaction") || + (row.startTime === 3000 || row.endTime === 4000 || row.category === "sponsor") || + (row.startTime === 1500 || row.endTime === 2750 || row.category === "sponsor") || + (row.startTime === 4050 || row.endTime === 4750 || row.category === "intro")) { + success = false; + break; + } + } + } + + if (success) done(); + else + done("Submitted times were not saved. Actual submissions: " + JSON.stringify(rows)); + } else { + done("Status code was " + res.statusCode); + } + }); + }).timeout(5000); + + it('Should reject multiple times if total is over 80% of video including previosuly submitted times(JSON method)', (done) => { + request.post(utils.getbaseURL() + + "/api/postVideoSponsorTimes", { + json: { + userID: "test", + videoID: "80percent_video", + segments: [{ + segment: [2000, 4000], + category: "sponsor" + },{ + segment: [1500, 2750], + category: "sponsor" + },{ + segment: [4050, 4750], + category: "sponsor" + }] + } + }, + (err, res, body) => { + if (err) done(err); + else if (res.statusCode === 400) { + let rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["80percent_video"]); + let success = true && rows.length == 2; + for (const row of rows) { + if ((row.startTime === 2000 || row.endTime === 4000 || row.category === "sponsor") || + (row.startTime === 1500 || row.endTime === 2750 || row.category === "sponsor") || + (row.startTime === 4050 || row.endTime === 4750 || row.category === "sponsor")) { + success = false; + break; + } + } + if (success) done(); + else + done("Submitted times were not saved. Actual submissions: " + JSON.stringify(rows)); + } else { + done("Status code was " + res.statusCode); + } + }); + }).timeout(5000); + it('Should be accepted if a non-sponsor is less than 1 second', (done) => { request.post(utils.getbaseURL() + "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30.5&userID=testing&category=intro", null, diff --git a/test/youtubeMock.js b/test/youtubeMock.js index cc722d9..7e15f64 100644 --- a/test/youtubeMock.js +++ b/test/youtubeMock.js @@ -8,9 +8,8 @@ YouTubeAPI.videos.list({ // https://developers.google.com/youtube/v3/docs/videos const YouTubeAPI = { - listVideos: (id, part, callback) => { + listVideos: (id, callback) => { YouTubeAPI.videos.list({ - part: part, id: id }, callback); },