Added function for timing out playlist adds when not logged in

This commit is contained in:
Kasper Rynning-Tønnesen
2018-09-28 12:24:37 +02:00
parent c0abda2e73
commit 5ab5f0f079
2 changed files with 429 additions and 413 deletions

View File

@@ -434,6 +434,46 @@ function left_channel(coll, guid, short_id, in_list, socket, change, caller) {
remove_unique_id(short_id);
}
function checkTimeout(type, timeout, channel, guid, conf_pass, this_pass, socket, callback, error_message){
if(conf_pass != "" && conf_pass == this_pass) {
callback();
return;
}
db.collection("timeout_api").find({
type: type,
guid: guid,
}, function(err, docs) {
if(docs.length > 0) {
var date = new Date(docs[0].createdAt);
date.setSeconds(date.getSeconds() + timeout);
var now = new Date();
var retry_in = (date.getTime() - now.getTime()) / 1000;
if(retry_in > 0) {
if(error_message) {
socket.emit("toast", error_message + Math.ceil(retry_in) + " seconds.");
} else {
socket.emit("toast", "wait_longer");
}
return;
}
}
var now_date = new Date();
db.collection("timeout_api").update({type: type, guid: guid}, {
$set: {
"createdAt": now_date,
type: type,
guid: guid,
},
}, {upsert: true}, function(err, docs) {
callback();
return;
});
});
}
module.exports.checkTimeout = checkTimeout;
module.exports.left_channel = left_channel;
module.exports.setChromecastHost = setChromecastHost;
module.exports.decodeChannelName = decodeChannelName;

View File

@@ -33,11 +33,10 @@ function addFromOtherList(arr, guid, offline, socket) {
var channel = arr.channel;//.replace(/ /g,'').toLowerCase();
var new_channel = Functions.encodeChannelName(arr.new_channel);//.replace(/ /g, '').toLowerCase();
db.collection("frontpage_lists").find({_id: new_channel}, function(err, fp) {
if(fp.length == 0) {
if(fp.length == 0 || channel == new_channel) {
socket.emit("toast", "nolist");
return;
}
Functions.getSessionAdminUser(Functions.getSession(socket), channel, function(userpass, adminpass) {
if(userpass != "" || arr.userpass == undefined) {
arr.userpass = userpass;
@@ -83,6 +82,7 @@ function addFromOtherList(arr, guid, offline, socket) {
db.collection(channel + "_settings").find({id: "config"}, function(e, this_conf) {
var hash = arr.adminpass;
if((this_conf[0].userpass == "" || !this_conf[0].userpass || this_conf[0].userpass == arr.userpass)) {
Functions.checkTimeout("add_playlist", 60, channel, guid, this_conf[0].adminpass, hash, socket, function() {
if(((this_conf[0].addsongs === true && (hash == this_conf[0].adminpass || this_conf[0].adminpass === "")) ||
this_conf[0].addsongs === false)) {
db.collection(new_channel).aggregate([
@@ -142,11 +142,12 @@ function addFromOtherList(arr, guid, offline, socket) {
socket.emit("toast", "listhaspass");
return;
}
}, "Log in to do that, or please wait ");
} else {
socket.emit("auth_required");
return;
}
})
});
} else {
socket.emit("toast", "other_list_pass");
return;
@@ -208,6 +209,7 @@ function addPlaylist(arr, guid, offline, socket) {
if((conf[0].userpass == "" || !conf[0].userpass || conf[0].userpass == arr.userpass)) {
if(((conf[0].addsongs === true && (hash == conf[0].adminpass || conf[0].adminpass === "")) ||
conf[0].addsongs === false)) {
Functions.checkTimeout("add_playlist", 60, channel, guid, conf[0].adminpass, hash, socket, function() {
var path = require('path');
var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js'));
var MongoClient = require('mongodb').MongoClient;
@@ -287,6 +289,7 @@ function addPlaylist(arr, guid, offline, socket) {
});
});
});
}, "Log in to do that, or please wait ");
} else {
socket.emit("toast", "listhaspass");
return;
@@ -310,7 +313,6 @@ function add_function(arr, coll, guid, offline, socket) {
var socketid = socket.zoff_id;
if(typeof(arr) === 'object' && arr !== undefined && arr !== null && arr !== "" && !isNaN(parseInt(arr.duration)))
{
if(coll == "" || coll == undefined || coll == null ||
!arr.hasOwnProperty("start") || !arr.hasOwnProperty("end")) {
var result = {
@@ -393,7 +395,7 @@ function add_function(arr, coll, guid, offline, socket) {
socket.emit('update_required', result);
return;
}
//coll = coll.replace(/ /g,'');
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass) {
if(adminpass != "" || arr.adminpass == undefined) {
arr.adminpass = Functions.hash_pass(adminpass);
@@ -414,8 +416,6 @@ function add_function(arr, coll, guid, offline, socket) {
var hash = arr.adminpass;
var duration = parseInt(arr.duration);
var source = arr.source;
/*db.collection(coll + "_settings").find(function(err, docs)
{*/
conf = docs;
if(docs !== null && docs.length !== 0 && ((docs[0].addsongs === true && (hash == docs[0].adminpass || docs[0].adminpass === "")) ||
docs[0].addsongs === false)) {
@@ -482,8 +482,7 @@ function add_function(arr, coll, guid, offline, socket) {
} else {
suggestedAdd.source = "youtube";
}
db.collection(coll).update({id: id}, {$set: suggestedAdd},
{upsert:true}, function(err, docs){
db.collection(coll).update({id: id}, {$set: suggestedAdd}, {upsert:true}, function(err, docs){
socket.emit("toast", "suggested");
var toSend = {id: id, title: title, duration: duration, source: suggestedAdd.source};
if(source == "soundcloud") toSend.thumbnail = arr.thumbnail;
@@ -497,7 +496,6 @@ function add_function(arr, coll, guid, offline, socket) {
}
});
}
//});
} else {
socket.emit("auth_required");
}
@@ -516,9 +514,9 @@ function add_function(arr, coll, guid, offline, socket) {
};
socket.emit('update_required', result);
}
}
}
function voteUndecided(msg, coll, guid, offline, socket) {
function voteUndecided(msg, coll, guid, offline, socket) {
var socketid = socket.zoff_id;
if(typeof(msg) === 'object' && msg !== undefined && msg !== null){
if(msg.hasOwnProperty("id")) msg.id = msg.id + "";
@@ -596,9 +594,9 @@ function add_function(arr, coll, guid, offline, socket) {
};
socket.emit('update_required', result);
}
}
}
function shuffle(msg, coll, guid, offline, socket) {
function shuffle(msg, coll, guid, offline, socket) {
var socketid = socket.zoff_id;
if(!msg.hasOwnProperty("channel") || typeof(msg.channel) != "string") {
var result = {
@@ -634,31 +632,9 @@ function add_function(arr, coll, guid, offline, socket) {
} else if(msg.hasOwnProperty("pass")) {
msg.pass = crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64");
}
db.collection("timeout_api").find({
type: "shuffle",
guid: coll,
}, 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: coll}, {
$set: {
"createdAt": now_date,
type: "shuffle",
guid: coll,
},
}, {upsert: true}, function(err, docs) {
Functions.checkTimeout("shuffle", 5, coll, coll, "foo", "bar", socket, function() {
Functions.check_inlist(coll, guid, socket, offline, undefined, "place 7");
var hash = 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 == msg.pass))) {
if(docs !== null && docs.length !== 0 && ((docs[0].adminpass == hash && docs[0].adminpass != "") || docs[0].shuffle === false))
@@ -689,11 +665,11 @@ function add_function(arr, coll, guid, offline, socket) {
}
};
});
});
});
}
function del(params, socket, socketid) {
});
}
function del(params, socket, socketid) {
if(params.id){
var coll = Functions.removeEmojis(params.channel).toLowerCase();
//coll = coll.replace(/_/g, "").replace(/ /g,'');
@@ -719,9 +695,9 @@ function add_function(arr, coll, guid, offline, socket) {
}
});
}
}
}
function delete_all(msg, coll, guid, offline, socket) {
function delete_all(msg, coll, guid, offline, socket) {
var socketid = socket.zoff_id;
if(typeof(msg) == 'object' ) {
if(!msg.hasOwnProperty('channel') || typeof(msg.channel) != "string") {
@@ -786,9 +762,9 @@ function add_function(arr, coll, guid, offline, socket) {
socket.emit('update_required', result);
return;
}
}
}
function vote(coll, id, guid, socket) {
function vote(coll, id, guid, socket) {
//coll = coll.replace(/ /g,'');
db.collection(coll).find({id:id, now_playing: false, type:"video"}, function(err, docs){
if(docs !== null && docs.length > 0 && !Functions.contains(docs[0].guids, guid))
@@ -805,13 +781,13 @@ function add_function(arr, coll, guid, offline, socket) {
socket.emit("toast", "alreadyvoted");
}
});
}
}
module.exports.addPlaylist = addPlaylist;
module.exports.addFromOtherList = addFromOtherList;
module.exports.add_function = add_function;
module.exports.voteUndecided = voteUndecided;
module.exports.shuffle = shuffle;
module.exports.del = del;
module.exports.delete_all = delete_all;
module.exports.vote = vote;
module.exports.addPlaylist = addPlaylist;
module.exports.addFromOtherList = addFromOtherList;
module.exports.add_function = add_function;
module.exports.voteUndecided = voteUndecided;
module.exports.shuffle = shuffle;
module.exports.del = del;
module.exports.delete_all = delete_all;
module.exports.vote = vote;