Fix for crashes

This commit is contained in:
Kasper Rynning-Tønnesen
2018-03-03 16:17:11 +01:00
parent 5c1e91467f
commit 731f26e3d4

View File

@@ -191,6 +191,7 @@ function change_song(coll, error, id, callback) {
}, { }, {
$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) {
@@ -234,6 +235,7 @@ function change_song(coll, error, id, callback) {
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((docs[0].skipped_time != undefined && docs[0].skipped_time != Functions.get_time()) || docs[0].skipped_time == undefined) { if((docs[0].skipped_time != undefined && docs[0].skipped_time != Functions.get_time()) || docs[0].skipped_time == undefined) {
db.collection(coll).update({now_playing:true, id:id}, { db.collection(coll).update({now_playing:true, id:id}, {
$set:{ $set:{
@@ -291,13 +293,13 @@ function change_song_post(coll, next_song, callback)
added:Functions.get_time() added:Functions.get_time()
} }
}, function(err, returnDocs){ }, function(err, returnDocs){
db.collection(coll + "_settings").update({id: "configs"}, { db.collection(coll + "_settings").update({id: "config"}, {
$set:{ $set:{
startTime:Functions.get_time(), startTime:Functions.get_time(),
skips:[] skips:[]
} }
}, function(err, returnDocs){ }, function(err, returnDocs){
db.collection(coll + "_settings").find(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});
List.send_play(coll); List.send_play(coll);
@@ -314,83 +316,91 @@ function change_song_post(coll, next_song, callback)
function send_list(coll, socket, send, list_send, configs, shuffled) function send_list(coll, socket, send, list_send, configs, shuffled)
{ {
db.collection(coll + "_settings").find(function(err, conf){ db.collection(coll + "_settings").find({id: "config"}, function(err, _conf){
db.collection(coll).find({views:{$exists:false}, type: {$ne: "suggested"}}, function(err, docs) var conf = _conf;
{ if(conf.length == 0) {
if(docs.length > 0) { var conf = {"id": "config", "addsongs":false, "adminpass":"", "allvideos":true, "frontpage":true, "longsongs":false, "removeplay": false, "shuffle": true, "skip": false, "skips": [], "startTime":Functions.get_time(), "views": [], "vote": false, "desc": "", userpass: ""};
db.collection(coll).find({now_playing: true}, function(err, np_docs) { db.collection(coll + "_settings").update({id: "config"}, conf, {upsert: true}, function(err, docs) {
if(np_docs.length == 0) { send_list(coll, socket, send, list_send, configs, shuffled);
db.collection(coll).aggregate([{ });
$match:{ } else {
views:{ db.collection(coll).find({views:{$exists:false}, type: {$ne: "suggested"}}, function(err, docs)
$exists: false {
}, if(docs.length > 0) {
type:{ db.collection(coll).find({now_playing: true}, function(err, np_docs) {
$ne: "suggested" if(np_docs.length == 0) {
} db.collection(coll).aggregate([{
} $match:{
}, { views:{
$sort:{ $exists: false
now_playing: -1, },
votes:-1, type:{
added:1, $ne: "suggested"
title: 1
}
}, {
$limit:1
}], function(err, now_playing_doc){
if(now_playing_doc[0].now_playing == false) {
db.collection(coll).update({id:now_playing_doc[0].id}, {
$set:{
now_playing:true,
votes:0,
guids:[],
added:Functions.get_time()
} }
}, function(err, returnDocs){ }
db.collection(coll + "_settings").update({ id: "config" }, { }, {
$sort:{
now_playing: -1,
votes:-1,
added:1,
title: 1
}
}, {
$limit:1
}], function(err, now_playing_doc){
if(now_playing_doc[0].now_playing == false) {
db.collection(coll).update({id:now_playing_doc[0].id}, {
$set:{ $set:{
startTime: Functions.get_time(), now_playing:true,
skips:[] votes:0,
guids:[],
added:Functions.get_time()
} }
}, function(err, returnDocs){ }, function(err, returnDocs){
Frontpage.update_frontpage(coll, now_playing_doc[0].id, now_playing_doc[0].title); db.collection(coll + "_settings").update({ id: "config" }, {
List.send_list(coll, socket, send, list_send, configs, shuffled); $set:{
startTime: Functions.get_time(),
skips:[]
}
}, function(err, returnDocs){
Frontpage.update_frontpage(coll, now_playing_doc[0].id, now_playing_doc[0].title);
List.send_list(coll, socket, send, list_send, configs, shuffled);
});
}); });
}); }
}
});
} else {
if(Functions.get_time()-conf[0].startTime > np_docs[0].duration){
List.change_song(coll, false, np_docs[0].id, function() {
List.send_list(coll, socket, send, list_send, configs, shuffled);
}); });
} else { } else {
if(list_send) { if(Functions.get_time()-conf[0].startTime > np_docs[0].duration){
io.to(coll).emit("channel", {type: "list", playlist: docs, shuffled: shuffled}); List.change_song(coll, false, np_docs[0].id, function() {
} else if(!list_send) { List.send_list(coll, socket, send, list_send, configs, shuffled);
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled}); });
} } else {
if(socket === undefined && send) { if(list_send) {
List.send_play(coll); io.to(coll).emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
} else if(send) { } else if(!list_send) {
List.send_play(coll, socket); socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
}
if(socket === undefined && send) {
List.send_play(coll);
} else if(send) {
List.send_play(coll, socket);
}
} }
} }
});
} else {
if(list_send) {
io.to(coll).emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
} else if(!list_send) {
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
}
if(socket === undefined && send) {
List.send_play(coll);
} else if(send) {
List.send_play(coll, socket);
} }
});
} else {
if(list_send) {
io.to(coll).emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
} else if(!list_send) {
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
} }
if(socket === undefined && send) { });
List.send_play(coll);
} else if(send) {
List.send_play(coll, socket);
}
}
if(configs) if(configs)
{ {
if(conf.length > 0) { if(conf.length > 0) {
@@ -405,7 +415,7 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
}); });
} }
} }
}); }
}); });
if(socket){ if(socket){
db.collection(coll).find({type:"suggested"}).sort({added: 1}, function(err, sugg){ db.collection(coll).find({type:"suggested"}).sort({added: 1}, function(err, sugg){