Prettier JSON and less globally exposed variables

This commit is contained in:
Kasper Rynning-Tønnesen
2018-05-30 14:19:10 +02:00
parent 68c24b6317
commit b971074788
11 changed files with 168 additions and 129 deletions

View File

@@ -26,7 +26,7 @@ var hbs = exphbs.create({
layoutsDir: publicPath + '/layouts/client',
partialsDir: publicPath + '/partials'
});
uniqid = require('uniqid');
var uniqid = require('uniqid');
app.engine('handlebars', hbs.engine);
@@ -45,36 +45,22 @@ app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(cookieParser());
app.set('json spaces', 2);
/* Starting DB and socketio */
io = require('socket.io')({
pingTimeout: 25000,
//path: '/zoff',
//"origins": ("https://zoff.me:443*,https://zoff.me:8080*,zoff.me:8080*,https://remote.zoff.me:443*,https://remote.zoff.me:8080*,https://fb.zoff.me:443*,https://fb.zoff.me:8080*,https://admin.zoff.me:443*,https://admin.zoff.me:8080*, http://localhost:8080*")});
});
db = require(pathThumbnails + '/handlers/db.js');
var socketIO = require(pathThumbnails +'/handlers/io.js');
socketIO();
app.socketIO = io;
request = require('request');
/* Globally needed "libraries" and files */
Functions = require(pathThumbnails + '/handlers/functions.js');
ListChange = require(pathThumbnails + '/handlers/list_change.js');
Chat = require(pathThumbnails + '/handlers/chat.js');
List = require(pathThumbnails + '/handlers/list.js');
Suggestions = require(pathThumbnails + '/handlers/suggestions.js');
ListSettings = require(pathThumbnails + '/handlers/list_settings.js');
Frontpage = require(pathThumbnails + '/handlers/frontpage.js');
Notifications = require(pathThumbnails + '/handlers/notifications.js');
Search = require(pathThumbnails + '/handlers/search.js');
crypto = require('crypto');
emojiStrip = Functions.removeEmojis;
Filter = require('bad-words');
filter = new Filter({ placeHolder: 'x'});
var Functions = require(pathThumbnails + '/handlers/functions.js');
var router = require(pathThumbnails + '/routing/client/router.js');
var api = require(pathThumbnails + '/routing/client/api.js');
var ico_router = require(pathThumbnails + '/routing/client/icons_routing.js');

View File

@@ -1,3 +1,9 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var db = require(pathThumbnails + '/handlers/db.js');
function get_history(channel, all, socket) {
var query = {};
if(all) {
@@ -67,7 +73,7 @@ function chat(msg, guid, offline, socket) {
return;
}
var coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass) {
if(userpass != "" || msg.pass == undefined) {
@@ -120,7 +126,7 @@ function all_chat(msg, guid, offline, socket) {
}
var coll = msg.channel.toLowerCase().replace(/ /g,'');
var data = msg.data;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.check_inlist(coll, guid, socket, offline);
if(data !== "" && data !== undefined && data !== null &&
@@ -268,11 +274,11 @@ function generate_name(guid, announce_payload, second) {
}
});
} else {
Chat.generate_name(guid, announce_payload, tmp_name);
generate_name(guid, announce_payload, tmp_name);
}
})
} else {
Chat.generate_name(guid, announce_payload, tmp_name);
generate_name(guid, announce_payload, tmp_name);
}
})
}
@@ -280,7 +286,7 @@ function generate_name(guid, announce_payload, second) {
function get_name(guid, announce_payload, first) {
db.collection("user_names").find({"guid": guid}, function(err, docs) {
if(docs.length == 0) {
Chat.generate_name(guid, announce_payload);
generate_name(guid, announce_payload);
} else {
name = docs[0].name;
}

View File

@@ -1,3 +1,5 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var db = require(pathThumbnails + '/handlers/db.js');
function frontpage_lists(msg, socket) {
if(msg == undefined || !msg.hasOwnProperty('version') || msg.version != VERSION || msg.version == undefined) {
var result = {

View File

@@ -8,6 +8,9 @@ try {
}
var mongojs = require('mongojs');
var connected_db = mongojs('mongodb://' + mongo_config.host + '/user_credentials');
var crypto = require('crypto');
var db = require(pathThumbnails + '/handlers/db.js');
var uniqid = require('uniqid');
function remove_unique_id(short_id) {
db.collection("unique_ids").update({"_id": "unique_ids"}, {$pull: {unique_ids: short_id}}, function(err, docs) {});
@@ -50,7 +53,7 @@ function getSession(socket) {
}
function remove_from_array(array, element){
if(Functions.contains(array, element)){
if(contains(array, element)){
var index = array.indexOf(element);
if(index != -1)
array.splice(index, 1);

View File

@@ -1,5 +1,19 @@
var cookieParser = require("cookie-parser");
var cookie = require("cookie");
var Functions = require(pathThumbnails + '/handlers/functions.js');
var ListChange = require(pathThumbnails + '/handlers/list_change.js');
var Chat = require(pathThumbnails + '/handlers/chat.js');
var List = require(pathThumbnails + '/handlers/list.js');
var Suggestions = require(pathThumbnails + '/handlers/suggestions.js');
var ListSettings = require(pathThumbnails + '/handlers/list_settings.js');
var Frontpage = require(pathThumbnails + '/handlers/frontpage.js');
var Search = require(pathThumbnails + '/handlers/search.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var db = require(pathThumbnails + '/handlers/db.js');
module.exports = function() {
io.on('connection', function(socket){
try {
@@ -68,7 +82,7 @@ module.exports = function() {
socketid = msg.socket_id;
socket.zoff_id = socketid;
coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
if(coll.indexOf("?") > -1){
coll = coll.substring(0, coll.indexOf("?"));
@@ -96,7 +110,7 @@ module.exports = function() {
_list = _list.substring(0, _list.indexOf("?"));
msg.channel = _list;
}
coll = emojiStrip(_list).toLowerCase();
coll = Functions.removeEmojis(_list).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -189,7 +203,7 @@ module.exports = function() {
offline = true;
if(channel != "") coll = channel;
if(coll !== undefined) {
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
db.collection("connected_users").findAndModify({
@@ -314,7 +328,7 @@ module.exports = function() {
_list = _list.substring(0, _list.indexOf("?"));
msg.channel = _list;
}
coll = emojiStrip(_list).toLowerCase();
coll = Functions.removeEmojis(_list).toLowerCase();
coll = coll.replace(/_/g, "");
//
coll = filter.clean(coll);
@@ -339,7 +353,7 @@ module.exports = function() {
try {
coll = obj.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -368,7 +382,7 @@ module.exports = function() {
try {
coll = arr.list.replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -387,7 +401,7 @@ module.exports = function() {
}
coll = msg.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -408,7 +422,7 @@ module.exports = function() {
try {
coll = msg.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -461,7 +475,7 @@ module.exports = function() {
try {
coll = msg.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -485,7 +499,7 @@ module.exports = function() {
try {
coll = obj.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -514,7 +528,7 @@ module.exports = function() {
}
if(msg.hasOwnProperty("channel") && msg.channel != "" && typeof(msg.channel) == "string") {
coll = msg.channel.replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
List.left_channel(coll, guid, short_id, in_list, socket, false);
}
@@ -546,7 +560,7 @@ module.exports = function() {
try {
coll = obj.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);

View File

@@ -1,5 +1,12 @@
var ColorThief = require('color-thief-jimp');
var Jimp = require('jimp');
var Functions = require(pathThumbnails + '/handlers/functions.js');
var Frontpage = require(pathThumbnails + '/handlers/frontpage.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var request = require('request');
var db = require(pathThumbnails + '/handlers/db.js');
function now_playing(list, fn, socket) {
if(typeof(list) !== 'string' || typeof(fn) !== 'function') {
@@ -47,7 +54,7 @@ function list(msg, guid, coll, offline, socket) {
return;
}
coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
var pass = crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64");
db.collection('frontpage_lists').find({"_id": coll}, function(err, frontpage_lists){
@@ -71,7 +78,7 @@ function list(msg, guid, coll, offline, socket) {
io.to(coll).emit("viewers", 1);
}
List.send_list(coll, socket, true, false, true);
send_list(coll, socket, true, false, true);
} else {
socket.emit("auth_required");
@@ -83,7 +90,7 @@ function list(msg, guid, coll, offline, socket) {
var configs = {"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: "", id: "config"};
db.collection(coll + "_settings").insert(configs, function(err, docs){
socket.join(coll);
List.send_list(coll, socket, true, false, true);
send_list(coll, socket, true, false, true);
db.collection("frontpage_lists").insert({"_id": coll, "count" : 0, "frontpage": true, "accessed": Functions.get_time(), "viewers": 1}, function(e,d){
});
Functions.check_inlist(coll, guid, socket, offline);
@@ -114,7 +121,7 @@ function skip(list, guid, coll, offline, socket) {
try {
coll = list.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -187,7 +194,7 @@ function skip(list, guid, coll, offline, socket) {
(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))
{
List.change_song(coll, error, video_id);
change_song(coll, error, video_id);
socket.emit("toast", "skip");
db.collection("user_names").find({"guid": guid}, function(err, docs) {
if(docs.length == 1) {
@@ -266,7 +273,7 @@ function change_song(coll, error, id, callback, socket) {
db.collection(coll).remove({now_playing:true, id:id}, function(err, docs){
var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
List.change_song_post(coll, next_song, callback, socket);
change_song_post(coll, next_song, callback, socket);
if(!callback) {
io.to(coll).emit("channel", {type: "deleted", value: now_playing_doc[0].id, removed: true});
}
@@ -284,7 +291,7 @@ function change_song(coll, error, id, callback, socket) {
},{multi:true}, function(err, docs){
var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
if(docs.n >= 1) List.change_song_post(coll, next_song, callback, socket);
if(docs.n >= 1) change_song_post(coll, next_song, callback, socket);
});
});
}
@@ -295,7 +302,7 @@ function change_song(coll, error, id, callback, socket) {
db.collection(coll).remove({now_playing:true, id:id}, function(err, docs){
var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
List.change_song_post(coll, next_song, callback, socket);
change_song_post(coll, next_song, callback, socket);
if(!callback) {
io.to(coll).emit("channel", {type: "deleted", value: now_playing_doc[0].id, removed: true});
}
@@ -313,7 +320,7 @@ function change_song(coll, error, id, callback, socket) {
},{multi:true}, function(err, docs){
var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
List.change_song_post(coll, next_song, callback, socket);
change_song_post(coll, next_song, callback, socket);
});
}
}
@@ -375,10 +382,10 @@ function change_song_post(coll, next_song, callback, socket) {
db.collection(coll + "_settings").find({id: "config"}, function(err, conf){
if(!callback) {
io.to(coll).emit("channel", {type: "song_change", time: Functions.get_time(), remove: conf[0].removeplay});
List.send_play(coll);
send_play(coll);
} else {
socket.to(coll).emit("channel", {type: "song_change", time: Functions.get_time(), remove: conf[0].removeplay});
List.send_play(coll, socket, true);
send_play(coll, socket, true);
callback();
}
Frontpage.update_frontpage(coll, docs[0].id, docs[0].title, docs[0].thumbnail, docs[0].source);
@@ -467,7 +474,7 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
}
}, function(err, returnDocs){
Frontpage.update_frontpage(coll, now_playing_doc[0].id, now_playing_doc[0].title, now_playing_doc[0].thumbnail, now_playing_doc[0].source);
List.send_list(coll, socket, send, list_send, configs, shuffled);
send_list(coll, socket, send, list_send, configs, shuffled);
});
});
}
@@ -492,8 +499,8 @@ function 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);
change_song(coll, false, np_docs[0].id, function() {
send_list(coll, socket, send, list_send, configs, shuffled);
}, socket);
} else {
if(list_send) {
@@ -502,9 +509,9 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
}
if(socket === undefined && send) {
List.send_play(coll);
send_play(coll);
} else if(send) {
List.send_play(coll, socket);
send_play(coll, socket);
}
}
}
@@ -516,9 +523,9 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
}
if(socket === undefined && send) {
List.send_play(coll);
send_play(coll);
} else if(send) {
List.send_play(coll, socket);
send_play(coll, socket);
}
}
});
@@ -591,13 +598,13 @@ function end(obj, coll, guid, offline, socket) {
if(docs[0].removeplay === true && startTime+parseInt(np[0].duration)<=Functions.get_time()+5)
{
db.collection(coll).remove({now_playing:true}, function(err, docs){
List.change_song_post(coll);
change_song_post(coll);
db.collection("frontpage_lists").update({_id:coll, count: {$gt: 0}}, {$inc:{count:-1}, $set:{accessed: Functions.get_time()}}, {upsert:true}, function(err, docs){});
});
}else{
if(startTime+parseInt(np[0].duration)<=Functions.get_time()+5)
{
List.change_song(coll, false, id);
change_song(coll, false, id);
}
}
});
@@ -626,7 +633,7 @@ function send_play(coll, socket, broadcast) {
if(err !== null) console.log(err);
try{
if(Functions.get_time()-conf[0].startTime > np[0].duration){
List.change_song(coll, false, np[0].id);
change_song(coll, false, np[0].id);
} else if(conf !== null && conf !== undefined && conf.length !== 0)
{
if(conf[0].adminpass !== "") conf[0].adminpass = true;
@@ -638,7 +645,7 @@ function send_play(coll, socket, broadcast) {
if(socket === undefined) {
io.to(coll).emit("np", toSend);
//
List.getNextSong(coll)
getNextSong(coll)
var url = 'https://img.youtube.com/vi/'+np[0].id+'/mqdefault.jpg';
if(np[0].source == "soundcloud") url = np[0].thumbnail;
sendColor(coll, false, url);

View File

@@ -1,3 +1,12 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var List = require(pathThumbnails + '/handlers/list.js');
var Frontpage = require(pathThumbnails + '/handlers/frontpage.js');
var Search = require(pathThumbnails + '/handlers/search.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var db = require(pathThumbnails + '/handlers/db.js');
function addFromOtherList(arr, guid, offline, socket) {
var socketid = socket.zoff_id;
if(typeof(arr) == "object") {
@@ -425,7 +434,7 @@ function add_function(arr, coll, guid, offline, socket) {
socket.emit("toast", "addedsong");
});
} else {
ListChange.vote(coll, id, guid, socket);
vote(coll, id, guid, socket);
}
});
} else {
@@ -460,7 +469,7 @@ function add_function(arr, coll, guid, offline, socket) {
} else if(docs[0].now_playing === true){
socket.emit("toast", "alreadyplay");
} else{
if(conf[0].vote === false) ListChange.vote(coll, id, guid, socket);
if(conf[0].vote === false) vote(coll, id, guid, socket);
else socket.emit("toast", "listhaspass");
}
});
@@ -519,7 +528,7 @@ function voteUndecided(msg, coll, guid, offline, socket) {
return;
}
coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass) {
if(adminpass != "" || msg.adminpass == undefined) {
@@ -535,13 +544,13 @@ function voteUndecided(msg, coll, guid, offline, socket) {
Functions.check_inlist(coll, guid, socket, offline);
if(msg.type == "del") {
ListChange.del(msg, socket, socketid);
del(msg, socket, socketid);
} else {
var id = msg.id;
var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(msg.adminpass), true));
if(docs !== null && docs.length !== 0 && ((docs[0].vote === true && (hash == docs[0].adminpass || docs[0].adminpass === "")) ||
docs[0].vote === false)) {
ListChange.vote(coll, id, guid, socket);
vote(coll, id, guid, socket);
} else {
socket.emit("toast", "listhaspass");
}
@@ -583,7 +592,7 @@ function shuffle(msg, coll, guid, offline, socket) {
return;
}
coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass) {
if(adminpass != "" || msg.adminpass == undefined) {
@@ -654,7 +663,7 @@ function shuffle(msg, coll, guid, offline, socket) {
function del(params, socket, socketid) {
if(params.id){
var coll = emojiStrip(params.channel).toLowerCase();
var coll = Functions.removeEmojis(params.channel).toLowerCase();
coll = coll.replace(/_/g, "").replace(/ /g,'');
coll = filter.clean(coll);
@@ -705,7 +714,7 @@ function delete_all(msg, coll, guid, offline, socket) {
coll = msg.channel;
}
coll = coll.replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass, gotten) {
if(adminpass != "" || msg.adminpass == undefined) {

View File

@@ -1,3 +1,9 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var db = require(pathThumbnails + '/handlers/db.js');
function password(inp, coll, guid, offline, socket) {
var sessionId = Functions.getSession(socket);
if(sessionId == "") sessionId = "empty";
@@ -22,7 +28,7 @@ function password(inp, coll, guid, offline, socket) {
try {
coll = inp.channel;
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -86,7 +92,7 @@ function conf_function(params, coll, guid, offline, socket) {
try {
coll = params.channel.replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);

View File

@@ -7,6 +7,8 @@ try {
console.log("Seems you forgot to create the file api_key.js in /server/config/. Have a look at api_key.example.js.");
process.exit();
}
var request = require('request');
var db = require(pathThumbnails + '/handlers/db.js');
function get_correct_info(song_generated, channel, broadcast, callback) {
channel = channel.replace(/ /g,'');

View File

@@ -1,4 +1,7 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var Notifications = require(pathThumbnails + '/handlers/notifications.js');
var crypto = require('crypto');
var db = require(pathThumbnails + '/handlers/db.js');
function thumbnail(msg, coll, guid, offline, socket) {
if(msg.thumbnail != undefined && msg.channel && msg.channel != undefined && Functions.isUrl(msg.thumbnail)){

View File

@@ -5,6 +5,7 @@ var mongojs = require('mongojs');
var ObjectId = mongojs.ObjectId;
var token_db = mongojs("tokens");
var cookieParser = require("cookie-parser");
var db = require(pathThumbnails + '/handlers/db.js');
var toShowChannel = {
start: 1,
@@ -112,7 +113,7 @@ router.route('/api/frontpages').get(function(req, res) {
db.collection("connected_users").find({"_id": "total_users"}, function(err, tot) {
var to_return = error.no_error;
to_return.results = {channels: docs, viewers: tot[0].total_users.length};
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
return;
});
});
@@ -140,7 +141,7 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) {
};
var to_send = error.formatting;
to_send.results.push(result);
res.status(400).send(JSON.stringify(to_send));
res.status(400).send(to_send);
return;
}
var token = "";
@@ -171,7 +172,7 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) {
};
var to_send = error.formatting;
to_send.results.push(result);
res.status(400).send(JSON.stringify(to_send));
res.status(400).send(to_send);
return;
}
@@ -197,18 +198,18 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) {
checkTimeout(guid, res, authorized, "DELETE", function() {
if(token != "" && !authorized) {
updateTimeout(guid, res, authorized, "DELETE", function(err, docs) {
res.status(400).send(JSON.stringify(error.wrong_token));
res.status(400).send(error.wrong_token);
return;
});
} else {
validateLogin(adminpass, userpass, channel_name, "delete", res, function(exists) {
if(!exists) {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
}
db.collection(channel_name).find({id:video_id, now_playing: false}, function(err, docs){
if(docs.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.local));
res.status(404).send(error.not_found.local);
return;
}
var dont_increment = false;
@@ -224,13 +225,13 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) {
if(!dont_increment) {
db.collection("frontpage_lists").update({_id: channel_name, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){
updateTimeout(guid, res, authorized, "DELETE", function(err, docs) {
res.status(200).send(JSON.stringify(error.no_error));
res.status(200).send(error.no_error);
return;
});
});
} else {
updateTimeout(guid, res, authorized, "DELETE", function(err, docs) {
res.status(200).send(JSON.stringify(error.no_error));
res.status(200).send(error.no_error);
return;
});
}
@@ -328,7 +329,7 @@ router.route('/api/conf/:channel_name').put(function(req, res) {
};
var to_send = error.formatting;
to_send.results.push(result);
res.status(400).send(JSON.stringify(result));
res.status(400).send(result);
return;
}
var cookie = req.cookies._uI;
@@ -352,13 +353,13 @@ router.route('/api/conf/:channel_name').put(function(req, res) {
checkTimeout(guid, res, authorized, "CONFIG", function() {
if(token != "" && !authorized) {
updateTimeout(guid, res, authorized, "CONFIG", function(err, docs) {
res.status(400).send(JSON.stringify(error.wrong_token));
res.status(400).send(error.wrong_token);
return;
});
} else {
validateLogin(adminpass, userpass, channel_name, "config", res, function(exists, conf) {
if(!exists && conf.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
}
@@ -405,7 +406,7 @@ router.route('/api/conf/:channel_name').put(function(req, res) {
updateTimeout(guid, res, authorized, "CONFIG", function(err, docs) {
var to_return = error.no_error;
to_return.results = [obj];
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
return;
});
});
@@ -454,7 +455,7 @@ router.route('/api/list/:channel_name/:video_id').put(function(req,res) {
};
var to_send = error.formatting;
to_send.results.push(result);
res.status(400).send(JSON.stringify(to_send));
res.status(400).send(to_send);
return;
}
var cookie = req.cookies._uI;
@@ -478,21 +479,21 @@ router.route('/api/list/:channel_name/:video_id').put(function(req,res) {
checkTimeout(guid, res, authorized, "PUT", function() {
if(token != "" && !authorized) {
updateTimeout(guid, res, authorized, "PUT", function(err, docs) {
res.status(400).send(JSON.stringify(error.wrong_token));
res.status(400).send(error.wrong_token);
return;
});
} else {
validateLogin(adminpass, userpass, channel_name, "vote", res, function(exists) {
if(!exists) {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
}
db.collection(channel_name).find({id: video_id, now_playing: false}, function(err, song) {
if(song.length == 0 || (song.hasOwnProperty("type") && song.type == "suggested")) {
res.status(404).send(JSON.stringify(error.not_found.local));
res.status(404).send(error.not_found.local);
return;
} else if(song[0].guids.indexOf(guid) > -1) {
res.status(409).send(JSON.stringify(error.conflicting));
res.status(409).send(error.conflicting);
return;
} else {
song[0].votes += 1;
@@ -506,7 +507,7 @@ router.route('/api/list/:channel_name/:video_id').put(function(req,res) {
updateTimeout(guid, res, authorized, "PUT", function(err, docs) {
var to_return = error.no_error;
to_return.results = song;
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
return;
});
});
@@ -551,7 +552,7 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) {
};
var to_send = error.formatting;
to_send.results.push(result);
res.status(400).send(JSON.stringify(to_send));
res.status(400).send(to_send);
return;
}
var cookie = req.cookies._uI;
@@ -572,7 +573,7 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) {
checkTimeout(guid, res, authorized, "POST", function() {
if(token != "" && !authorized) {
updateTimeout(guid, res, authorized, "POST", function(err, docs) {
res.status(400).send(JSON.stringify(error.wrong_token));
res.status(400).send(error.wrong_token);
return;
});
} else {
@@ -583,10 +584,10 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) {
incrementToken(token);
}
if(conf.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
} else if(conf[0].userpass != userpass && conf[0].userpass != "" && conf[0].userpass != undefined) {
res.status(403).send(JSON.stringify(error.not_authenticated));
res.status(403).send(error.not_authenticated);
return;
}
updateTimeout(guid, res, authorized, "POST", function(err, docs) {
@@ -598,12 +599,12 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) {
list[0].thumbnail = "https://img.youtube.com/vi/" + list[0].id + "/mqdefault.jpg"
}
to_return.results = list;
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
return;
});
});
} else {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
}
});
@@ -686,7 +687,7 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) {
};
var to_send = error.formatting;
to_send.results.push(result);
res.status(400).send(JSON.stringify(to_send));
res.status(400).send(to_send);
return;
}
var cookie = req.cookies._uI;
@@ -710,7 +711,7 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) {
checkTimeout(guid, res, authorized, "POST", function() {
if(token != "" && !authorized) {
updateTimeout(guid, res, authorized, "POST", function(err, docs) {
res.status(400).send(JSON.stringify(error.wrong_token));
res.status(400).send(error.wrong_token);
return;
});
} else {
@@ -720,7 +721,7 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) {
if(result.length == 0 || result[0].type == "suggested") {
var song_type = authenticated ? "video" : "suggested";
if(fetch_only && result.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.local));
res.status(404).send(error.not_found.local);
return;
}
db.collection(channel_name).find({now_playing: true}, function(err, now_playing) {
@@ -742,7 +743,7 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) {
} else if(source == "youtube") new_song.thumbnail = "https://img.youtube.com/vi/" + new_song.id + "/mqdefault.jpg";
runFunction(new_song, channel_name, false, function(element, found) {
if(!found) {
res.status(404).send(JSON.stringify(error.not_found.youtube));
res.status(404).send(error.not_found.youtube);
return;
}
new_song = element;
@@ -793,10 +794,10 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) {
} else if(fetch_only) {
var to_return = error.no_error;
to_return.results = result;
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
return;
} else {
res.status(409).send(JSON.stringify(error.conflicting));
res.status(409).send(error.conflicting);
return;
}
});
@@ -844,18 +845,18 @@ router.route('/api/list/:channel_name').get(function(req, res) {
if(docs.length > 0) {
db.collection(channel_name + "_settings").find({ id: "config" }, function(err, conf) {
if(conf.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
} else if(conf[0].userpass != "" && conf[0].userpass != undefined) {
res.status(403).send(JSON.stringify(error.not_authenticated));
res.status(403).send(error.not_authenticated);
return;
}
var to_return = error.no_error;
to_return.results = docs;
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
});
} else {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
}
});
});
@@ -874,14 +875,14 @@ router.route('/api/list/:channel_name/:video_id').get(function(req, res) {
db.collection(channel_name).find(searchQuery, toShowChannel, function(err, docs) {
db.collection(channel_name + "_settings").find({ id: "config" }, function(err, conf) {
if(conf.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
} else if(conf[0].userpass != "" && conf[0].userpass != undefined) {
res.status(403).send(JSON.stringify(error.not_authenticated));
res.status(403).send(error.not_authenticated);
return;
}
if(docs.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.local));
res.status(404).send(error.not_found.local);
return;
}
var to_return = error.no_error;
@@ -892,7 +893,7 @@ router.route('/api/list/:channel_name/:video_id').get(function(req, res) {
docs[0].thumbnail = "https://img.youtube.com/vi/" + docs[0].id + "/mqdefault.jpg"
}
to_return.results = docs;
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
return;
});
});
@@ -919,12 +920,12 @@ router.route('/api/conf/:channel_name').get(function(req, res) {
}
var to_return = error.no_error;
to_return.results = [conf];
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
} else if(docs.length > 0 && docs[0].userpass != "" && docs[0].userpass != undefined){
res.status(403).send(JSON.stringify(error.not_authenticated));
res.status(403).send(error.not_authenticated);
return;
} else {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
}
});
@@ -961,7 +962,7 @@ router.route('/api/conf/:channel_name').post(function(req, res) {
};
var to_send = error.formatting;
to_send.results.push(result);
res.status(400).send(JSON.stringify(to_send));
res.status(400).send(to_send);
return;
}
var cookie = req.cookies._uI;
@@ -983,7 +984,7 @@ router.route('/api/conf/:channel_name').post(function(req, res) {
checkTimeout(guid, res, authorized, "POST", function() {
if(token != "" && !authorized) {
updateTimeout(guid, res, authorized, "DELETE", function(err, docs) {
res.status(400).send(JSON.stringify(error.wrong_token));
res.status(400).send(error.wrong_token);
return;
});
} else {
@@ -1006,13 +1007,13 @@ router.route('/api/conf/:channel_name').post(function(req, res) {
updateTimeout(guid, res, authorized, "POST", function(err, docs) {
var to_return = error.no_error;
to_return.results = [conf];
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
});
} else if(docs.length > 0 && docs[0].userpass != userpass) {
res.status(403).send(JSON.stringify(error.not_authenticated));
res.status(403).send(error.not_authenticated);
return;
} else {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
}
});
@@ -1037,7 +1038,7 @@ function checkOveruseApiToken(authorized, token_docs, res, callback) {
var retry_in = (date.getTime() - now.getTime()) / 1000;
if(this_doc.used > token_docs[0].limit && retry_in > 0) {
res.header({'Retry-After': retry_in});
res.status(429).send(JSON.stringify(error.tooMany));
res.status(429).send(error.tooMany);
return;
} else {
db.collection("timeout_api").update({guid: token_docs[0].token}, {$inc: {used: 1}}, function(e, d) {
@@ -1083,7 +1084,7 @@ router.route('/api/list/:channel_name').post(function(req, res) {
};
var to_send = error.formatting;
to_send.results.push(result);
res.status(400).send(JSON.stringify(to_send));
res.status(400).send(to_send);
return;
}
@@ -1107,7 +1108,7 @@ router.route('/api/list/:channel_name').post(function(req, res) {
checkTimeout(guid, res, authorized, "POST", function() {
if(token != "" && !authorized) {
updateTimeout(guid, res, authorized, "POST", function(err, docs) {
res.status(400).send(JSON.stringify(error.wrong_token));
res.status(400).send(error.wrong_token);
return;
});
} else {
@@ -1141,10 +1142,10 @@ router.route('/api/list/:channel_name').post(function(req, res) {
if(list.length > 0) {
db.collection(channel_name + "_settings").find({ id: "config" }, function(err, conf) {
if(conf.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
} else if(conf[0].userpass != userpass && conf[0].userpass != "" && conf[0].userpass != undefined) {
res.status(403).send(JSON.stringify(error.not_authenticated));
res.status(403).send(error.not_authenticated);
return;
}
if(authorized) {
@@ -1153,12 +1154,12 @@ router.route('/api/list/:channel_name').post(function(req, res) {
updateTimeout(guid, res, authorized, "POST", function(err, docs) {
var to_return = error.no_error;
to_return.results = list;
res.status(200).send(JSON.stringify(to_return));
res.status(200).send(to_return);
return;
});
});
} else {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
}
});
@@ -1374,7 +1375,7 @@ function checkTimeout(guid, res, authorized, type, callback) {
var retry_in = (date.getTime() - now.getTime()) / 1000;
if(retry_in > 0) {
res.header({'Retry-After': retry_in});
res.status(429).send(JSON.stringify(error.tooMany));
res.status(429).send(error.tooMany);
return;
}
}
@@ -1396,10 +1397,10 @@ function validateLogin(adminpass, userpass, channel_name, type, res, callback) {
if(conf.length > 0 && ((conf[0].userpass == undefined || conf[0].userpass == "" || conf[0].userpass == userpass))) {
exists = true;
} else if(conf.length > 0 && type == "config") {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
} else if(conf.length == 0) {
res.status(404).send(JSON.stringify(error.not_found.list));
res.status(404).send(error.not_found.list);
return;
}
@@ -1414,7 +1415,7 @@ function validateLogin(adminpass, userpass, channel_name, type, res, callback) {
} else if(type == "add") {
callback(exists, conf, false);
} else {
res.status(403).send(JSON.stringify(error.not_authenticated));
res.status(403).send(error.not_authenticated);
return;
}
});
@@ -1432,7 +1433,7 @@ function postEnd(channel_name, configs, new_song, guid, res, authenticated, auth
to_return.success = true;
}
to_return.results = [new_song];
res.status(authenticated ? 200 : 403).send(JSON.stringify(to_return));
res.status(authenticated ? 200 : 403).send(to_return);
return;
});
});