Limiting on how much you can shuffle

This commit is contained in:
Kasper Rynning-Tønnesen
2018-03-01 15:20:26 +01:00
parent adc1b2bf31
commit 7aa4b2d22a
4 changed files with 134 additions and 67 deletions

View File

@@ -8,6 +8,7 @@ try {
} }
var mongojs = require('mongojs'); var mongojs = require('mongojs');
var db = mongojs('mongodb://' + mongo_config.host + '/' + mongo_config.config); 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("chat_logs").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 600 });
db.collection("timeout_api").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 5 }); db.collection("timeout_api").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 5 });

View File

@@ -242,40 +242,63 @@ function shuffle(msg, coll, guid, offline, socket) {
return; return;
} }
Functions.check_inlist(coll, guid, socket, offline); db.collection("timeout_api").find({
var hash; type: "shuffle",
if(msg.adminpass === "") hash = msg.adminpass; guid: guid,
else hash = Functions.hash_pass(Functions.decrypt_string(socketid, msg.adminpass)); }, function(err, docs) {
db.collection(coll + "_settings").find(function(err, docs){ if(docs.length > 0) {
if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == Functions.decrypt_string(socketid, msg.pass)))) { var date = new Date(docs[0].createdAt);
if(docs !== null && docs.length !== 0 && ((docs[0].adminpass == hash || docs[0].adminpass === "") || docs[0].shuffle === false)) date.setSeconds(date.getSeconds() + 5);
{ var now = new Date();
db.collection(coll).find({now_playing:false}).forEach(function(err, docs){ var retry_in = (date.getTime() - now.getTime()) / 1000;
if(!docs){ if(retry_in > 0) {
List.send_list(coll, undefined, false, true, false, true); socket.emit("toast", "wait_longer");
socket.emit("toast", "shuffled"); return;
}
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 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 }else
socket.emit("toast", "wrongpass"); socket.emit("toast", "wrongpass");
} }

View File

@@ -10,6 +10,9 @@ var Admin = {
case "faulty_start_end": case "faulty_start_end":
msg = "You tried to send a faulty start/end value. Try again.."; msg = "You tried to send a faulty start/end value. Try again..";
break; 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": case "suggested_description":
msg = "The description has been suggested!"; msg = "The description has been suggested!";
break; break;

View File

@@ -1,6 +1,8 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var path = require('path'); var path = require('path');
var mongojs = require('mongojs');
var ObjectId = mongojs.ObjectId;
router.use(function(req, res, next) { router.use(function(req, res, next) {
next(); // make sure we go to the next routes and don't stop here 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; return;
} }
validateLogin(adminpass, userpass, channel_name, "delete", res, function(exists) { db.collection("timeout_api").find({
if(!exists) { type: "DELETE",
res.sendStatus(404); guid: guid,
return; }, 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){ validateLogin(adminpass, userpass, channel_name, "delete", res, function(exists) {
if(docs.length == 0) { if(!exists) {
res.sendStatus(404); res.sendStatus(404);
return; return;
} }
dont_increment = true; db.collection(channel_name).find({id:video_id, now_playing: false}, function(err, docs){
if(docs[0]){ if(docs.length == 0) {
if(docs[0].type == "suggested"){ res.sendStatus(404);
dont_increment = false; return;
} }
db.collection(channel_name).remove({id:video_id}, function(err, docs){ dont_increment = true;
io.to(channel_name).emit("channel", {type:"deleted", value: video_id}); if(docs[0]){
if(dont_increment) { if(docs[0].type == "suggested"){
db.collection("frontpage_lists").update({_id: channel_name, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){ dont_increment = false;
res.sendStatus(200);
return;
});
} else {
res.sendStatus(200);
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){
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; return;
} }
db.collection("timeout_api").find({ db.collection("timeout_api").find({
type: "DELETE", type: "CONFIG",
guid: guid, guid: guid,
}, function(err, docs) { }, function(err, docs) {
if(docs.length > 0) { if(docs.length > 0) {
@@ -156,9 +190,11 @@ router.route('/api/conf/:channel_name').put(function(req, res) {
date.setSeconds(date.getSeconds() + 5); date.setSeconds(date.getSeconds() + 5);
var now = new Date(); var now = new Date();
var retry_in = (date.getTime() - now.getTime()) / 1000; var retry_in = (date.getTime() - now.getTime()) / 1000;
res.header({'Retry-After': retry_in}); if(retry_in > 0) {
res.sendStatus(429); res.header({'Retry-After': retry_in});
return; res.sendStatus(429);
return;
}
} }
validateLogin(adminpass, userpass, channel_name, "config", res, function(exists) { validateLogin(adminpass, userpass, channel_name, "config", res, function(exists) {
if(!exists) { if(!exists) {
@@ -203,10 +239,10 @@ router.route('/api/conf/:channel_name').put(function(req, res) {
frontpage:frontpage, accessed: Functions.get_time()} frontpage:frontpage, accessed: Functions.get_time()}
}, },
{upsert:true}, function(err, docs){ {upsert:true}, function(err, docs){
db.collection("timeout_api").update({type: "DELETE", guid: guid}, { db.collection("timeout_api").update({type: "CONFIG", guid: guid}, {
$set: { $set: {
"createdAt": new Date(), "createdAt": new Date(),
type: "DELETE", type: "CONFIG",
guid: guid, guid: guid,
}, },
}, {upsert: true}, function(err, docs) { }, {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) { router.route('/api/list/:channel_name/:video_id').put(function(req,res) {
res.header("Access-Control-Allow-Origin", "*"); 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); date.setSeconds(date.getSeconds() + 5);
var now = new Date(); var now = new Date();
var retry_in = (date.getTime() - now.getTime()) / 1000; var retry_in = (date.getTime() - now.getTime()) / 1000;
res.header({'Retry-After': retry_in}); if(retry_in > 0) {
res.sendStatus(429); res.header({'Retry-After': retry_in});
return; res.sendStatus(429);
return;
}
} }
validateLogin(adminpass, userpass, channel_name, "vote", res, function(exists) { 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); date.setSeconds(date.getSeconds() + 5);
var now = new Date(); var now = new Date();
var retry_in = (date.getTime() - now.getTime()) / 1000; var retry_in = (date.getTime() - now.getTime()) / 1000;
res.header({'Retry-After': retry_in}); if(retry_in > 0) {
res.sendStatus(429); res.header({'Retry-After': retry_in});
return; res.sendStatus(429);
return;
}
} }
validateLogin(adminpass, userpass, channel_name, "add", res, function(exists) { validateLogin(adminpass, userpass, channel_name, "add", res, function(exists) {