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,6 +242,28 @@ function shuffle(msg, coll, guid, offline, socket) {
return; return;
} }
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); Functions.check_inlist(coll, guid, socket, offline);
var hash; var hash;
if(msg.adminpass === "") hash = msg.adminpass; if(msg.adminpass === "") hash = msg.adminpass;
@@ -275,7 +297,8 @@ function shuffle(msg, coll, guid, offline, socket) {
List.getNextSong(coll); 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,6 +45,21 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) {
return; 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;
}
}
validateLogin(adminpass, userpass, channel_name, "delete", res, function(exists) { validateLogin(adminpass, userpass, channel_name, "delete", res, function(exists) {
if(!exists) { if(!exists) {
res.sendStatus(404); res.sendStatus(404);
@@ -62,18 +79,35 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) {
io.to(channel_name).emit("channel", {type:"deleted", value: video_id}); io.to(channel_name).emit("channel", {type:"deleted", value: video_id});
if(dont_increment) { 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("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); res.sendStatus(200);
return; return;
}); });
});
} else { } 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); res.sendStatus(200);
return; return;
});
} }
}); });
} }
}); });
}); });
}); });
});
function cleanChannelName(channel_name) { function cleanChannelName(channel_name) {
var coll = emojiStrip(channel_name).toLowerCase(); var coll = emojiStrip(channel_name).toLowerCase();
@@ -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,10 +190,12 @@ 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;
if(retry_in > 0) {
res.header({'Retry-After': retry_in}); res.header({'Retry-After': retry_in});
res.sendStatus(429); res.sendStatus(429);
return; return;
} }
}
validateLogin(adminpass, userpass, channel_name, "config", res, function(exists) { validateLogin(adminpass, userpass, channel_name, "config", res, function(exists) {
if(!exists) { if(!exists) {
res.sendStatus(404); res.sendStatus(404);
@@ -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,10 +290,12 @@ 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;
if(retry_in > 0) {
res.header({'Retry-After': retry_in}); res.header({'Retry-After': retry_in});
res.sendStatus(429); res.sendStatus(429);
return; return;
} }
}
validateLogin(adminpass, userpass, channel_name, "vote", res, function(exists) { validateLogin(adminpass, userpass, channel_name, "vote", res, function(exists) {
if(!exists) { if(!exists) {
@@ -338,10 +376,12 @@ 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;
if(retry_in > 0) {
res.header({'Retry-After': retry_in}); res.header({'Retry-After': retry_in});
res.sendStatus(429); res.sendStatus(429);
return; return;
} }
}
validateLogin(adminpass, userpass, channel_name, "add", res, function(exists) { validateLogin(adminpass, userpass, channel_name, "add", res, function(exists) {
db.collection(channel_name).find({id: video_id}, function(err, result) { db.collection(channel_name).find({id: video_id}, function(err, result) {