indentations on server.js

This commit is contained in:
Kasper Rynning-Tønnesen
2017-05-06 13:46:17 +02:00
parent 627719a967
commit a84f338534
3 changed files with 1346 additions and 1289 deletions

File diff suppressed because one or more lines are too long

View File

@@ -566,8 +566,9 @@ io.on('connection', function(socket){
} else if(full_list) { } else if(full_list) {
np = false; np = false;
} }
} else } else {
np = false; np = false;
}
db.collection(coll).update({id: id}, {"added": added,"guids":guids,"id":id,"now_playing":np,"title":title,"votes":votes, "duration":duration}, {upsert: true}, function(err, docs){ db.collection(coll).update({id: id}, {"added": added,"guids":guids,"id":id,"now_playing":np,"title":title,"votes":votes, "duration":duration}, {upsert: true}, function(err, docs){
if(np) if(np)
{ {
@@ -582,9 +583,9 @@ io.on('connection', function(socket){
} }
db.collection("frontpage_lists").update({_id:coll}, {$inc:{count:1}, $set:{accessed: get_time()}}, {upsert:true}, function(err, docs){}); db.collection("frontpage_lists").update({_id:coll}, {$inc:{count:1}, $set:{accessed: get_time()}}, {upsert:true}, function(err, docs){});
}); });
if(!full_list) if(!full_list) {
socket.emit("toast", "addedsong"); socket.emit("toast", "addedsong");
else if(full_list && last) { } else if(full_list && last) {
socket.emit("toast", "addedplaylist"); socket.emit("toast", "addedplaylist");
} }
}); });
@@ -597,13 +598,19 @@ io.on('connection', function(socket){
socket.emit("toast", "addedplaylist"); socket.emit("toast", "addedplaylist");
} }
}); });
}else if(!full_list) } else if(!full_list) {
{
db.collection(coll).find({id: id}, function(err, docs) { db.collection(coll).find({id: id}, function(err, docs) {
if(docs.length === 0) { if(docs.length === 0) {
db.collection(coll).update({id: id}, db.collection(coll).update({id: id}, {$set:{
{$set:{ "added":get_time(),
"added":get_time(), "guids": [guid], "id":id, "now_playing": false, "title":title, "votes":1, "duration":duration, type:"suggested"}}, "guids": [guid],
"id":id,
"now_playing": false,
"title":title,
"votes":1,
"duration":duration,
"type":"suggested"}
},
{upsert:true}, function(err, docs){ {upsert:true}, function(err, docs){
socket.emit("toast", "suggested"); socket.emit("toast", "suggested");
io.to(coll).emit("suggested", {id: id, title: title, duration: duration}); io.to(coll).emit("suggested", {id: id, title: title, duration: duration});
@@ -858,11 +865,11 @@ io.on('connection', function(socket){
var hash; var hash;
if(params.description) description = params.description; if(params.description) description = params.description;
if(adminpass !== "") if(adminpass !== "") {
hash = hash_pass(decrypt_string(socketid, adminpass)); hash = hash_pass(decrypt_string(socketid, adminpass));
else } else {
hash = adminpass; hash = adminpass;
}
db.collection(coll).find({views:{$exists:true}}, function(err, docs){ db.collection(coll).find({views:{$exists:true}}, function(err, docs){
if(docs !== null && docs.length !== 0 && docs[0].adminpass === "" || docs[0].adminpass == hash) if(docs !== null && docs.length !== 0 && docs[0].adminpass === "" || docs[0].adminpass == hash)
{ {
@@ -878,7 +885,8 @@ io.on('connection', function(socket){
adminpass:hash, adminpass:hash,
desc: description, desc: description,
userpass: userpass, userpass: userpass,
}}, function(err, docs){ }
}, function(err, docs){
db.collection(coll).find({views:{$exists:true}}, function(err, docs){ db.collection(coll).find({views:{$exists:true}}, function(err, docs){
if(docs[0].adminpass !== "") docs[0].adminpass = true; if(docs[0].adminpass !== "") docs[0].adminpass = true;
if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true;
@@ -886,8 +894,9 @@ io.on('connection', function(socket){
io.to(coll).emit("conf", docs); io.to(coll).emit("conf", docs);
socket.emit("toast", "savedsettings"); socket.emit("toast", "savedsettings");
db.collection("frontpage_lists").update({_id: coll}, db.collection("frontpage_lists").update({_id: coll}, {$set:{
{$set:{frontpage:frontpage, accessed: get_time()}}, frontpage:frontpage, accessed: get_time()}
},
{upsert:true}, function(err, docs){}); {upsert:true}, function(err, docs){});
}); });
}); });
@@ -1097,16 +1106,15 @@ function change_name(name, guid, oldname){
} }
} }
function update_frontpage(coll, id, title) function update_frontpage(coll, id, title) {
{ db.collection("frontpage_lists").update({_id: coll}, {$set: {
db.collection("frontpage_lists").update( id: id,
{_id: coll}, title: title,
{$set:{id: id, title: title, accessed: get_time()}}, accessed: get_time()}
{upsert: true}, function(err, returnDocs){}); },{upsert: true}, function(err, returnDocs){});
} }
function del(params, socket, socketid) function del(params, socket, socketid) {
{
if(params.id){ if(params.id){
var coll = emojiStrip(params.channel).toLowerCase(); var coll = emojiStrip(params.channel).toLowerCase();
//coll = decodeURIComponent(coll); //coll = decodeURIComponent(coll);
@@ -1162,13 +1170,11 @@ function check_inlist(coll, guid, socket, name, offline)
} }
} }
function hash_pass(adminpass) function hash_pass(adminpass) {
{
return crypto.createHash('sha256').update(adminpass).digest('base64'); return crypto.createHash('sha256').update(adminpass).digest('base64');
} }
function vote(coll, id, guid, socket, full_list, last) function vote(coll, id, guid, socket, full_list, last) {
{
db.collection(coll).find({id:id, now_playing: false}, function(err, docs){ db.collection(coll).find({id:id, now_playing: false}, function(err, docs){
if(docs !== null && docs.length > 0 && !contains(docs[0].guids, guid)) if(docs !== null && docs.length > 0 && !contains(docs[0].guids, guid))
{ {
@@ -1186,16 +1192,29 @@ function vote(coll, id, guid, socket, full_list, last)
} }
function change_song(coll, error, id) function change_song(coll, error, id) {
{
db.collection(coll).find({views:{$exists:true}}, function(err, docs){ db.collection(coll).find({views:{$exists:true}}, function(err, docs){
var startTime = docs[0].startTime; var startTime = docs[0].startTime;
if(docs !== null && docs.length !== 0) if(docs !== null && docs.length !== 0)
{ {
db.collection(coll).aggregate([ db.collection(coll).aggregate([{
{$match:{views:{$exists: false}, type:{$ne: "suggested"}}}, $match:{
{$sort:{now_playing: -1, votes:-1, added:1, title: 1}}, views:{
{$limit:2 $exists: false
},
type:{
$ne: "suggested"
}
}
}, {
$sort:{
now_playing: -1,
votes:-1,
added:1,
title: 1
}
}, {
$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) {
//db.collection(coll).find({now_playing:true}, function(err, now_playing_doc){ //db.collection(coll).find({now_playing:true}, function(err, now_playing_doc){
@@ -1212,12 +1231,13 @@ function change_song(coll, error, id)
} else { } else {
if(skipped[coll] != get_time()){ if(skipped[coll] != get_time()){
skipped[coll] = get_time(); skipped[coll] = get_time();
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,
votes:0, votes:0,
guids:[] guids:[]
}},{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); if(docs.n >= 1) change_song_post(coll, next_song);
@@ -1238,12 +1258,13 @@ function change_song(coll, error, id)
} else { } else {
if(skipped[coll] != get_time()){ if(skipped[coll] != get_time()){
//skipped[coll] = get_time(); //skipped[coll] = get_time();
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,
votes:0, votes:0,
guids:[] guids:[]
}},{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); if(docs.n >= 1) change_song_post(coll, next_song);
@@ -1260,26 +1281,41 @@ function change_song(coll, error, id)
function change_song_post(coll, next_song) function change_song_post(coll, next_song)
{ {
db.collection(coll).aggregate([ db.collection(coll).aggregate([{
{$match:{now_playing:false, type:{$ne: "suggested"}}}, $match:{
{$sort:{votes:-1, added:1, title: 1}}, now_playing:false,
{$limit:1 type:{
$ne: "suggested"
}
}
}, {
$sort:{
votes:-1,
added:1,
title: 1
}
}, {
$limit:1
}], function(err, docs){ }], function(err, docs){
if(docs !== null && docs.length > 0){ if(docs !== null && docs.length > 0){
var id = docs[0].id; var id = docs[0].id;
if(next_song && next_song != id) { if(next_song && next_song != id) {
return; return;
} }
db.collection(coll).update({id:id}, db.collection(coll).update({id:id},{
{$set:{ $set:{
now_playing:true, now_playing:true,
votes:0, votes:0,
guids:[], guids:[],
added:get_time() added:get_time()
}}, function(err, returnDocs){ }
db.collection(coll).update({views:{$exists:true}}, }, function(err, returnDocs){
{$set:{startTime:get_time(), skips:[] db.collection(coll).update({views:{$exists:true}},{
}}, function(err, returnDocs){ $set:{
startTime:get_time(),
skips:[]
}
}, function(err, returnDocs){
db.collection(coll).find({views:{$exists:true}}, function(err, conf){ db.collection(coll).find({views:{$exists:true}}, function(err, conf){
io.to(coll).emit("channel", {type: "song_change", time: get_time(), remove: conf[0].removeplay}); io.to(coll).emit("channel", {type: "song_change", time: get_time(), remove: conf[0].removeplay});
send_play(coll); send_play(coll);
@@ -1300,22 +1336,40 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
if(docs.length > 0) { if(docs.length > 0) {
db.collection(coll).find({now_playing: true}, function(err, np_docs) { db.collection(coll).find({now_playing: true}, function(err, np_docs) {
if(np_docs.length == 0) { if(np_docs.length == 0) {
db.collection(coll).aggregate([ db.collection(coll).aggregate([{
{$match:{views:{$exists: false}, type:{$ne: "suggested"}}}, $match:{
{$sort:{now_playing: -1, votes:-1, added:1, title: 1}}, views:{
{$limit:1 $exists: false
},
type:{
$ne: "suggested"
}
}
}, {
$sort:{
now_playing: -1,
votes:-1,
added:1,
title: 1
}
}, {
$limit:1
}], function(err, now_playing_doc){ }], function(err, now_playing_doc){
if(now_playing_doc[0].now_playing == false) { if(now_playing_doc[0].now_playing == false) {
db.collection(coll).update({id:now_playing_doc[0].id}, db.collection(coll).update({id:now_playing_doc[0].id}, {
{$set:{ $set:{
now_playing:true, now_playing:true,
votes:0, votes:0,
guids:[], guids:[],
added:get_time() added:get_time()
}}, function(err, returnDocs){ }
db.collection(coll).update({views:{$exists:true}}, }, function(err, returnDocs){
{$set:{startTime:get_time(), skips:[] db.collection(coll).update({views:{$exists:true}}, {
}}, function(err, returnDocs){ $set:{
startTime: get_time(),
skips:[]
}
}, function(err, returnDocs){
update_frontpage(coll, now_playing_doc[0].id, now_playing_doc[0].title); update_frontpage(coll, now_playing_doc[0].id, now_playing_doc[0].title);
send_list(coll, socket, send, list_send, configs, shuffled); send_list(coll, socket, send, list_send, configs, shuffled);
}); });
@@ -1323,26 +1377,30 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
} }
}); });
} else { } else {
if(list_send) if(list_send) {
io.to(coll).emit("channel", {type: "list", playlist: docs, shuffled: shuffled}); io.to(coll).emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
else if(!list_send) } else if(!list_send) {
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled}); socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
if(socket === undefined && send) }
if(socket === undefined && send) {
send_play(coll); send_play(coll);
else if(send) } else if(send) {
send_play(coll, socket); send_play(coll, socket);
} }
}
}); });
} else { } else {
if(list_send) if(list_send) {
io.to(coll).emit("channel", {type: "list", playlist: docs, shuffled: shuffled}); io.to(coll).emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
else if(!list_send) } else if(!list_send) {
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled}); socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
if(socket === undefined && send) }
if(socket === undefined && send) {
send_play(coll); send_play(coll);
else if(send) } else if(send) {
send_play(coll, socket); send_play(coll, socket);
} }
}
}); });
@@ -1365,7 +1423,6 @@ function send_play(coll, socket)
{ {
db.collection(coll).find({now_playing:true}, function(err, np){ db.collection(coll).find({now_playing:true}, function(err, np){
db.collection(coll).find({views:{$exists:true}}, function(err, conf){ db.collection(coll).find({views:{$exists:true}}, function(err, conf){
if(err !== null) console.log(err); if(err !== null) console.log(err);
try{ try{
if(get_time()-conf[0].startTime > np[0].duration){ if(get_time()-conf[0].startTime > np[0].duration){