diff --git a/server/handlers/db.js b/server/handlers/db.js index 04bd3a63..17154969 100644 --- a/server/handlers/db.js +++ b/server/handlers/db.js @@ -8,6 +8,7 @@ try { } var mongojs = require('mongojs'); var db = mongojs('mongodb://' + mongo_config.host + '/' + mongo_config.config); +var ObjectId = mongojs.ObjectId; db.collection("chat_logs").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 600 }); db.collection("timeout_api").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 5 }); diff --git a/server/handlers/list_change.js b/server/handlers/list_change.js index 005a8a5a..58977aa9 100644 --- a/server/handlers/list_change.js +++ b/server/handlers/list_change.js @@ -242,40 +242,63 @@ function shuffle(msg, coll, guid, offline, socket) { return; } - Functions.check_inlist(coll, guid, socket, offline); - var hash; - if(msg.adminpass === "") hash = msg.adminpass; - else hash = Functions.hash_pass(Functions.decrypt_string(socketid, msg.adminpass)); - db.collection(coll + "_settings").find(function(err, docs){ - if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == Functions.decrypt_string(socketid, msg.pass)))) { - if(docs !== null && docs.length !== 0 && ((docs[0].adminpass == hash || docs[0].adminpass === "") || docs[0].shuffle === false)) - { - db.collection(coll).find({now_playing:false}).forEach(function(err, docs){ - if(!docs){ - List.send_list(coll, undefined, false, true, false, true); - socket.emit("toast", "shuffled"); - - return; - }else{ - num = Math.floor(Math.random()*1000000); - db.collection(coll).update({id:docs.id}, {$set:{added:num}}); - } - }); - }else - socket.emit("toast", "wrongpass"); - } else { - socket.emit("auth_required"); + db.collection("timeout_api").find({ + type: "shuffle", + guid: guid, + }, function(err, docs) { + if(docs.length > 0) { + var date = new Date(docs[0].createdAt); + date.setSeconds(date.getSeconds() + 5); + var now = new Date(); + var retry_in = (date.getTime() - now.getTime()) / 1000; + if(retry_in > 0) { + socket.emit("toast", "wait_longer"); + return; + } } + var now_date = new Date(); + db.collection("timeout_api").update({type: "shuffle", guid: guid}, { + $set: { + "createdAt": now_date, + type: "shuffle", + guid: guid, + }, + }, {upsert: true}, function(err, docs) { + Functions.check_inlist(coll, guid, socket, offline); + var hash; + if(msg.adminpass === "") hash = msg.adminpass; + else hash = Functions.hash_pass(Functions.decrypt_string(socketid, msg.adminpass)); + db.collection(coll + "_settings").find(function(err, docs){ + if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == Functions.decrypt_string(socketid, msg.pass)))) { + if(docs !== null && docs.length !== 0 && ((docs[0].adminpass == hash || docs[0].adminpass === "") || docs[0].shuffle === false)) + { + db.collection(coll).find({now_playing:false}).forEach(function(err, docs){ + if(!docs){ + List.send_list(coll, undefined, false, true, false, true); + socket.emit("toast", "shuffled"); + + return; + }else{ + num = Math.floor(Math.random()*1000000); + db.collection(coll).update({id:docs.id}, {$set:{added:num}}); + } + }); + }else + socket.emit("toast", "wrongpass"); + } else { + socket.emit("auth_required"); + } + }); + + var complete = function(tot, curr){ + if(tot == curr) + { + List.send_list(coll, undefined, false, true, false); + List.getNextSong(coll); + } + }; + }); }); - - var complete = function(tot, curr){ - if(tot == curr) - { - List.send_list(coll, undefined, false, true, false); - List.getNextSong(coll); - } - }; - }else socket.emit("toast", "wrongpass"); } diff --git a/server/public/assets/js/admin.js b/server/public/assets/js/admin.js index bf662f50..244c074a 100755 --- a/server/public/assets/js/admin.js +++ b/server/public/assets/js/admin.js @@ -10,6 +10,9 @@ var Admin = { case "faulty_start_end": msg = "You tried to send a faulty start/end value. Try again.."; break; + case "wait_longer": + msg = Helper.rnd(["Have you tried to wait longer between commands?!", "Looks like you're clicking that button too much..", "You need to wait longer between clicks.."]); + break; case "suggested_description": msg = "The description has been suggested!"; break; diff --git a/server/routing/client/api.js b/server/routing/client/api.js index a3310f9f..f7bb11d2 100644 --- a/server/routing/client/api.js +++ b/server/routing/client/api.js @@ -1,6 +1,8 @@ var express = require('express'); var router = express.Router(); var path = require('path'); +var mongojs = require('mongojs'); +var ObjectId = mongojs.ObjectId; router.use(function(req, res, next) { next(); // make sure we go to the next routes and don't stop here @@ -43,34 +45,66 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) { return; } - validateLogin(adminpass, userpass, channel_name, "delete", res, function(exists) { - if(!exists) { - res.sendStatus(404); - return; + db.collection("timeout_api").find({ + type: "DELETE", + guid: guid, + }, function(err, docs) { + if(docs.length > 0) { + var date = new Date(docs[0].createdAt); + date.setSeconds(date.getSeconds() + 5); + var now = new Date(); + var retry_in = (date.getTime() - now.getTime()) / 1000; + if(retry_in > 0) { + res.header({'Retry-After': retry_in}); + res.sendStatus(429); + return; + } } - db.collection(channel_name).find({id:video_id, now_playing: false}, function(err, docs){ - if(docs.length == 0) { + validateLogin(adminpass, userpass, channel_name, "delete", res, function(exists) { + if(!exists) { res.sendStatus(404); return; } - dont_increment = true; - if(docs[0]){ - if(docs[0].type == "suggested"){ - dont_increment = false; + db.collection(channel_name).find({id:video_id, now_playing: false}, function(err, docs){ + if(docs.length == 0) { + res.sendStatus(404); + return; } - db.collection(channel_name).remove({id:video_id}, function(err, docs){ - io.to(channel_name).emit("channel", {type:"deleted", value: video_id}); - if(dont_increment) { - db.collection("frontpage_lists").update({_id: channel_name, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){ - res.sendStatus(200); - return; - }); - } else { - res.sendStatus(200); - return; + dont_increment = true; + if(docs[0]){ + if(docs[0].type == "suggested"){ + dont_increment = false; } - }); - } + db.collection(channel_name).remove({id:video_id}, function(err, docs){ + io.to(channel_name).emit("channel", {type:"deleted", value: video_id}); + if(dont_increment) { + db.collection("frontpage_lists").update({_id: channel_name, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){ + db.collection("timeout_api").update({type: "DELETE", guid: guid}, { + $set: { + "createdAt": new Date(), + type: "DELETE", + guid: guid, + }, + }, {upsert: true}, function(err, docs) { + res.sendStatus(200); + return; + }); + }); + } else { + db.collection("timeout_api").update({type: "DELETE", guid: guid}, { + $set: { + "createdAt": new Date(), + type: "DELETE", + guid: guid, + }, + }, {upsert: true}, function(err, docs) { + res.sendStatus(200); + return; + }); + } + }); + } + }); }); }); }); @@ -148,7 +182,7 @@ router.route('/api/conf/:channel_name').put(function(req, res) { return; } db.collection("timeout_api").find({ - type: "DELETE", + type: "CONFIG", guid: guid, }, function(err, docs) { if(docs.length > 0) { @@ -156,9 +190,11 @@ router.route('/api/conf/:channel_name').put(function(req, res) { date.setSeconds(date.getSeconds() + 5); var now = new Date(); var retry_in = (date.getTime() - now.getTime()) / 1000; - res.header({'Retry-After': retry_in}); - res.sendStatus(429); - return; + if(retry_in > 0) { + res.header({'Retry-After': retry_in}); + res.sendStatus(429); + return; + } } validateLogin(adminpass, userpass, channel_name, "config", res, function(exists) { if(!exists) { @@ -203,10 +239,10 @@ router.route('/api/conf/:channel_name').put(function(req, res) { frontpage:frontpage, accessed: Functions.get_time()} }, {upsert:true}, function(err, docs){ - db.collection("timeout_api").update({type: "DELETE", guid: guid}, { + db.collection("timeout_api").update({type: "CONFIG", guid: guid}, { $set: { "createdAt": new Date(), - type: "DELETE", + type: "CONFIG", guid: guid, }, }, {upsert: true}, function(err, docs) { @@ -217,7 +253,7 @@ router.route('/api/conf/:channel_name').put(function(req, res) { }); }); }); -}) +}); router.route('/api/list/:channel_name/:video_id').put(function(req,res) { res.header("Access-Control-Allow-Origin", "*"); @@ -254,9 +290,11 @@ router.route('/api/list/:channel_name/:video_id').put(function(req,res) { date.setSeconds(date.getSeconds() + 5); var now = new Date(); var retry_in = (date.getTime() - now.getTime()) / 1000; - res.header({'Retry-After': retry_in}); - res.sendStatus(429); - return; + if(retry_in > 0) { + res.header({'Retry-After': retry_in}); + res.sendStatus(429); + return; + } } validateLogin(adminpass, userpass, channel_name, "vote", res, function(exists) { @@ -338,9 +376,11 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { date.setSeconds(date.getSeconds() + 5); var now = new Date(); var retry_in = (date.getTime() - now.getTime()) / 1000; - res.header({'Retry-After': retry_in}); - res.sendStatus(429); - return; + if(retry_in > 0) { + res.header({'Retry-After': retry_in}); + res.sendStatus(429); + return; + } } validateLogin(adminpass, userpass, channel_name, "add", res, function(exists) {