Reindented some

This commit is contained in:
Kasper Rynning-Tønnesen
2015-11-13 23:27:41 +01:00
parent 30b6c2b888
commit 69a5dba6b1

View File

@@ -20,17 +20,17 @@ var server;
}*/ }*/
//catch(err){ //catch(err){
console.log("Starting without https (probably on localhost)"); console.log("Starting without https (probably on localhost)");
//if(err["errno"] != 34)console.log(err); //if(err["errno"] != 34)console.log(err);
var cors_proxy = require('cors-anywhere'); var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({ cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'], requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'], removeHeaders: ['cookie', 'cookie2'],
}).listen(8080, function() { }).listen(8080, function() {
console.log('Running CORS Anywhere on :' + 8080); console.log('Running CORS Anywhere on :' + 8080);
}); });
var http = require('http'); var http = require('http');
server = http.createServer(handler); server = http.createServer(handler);
//} //}
var io = require('socket.io')(server, {'pingTimeout': 25000}); var io = require('socket.io')(server, {'pingTimeout': 25000});
@@ -67,6 +67,7 @@ db.on('error',function(err) {
}); });
io.on('connection', function(socket){ io.on('connection', function(socket){
console.log("connection");
socket.emit("get_list"); socket.emit("get_list");
var guid = hash_pass(socket.handshake.headers["user-agent"] + socket.handshake.address + socket.handshake.headers["accept-language"]); var guid = hash_pass(socket.handshake.headers["user-agent"] + socket.handshake.address + socket.handshake.headers["accept-language"]);
@@ -111,6 +112,7 @@ io.on('connection', function(socket){
socket.on('frontpage_lists', function() socket.on('frontpage_lists', function()
{ {
console.log("connected");
var playlists_to_send = []; var playlists_to_send = [];
var i = 0; var i = 0;
var playlists_to_send = []; var playlists_to_send = [];
@@ -140,9 +142,8 @@ io.on('connection', function(socket){
var title = list[0]["title"]; var title = list[0]["title"];
var pinned = 0; var pinned = 0;
if(conf[0].pinned == 1) pinned = 1; if(conf[0].pinned == 1) pinned = 1;
try{ try{var viewers = lists[name].length;}
var viewers = lists[name].length; catch(err){var viewers = 0;}
}catch(err){var viewers = 0;}
var to_push = [viewers, id, title, name, count, pinned]; var to_push = [viewers, id, title, name, count, pinned];
if(conf[0].pinned == 1) if(conf[0].pinned == 1)
{ {
@@ -159,8 +160,7 @@ io.on('connection', function(socket){
socket.on('now_playing', function(list) socket.on('now_playing', function(list)
{ {
db.collection(list).find({now_playing:true}, function(err, docs) db.collection(list).find({now_playing:true}, function(err, docs){
{
var title = docs[0]["title"]; var title = docs[0]["title"];
socket.emit("title", title); socket.emit("title", title);
}); });
@@ -168,7 +168,6 @@ io.on('connection', function(socket){
socket.on('id', function(arr) socket.on('id', function(arr)
{ {
if(arr.length == 3) if(arr.length == 3)
io.to(arr[0]).emit(arr[0], [arr[1], arr[2]]); io.to(arr[0]).emit(arr[0], [arr[1], arr[2]]);
}); });
@@ -190,17 +189,13 @@ io.on('connection', function(socket){
io.to(coll).emit("viewers", lists[coll].length); io.to(coll).emit("viewers", lists[coll].length);
db.getCollectionNames(function(err, docs){ db.getCollectionNames(function(err, docs){
if(contains(docs, coll)) if(contains(docs, coll))
{ {
send_list(coll, socket, true, false, true); send_list(coll, socket, true, false, true);
}else }else{
{
db.createCollection(coll, function(err, docs){ db.createCollection(coll, function(err, docs){
db.collection(coll).insert({"addsongs":false, "adminpass":"", "allvideos":false, "frontpage":true, "longsongs":false, "removeplay": false, "shuffle": true, "skip": false, "skips": [], "startTime":get_time(), "views": [], "vote": false, "desc": ""}, function(err, docs) db.collection(coll).insert({"addsongs":false, "adminpass":"", "allvideos":false, "frontpage":true, "longsongs":false, "removeplay": false, "shuffle": true, "skip": false, "skips": [], "startTime":get_time(), "views": [], "vote": false, "desc": ""}, function(err, docs){
{
send_list(coll, socket, true, false, true); send_list(coll, socket, true, false, true);
}); });
}); });
} }
@@ -221,12 +216,10 @@ io.on('connection', function(socket){
var startTime = docs[0]["startTime"]; var startTime = docs[0]["startTime"];
if(docs[0]["removeplay"] == true) if(docs[0]["removeplay"] == true)
{ {
db.collection(coll).remove({now_playing:true}, function(err, docs) db.collection(coll).remove({now_playing:true}, function(err, docs){
{
change_song_post(coll); change_song_post(coll);
}) });
}else }else{
{
if(startTime+parseInt(np[0]["duration"])<=get_time()+2) if(startTime+parseInt(np[0]["duration"])<=get_time()+2)
{ {
db.collection(coll).update({now_playing:true, id:id}, db.collection(coll).update({now_playing:true, id:id},
@@ -234,23 +227,25 @@ io.on('connection', function(socket){
now_playing:false, now_playing:false,
votes:0, votes:0,
guids:[] guids:[]
}}, function(err, docs) }}, function(err, docs){
{
if(docs["n"] == 1) if(docs["n"] == 1)
{ {
db.collection(coll).aggregate([ db.collection(coll).aggregate([
{$match:{now_playing:false}}, {$match:{now_playing:false}},
{$sort:{votes:-1, added:1}}, {$sort:{votes:-1, added:1}},
{$limit:1}], function(err, docs){ {$limit:1
}], function(err, docs){
if(docs !== null && docs.length > 0){ if(docs !== null && docs.length > 0){
db.collection(coll).update({id:docs[0]["id"]}, db.collection(coll).update({id:docs[0]["id"]},
{$set:{ {$set:{
now_playing:true, now_playing:true,
votes:0, votes:0,
guids:[], guids:[],
added:get_time()}}, function(err, docs){ added:get_time()
}}, function(err, docs){
db.collection(coll).update({views:{$exists:true}}, db.collection(coll).update({views:{$exists:true}},
{$set:{startTime:get_time(), skips:[]}}, function(err, docs){ {$set:{startTime:get_time(), skips:[]
}}, function(err, docs){
io.to(coll).emit("channel", ["song_change", get_time()]); io.to(coll).emit("channel", ["song_change", get_time()]);
send_play(coll); send_play(coll);
}); });
@@ -258,9 +253,7 @@ io.on('connection', function(socket){
} }
}); });
} }
}); });
} }
} }
}); });
@@ -327,8 +320,7 @@ io.on('connection', function(socket){
{ {
var id = msg[1]; var id = msg[1];
var hash = hash_pass(msg[3]); var hash = hash_pass(msg[3]);
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]["vote"] == true && (hash == docs[0]["adminpass"] || docs[0]["adminpass"] == "")) if(docs !== null && docs.length != 0 && ((docs[0]["vote"] == true && (hash == docs[0]["adminpass"] || docs[0]["adminpass"] == ""))
|| docs[0]["vote"] == false)) || docs[0]["vote"] == false))
{ {
@@ -361,14 +353,13 @@ io.on('connection', function(socket){
{ {
if(docs[0]["adminpass"] == "" || docs[0]["adminpass"] == hash_pass(opw)) if(docs[0]["adminpass"] == "" || docs[0]["adminpass"] == hash_pass(opw))
{ {
db.collection(coll).update({views:{$exists:true}}, {$set:{adminpass:hash_pass(pw)}}, function(err, docs) db.collection(coll).update({views:{$exists:true}}, {$set:{adminpass:hash_pass(pw)}}, function(err, docs){
{
if(inp.length == 3) if(inp.length == 3)
socket.emit("toast", "changedpass"); socket.emit("toast", "changedpass");
else else
socket.emit("toast", "correctpass"); socket.emit("toast", "correctpass");
socket.emit("pw", pw); socket.emit("pw", pw);
}) });
}else }else
socket.emit("toast", "wrongpass"); socket.emit("toast", "wrongpass");
} }
@@ -464,16 +455,14 @@ io.on('connection', function(socket){
shuffle:shuffling, shuffle:shuffling,
longsongs:longsongs, longsongs:longsongs,
adminpass:hash, adminpass:hash,
desc: description}}, function(err, docs){ desc: description
db.collection(coll).find({views:{$exists:true}}, function(err, docs) }}, function(err, docs){
{ db.collection(coll).find({views:{$exists:true}}, function(err, docs){
io.to(coll).emit("conf", docs); io.to(coll).emit("conf", docs);
socket.emit("toast", "savedsettings"); socket.emit("toast", "savedsettings");
}); });
}); });
}else{
}else
{
socket.emit("toast", "wrongpass"); socket.emit("toast", "wrongpass");
} }
}); });
@@ -510,7 +499,7 @@ io.on('connection', function(socket){
{ {
send_list(coll, undefined, false, true, false); send_list(coll, undefined, false, true, false);
} }
}; }
}else }else
socket.emit("toast", "wrongpass"); socket.emit("toast", "wrongpass");
}); });
@@ -530,7 +519,6 @@ io.on('connection', function(socket){
socket.leave(coll); socket.leave(coll);
} }
} }
} }
}); });
@@ -646,19 +634,16 @@ function change_song(coll, id, np_id)
{ {
if(docs[0]["removeplay"] == true) if(docs[0]["removeplay"] == true)
{ {
db.collection(coll).remove({now_playing:true}, function(err, docs) db.collection(coll).remove({now_playing:true}, function(err, docs){
{
change_song_post(coll); change_song_post(coll);
}) });
}else }else{
{
db.collection(coll).update({now_playing:true}, db.collection(coll).update({now_playing:true},
{$set:{ {$set:{
now_playing:false, now_playing:false,
votes:0, votes:0,
guids:[] guids:[]
}},{multi:true}, function(err, docs) }},{multi:true}, function(err, docs){
{
change_song_post(coll); change_song_post(coll);
}); });
} }
@@ -671,20 +656,22 @@ function change_song_post(coll)
db.collection(coll).aggregate([ db.collection(coll).aggregate([
{$match:{now_playing:false}}, {$match:{now_playing:false}},
{$sort:{votes:-1, added:1}}, {$sort:{votes:-1, added:1}},
{$limit:1}], function(err, docs){ {$limit:1
}], function(err, docs){
if(docs !== null && docs.length > 0){ if(docs !== null && docs.length > 0){
db.collection(coll).update({id:docs[0]["id"]}, db.collection(coll).update({id:docs[0]["id"]},
{$set:{ {$set:{
now_playing:true, now_playing:true,
votes:0, votes:0,
guids:[], guids:[],
added:get_time()}}, function(err, docs){ added:get_time()
}}, function(err, docs){
db.collection(coll).update({views:{$exists:true}}, db.collection(coll).update({views:{$exists:true}},
{$set:{startTime:get_time(), skips:[]}}, function(err, docs){ {$set:{startTime:get_time(), skips:[]
}}, function(err, docs){
io.to(coll).emit("channel", ["song_change", get_time()]); io.to(coll).emit("channel", ["song_change", get_time()]);
send_play(coll); send_play(coll);
}); });
}); });
} }
}); });
@@ -713,7 +700,6 @@ function send_list(coll, socket, send, list_send, configs)
io.to(coll).emit("conf", conf); io.to(coll).emit("conf", conf);
}); });
} }
} }
function send_play(coll, socket) function send_play(coll, socket)
@@ -759,8 +745,8 @@ function rndName(seed, len) {
is_vowel = false; is_vowel = false;
var arr; var arr;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
if (is_vowel) arr = vowels if (is_vowel) arr = vowels;
else arr = consts else arr = consts;
is_vowel = !is_vowel; is_vowel = !is_vowel;
word += arr[(seed[i%seed.length].charCodeAt()+i) % (arr.length-1)]; word += arr[(seed[i%seed.length].charCodeAt()+i) % (arr.length-1)];
} }
@@ -775,6 +761,5 @@ function uniqueID(seed, minlen){
id = rndName(String(len)+id, len); id = rndName(String(len)+id, len);
len += 0.1; // try 10 times at each length len += 0.1; // try 10 times at each length
} }
return id; return id;
} }