Less db-queries for settings

This commit is contained in:
Kasper Rynning-Tønnesen
2018-06-05 15:08:48 +02:00
parent 9a4ffdc57c
commit 48bb88d8e0

View File

@@ -188,47 +188,47 @@ function skip(list, guid, coll, offline, socket) {
else else
hash = ""; hash = "";
db.collection(coll + "_settings").find(function(err, docs){ //db.collection(coll + "_settings").find(function(err, docs){
if(docs !== null && docs.length !== 0) if(docs !== null && docs.length !== 0)
{
if(!docs[0].skip || (docs[0].adminpass == hash && docs[0].adminpass !== "") || error)
{ {
if(!docs[0].skip || (docs[0].adminpass == hash && docs[0].adminpass !== "") || error) db.collection("frontpage_lists").find({"_id": coll}, function(err, frontpage_viewers){
{ if((frontpage_viewers[0].viewers/2 <= docs[0].skips.length+1 && !Functions.contains(docs[0].skips, guid) && frontpage_viewers[0].viewers != 2) ||
db.collection("frontpage_lists").find({"_id": coll}, function(err, frontpage_viewers){ (frontpage_viewers[0].viewers == 2 && docs[0].skips.length+1 == 2 && !Functions.contains(docs[0].skips, guid)) ||
if((frontpage_viewers[0].viewers/2 <= docs[0].skips.length+1 && !Functions.contains(docs[0].skips, guid) && frontpage_viewers[0].viewers != 2) || (docs[0].adminpass == hash && docs[0].adminpass !== "" && docs[0].skip))
(frontpage_viewers[0].viewers == 2 && docs[0].skips.length+1 == 2 && !Functions.contains(docs[0].skips, guid)) || {
(docs[0].adminpass == hash && docs[0].adminpass !== "" && docs[0].skip)) change_song(coll, error, video_id, docs);
{ socket.emit("toast", "skip");
change_song(coll, error, video_id); db.collection("user_names").find({"guid": guid}, function(err, docs) {
socket.emit("toast", "skip"); if(docs.length == 1) {
db.collection("user_names").find({"guid": guid}, function(err, docs) { db.collection("registered_users").find({"_id": docs[0].name}, function(err, n) {
if(docs.length == 1) { var icon = false;
db.collection("registered_users").find({"_id": docs[0].name}, function(err, n) { if(n.length > 0 && n[0].icon) {
var icon = false; icon = n[0].icon;
if(n.length > 0 && n[0].icon) { }
icon = n[0].icon; io.to(coll).emit('chat', {from: docs[0].name, icon: icon, msg: " skipped"});
} });
io.to(coll).emit('chat', {from: docs[0].name, icon: icon, msg: " skipped"}); }
}); });
} }else if(!Functions.contains(docs[0].skips, guid)){
}); db.collection(coll + "_settings").update({ id: "config" }, {$push:{skips:guid}}, function(err, d){
}else if(!Functions.contains(docs[0].skips, guid)){ if(frontpage_viewers[0].viewers == 2)
db.collection(coll + "_settings").update({ id: "config" }, {$push:{skips:guid}}, function(err, d){ to_skip = 1;
if(frontpage_viewers[0].viewers == 2) else
to_skip = 1; to_skip = (Math.ceil(frontpage_viewers[0].viewers/2) - docs[0].skips.length-1);
else socket.emit("toast", to_skip + " more are needed to skip!");
to_skip = (Math.ceil(frontpage_viewers[0].viewers/2) - docs[0].skips.length-1); socket.to(coll).emit('chat', {from: name, msg: " voted to skip"});
socket.emit("toast", to_skip + " more are needed to skip!"); });
socket.to(coll).emit('chat', {from: name, msg: " voted to skip"}); }else{
}); socket.emit("toast", "alreadyskip");
}else{ }
socket.emit("toast", "alreadyskip"); });
} }else
}); socket.emit("toast", "noskip");
}else }
socket.emit("toast", "noskip"); //});
}
});
} else { } else {
socket.emit("auth_required"); socket.emit("auth_required");
} }
@@ -245,11 +245,11 @@ function skip(list, guid, coll, offline, socket) {
} }
} }
function change_song(coll, error, id, callback, socket) { function change_song(coll, error, id, conf, callback, socket) {
//coll = coll.replace(/ /g,''); //coll = coll.replace(/ /g,'');
db.collection(coll + "_settings").find(function(err, docs){ //db.collection(coll + "_settings").find(function(err, docs){
var startTime = docs[0].startTime; var startTime = conf[0].startTime;
if(docs !== null && docs.length !== 0) if(conf !== null && conf.length !== 0)
{ {
db.collection(coll).aggregate([{ db.collection(coll).aggregate([{
$match:{ $match:{
@@ -270,7 +270,6 @@ function change_song(coll, error, id, callback, socket) {
}, { }, {
$limit:2 $limit:2
}], function(err, now_playing_doc){ }], function(err, now_playing_doc){
if((id && id == now_playing_doc[0].id) || !id) { if((id && id == now_playing_doc[0].id) || !id) {
if(error){ if(error){
request('http://img.youtube.com/vi/'+now_playing_doc[0].id+'/mqdefault.jpg', function (err, response, body) { request('http://img.youtube.com/vi/'+now_playing_doc[0].id+'/mqdefault.jpg', function (err, response, body) {
@@ -278,14 +277,16 @@ function change_song(coll, error, id, callback, socket) {
db.collection(coll).remove({now_playing:true, id:id}, function(err, docs){ db.collection(coll).remove({now_playing:true, id:id}, function(err, docs){
var next_song; var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id; if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
change_song_post(coll, next_song, callback, socket); change_song_post(coll, next_song, conf, callback, socket);
if(!callback) { if(!callback) {
io.to(coll).emit("channel", {type: "deleted", value: now_playing_doc[0].id, removed: true}); io.to(coll).emit("channel", {type: "deleted", value: now_playing_doc[0].id, removed: true});
} }
db.collection("frontpage_lists").update({_id: coll, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){}); if(docs.deletedCount == 1) {
db.collection("frontpage_lists").update({_id: coll, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){});
}
}); });
} else { } else {
if((docs[0].skipped_time != undefined && docs[0].skipped_time != Functions.get_time()) || docs[0].skipped_time == undefined) { if((conf[0].skipped_time != undefined && conf[0].skipped_time != Functions.get_time()) ||conf[0].skipped_time == undefined) {
db.collection(coll + "_settings").update({id: "config"}, {$set: {skipped_time: Functions.get_time()}}, function(err, updated){ db.collection(coll + "_settings").update({id: "config"}, {$set: {skipped_time: Functions.get_time()}}, function(err, updated){
db.collection(coll).update({now_playing:true, id:id}, { db.collection(coll).update({now_playing:true, id:id}, {
$set:{ $set:{
@@ -296,26 +297,28 @@ function change_song(coll, error, id, callback, socket) {
},{multi:true}, function(err, docs){ },{multi:true}, function(err, docs){
var next_song; var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id; if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
if(docs.n >= 1) change_song_post(coll, next_song, callback, socket); if(docs.n >= 1) change_song_post(coll, next_song, conf, callback, socket);
}); });
}); });
} }
} }
}); });
} else if(docs[0].removeplay === true){ } else if(conf[0].removeplay === true){
db.collection(coll).remove({now_playing:true, id:id}, function(err, docs){ db.collection(coll).remove({now_playing:true, id:id}, function(err, docs){
var next_song; var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id; if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
change_song_post(coll, next_song, callback, socket); change_song_post(coll, next_song, conf, callback, socket);
if(!callback) { if(!callback) {
io.to(coll).emit("channel", {type: "deleted", value: now_playing_doc[0].id, removed: true}); io.to(coll).emit("channel", {type: "deleted", value: now_playing_doc[0].id, removed: true});
} }
db.collection("frontpage_lists").update({_id: coll, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){}); if(docs.deletedCount == 1) {
db.collection("frontpage_lists").update({_id: coll, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){});
}
}); });
} else { } else {
if((docs[0].skipped_time != undefined && docs[0].skipped_time != Functions.get_time()) || docs[0].skipped_time == undefined) { if((conf[0].skipped_time != undefined && conf[0].skipped_time != Functions.get_time()) ||conf[0].skipped_time == undefined) {
db.collection(coll).update({now_playing:true, id:id}, { db.collection(coll).update({now_playing:true, id:id}, {
$set:{ $set:{
now_playing:false, now_playing:false,
@@ -325,14 +328,14 @@ function change_song(coll, error, id, callback, socket) {
},{multi:true}, function(err, docs){ },{multi:true}, function(err, docs){
var next_song; var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id; if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
change_song_post(coll, next_song, callback, socket); change_song_post(coll, next_song, conf, callback, socket);
}); });
} }
} }
} else { } else {
if(now_playing_doc[0].now_playing == true && now_playing_doc.length > 1 && now_playing_doc[1].id == id) { if(now_playing_doc[0].now_playing == true && now_playing_doc.length > 1 && now_playing_doc[1].id == id) {
db.collection(coll).update({id: now_playing_doc[0].id}, {$set: {now_playing: false}}, function(e, d) { db.collection(coll).update({id: now_playing_doc[0].id}, {$set: {now_playing: false}}, function(e, d) {
change_song(coll, error, id, callback, socket); change_song(coll, error, id, conf, callback, socket);
}) })
} else { } else {
return; return;
@@ -340,10 +343,10 @@ function change_song(coll, error, id, callback, socket) {
} }
}); });
} }
}); //});
} }
function change_song_post(coll, next_song, callback, socket) { function change_song_post(coll, next_song, conf, callback, socket) {
//coll = coll.replace(/ /g,''); //coll = coll.replace(/ /g,'');
db.collection(coll).aggregate([{ db.collection(coll).aggregate([{
$match:{ $match:{
@@ -384,7 +387,7 @@ function change_song_post(coll, next_song, callback, socket) {
skips:[] skips:[]
} }
}, function(err, returnDocs){ }, function(err, returnDocs){
db.collection(coll + "_settings").find({id: "config"}, function(err, conf){ //db.collection(coll + "_settings").find({id: "config"}, function(err, conf){
if(!callback) { if(!callback) {
io.to(coll).emit("channel", {type: "song_change", time: Functions.get_time(), remove: conf[0].removeplay}); io.to(coll).emit("channel", {type: "song_change", time: Functions.get_time(), remove: conf[0].removeplay});
send_play(coll); send_play(coll);
@@ -394,7 +397,7 @@ function change_song_post(coll, next_song, callback, socket) {
callback(); callback();
} }
Frontpage.update_frontpage(coll, docs[0].id, docs[0].title, docs[0].thumbnail, docs[0].source); Frontpage.update_frontpage(coll, docs[0].id, docs[0].title, docs[0].thumbnail, docs[0].source);
}); //});
}); });
}); });
} }
@@ -504,7 +507,7 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
}) })
} else { } else {
if(Functions.get_time()-conf[0].startTime > np_docs[0].duration){ if(Functions.get_time()-conf[0].startTime > np_docs[0].duration){
change_song(coll, false, np_docs[0].id, function() { change_song(coll, false, np_docs[0].id, conf, function() {
send_list(coll, socket, send, list_send, configs, shuffled); send_list(coll, socket, send, list_send, configs, shuffled);
}, socket); }, socket);
} else { } else {
@@ -598,21 +601,21 @@ function end(obj, coll, guid, offline, socket) {
db.collection(coll).find({now_playing:true}, function(err, np){ db.collection(coll).find({now_playing:true}, function(err, np){
if(err !== null) console.log(err); if(err !== null) console.log(err);
if(np !== null && np !== undefined && np.length == 1 && np[0].id == id){ if(np !== null && np !== undefined && np.length == 1 && np[0].id == id){
db.collection(coll + "_settings").find(function(err, docs){ //db.collection(coll + "_settings").find(function(err, docs){
var startTime = docs[0].startTime; var startTime = docs[0].startTime;
if(docs[0].removeplay === true && startTime+parseInt(np[0].duration)<=Functions.get_time()+5) if(docs[0].removeplay === true && startTime+parseInt(np[0].duration)<=Functions.get_time()+5) {
{ db.collection(coll).remove({now_playing:true, id: id}, function(err, docs){
db.collection(coll).remove({now_playing:true}, function(err, docs){ change_song_post(coll, undefined, docs);
change_song_post(coll); if(docs.deletedCount == 1) {
db.collection("frontpage_lists").update({_id:coll, count: {$gt: 0}}, {$inc:{count:-1}, $set:{accessed: Functions.get_time()}}, {upsert:true}, function(err, docs){}); db.collection("frontpage_lists").update({_id:coll, count: {$gt: 0}}, {$inc:{count:-1}, $set:{accessed: Functions.get_time()}}, {upsert:true}, function(err, docs){});
}
}); });
}else{ } else {
if(startTime+parseInt(np[0].duration)<=Functions.get_time()+5) if(startTime+parseInt(np[0].duration)<=Functions.get_time()+5) {
{ change_song(coll, false, id, docs);
change_song(coll, false, id);
} }
} }
}); //});
} }
}); });
} else { } else {
@@ -638,7 +641,7 @@ function send_play(coll, socket, broadcast) {
if(err !== null) console.log(err); if(err !== null) console.log(err);
try{ try{
if(Functions.get_time()-conf[0].startTime > np[0].duration){ if(Functions.get_time()-conf[0].startTime > np[0].duration){
change_song(coll, false, np[0].id); change_song(coll, false, np[0].id, conf);
} else if(conf !== null && conf !== undefined && conf.length !== 0) } else if(conf !== null && conf !== undefined && conf.length !== 0)
{ {
if(conf[0].adminpass !== "") conf[0].adminpass = true; if(conf[0].adminpass !== "") conf[0].adminpass = true;